-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgcj_crawler.py
More file actions
executable file
·281 lines (239 loc) · 11.6 KB
/
gcj_crawler.py
File metadata and controls
executable file
·281 lines (239 loc) · 11.6 KB
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#!/usr/bin/python3 -B
from crawler.crawler import Crawler
import sys
import os
import csv
import time
from selenium.webdriver.common.by import By
class GCJCrawler(Crawler):
def __init__(self):
super().__init__('./chromedriver', headless=False)
def _get_prob_info(self):
id_list = self.driver.execute_script(
'var id_list = []; \
for (i = 0; i < GCJ.problems.length; i++) { \
id_list[i] = {}; \
id_list[i]["id"] = GCJ.problems[i].id; \
id_list[i]["io"] = GCJ.io[i].length; \
}; \
return id_list;'
)
return id_list
def _parse_old_ver(self, url, round_cnt, prob_cnt):
print (url)
index = int(url.split('/')[-2])
board_url = url.replace('dashboard', 'scoreboard?c=%d#vf=1' %index)
author_info_list = []
while True:
print (board_url)
self.driver.get(board_url)
prob_info = self._get_prob_info()
author_list = self.get_all_elements(By.CSS_SELECTOR, '#scb-table-body > tr')
for author in author_list:
author_info = {}
info_list = self.get_all_elements(By.TAG_NAME, 'td', element=author)
prob_index = 0
prob_io_index = 0
new_prob_cnt = prob_cnt
for i, info in enumerate(info_list):
if i == 0:
author_info['rank_%d' %round_cnt] = info.text.replace('\n ', '')
elif i == 1:
try:
country = self.get_element(By.TAG_NAME, 'img', element=info)
country = country.get_attribute('title')
except:
country = ''
author_info['country'] = country.replace(' ', '_')
elif i == 2:
author_info['name'] = info.text
elif i == 3:
author_info['score_%d' %round_cnt] = info.text
elif i == 4:
continue
else:
if '--' in info.text or 'Time' in info.text:
author_info['prob_%d' %new_prob_cnt] = None
else:
download_url = url.replace('dashboard',
'scoreboard/do/?cmd=GetSourceCode&problem=')
download_url += '%s&io_set_id=%d&username=%s' %(
prob_info[prob_index]['id'],
prob_io_index,
author_info['name']
)
sol_path = self.code_path + '/%s_%s_%d.zip' \
%(author_info['name'],
author_info['country'],
new_prob_cnt)
# sol = self.get_all_elements(By.TAG_NAME, 'img', element=info)[1]
# sol.click()
# alert = self.driver.switch_to_alert()
# alert.accept()
print ('Move to %s' %sol_path)
download = self.download_file_url(download_url, sol_path)
if download:
author_info['prob_%d' %new_prob_cnt] = sol_path
else:
author_info['prob_%d' %new_prob_cnt] = 'Fail'
if prob_info[prob_index]['io'] == (prob_io_index + 1):
prob_index += 1
prob_io_index = 0
else:
prob_io_index += 1
new_prob_cnt += 1
print (author_info)
author_info_list.append(author_info)
try:
next_page = self.get_all_elements(By.CSS_SELECTOR, '#scb-range-links > a')[-1]
except:
break
if 'Next' in next_page.text:
next_page.click()
board_url = self.driver.current_url
self.driver.execute_script('window.open()')
self.driver.close()
self.driver.switch_to.window(self.driver.window_handles[-1])
else:
break
return author_info_list, new_prob_cnt
def _get_solution(self, author_info, url, total_prob, prob_cnt):
print (url)
self.driver.execute_script('window.open()')
self.driver.switch_to.window(self.driver.window_handles[-1])
self.driver.get(url)
board = self.get_all_elements(By.CSS_SELECTOR,
'#scoreboard > div > div:nth-child(2) > div')
prob_info = {}
for i in range(total_prob):
prob_info['prob_%d' %i] = None
while True:
for i, row in enumerate(board):
if i == 0:
continue
elif row.text == '':
break
else:
for p in range(total_prob):
if prob_info['prob_%d' %p] is None:
prob = self.get_element(By.CSS_SELECTOR,
'div.ranking-table__row > div:nth-child(%d)' %(p+1), element=row)
if 'check' in prob.text:
try:
sol = self.get_element(By.TAG_NAME, 'button', element=prob)
sol.click()
sol_path = self.code_path + '/%s_%s_%d.txt' \
%(author_info['name'],
author_info['country'],
prob_cnt+p)
self.download_file_click(sol_path)
prob_info['prob_%d' %p] = sol_path
except:
# No solution
pass
next_prob = self.get_element(By.CLASS_NAME, 'nav-chevron-right')
if next_prob.get_attribute('disabled'):
break
else:
next_prob.click()
self.driver.close()
self.driver.switch_to.window(self.driver.window_handles[0])
return prob_info
def _parse_new_ver(self, url, round_cnt, prob_cnt):
print (url)
self.driver.get(url)
before_rank = ''
author_info_list = []
while True:
prob_list = self.get_all_elements(By.CLASS_NAME,
'problems-bar-graphs-aggregate-graphs')
total_prob = len(prob_list)
author_list = self.get_all_elements(By.CSS_SELECTOR,
'#scoreboard > div:nth-child(1) > div:nth-child(1) > div')
for i, author in enumerate(author_list):
if i == 0:
continue
author_info = {}
info_list = self.get_all_elements(By.TAG_NAME, 'div', element=author)
for j, info in enumerate(info_list):
if j == 0:
if i == 1:
while(before_rank == info.text):
pass
before_rank = info.text
author_info['rank_%d' %round_cnt] = info.text
elif j == 1:
link = self.get_element(By.TAG_NAME, 'a', element=info)
info2 = self.get_element(By.TAG_NAME, 'p', element=link)
author_info['name'] = info2.text
country = self.get_element(By.TAG_NAME, 'img', element=info2)
country = country.get_attribute('src')
country = country.split('/')[-1].split('-')[0]
author_info['country'] = country
sol_link = link.get_attribute('href')
prob_info = self._get_solution(author_info, sol_link,
total_prob, prob_cnt)
author_info.update(prob_info)
elif j == 2:
score = self.get_element(By.TAG_NAME, 'span', element=info)
author_info['score_%d' %round_cnt] = score.text
print (author_info)
author_info_list.append(author_info)
pagination = self.get_element(By.CLASS_NAME, 'ranking-table-pagination')
next_page = self.get_element(By.CSS_SELECTOR,
'div.ranking-table-pagination-pane.ranking-table-pagination-pane__right > \
button:nth-child(4)', element=pagination)
if next_page.get_attribute('disabled'):
break
else:
next_page.click()
return author_info_list, prob_cnt+total_prob
def run(self):
start = 2008
end = 2020
if len(sys.argv) == 2:
start = int(sys.argv[1])
end = start + 1
print (start, end)
for year in range(start, end):
year_url = 'https://codingcompetitions.withgoogle.com/' + \
'codejam/archive/%d' %year
self.driver.get(year_url)
print (year_url)
self.code_path = os.path.dirname(os.path.abspath(__file__))
self.code_path += '/downloads/GCJ/%d' %year
if not os.path.exists(self.code_path):
os.makedirs(self.code_path)
self.get_all_elements(By.CLASS_NAME, 'schedule-row')
round_count = len(self.get_all_elements(By.CLASS_NAME, 'schedule-row'))
total_author_info = {}
prob_cnt = 0
for cnt in range(1, round_count+1):
self.driver.get(year_url)
round_url = self.get_element(By.ID, 'archive-view-cta-%d' %cnt)
round_url = round_url.get_attribute('href')
if '/dashboard' in round_url:
author_info_list, prob_cnt = \
self._parse_old_ver(round_url, cnt, prob_cnt)
else:
author_info_list, prob_cnt = \
self._parse_new_ver(round_url, cnt, prob_cnt)
for author_info in author_info_list:
key = '%s.%s' %(author_info['name'], author_info['country'])
if key in total_author_info.keys():
total_author_info[key].update(author_info)
else:
total_author_info[key] = author_info
with open('gcj_result_%d.csv' %year, 'w') as csvfile:
fieldnames = ['name', 'country']
for cnt in range(1, round_count+1):
fieldnames += ['rank_%d' %cnt]
fieldnames += ['score_%d' %cnt]
for cnt in range(prob_cnt):
fieldnames += ['prob_%d' %cnt]
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
for author in total_author_info.keys():
writer.writerow(total_author_info[author])
c = GCJCrawler()
c.run()