forked from maged918/tagaware-rec-sys
-
Notifications
You must be signed in to change notification settings - Fork 0
/
crawler.py
176 lines (149 loc) · 5.39 KB
/
crawler.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import requests
import subprocess
import json
import requests
import urllib3
import os
from bs4 import BeautifulSoup
from random import sample
from requests.auth import HTTPProxyAuth
from urllib3 import request
from urllib3 import util
flags = {
'proxy': 0
}
password = 'PASSWORD'
#subprocess.call(["touch", "Submissions.txt"])
#subprocess.call(["touch", "All-Submissions.txt"])
#subprocess.call(["touch", "Problems-tags.txt"])
fi = open("Div1-Submissions.txt" , "a")
fi1 = open("All-Submissions.txt" , "a")
fi2 = open("Div1-Problems-tags.txt" , "a")
if not os.path.exists('./source-code'):
os.makedirs('./source-code')
if flags['proxy'] == 1:
proxyDict = {
'http' : 'http://50.0.0.5:8080',
'https' : 'http://50.0.0.5:8080'
}
auth = HTTPProxyAuth('maged.shalaby', password)
else:
proxyDict = {}
auth = {}
problem_limit = 10
total_limit = 100
bs4_error_text = '<ERROR>'
contest_range = range(1, 250)
def filter_Contests(contests):
filtered = []
Type = 'CF'
div = 'Div. 1'
for contest in contests:
if(div in contest['name'] and contest['type'] == Type and contest['phase'] == 'FINISHED' and contest['id'] in contest_range):
filtered.append(contest)
return filtered
def filter_Submissions(submissions, contestID, flag):
requestParams = {'contestId':int(contestID), 'from':1, 'count':1}
req = requests.get("http://codeforces.com/api/contest.standings", params = requestParams,
proxies = proxyDict, auth = auth)
problems = req.json()['result']['problems']
dictionary = {}
filtered = []
language = 'C++'
dictionary_problems = 'ABCDEFGHIJKL'
for i in range (0, len(dictionary_problems)):
dictionary[dictionary_problems[i]] = []
for submission in submissions:
if(submission['problem']['index'] in dictionary and submission['verdict'] == 'OK' and language in submission['programmingLanguage']):
dictionary[submission['problem']['index']].append(submission)
if(flag=='filter'):
for problem in problems:
if(problem['index'] in dictionary and len(dictionary[problem['index']]) >= problem_limit):
filtered.extend(sample(dictionary[problem['index']], problem_limit))
fi2.write("Contest ID: " + str(contestID) + "\n")
fi2.write("Index: " + str(problem['index']) + "\n")
fi2.write("Name: " + str(problem['name']) + "\n")
fi2.write("Tags: ")
tags = problem['tags']
fi2.write(toString(tags))
fi2.write("\n\n\n")
fi2.flush()
print(contestID, problem['index'])
#fi2.write("\n\n")
return filtered
else:
for problem in problems:
filtered.extend(dictionary[problem['index']])
return filtered
def create_Contest(status, name):
for submission in status:
fi.write("Contest ID: " + str(submission['contestId']) + "\n")
fi.write("Index: " + str(submission['problem']['index']) + "\n")
fi.write("Handle: " + str(submission['author']['members'][0]['handle']) + "\n")
fi.write("Submission ID: " + str(submission['id']) + "\n")
fi.write("Submission Time: " + str(submission['creationTimeSeconds']))
print(submission['id'])
create_code(submission)
fi.write("\n\n")
fi.flush()
def create_all_submissions(status, name):
for submission in status:
fi1.write("Contest ID: " + str(submission['contestId']) + "\n")
fi1.write("Index: " + str(submission['problem']['index']) + "\n")
fi1.write("Handle: " + str(submission['author']['members'][0]['handle']) + "\n")
fi1.write("Submission ID: " + str(submission['id']) + "\n")
fi1.write("Submission Time: " + str(submission['creationTimeSeconds']))
fi1.write("\n\n")
fi1.flush()
def toString(lists):
tags = ""
if(len(lists) >= 1):
tags = str(lists[0])
for i in range(1, len(lists)):
l = lists[i]
tags = tags + ", " + str(l)
return tags
def create_code(submission):
url = "http://codeforces.com/contest/" + str(submission['contestId']) + "/submission/" + str(submission['id'])
if(flags['proxy']==1):
http = urllib3.proxy_from_url(proxyDict['http'])
else:
http = urllib3.connection_from_url(url)
handle = http.request('GET',url)
html_gunk = handle.data
#print(html_gunk)
soup = BeautifulSoup(html_gunk, 'html.parser')
#subprocess.call(["touch", './source-code/' + str(submission['id']) + ".cpp"])
fi3 = open('./source-code/' + str(submission['id']) + ".cpp" , "w")
fi3.write("//Language: " + str(submission['programmingLanguage']) + "\n\n\n")
try:
result = soup.pre.get_text().encode('utf-8', errors='replace').decode('utf-8')
except AttributeError:
result = bs4_error_text
except UnicodeDecodeError:
result = '<CHAR>'
except UnicodeEncodeError:
result = '<CHAR>'
fi3.write(result)
fi3.close()
'''
MAIN
'''
requestParams = {'gym':'false'}
r = requests.get("http://www.codeforces.com/api/contest.list", params=requestParams, proxies = proxyDict, auth = auth)
contests = r.json()
filtered = filter_Contests(contests['result'])
print('Number of filtered contests', len(filtered))
for contest in filtered:
# contest = filtered[0]
requestParams = {'contestId':contest['id']}
print('Requesting submissions of contest id', contest['id'])
r2 = requests.get("http://codeforces.com/api/contest.status", params = requestParams,
proxies = proxyDict, auth = auth)
status = r2.json()
# all_submissions = filter_Submissions(status['result'], contest['id'], 'all')
# create_all_submissions(all_submissions, contest['name'])
filtered_submissions = filter_Submissions(status['result'], contest['id'], 'filter')
create_Contest(filtered_submissions, contest['name'])
fi.close()
fi2.close()