From 78ac5928d64e5d0ad26673e7e429c1ff386f567b Mon Sep 17 00:00:00 2001 From: Mamdouh Ahmed Moustafa Date: Thu, 12 May 2016 18:29:52 +0200 Subject: [PATCH 1/2] Update main.py --- main.py | 57 ++++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 36 insertions(+), 21 deletions(-) diff --git a/main.py b/main.py index 8a4b987..993c3f7 100644 --- a/main.py +++ b/main.py @@ -3,9 +3,9 @@ import time, os MAX_SUBS = 1000000 -MAX_CF_CONTEST_ID = 600 +MAX_CF_CONTEST_ID = 700 MAGIC_START_POINT = 17000 - +cnt=1 handle='tacklemore' SOURCE_CODE_BEGIN = '
'
@@ -15,9 +15,9 @@
 EXT = {'C++': 'cpp', 'C': 'c', 'Java': 'java', 'Python': 'py', 'Delphi': 'dpr', 'FPC': 'pas', 'C#': 'cs'}
 EXT_keys = EXT.keys()
 
-replacer = {'"': '\"', '>': '>', '<': '<', '&': '&', "'": "'"}
+replacer = {'"': '\"', '>': '>', '<': '<', '&': '&', "'": "'", "'": "'"}
 keys = replacer.keys()
-
+illegal = {'|','\\','/','?','*','\"',':','<','>'}
 def get_ext(comp_lang):
     if 'C++' in comp_lang:
         return 'cpp'
@@ -26,6 +26,11 @@ def get_ext(comp_lang):
             return EXT[key]
     return ""
 
+def clean_name(string):
+    for ch in illegal:
+        string = string.replace(ch,'_')
+    return string;
+
 def parse(source_code):
     for key in keys:
         source_code = source_code.replace(key, replacer[key])
@@ -45,22 +50,32 @@ def parse(source_code):
 
 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("
", 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() + while submission['verdict'] == u'OK' and submission['contestId'] < MAX_CF_CONTEST_ID: + try: + 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("", 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) + #print str(cnt) + print str(con_id) + str(prob_id) + " " + prob_name + " " + cnt = cnt + 1 + file = open(new_directory + '/' + prob_id + ' [ ' + clean_name(prob_name) + ' ]' + '.' + ext, 'w') + file.write(result) + file.close() + break + except: + print "Exception: probably connection failure" + else : + print "Failed submission" end_time = time.time() - +print 'Downloaded %d submissions' % cnt print 'Execution time %d seconds' % int(end_time - start_time) From a97c27a9ca5b1892e65fed41524c306804800088 Mon Sep 17 00:00:00 2001 From: Mamdouh Ahmed Moustafa Date: Tue, 27 Dec 2016 05:34:50 +0200 Subject: [PATCH 2/2] Update main.py --- main.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/main.py b/main.py index 993c3f7..52a700a 100644 --- a/main.py +++ b/main.py @@ -3,10 +3,10 @@ import time, os MAX_SUBS = 1000000 -MAX_CF_CONTEST_ID = 700 +MAX_CF_CONTEST_ID = 900 MAGIC_START_POINT = 17000 cnt=1 -handle='tacklemore' +handle='sherbi7y' SOURCE_CODE_BEGIN = '
'
 SUBMISSION_URL = 'http://codeforces.com/contest/{ContestId}/submission/{SubmissionId}'
@@ -56,7 +56,6 @@ 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', '') @@ -69,11 +68,14 @@ def parse(source_code): print str(con_id) + str(prob_id) + " " + prob_name + " " cnt = cnt + 1 file = open(new_directory + '/' + prob_id + ' [ ' + clean_name(prob_name) + ' ]' + '.' + ext, 'w') - file.write(result) + if ext == "cpp": + file.write(result[result.index("#include"):-1]) + else: + file.write(result) file.close() break - except: - print "Exception: probably connection failure" + except Exception as e: + print "Exception: "+str(e) else : print "Failed submission" end_time = time.time()