forked from Abhijeet-AR/Competitive_Programming_Score_API
-
Notifications
You must be signed in to change notification settings - Fork 0
/
details_soup.py
229 lines (155 loc) · 7.8 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
import json
import re
import requests
from bs4 import BeautifulSoup
class UsernameError(Exception):
pass
class PlatformError(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:
rank = soup.find('div', class_='rating-number').text
except AttributeError:
raise UsernameError('User not Found')
rating = soup.find('span', class_='rating').text
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
def contests_details_get():
rating_table = soup.find('table', class_='rating-table')
rating_table_rows = rating_table.find_all('td')
'''Can add ranking url to contests'''
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)}
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)}
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)}
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
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': re.findall('\d+', no_solved[0].text)[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': re.findall('\d+', no_solved[1].text)[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
full, partial = problems_solved_get()
details = {'status': 'Success', 'rank': int(rank), 'rating': rating, 'global_rank': int(global_rank),
'country_rank': int(country_rank), 'contests': contests_details_get(),
'contest_ratings': contest_rating_details_get(), 'fully_solved': full, 'partially_solved': partial}
return details
def __codeforces(self):
url = 'https://codeforces.com/api/user.info?handles={}'.format(self.__username)
page = requests.get(url)
if page.status_code != 200:
raise UsernameError('User not Found')
details_api = page.json()
if details_api['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'
details = {'status': 'Success', 'username': self.__username, 'platform': 'Codeforces',
'rating': rating, 'max rating': max_rating, 'rank': rank, 'max rank': max_rank}
return details
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:])
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 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()
raise PlatformError('Platform not Found')
if __name__ == '__main__':
ud = UserData('ainesh02')
ans = ud.get_details('codeforces')
print(ans)