From 5177c7fcb5a9e4069e5c7e516b4a709478ccb434 Mon Sep 17 00:00:00 2001 From: David Cairuz Date: Sun, 25 Nov 2018 16:49:58 -0200 Subject: [PATCH] Tried to fix problems with python version Also added one replacer to " ' " and modified the START_POINT so it would work for me. --- main.py | 47 +++++++++++++++++++++++++++++------------------ 1 file changed, 29 insertions(+), 18 deletions(-) diff --git a/main.py b/main.py index 8a4b987..26ba5a6 100644 --- a/main.py +++ b/main.py @@ -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 = '
'
+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 = {'"': '\"', '>': '>', '<': '<', '&': '&', "'": "'"}
+replacer = {'"': '\"', '>': '>', '<': '<', '&': '&', "'": "'", "'": "'"}
 keys = replacer.keys()
 
 def get_ext(comp_lang):
@@ -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']
@@ -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("
", 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))