1
1
import json
2
2
import re
3
+ # DO NOT import this after requests
4
+ import grequests
3
5
import requests
4
6
import os
5
7
@@ -114,7 +116,7 @@ def problems_solved_get():
114
116
115
117
categories = problem_solved_section .find_all ('article' )
116
118
117
- fully_solved = {'count' : int (re .findall ('\d+' , no_solved [0 ].text )[0 ])}
119
+ fully_solved = {'count' : int (re .findall (r '\d+' , no_solved [0 ].text )[0 ])}
118
120
119
121
if fully_solved ['count' ] != 0 :
120
122
for category in categories [0 ].find_all ('p' ):
@@ -125,7 +127,7 @@ def problems_solved_get():
125
127
fully_solved [category_name ].append ({'name' : prob .text ,
126
128
'link' : 'https://www.codechef.com' + prob ['href' ]})
127
129
128
- partially_solved = {'count' : int (re .findall ('\d+' , no_solved [1 ].text )[0 ])}
130
+ partially_solved = {'count' : int (re .findall (r '\d+' , no_solved [1 ].text )[0 ])}
129
131
if partially_solved ['count' ] != 0 :
130
132
for category in categories [1 ].find_all ('p' ):
131
133
category_name = category .find ('strong' ).text [:- 1 ]
@@ -160,16 +162,40 @@ def user_details_get():
160
162
return details
161
163
162
164
def __codeforces (self ):
163
- url = 'https://codeforces.com/api/user.info?handles={}' .format (self .__username )
165
+ urls = {
166
+ "user_info" : {"url" : f'https://codeforces.com/api/user.info?handles={ self .__username } ' },
167
+ "user_contests" : {"url" : f'https://codeforces.com/contests/with/{ self .__username } ' }
168
+ }
164
169
165
- page = requests .get (url )
166
-
167
- if page .status_code != 200 :
168
- raise UsernameError ('User not Found' )
170
+ reqs = [grequests .get (item ["url" ]) for item in urls .values () if item .get ("url" )]
169
171
170
- details_api = page . json ( )
172
+ responses = grequests . map ( reqs )
171
173
172
- if details_api ['status' ] != 'OK' :
174
+ details_api = {}
175
+ contests = []
176
+ for page in responses :
177
+ if page .status_code != 200 :
178
+ raise UsernameError ('User not Found' )
179
+ if page .request .url == urls ["user_info" ]["url" ]:
180
+ details_api = page .json ()
181
+ elif page .request .url == urls ["user_contests" ]["url" ]:
182
+ soup = BeautifulSoup (page .text , 'html.parser' )
183
+ table = soup .find ('table' , attrs = {'class' : 'user-contests-table' })
184
+ table_body = table .find ('tbody' )
185
+
186
+ rows = table_body .find_all ('tr' )
187
+ for row in rows :
188
+ cols = row .find_all ('td' )
189
+ cols = [ele .text .strip () for ele in cols ]
190
+ contests .append ({
191
+ "Contest" : cols [1 ],
192
+ "Rank" : cols [3 ],
193
+ "Solved" : cols [4 ],
194
+ "Rating Change" : cols [5 ],
195
+ "New Rating" : cols [6 ]
196
+ })
197
+
198
+ if details_api .get ('status' ) != 'OK' :
173
199
raise UsernameError ('User not Found' )
174
200
175
201
details_api = details_api ['result' ][0 ]
@@ -186,10 +212,16 @@ def __codeforces(self):
186
212
rank = 'Unrated'
187
213
max_rank = 'Unrated'
188
214
189
- details = {'status' : 'Success' , 'username' : self .__username , 'platform' : 'Codeforces' ,
190
- 'rating' : rating , 'max rating' : max_rating , 'rank' : rank , 'max rank' : max_rank }
191
-
192
- return details
215
+ return {
216
+ 'status' : 'Success' ,
217
+ 'username' : self .__username ,
218
+ 'platform' : 'Codeforces' ,
219
+ 'rating' : rating ,
220
+ 'max rating' : max_rating ,
221
+ 'rank' : rank ,
222
+ 'max rank' : max_rank ,
223
+ 'contests' : contests
224
+ }
193
225
194
226
def __spoj (self ):
195
227
url = 'https://www.spoj.com/users/{}/' .format (self .__username )
@@ -365,6 +397,6 @@ def get_details(self, platform):
365
397
if __name__ == '__main__' :
366
398
ud = UserData ('abhijeet_ar' )
367
399
368
- ans = ud .get_details ('codechef ' )
400
+ ans = ud .get_details ('codeforces ' )
369
401
370
402
print (ans )
0 commit comments