-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathsolved_update.py
More file actions
305 lines (281 loc) · 10.9 KB
/
Copy pathsolved_update.py
File metadata and controls
305 lines (281 loc) · 10.9 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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
import requests
import bs4
import os, django, json
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "codedigger.settings")
django.setup()
from lists.models import Solved
from user.models import Profile, User
from problem.models import Problem
from codeforces.api import user_status
from user.exception import ValidationException
def codechef(user, prob):
if user is None or Solved.objects.filter(problem=prob, user=user).exists():
return
codechef_handle = Profile.objects.get(owner=user).codechef
if codechef_handle is None:
return
url = 'https://www.codechef.com/users/' + str(codechef_handle)
res = requests.get(url)
soup = bs4.BeautifulSoup(res.content, 'html.parser')
problems_solved = soup.find(
'section', {'class': 'rating-data-section problems-solved'})
if not problems_solved or problems_solved.find(
'h5').text == 'Fully Solved (0)':
return
probset = set([])
for ele in problems_solved.find('article').find_all('a'):
probset.add(ele.text)
if prob.prob_id in probset:
Solved.objects.create(user=user, problem=prob)
def spoj(user, prob):
if user is None or Solved.objects.filter(problem=prob, user=user).exists():
return
spoj_handle = Profile.objects.get(owner=user).spoj
if spoj_handle == None:
return
url = 'https://www.spoj.com/status/' + str(
prob.prob_id) + ',' + str(spoj_handle)
res = requests.get(url)
soup = bs4.BeautifulSoup(res.content, 'html.parser')
status = soup.find('td', {'status': '15'})
if status is not None:
Solved.objects.create(user=user, problem=prob)
def codeforces(user):
if user is None:
return
cf_handle = Profile.objects.get(owner=user).codeforces
if cf_handle == None:
return
try:
submission_user = user_status(cf_handle)
except ValidationException:
return
limit = 10
for ele in submission_user:
if 'verdict' not in ele or 'contestId' not in ele or ele[
'verdict'] != 'OK':
continue
prob_id = str(ele['problem']['contestId']) + str(
ele['problem']['index'])
prob = Problem.objects.filter(prob_id=prob_id, platform='F')
if not prob.exists():
continue
solve, created = Solved.objects.get_or_create(user=user,
problem=prob[0])
if not created:
limit -= 1
if limit <= 0:
break
continue
# url = 'https://codeforces.com/api/user.status?handle=' + str(cf_handle)
# res = requests.get(url)
# if res.status_code != 200:
# return
# req = res.json()
# if req['status'] != 'OK':
# return
def uva(user):
if user is None:
return
uva_id = Profile.objects.get(owner=user).uva_id
if uva_id is None:
return
url1 = "https://uhunt.onlinejudge.org/api/subs-user/" + str(uva_id)
req1 = requests.get(url1).json()
sorted_req1 = sorted(req1['subs'], key=lambda k: k[4], reverse=True)
limit = 10
for ele in sorted_req1:
if str(ele[2]) != '90':
continue
prob = Problem.objects.filter(prob_id=str(ele[1]), platform='U')
if not prob.exists():
continue
solve, created = Solved.objects.get_or_create(user=user,
problem=prob[0])
if not created:
limit -= 1
if limit <= 0:
break
continue
def atcoder(user):
if user is None:
return
atcoder_handle = Profile.objects.get(owner=user).atcoder
if atcoder_handle is None:
return
url = 'https://kenkoooo.com/atcoder/atcoder-api/results?user=' + str(
atcoder_handle)
req = requests.get(url).json()
sorted_req = sorted(req, key=lambda k: k['epoch_second'], reverse=True)
limit = 10
for ele in sorted_req:
if ele['result'] != "AC":
continue
prob = Problem.objects.filter(prob_id=ele['problem_id'], platform='A')
if not prob.exists():
continue
solve, created = Solved.objects.get_or_create(user=user,
problem=prob[0])
if not created:
limit -= 1
if limit <= 0:
break
continue
def atcoder_scraper_check(user, prob):
if user is None or Solved.objects.filter(problem=prob, user=user).exists():
return
atcoder_handle = Profile.objects.get(owner=user).atcoder
if atcoder_handle is None:
return
contest = prob.contest_id
taskname = prob.prob_id
URL = "https://atcoder.jp/contests/" + contest + "/submissions/?f.User=" + user.username + "&" + "f.Task=" + taskname
r = requests.get(URL)
soup = bs4.BeautifulSoup(r.content, 'html5lib')
check = soup.find_all("span", {"class": "label label-success"})
if check:
Solved.objects.create(user=user, problem=prob)
def UpdateforUserCodeforces(user, limit):
# limit should either be None, or be an integer greater than or equal to 1.
if user is None:
return (False, "Given User object cannot be None")
cf_handle = Profile.objects.get(owner=user).codeforces
if cf_handle == None:
return (False, "cf_handle cannot be None.")
try:
submission_user = user_status(cf_handle)
except ValidationException:
return (False, "Not able to fetch submission data from codeforces.")
for ele in submission_user:
if 'verdict' not in ele or 'contestId' not in ele or ele[
'verdict'] != 'OK':
continue
prob_id = str(ele['problem']['contestId']) + str(
ele['problem']['index'])
prob = Problem.objects.filter(prob_id=prob_id, platform='F')
if not prob.exists():
continue
solve, created = Solved.objects.get_or_create(user=user,
problem=prob[0])
if not created and limit != None:
limit -= 1
if limit <= 0:
break
continue
return (True,
"Submission data for the given user has been saved successfully.")
def UpdateforUserCodechef(user, limit):
if user is None:
return (False, "Given User object cannot be None")
codechef_handle = Profile.objects.get(owner=user).codechef
if codechef_handle is None:
return (False, "codechef_handle cannot be None.")
url = 'https://www.codechef.com/users/' + str(codechef_handle)
res = requests.get(url)
soup = bs4.BeautifulSoup(res.content, 'html.parser')
problems_solved = soup.find(
'section', {'class': 'rating-data-section problems-solved'})
if problems_solved is None:
return (
True,
"Submission data for the given user has been saved successfully.")
if problems_solved.find('h5').text == 'Fully Solved (0)':
return (
True,
"Submission data for the given user has been saved successfully.")
for ele in problems_solved.find('article').find_all('a'):
prob = Problem.objects.filter(prob_id=ele.text, platform='C')
if not prob.exists():
continue
solve, created = Solved.objects.get_or_create(problem=prob[0],
user=user)
if not created and limit != None:
limit -= 1
if limit <= 0:
break
continue
return (True,
"Submission data for the given user has been saved successfully.")
def UpdateforUserAtcoder(user, limit):
if user is None:
return (False, "Given User object cannot be None")
atcoder_handle = Profile.objects.get(owner=user).atcoder
if atcoder_handle is None:
return (False, "atcoder_handle cannot be None.")
url = 'https://kenkoooo.com/atcoder/atcoder-api/results?user=' + atcoder_handle
req = requests.get(url)
if req.status_code != 200:
return (False, "User with the given handle does not exist.")
req = req.json()
sorted_req = sorted(req, key=lambda k: k['epoch_second'], reverse=True)
for ele in sorted_req:
if ele['result'] != "AC":
continue
prob = Problem.objects.filter(prob_id=ele['problem_id'], platform='A')
if not prob.exists():
continue
solve, created = Solved.objects.get_or_create(user=user,
problem=prob[0])
if not created and limit != None:
limit -= 1
if limit <= 0:
break
continue
return (True,
"Submission data for the given user has been saved successfully.")
def UpdateforUserSpoj(user, limit):
if user is None:
return (False, "Given User object cannot be None")
spoj_handle = Profile.objects.get(owner=user).spoj
if spoj_handle == None:
return (False, "spoj_handle cannot be None.")
url = 'https://www.spoj.com/users/' + spoj_handle
res = requests.get(url)
soup = bs4.BeautifulSoup(res.content, 'html.parser')
problems = soup.find('table', {'class': 'table table-condensed'})
if problems is None:
return (
True,
"Submission data for the given user has been saved successfully.")
for ele in problems.find_all('td'):
if ele.text == "":
continue
prob = Problem.objects.filter(prob_id=ele.text, platform='S')
if not prob.exists():
continue
solve, created = Solved.objects.get_or_create(problem=prob[0],
user=user)
if not created and limit != None:
limit -= 1
if limit <= 0:
break
continue
return (True,
"Submission data for the given user has been saved successfully.")
def UpdateforUserUva(user, limit):
if user is None:
return (False, "Given User object cannot be None")
uva_id = Profile.objects.get(owner=user).uva_id
if uva_id is None:
return (False, "uva_id cannot be None.")
url1 = "https://uhunt.onlinejudge.org/api/subs-user/" + str(uva_id)
req1 = requests.get(url1)
if req1.status_code != 200:
return (False, "User with the given UVA-ID does not exist")
req1 = req1.json()
sorted_req1 = sorted(req1['subs'], key=lambda k: k[4], reverse=True)
for ele in sorted_req1:
if str(ele[2]) != '90':
continue
prob = Problem.objects.filter(prob_id=str(ele[1]), platform='U')
if not prob.exists():
continue
solve, created = Solved.objects.get_or_create(user=user,
problem=prob[0])
if not created and limit != None:
limit -= 1
if limit <= 0:
break
continue
return (True,
"Submission data for the given user has been saved successfully.")