From 90f3b2cc7356d05985530a99f2065e7a6086c200 Mon Sep 17 00:00:00 2001 From: Andrii Date: Sat, 23 Mar 2019 12:51:47 +0200 Subject: [PATCH 1/3] fixes invalid characters in Windows filenames --- main.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/main.py b/main.py index 8a4b987..ebdfd42 100644 --- a/main.py +++ b/main.py @@ -1,12 +1,14 @@ import urllib import json import time, os +import platform +import re MAX_SUBS = 1000000 -MAX_CF_CONTEST_ID = 600 +MAX_CF_CONTEST_ID = 1140 MAGIC_START_POINT = 17000 -handle='tacklemore' +handle='-your-handle-here-' SOURCE_CODE_BEGIN = '
'
 SUBMISSION_URL = 'http://codeforces.com/contest/{ContestId}/submission/{SubmissionId}'
@@ -49,18 +51,22 @@ def parse(source_code):
         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("
", 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) + + if platform.system() == 'Windows': + prob_name = re.sub('[\\\:*?"<>|/]', '', prob_name) + file = open(new_directory + '/' + prob_id + '[ ' + prob_name + ' ]' + '.' + ext, 'w') file.write(result) - file.close() + file.close() end_time = time.time() print 'Execution time %d seconds' % int(end_time - start_time) From be71b0ba4c5aec9537d7324d32a108f6cd4648cb Mon Sep 17 00:00:00 2001 From: Andrii Date: Sat, 23 Mar 2019 14:02:17 +0200 Subject: [PATCH 2/3] up to Python3 --- .vscode/settings.json | 3 +++ main.py | 14 +++++++------- 2 files changed, 10 insertions(+), 7 deletions(-) create mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..500bc70 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "python.linting.pylintEnabled": true +} \ No newline at end of file diff --git a/main.py b/main.py index ebdfd42..3cbeb5a 100644 --- a/main.py +++ b/main.py @@ -36,10 +36,10 @@ 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() +user_info = urllib.request.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...' + print('Oops.. Something went wrong...') exit(0) submissions = dic['result'] @@ -50,7 +50,7 @@ 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() + submission_info = urllib.request.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("", start_pos) @@ -62,11 +62,11 @@ def parse(source_code): os.makedirs(new_directory) if platform.system() == 'Windows': - prob_name = re.sub('[\\\:*?"<>|/]', '', prob_name) + prob_name = re.sub('[\\:*?"<>|/]', '', prob_name) file = open(new_directory + '/' + prob_id + '[ ' + prob_name + ' ]' + '.' + ext, 'w') - file.write(result) - file.close() + file.write(result) + file.close() end_time = time.time() -print 'Execution time %d seconds' % int(end_time - start_time) +print('Execution time %d seconds' % int(end_time - start_time)) From 9135eb2702adc0d5f6a5a48b55918e189dc11042 Mon Sep 17 00:00:00 2001 From: Andrii Date: Sat, 23 Mar 2019 15:42:35 +0200 Subject: [PATCH 3/3] fixed http parser --- .gitignore | 2 ++ main.py | 29 ++++++++++++++++------------- 2 files changed, 18 insertions(+), 13 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b8a4c25 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +omelianenko +.vscode \ No newline at end of file diff --git a/main.py b/main.py index 3cbeb5a..3eb661a 100644 --- a/main.py +++ b/main.py @@ -1,4 +1,4 @@ -import urllib +import urllib.request as urllib import json import time, os import platform @@ -6,18 +6,18 @@ MAX_SUBS = 1000000 MAX_CF_CONTEST_ID = 1140 -MAGIC_START_POINT = 17000 +MAGIC_START_POINT = 1 -handle='-your-handle-here-' +handle='omelianenko' -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):
@@ -35,8 +35,7 @@ def parse(source_code):
 
 if not os.path.exists(handle):
     os.makedirs(handle)
-
-user_info = urllib.request.urlopen(USER_INFO_URL.format(handle=handle, count=MAX_SUBS)).read()
+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...')
@@ -46,15 +45,17 @@ def parse(source_code):
 start_time = time.time()
 
 for submission in submissions:
-    if submission['verdict'] == u'OK' and submission['contestId'] < MAX_CF_CONTEST_ID:
+    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.request.urlopen(SUBMISSION_URL.format(ContestId=con_id, SubmissionId=sub_id)).read()
+
+        with urllib.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) @@ -64,9 +65,11 @@ def parse(source_code): if platform.system() == 'Windows': prob_name = re.sub('[\\:*?"<>|/]', '', prob_name) - file = open(new_directory + '/' + prob_id + '[ ' + prob_name + ' ]' + '.' + ext, 'w') - file.write(result) - file.close() + 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)) +