-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathdetails_soup.py
607 lines (476 loc) · 25.6 KB
/
details_soup.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
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
import json
import re
# DO NOT import this after requests
import grequests
import requests
import os
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from util import get_safe_nested_key
class UsernameError(Exception):
pass
class PlatformError(Exception):
pass
class BrokenChangesError(Exception):
pass
class UserData:
def __init__(self, username=None):
self.__username = username
def update_username(self, username):
self.__username = username
def __codechef(self):
url = 'https://www.codechef.com/users/{}'.format(self.__username)
page = requests.get(url)
soup = BeautifulSoup(page.text, 'html.parser')
try:
rating = soup.find('div', class_='rating-number').text
except AttributeError:
raise UsernameError('User not Found')
stars = soup.find('span', class_='rating')
if stars:
stars = stars.text
highest_rating_container = soup.find('div', class_='rating-header')
highest_rating = highest_rating_container.find_next('small').text.split()[-1].rstrip(')')
rating_ranks_container = soup.find('div', class_='rating-ranks')
rating_ranks = rating_ranks_container.find_all('a')
global_rank = rating_ranks[0].strong.text
country_rank = rating_ranks[1].strong.text
if global_rank != 'NA' and global_rank != 'Inactive':
global_rank = int(global_rank)
country_rank = int(country_rank)
def contests_details_get():
rating_table = soup.find('table', class_='rating-table')
if not rating_table:
return []
rating_table_rows = rating_table.find_all('td')
'''Can add ranking url to contests'''
try:
long_challenge = {'name': 'Long Challenge', 'rating': int(rating_table_rows[1].text),
'global_rank': int(rating_table_rows[2].a.hx.text),
'country_rank': int(rating_table_rows[3].a.hx.text)}
except ValueError:
long_challenge = {'name': 'Long Challenge', 'rating': int(rating_table_rows[1].text),
'global_rank': rating_table_rows[2].a.hx.text,
'country_rank': rating_table_rows[3].a.hx.text}
try:
cook_off = {'name': 'Cook-off',
'rating': int(rating_table_rows[5].text),
'global_rank': int(rating_table_rows[6].a.hx.text),
'country_rank': int(rating_table_rows[7].a.hx.text)}
except ValueError:
cook_off = {'name': 'Cook-off',
'rating': int(rating_table_rows[5].text),
'global_rank': rating_table_rows[6].a.hx.text,
'country_rank': rating_table_rows[7].a.hx.text}
try:
lunch_time = {'name': 'Lunch Time', 'rating': int(rating_table_rows[9].text),
'global_rank': int(rating_table_rows[10].a.hx.text),
'country_rank': int(rating_table_rows[11].a.hx.text)}
except ValueError:
lunch_time = {'name': 'Lunch Time', 'rating': int(rating_table_rows[9].text),
'global_rank': rating_table_rows[10].a.hx.text,
'country_rank': rating_table_rows[11].a.hx.text}
return [long_challenge, cook_off, lunch_time]
def contest_rating_details_get():
start_ind = page.text.find('[', page.text.find('all_rating'))
end_ind = page.text.find(']', start_ind) + 1
next_opening_brack = page.text.find('[', start_ind + 1)
while next_opening_brack < end_ind:
end_ind = page.text.find(']', end_ind + 1) + 1
next_opening_brack = page.text.find('[', next_opening_brack + 1)
all_rating = json.loads(page.text[start_ind: end_ind])
for rating_contest in all_rating:
rating_contest.pop('color')
return all_rating
def problems_solved_get():
problem_solved_section = soup.find('section', class_='rating-data-section problems-solved')
no_solved = problem_solved_section.find_all('h5')
categories = problem_solved_section.find_all('article')
fully_solved = {'count': int(re.findall(r'\d+', no_solved[0].text)[0])}
if fully_solved['count'] != 0:
for category in categories[0].find_all('p'):
category_name = category.find('strong').text[:-1]
fully_solved[category_name] = []
for prob in category.find_all('a'):
fully_solved[category_name].append({'name': prob.text,
'link': 'https://www.codechef.com' + prob['href']})
partially_solved = {'count': int(re.findall(r'\d+', no_solved[1].text)[0])}
if partially_solved['count'] != 0:
for category in categories[1].find_all('p'):
category_name = category.find('strong').text[:-1]
partially_solved[category_name] = []
for prob in category.find_all('a'):
partially_solved[category_name].append({'name': prob.text,
'link': 'https://www.codechef.com' + prob['href']})
return fully_solved, partially_solved
def user_details_get():
user_details_attribute_exclusion_list = {'username', 'link', 'teams list', 'discuss profile'}
header_containers = soup.find_all('header')
name = header_containers[1].find('h1', class_="h2-style").text
user_details_section = soup.find('section', class_='user-details')
user_details_list = user_details_section.find_all('li')
user_details_response = {'name': name, 'username': user_details_list[0].text.split('★')[-1].rstrip('\n')}
for user_details in user_details_list:
attribute, value = user_details.text.split(':')[:2]
attribute = attribute.strip().lower()
value = value.strip()
if attribute not in user_details_attribute_exclusion_list:
user_details_response[attribute] = value
return user_details_response
full, partial = problems_solved_get()
details = {'status': 'Success', 'rating': int(rating), 'stars': stars, 'highest_rating': int(highest_rating),
'global_rank': global_rank, 'country_rank': country_rank,
'user_details': user_details_get(), 'contests': contests_details_get(),
'contest_ratings': contest_rating_details_get(), 'fully_solved': full, 'partially_solved': partial}
return details
def __codeforces(self):
urls = {
"user_info": {"url": f'https://codeforces.com/api/user.info?handles={self.__username}'},
"user_contests": {"url": f'https://codeforces.com/contests/with/{self.__username}'}
}
reqs = [grequests.get(item["url"]) for item in urls.values() if item.get("url")]
responses = grequests.map(reqs)
details_api = {}
contests = []
for page in responses:
if page.status_code != 200:
raise UsernameError('User not Found')
if page.request.url == urls["user_info"]["url"]:
details_api = page.json()
elif page.request.url == urls["user_contests"]["url"]:
soup = BeautifulSoup(page.text, 'html.parser')
table = soup.find('table', attrs={'class': 'user-contests-table'})
table_body = table.find('tbody')
rows = table_body.find_all('tr')
for row in rows:
cols = row.find_all('td')
cols = [ele.text.strip() for ele in cols]
contests.append({
"Contest": cols[1],
"Rank": cols[3],
"Solved": cols[4],
"Rating Change": cols[5],
"New Rating": cols[6]
})
if details_api.get('status') != 'OK':
raise UsernameError('User not Found')
details_api = details_api['result'][0]
try:
rating = details_api['rating']
max_rating = details_api['maxRating']
rank = details_api['rank']
max_rank = details_api['maxRank']
except KeyError:
rating = 'Unrated'
max_rating = 'Unrated'
rank = 'Unrated'
max_rank = 'Unrated'
return {
'status': 'Success',
'username': self.__username,
'platform': 'Codeforces',
'rating': rating,
'max rating': max_rating,
'rank': rank,
'max rank': max_rank,
'contests': contests
}
def __spoj(self):
url = 'https://www.spoj.com/users/{}/'.format(self.__username)
page = requests.get(url)
soup = BeautifulSoup(page.text, 'html.parser')
details_container = soup.find_all('p')
points = details_container[2].text.split()[3][1:]
rank = details_container[2].text.split()[2][1:]
join_date = details_container[1].text.split()[1] + ' ' + details_container[1].text.split()[2]
institute = ' '.join(details_container[3].text.split()[1:])
try:
points = float(points)
except ValueError:
raise UsernameError('User not Found')
def get_solved_problems():
table = soup.find('table', class_='table table-condensed')
rows = table.findChildren('td')
solved_problems = []
for row in rows:
if row.a.text:
solved_problems.append(row.a.text)
return solved_problems
def get_todo():
try:
table = soup.find_all('table', class_='table')[1]
except:
return None
rows = table.findChildren('td')
todo_problems = []
for row in rows:
if row.a.text:
todo_problems.append(row.a.text)
return todo_problems
details = {'status': 'Success', 'username': self.__username, 'platform': 'SPOJ',
'points': float(points), 'rank': int(rank), 'solved': get_solved_problems(),
'todo': get_todo(), 'join data': join_date, 'institute': institute}
return details
def __interviewbit(self):
url = 'https://www.interviewbit.com/profile/{}'.format(self.__username)
page = requests.get(url)
if page.status_code != 200:
raise UsernameError('User not Found')
soup = BeautifulSoup(page.text, 'html.parser')
details_main = soup.find('div', class_='user-stats')
details_container = details_main.findChildren('div', recursive=False)
details = {'status': 'Success', 'username': self.__username, 'platform': 'Interviewbit',
'rank': int(details_container[0].find('div', class_='txt').text),
'score': int(details_container[1].find('div', class_='txt').text),
'streak': details_container[2].find('div', class_='txt').text}
return details
def __atcoder(self):
url = "https://atcoder.jp/users/{}".format(self.__username)
page = requests.get(url)
if page.status_code != 200:
raise UsernameError("User not Found")
soup = BeautifulSoup(page.text, "html.parser")
tables = soup.find_all("table", class_="dl-table")
if len(tables) < 2:
details = {
"status": "Success",
"username": self.__username,
"platform": "Atcoder",
"rating": "NA",
"highest": "NA",
"rank": "NA",
"level": "NA",
}
return details
rows = tables[1].find_all("td")
try:
rank = int(rows[0].text[:-2])
current_rating = int(rows[1].text)
spans = rows[2].find_all("span")
highest_rating = int(spans[0].text)
level = spans[2].text
except Exception as E:
raise BrokenChangesError(E)
details = {
"status": "Success",
"username": self.__username,
"platform": "Atcoder",
"rating": current_rating,
"highest": highest_rating,
"rank": rank,
"level": level,
}
return details
# DEPRECATED
def __leetcode(self):
url = 'https://leetcode.com/{}'.format(self.__username)
if requests.get(url).status_code != 200:
raise UsernameError('User not Found')
options = webdriver.ChromeOptions()
options.binary_location = os.environ.get("GOOGLE_CHROME_BIN")
options.add_argument("--headless")
options.add_argument("--disable-dev-shm-usage")
options.add_argument("--no-sandbox")
# driver = webdriver.PhantomJS(executable_path='./phantomjs')
driver = webdriver.Chrome(options=options, executable_path=os.environ.get("CHROMEDRIVER_PATH"))
try:
driver.get(url)
driver.implicitly_wait(10)
hover_ranking = driver.find_element_by_xpath(
'/html/body/div[1]/div[2]/div/div[1]/div[1]/div[2]/div/div[1]/div[3]/div')
ActionChains(driver).move_to_element(to_element=hover_ranking).perform()
ranking = driver.find_element_by_xpath('/html/body/div[4]/div/div/div/div[2]').text
print('rank: ', ranking)
total_problems_solved = driver.find_element_by_xpath(
'/html/body/div[1]/div[2]/div/div[1]/div[2]/div/div[1]/div[1]/div[2]').text
acceptance_rate_span_1 = driver.find_element_by_xpath(
'/html/body/div[1]/div[2]/div/div[1]/div[2]/div/div[1]/div[2]/div[2]/div/div[1]/span[1]').text
acceptance_rate_span_2 = driver.find_element_by_xpath(
'/html/body/div[1]/div[2]/div/div[1]/div[2]/div/div[1]/div[2]/div[2]/div/div[1]/span[2]').text
acceptance_rate = str(acceptance_rate_span_1) + str(acceptance_rate_span_2)
easy_questions_solved = driver.find_element_by_xpath(
'//*[@id="profile-root"]/div[2]/div/div[1]/div[2]/div/div[2]/div/div[1]/div[2]/span[1]').text
total_easy_questions = driver.find_element_by_xpath(
'//*[@id="profile-root"]/div[2]/div/div[1]/div[2]/div/div[2]/div/div[1]/div[2]/span[2]').text
medium_questions_solved = driver.find_element_by_xpath(
'//*[@id="profile-root"]/div[2]/div/div[1]/div[2]/div/div[2]/div/div[2]/div[2]/span[1]').text
total_medium_questions = driver.find_element_by_xpath(
'//*[@id="profile-root"]/div[2]/div/div[1]/div[2]/div/div[2]/div/div[2]/div[2]/span[2]').text
hard_questions_solved = driver.find_element_by_xpath(
'//*[@id="profile-root"]/div[2]/div/div[1]/div[2]/div/div[2]/div/div[3]/div[2]/span[1]').text
total_hard_questions = driver.find_element_by_xpath(
'//*[@id="profile-root"]/div[2]/div/div[1]/div[2]/div/div[2]/div/div[3]/div[2]/span[2]').text
contribution_points = driver.find_element_by_xpath(
'/html/body/div[1]/div[2]/div/div[1]/div[3]/div[2]/div/div/div/li[1]/span').text
contribution_problems = driver.find_element_by_xpath(
'/html/body/div[1]/div[2]/div/div[1]/div[3]/div[2]/div/div/div/li[2]/span').text
contribution_testcases = driver.find_element_by_xpath(
'/html/body/div[1]/div[2]/div/div[1]/div[3]/div[2]/div/div/div/li[3]/span').text
reputation = driver.find_element_by_xpath(
'/html/body/div[1]/div[2]/div/div[1]/div[4]/div[2]/div/div/div/li/span').text
finally:
driver.close()
driver.quit()
details = {'status': 'Success', 'ranking': ranking[9:],
'total_problems_solved': total_problems_solved,
'acceptance_rate': acceptance_rate,
'easy_questions_solved': easy_questions_solved,
'total_easy_questions': total_easy_questions,
'medium_questions_solved': medium_questions_solved,
'total_medium_questions': total_medium_questions,
'hard_questions_solved': hard_questions_solved,
'total_hard_questions': total_hard_questions,
'contribution_points': contribution_points,
'contribution_problems': contribution_problems,
'contribution_testcases': contribution_testcases,
'reputation': reputation}
return details
def __leetcode_v2(self):
def __parse_response(response):
total_submissions_count = 0
total_easy_submissions_count = 0
total_medium_submissions_count = 0
total_hard_submissions_count = 0
ac_submissions_count = 0
ac_easy_submissions_count = 0
ac_medium_submissions_count = 0
ac_hard_submissions_count = 0
total_easy_questions = 0
total_medium_questions = 0
total_hard_questions = 0
total_problems_solved = 0
easy_questions_solved = 0
medium_questions_solved = 0
hard_questions_solved = 0
acceptance_rate = 0
easy_acceptance_rate = 0
medium_acceptance_rate = 0
hard_acceptance_rate = 0
total_problems_submitted = 0
easy_problems_submitted = 0
medium_problems_submitted = 0
hard_problems_submitted = 0
ranking = get_safe_nested_key(['data', 'matchedUser', 'profile', 'ranking'], response)
if ranking > 100000:
ranking = '~100000'
reputation = get_safe_nested_key(['data', 'matchedUser', 'profile', 'reputation'], response)
total_questions_stats = get_safe_nested_key(['data', 'allQuestionsCount'], response)
for item in total_questions_stats:
if item['difficulty'] == "Easy":
total_easy_questions = item['count']
if item['difficulty'] == "Medium":
total_medium_questions = item['count']
if item['difficulty'] == "Hard":
total_hard_questions = item['count']
ac_submissions = get_safe_nested_key(['data', 'matchedUser', 'submitStats', 'acSubmissionNum'], response)
for submission in ac_submissions:
if submission['difficulty'] == "All":
total_problems_solved = submission['count']
ac_submissions_count = submission['submissions']
if submission['difficulty'] == "Easy":
easy_questions_solved = submission['count']
ac_easy_submissions_count = submission['submissions']
if submission['difficulty'] == "Medium":
medium_questions_solved = submission['count']
ac_medium_submissions_count = submission['submissions']
if submission['difficulty'] == "Hard":
hard_questions_solved = submission['count']
ac_hard_submissions_count = submission['submissions']
total_submissions = get_safe_nested_key(['data', 'matchedUser', 'submitStats', 'totalSubmissionNum'],
response)
for submission in total_submissions:
if submission['difficulty'] == "All":
total_problems_submitted = submission['count']
total_submissions_count = submission['submissions']
if submission['difficulty'] == "Easy":
easy_problems_submitted = submission['count']
total_easy_submissions_count = submission['submissions']
if submission['difficulty'] == "Medium":
medium_problems_submitted = submission['count']
total_medium_submissions_count = submission['submissions']
if submission['difficulty'] == "Hard":
hard_problems_submitted = submission['count']
total_hard_submissions_count = submission['submissions']
if total_submissions_count > 0:
acceptance_rate = round(ac_submissions_count * 100 / total_submissions_count, 2)
if total_easy_submissions_count > 0:
easy_acceptance_rate = round(ac_easy_submissions_count * 100 / total_easy_submissions_count, 2)
if total_medium_submissions_count > 0:
medium_acceptance_rate = round(ac_medium_submissions_count * 100 / total_medium_submissions_count, 2)
if total_hard_submissions_count > 0:
hard_acceptance_rate = round(ac_hard_submissions_count * 100 / total_hard_submissions_count, 2)
contribution_points = get_safe_nested_key(['data', 'matchedUser', 'contributions', 'points'],
response)
contribution_problems = get_safe_nested_key(['data', 'matchedUser', 'contributions', 'questionCount'],
response)
contribution_testcases = get_safe_nested_key(['data', 'matchedUser', 'contributions', 'testcaseCount'],
response)
return {
'status': 'Success',
'ranking': str(ranking),
'total_problems_submitted': str(total_problems_submitted),
'total_problems_solved': str(total_problems_solved),
'acceptance_rate': f"{acceptance_rate}%",
'easy_problems_submitted': str(easy_problems_submitted),
'easy_questions_solved': str(easy_questions_solved),
'easy_acceptance_rate': f"{easy_acceptance_rate}%",
'total_easy_questions': str(total_easy_questions),
'medium_problems_submitted': str(medium_problems_submitted),
'medium_questions_solved': str(medium_questions_solved),
'medium_acceptance_rate': f"{medium_acceptance_rate}%",
'total_medium_questions': str(total_medium_questions),
'hard_problems_submitted': str(hard_problems_submitted),
'hard_questions_solved': str(hard_questions_solved),
'hard_acceptance_rate': f"{hard_acceptance_rate}%",
'total_hard_questions': str(total_hard_questions),
'contribution_points': str(contribution_points),
'contribution_problems': str(contribution_problems),
'contribution_testcases': str(contribution_testcases),
'reputation': str(reputation)
}
url = f'https://leetcode.com/{self.__username}'
if requests.get(url).status_code != 200:
raise UsernameError('User not Found')
payload = {
"operationName": "getUserProfile",
"variables": {
"username": self.__username
},
"query": "query getUserProfile($username: String!) { allQuestionsCount { difficulty count } matchedUser(username: $username) { contributions { points questionCount testcaseCount } profile { reputation ranking } submitStats { acSubmissionNum { difficulty count submissions } totalSubmissionNum { difficulty count submissions } } }}"
}
res = requests.post(url='https://leetcode.com/graphql',
json=payload,
headers={'referer': f'https://leetcode.com/{self.__username}/'})
res.raise_for_status()
res = res.json()
return __parse_response(res)
def get_details(self, platform):
if platform == 'codechef':
return self.__codechef()
if platform == 'codeforces':
return self.__codeforces()
if platform == 'spoj':
try:
return self.__spoj()
except AttributeError:
raise UsernameError('User not Found')
if platform == 'interviewbit':
return self.__interviewbit()
if platform == 'leetcode':
return self.__leetcode_v2()
if platform == 'atcoder':
return self.__atcoder()
raise PlatformError('Platform not Found')
if __name__ == '__main__':
ud = UserData('uwi')
ans = ud.get_details('leetcode')
print(ans)
# leetcode backward compatibility test. Commenting it out as it will fail in future
# leetcode_ud = UserData('saurabhprakash')
# leetcode_ans = leetcode_ud.get_details('leetcode')
# assert leetcode_ans == dict(status='Success', ranking='~100000', total_problems_solved='10',
# acceptance_rate='56.0%', easy_questions_solved='3', total_easy_questions='457',
# medium_questions_solved='5', total_medium_questions='901', hard_questions_solved='2',
# total_hard_questions='365', contribution_points='58', contribution_problems='0',
# contribution_testcases='0', reputation='0')