forked from justefg/CodeforcesSolutionDownloader
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
66 lines (52 loc) · 2.28 KB
/
main.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
import urllib
import json
import time, os
MAX_SUBS = 1000000
MAX_CF_CONTEST_ID = 600
MAGIC_START_POINT = 17000
handle='tacklemore'
SOURCE_CODE_BEGIN = '<pre class="prettyprint program-source" style="padding: 0.5em;">'
SUBMISSION_URL = 'http://codeforces.com/contest/{ContestId}/submission/{SubmissionId}'
USER_INFO_URL = 'http://codeforces.com/api/user.status?handle={handle}&from=1&count={count}'
EXT = {'C++': 'cpp', 'C': 'c', 'Java': 'java', 'Python': 'py', 'Delphi': 'dpr', 'FPC': 'pas', 'C#': 'cs'}
EXT_keys = EXT.keys()
replacer = {'"': '\"', '>': '>', '<': '<', '&': '&', "'": "'"}
keys = replacer.keys()
def get_ext(comp_lang):
if 'C++' in comp_lang:
return 'cpp'
for key in EXT_keys:
if key in comp_lang:
return EXT[key]
return ""
def parse(source_code):
for key in keys:
source_code = source_code.replace(key, replacer[key])
return source_code
if not os.path.exists(handle):
os.makedirs(handle)
user_info = urllib.urlopen(USER_INFO_URL.format(handle=handle, count=MAX_SUBS)).read()
dic = json.loads(user_info)
if dic['status'] != u'OK':
print 'Oops.. Something went wrong...'
exit(0)
submissions = dic['result']
start_time = time.time()
for submission in submissions:
if submission['verdict'] == u'OK' and submission['contestId'] < MAX_CF_CONTEST_ID:
con_id, sub_id = submission['contestId'], submission['id'],
prob_name, prob_id = submission['problem']['name'], submission['problem']['index']
comp_lang = submission['programmingLanguage']
submission_info = urllib.urlopen(SUBMISSION_URL.format(ContestId=con_id, SubmissionId=sub_id)).read()
start_pos = submission_info.find(SOURCE_CODE_BEGIN, MAGIC_START_POINT) + len(SOURCE_CODE_BEGIN)
end_pos = submission_info.find("</pre>", start_pos)
result = parse(submission_info[start_pos:end_pos]).replace('\r', '')
ext = get_ext(comp_lang)
new_directory = handle + '/' + str(con_id)
if not os.path.exists(new_directory):
os.makedirs(new_directory)
file = open(new_directory + '/' + prob_id + '[ ' + prob_name + ' ]' + '.' + ext, 'w')
file.write(result)
file.close()
end_time = time.time()
print 'Execution time %d seconds' % int(end_time - start_time)