-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathChef.py
80 lines (63 loc) · 2.53 KB
/
Chef.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
from GitterClasses import *
problemlist = []
def buildProblemListAndFileStructure(user):
url = 'https://www.codechef.com/users/' + user
print '\nAttempting First Fetch'
OK = False
while OK is False:
try:
response = urllib2.urlopen(url) # open webpage
print 'Success'
OK = True
except urllib2.HTTPError as e:
print 'Failure.\nAn HTTP error occured : ' + str(e.code)
print 'Refetching..'
html = response.read() # read the response
soup = BeautifulSoup(html, "html.parser")
tdps = soup.findAll('p')
finaltdp = []
for tdp in tdps:
if len(tdp.findAll('b')) != 0 and len(tdp.findAll('span')) != 0:
finaltdp.append(tdp)
if not os.path.exists(Config.githubRepo): # if directory doesn't exist
print 'Creating Directory: ' + Config.githubRepo
os.makedirs(Config.githubRepo)
for tdp in finaltdp:
contestcode = tdp.find('b')
contestcode = contestcode.get_text()
print contestcode
if not os.path.exists(Config.githubRepo + '/' + contestcode + '/'): # if directory doesn't exist
# print 'Creating Directory: CodechefGitterSolutions/' +
# contestcode + '/'
os.makedirs(
Config.githubRepo +
'/' +
contestcode +
'/') # create directory
problems = tdp.findAll('a')
for problem in problems:
print '--> ' + problem['href']
problemlist.append(
Problem(contestcode,
problem['href'],
[])) # instantiate object and add to list
print str(len(problemlist)) + ' problems found.\n'
totalFetchCount = 0
def updateSubmissionList(): # fill submission details in submissionlist of each problem
global totalFetchCount
print 'Updating submission details.'
i = 0
total = len(problemlist)
for problem in problemlist:
problem.updateSubmissionList()
i = i + 1
print str(float(i * 100) / total)[0:5] + '% done.\n'
totalFetchCount = totalFetchCount + len(problem.submissionList)
def fetchSubmissions():
print 'Fetching all required solutions (' + str(totalFetchCount) + ' total)'
fetchedCount = 0
for problem in problemlist:
problem.fetchAllSubmissions()
fetchedCount = fetchedCount + len(problem.submissionList)
if fetchedCount is not 0:
print str(float(fetchedCount * 100) / totalFetchCount)[0:5] + '% done.\n'