-
Notifications
You must be signed in to change notification settings - Fork 1
/
contest-threaded.py
163 lines (145 loc) · 5.28 KB
/
contest-threaded.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
from urllib.request import urlopen
import os
import os.path
import urllib.request
import json
import sys, time
from multiprocessing import Process, Queue, Lock, Pipe
import re, traceback
from bs4 import BeautifulSoup
#If you are getting error use difference token
NO_OF_USER_ON_ONE_PAGE=30
if len(sys.argv)==1:
print("Usage: python crawl_code.py OUTPUT_FOLDER")
output_folder=sys.argv[1]
token="NjY0ZGIxN2MyZmUwZmI2N2YyMGQ1MTIzNDI3Yjg5Y2J8fDE1NTAyMzk4MDg2MjkzODI%3D"
contest_id=sys.argv[1]
no_of_participants=5024
FLAG=1 # make 0 if you want to save without username
def get_page_links(NO_OF_PARTICIPANTS,contest_id,token):
links=[]
for i in range(int(NO_OF_PARTICIPANTS/NO_OF_USER_ON_ONE_PAGE)):
start_pos=i*NO_OF_USER_ON_ONE_PAGE+1
links+=['https://code.google.com/codejam/contest/'+str(contest_id)+'/scoreboard/do/?cmd=GetScoreboard&contest_id='+str(contest_id)+'&show_type=all&start_pos='+str(start_pos)+'&csrfmiddlewaretoken='+str(token)]
print("Total links :",len(links))
return links
def get_user_links(usernames,contest_id,token,problem_id):
links=[]
for username in usernames:
links+=['https://code.google.com/codejam/contest/'+str(contest_id)+'/scoreboard/do/?cmd=GetSourceCode&problem='+str(problem_id)+'&io_set_id='+str(set_id)+'&username='+username+'&csrfmiddlewaretoken='+str(token)]
print("No of usernames found: "+str(len(links)))
return links
def get_problem_id():
web = urlopen('https://code.google.com/codejam/contest/'+str(contest_id)+'/scoreboard')
soup = BeautifulSoup(web.read(), 'lxml')
data = soup.find_all("script")[1].string
m = re.findall('GCJ.problems.push.+?\n.+?\"id\": \"(.+?)\",', data)
return m
pagelinks=get_page_links(no_of_participants,contest_id,token)
# q = []
THREADS=int(sys.argv[2])
NO_OF_PAGES_PER_THREAD=int(len(pagelinks)/THREADS)+1
def extractPages(id,):
file=open("TEMP/T"+str(id),'w')
usernames=[]
start=id*NO_OF_PAGES_PER_THREAD
end=min((id+1)*NO_OF_PAGES_PER_THREAD,len(pagelinks))
print(start,end-1)
i=1
length=len(pagelinks[start:end])
for link in pagelinks[start:end]:
while True:
try:
print("Thread "+str(id)+" Fetching Page "+str(i)+"/"+str(length))
# l = urlopen(link)
# # web = l
# # print(link.split('/do/'))
# web = urlopen(link.split('/do/')[0])
# soup = BeautifulSoup(web.read(), 'lxml')
# data = soup.find_all("script")[1].string
# # print(data)
# m = re.findall('GCJ.problems.push.+?\n.+?\"id\": \"(.+?)\",', data)
# # print(m)
# print("===============================================================\n")
res=json.load(urlopen(link))
for row in res['rows']:
usernames+=[row['n']]
i += 1
except KeyboardInterrupt:
exit()
except :
traceback.print_exc(file=sys.stdout)
print("exception")
continue
break
for user in usernames:
file.write(user+"\n")
file.close()
# lock.acquire()
# q.put(usernames)
# print(q)
# lock.release()
os.system("rm -rf TEMP")
os.mkdir("TEMP")
def extractCode(id,NO_OF_USER_PER_THREAD,usernames):
start=id*NO_OF_USER_PER_THREAD
end=min((id+1)*NO_OF_USER_PER_THREAD,len(usernames))
print(start,end-1)
length=len(usernames[start:end])
code_no=1
userlinks=get_user_links(usernames[start:end],contest_id,token,problem_id)
for link,username in zip(userlinks,usernames[start:end]):
try:
if FLAG==1:
name = username
else:
name= "Code"+str(code_no)
print("Thread "+str(id)+" Fetching code "+str(code_no)+"/"+str(length))
urllib.request.urlretrieve(link,output_folder+"/"+name+".zip")
code_no+=1
except KeyboardInterrupt:
exit()
except:
traceback.print_exc(file=sys.stdout)
print("Error for user: "+username+" Link:"+link)
def workers():
p = []
usernames = []
for id in range(0,THREADS):
p += [Process(target=extractPages, args=(id,))]
print(len(p))
for thread in p:
thread.start()
for thread in p:
thread.join()
usernames=[]
for id in range(0,THREADS):
with open("TEMP/T"+str(id)) as f:
for user in f:
usernames+=[user.rstrip()]
print(len(usernames))
NO_OF_USER_PER_THREAD=int(len(usernames)/THREADS)+1
p = []
for id in range(0,THREADS):
p += [Process(target=extractCode, args=(id,NO_OF_USER_PER_THREAD,usernames))]
for thread in p:
thread.start()
for thread in p:
thread.join()
usernames = []
NO_OF_USER_PER_THREAD = 0
pids = get_problem_id()
os.mkdir(output_folder)
c_of = output_folder
for problem_id in pids:
os.mkdir(c_of+'/'+str(problem_id))
output_folder = c_of+'/'+str(problem_id)
p_of = output_folder
for set_id in range(0,2):
output_folder = p_of+'/'+str(set_id)
os.mkdir(output_folder)
print(output_folder)
workers()
time.sleep(10)
# problem_id="5719039502450688"
# set_id=1 # 0 for small and 1 for large