Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 29 additions & 18 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
import urllib
#-*- coding: utf-8 -*-
import urllib3
import json
import time, os
from urllib.request import urlopen, Request

MAX_SUBS = 1000000
MAX_CF_CONTEST_ID = 600
MAGIC_START_POINT = 17000
MAGIC_START_POINT = 1

handle='tacklemore'
handle='your handle here'

SOURCE_CODE_BEGIN = '<pre class="prettyprint program-source" style="padding: 0.5em;">'
SOURCE_CODE_BEGIN = '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 = {'&quot;': '\"', '&gt;': '>', '&lt;': '<', '&amp;': '&', "&apos;": "'"}
replacer = {'&quot;': '\"', '&gt;': '>', '&lt;': '<', '&amp;': '&', "&apos;": "'", "&#39;": "'"}
keys = replacer.keys()

def get_ext(comp_lang):
Expand All @@ -34,10 +36,14 @@ def parse(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)
req = Request(USER_INFO_URL.format(handle=handle, count=MAX_SUBS), headers={'Accept-Charset': 'utf-8', 'Accept-Language': 'zh-tw,en-us;q=0.5'})

with urlopen(USER_INFO_URL.format(handle=handle, count=MAX_SUBS)) as rsq:
user_info = rsq.read().decode()
dic = json.loads(user_info)

if dic['status'] != u'OK':
print 'Oops.. Something went wrong...'
print ('Something went wrong...')
exit(0)

submissions = dic['result']
Expand All @@ -48,19 +54,24 @@ def parse(source_code):
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()

req = Request(SUBMISSION_URL.format(ContestId=con_id, SubmissionId=sub_id), headers={'Accept-Charset': 'utf-8', 'Accept-Language': 'zh-tw,en-us;q=0.5'})
with urlopen(SUBMISSION_URL.format(ContestId=con_id, SubmissionId=sub_id)) as rsq:
submission_info = rsq.read().decode()

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', '')
result = parse(submission_info[start_pos:end_pos]).replace('\r', '').replace("'", '"')
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()
new_directory = handle
''' If you want to download every contest into a separate folder, uncomment the block of code below '''
# new_directory = handle + '/' + str(con_id)
# if not os.path.exists(new_directory):
# os.makedirs(new_directory)

with open("{}/{}{}[{}].{}".format(new_directory, con_id, prob_id, prob_name, ext), 'w', encoding='utf-8') as f:
f.write(result)

print 'Execution time %d seconds' % int(end_time - start_time)
end_time = time.time()
print ('Execution time %d seconds' % int(end_time - start_time))