-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathutils.py
239 lines (194 loc) · 7.39 KB
/
utils.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
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
import re
import json
import requests
from bs4 import BeautifulSoup as bs4
from rest_framework.exceptions import ValidationError
from rest_framework.response import Response
from lists.utils import (sub_page_number, get_next_url, get_prev_url,
get_total_page, getqs)
from utils.exception import ValidationException
from codeforces.api import user_status
from .serializers import ProbSerializer
def codeforces_status(handle):
# Deprecated
RContest = set()
VContest = set()
SolvedInContest = set()
Upsolved = set()
Wrong = set()
try:
submissions = user_status(handle=handle)
except ValidationError:
return (RContest, VContest, SolvedInContest, Upsolved, Wrong)
for submission in submissions:
if 'contestId' in submission:
# to be sure this is a contest problem
contestId = submission['contestId']
if submission['author']['participantType'] == 'CONTESTANT':
RContest.add(contestId)
elif submission['author']['participantType'] != 'PRACTICE':
VContest.add(contestId)
if 'verdict' in submission:
# to be sure verdict is present
if submission['verdict'] == 'OK':
if submission['author']['participantType'] != 'PRACTICE':
SolvedInContest.add(
str(submission['problem']['contestId']) +
submission['problem']['index'])
else:
Upsolved.add(
str(submission['problem']['contestId']) +
submission['problem']['index'])
for submission in submissions:
if 'contestId' in submission:
if 'verdict' in submission:
# to be sure verdict is present
prob_id = str(submission['problem']
['contestId']) + submission['problem']['index']
if submission[
'verdict'] != 'OK' and prob_id not in SolvedInContest and prob_id not in Upsolved:
Wrong.add(prob_id)
return (RContest, VContest, SolvedInContest, Upsolved, Wrong)
def codechef_status(handle):
Practice = set()
SolvedInContest = set()
Contest = set()
ContestName = {}
url = "https://www.codechef.com/users/" + handle
res = requests.get(url)
if res.status_code != 200:
return (Practice, SolvedInContest, Contest, ContestName)
soup = bs4(res.content, 'html5lib')
finding_rating = re.findall(r'var all_rating = .*;', str(soup))
finds = len(finding_rating) == 1
if finds:
s = finding_rating[0].replace('var all_rating = ', '').replace(';', '')
contest_details = json.loads(s)
del s
del finding_rating
Contest = set()
ContestName = {}
for contest in contest_details:
Contest.add(contest['code'])
ContestName[contest['code']] = contest['name']
problems_solved = soup.find(
'section', {'class': 'rating-data-section problems-solved'})
del soup
if problems_solved.find('h5').text == 'Fully Solved (0)':
return (Practice, SolvedInContest, Contest, ContestName)
else:
FullySolved = problems_solved.find('article')
del problems_solved
if FullySolved.find('p').find('strong').text == 'Practice:':
Practice = {x.text for x in FullySolved.find('p').findAll('a')}
SolvedInContest = set()
for y in FullySolved.findAll('p')[1:]:
for x in y.findAll('a'):
SolvedInContest.add(x.text)
else:
SolvedInContest = {
x.text
for x in problems_solved.find('article').findAll('a')
}
return (Practice, SolvedInContest, Contest, ContestName)
def atcoder_status(handle):
url = "https://atcoder.jp/users/" + handle + "/history"
res = requests.get(url)
contests_details = set()
all_contest = set()
solved = set()
wrong = set()
if res.status_code != 200:
return (contests_details, all_contest, solved, wrong)
soup = bs4(res.content, 'html5lib')
contestTable = soup.find('table', {'id': 'history'})
del soup
if contestTable != None:
contests = contestTable.find('tbody').findAll('tr')
del contestTable
for contest in contests:
contests_details.add(
contest.findAll('td')[1].find('a')['href'].split('/')[-1])
del contests
url = "https://kenkoooo.com/atcoder/atcoder-api/results?user=" + handle
res = requests.get(url)
if res.status_code != 200:
return (contests_details, all_contest, solved, wrong)
data = res.json()
for sub in data:
all_contest.add(sub["contest_id"])
if sub["result"] == "AC":
solved.add(sub["problem_id"])
for sub in data:
if sub["result"] != "AC" and sub["problem_id"] not in solved:
wrong.add(sub["problem_id"])
return (contests_details, all_contest, solved, wrong)
def get_page_number(page, default=1):
if page == None:
return default
elif page.isdigit():
return int(page)
else:
raise ValidationException('Page must be an integer.')
def get_problem_filter_response(user, page_number, per_page, url, problem_qs):
# param:
# user: Object of User Model
# page_number: Current Page Number
# per_page: number of problems in a page
# url: Base url
# problem_qs: List of Problems Total
if not user.is_authenticated:
user = None
total_problems = problem_qs.count()
total_page = get_total_page(total_problems, per_page)
if page_number > total_page:
raise ValidationException('Page Number Out of Bound')
qs = getqs(problem_qs, per_page, page_number)
res = {
"status": "OK",
"result": ProbSerializer(qs, many=True, context={
"user": user
}).data,
'link': {
'first': sub_page_number(url, 1),
'last': sub_page_number(url, total_page),
'prev': get_prev_url(page_number, url),
'next': get_next_url(page_number, url, total_page),
},
'meta': {
'current_page': page_number,
'from': (page_number - 1) * per_page + 1,
'last_page': total_page,
'path': url,
'per_page': per_page,
'to':
total_problems if page_number == total_page else page_number *
per_page,
'total': total_problems
}
}
return res
def get_upsolve_response_dict(user_contest_details, path, page, total_contest,
per_page):
total_page = get_total_page(total_contest, per_page)
Prev = get_prev_url(page, path)
Next = get_next_url(page, path, total_page)
return {
'status': 'OK',
'result': user_contest_details,
'links': {
'first': path + 'page=1',
'last': path + 'page=' + str(total_page),
'prev': Prev,
'next': Next
},
'meta': {
'current_page': page,
'from': (page - 1) * per_page + 1,
'last_page': total_page,
'path': path,
'per_page': per_page,
'to': total_contest if page == total_page else page * per_page,
'total': total_contest
}
}