├── README.md └── main.py /README.md: -------------------------------------------------------------------------------- 1 | CodeforcesSolutionDownloader 2 | ============================ 3 | 4 | a small script for downloading ACCEPTED solutions from codeforces.com 5 | 6 | Change the handle variable's value to your handle and then run main.py. It will create folders with the CF rounds and place your solutions there. 7 | -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | import urllib 2 | import json 3 | import time, os 4 | 5 | MAX_SUBS = 1000000 6 | MAX_CF_CONTEST_ID = 600 7 | MAGIC_START_POINT = 17000 8 | 9 | handle='tacklemore' 10 | 11 | SOURCE_CODE_BEGIN = '
' 12 | SUBMISSION_URL = 'http://codeforces.com/contest/{ContestId}/submission/{SubmissionId}' 13 | USER_INFO_URL = 'http://codeforces.com/api/user.status?handle={handle}&from=1&count={count}' 14 | 15 | EXT = {'C++': 'cpp', 'C': 'c', 'Java': 'java', 'Python': 'py', 'Delphi': 'dpr', 'FPC': 'pas', 'C#': 'cs'} 16 | EXT_keys = EXT.keys() 17 | 18 | replacer = {'"': '\"', '>': '>', '<': '<', '&': '&', "'": "'"} 19 | keys = replacer.keys() 20 | 21 | def get_ext(comp_lang): 22 | if 'C++' in comp_lang: 23 | return 'cpp' 24 | for key in EXT_keys: 25 | if key in comp_lang: 26 | return EXT[key] 27 | return "" 28 | 29 | def parse(source_code): 30 | for key in keys: 31 | source_code = source_code.replace(key, replacer[key]) 32 | return source_code 33 | 34 | if not os.path.exists(handle): 35 | os.makedirs(handle) 36 | 37 | user_info = urllib.urlopen(USER_INFO_URL.format(handle=handle, count=MAX_SUBS)).read() 38 | dic = json.loads(user_info) 39 | if dic['status'] != u'OK': 40 | print 'Oops.. Something went wrong...' 41 | exit(0) 42 | 43 | submissions = dic['result'] 44 | start_time = time.time() 45 | 46 | for submission in submissions: 47 | if submission['verdict'] == u'OK' and submission['contestId'] < MAX_CF_CONTEST_ID: 48 | con_id, sub_id = submission['contestId'], submission['id'], 49 | prob_name, prob_id = submission['problem']['name'], submission['problem']['index'] 50 | comp_lang = submission['programmingLanguage'] 51 | submission_info = urllib.urlopen(SUBMISSION_URL.format(ContestId=con_id, SubmissionId=sub_id)).read() 52 | 53 | start_pos = submission_info.find(SOURCE_CODE_BEGIN, MAGIC_START_POINT) + len(SOURCE_CODE_BEGIN) 54 | end_pos = submission_info.find("", start_pos) 55 | result = parse(submission_info[start_pos:end_pos]).replace('\r', '') 56 | ext = get_ext(comp_lang) 57 | 58 | new_directory = handle + '/' + str(con_id) 59 | if not os.path.exists(new_directory): 60 | os.makedirs(new_directory) 61 | file = open(new_directory + '/' + prob_id + '[ ' + prob_name + ' ]' + '.' + ext, 'w') 62 | file.write(result) 63 | file.close() 64 | end_time = time.time() 65 | 66 | print 'Execution time %d seconds' % int(end_time - start_time) 67 | --------------------------------------------------------------------------------