11import json
22import re
3+ # DO NOT import this after requests
4+ import grequests
35import requests
46import os
57
@@ -114,7 +116,7 @@ def problems_solved_get():
114116
115117 categories = problem_solved_section.find_all('article')
116118
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])}
118120
119121 if fully_solved['count'] != 0:
120122 for category in categories[0].find_all('p'):
@@ -125,7 +127,7 @@ def problems_solved_get():
125127 fully_solved[category_name].append({'name': prob.text,
126128 'link': 'https://www.codechef.com' + prob['href']})
127129
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])}
129131 if partially_solved['count'] != 0:
130132 for category in categories[1].find_all('p'):
131133 category_name = category.find('strong').text[:-1]
@@ -160,16 +162,40 @@ def user_details_get():
160162 return details
161163
162164 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+ }
164169
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")]
169171
170- details_api = page.json( )
172+ responses = grequests.map(reqs )
171173
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':
173199 raise UsernameError('User not Found')
174200
175201 details_api = details_api['result'][0]
@@ -186,10 +212,16 @@ def __codeforces(self):
186212 rank = 'Unrated'
187213 max_rank = 'Unrated'
188214
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+ }
193225
194226 def __spoj(self):
195227 url = 'https://www.spoj.com/users/{}/'.format(self.__username)
@@ -365,6 +397,6 @@ def get_details(self, platform):
365397if __name__ == '__main__':
366398 ud = UserData('abhijeet_ar')
367399
368- ans = ud.get_details('codechef ')
400+ ans = ud.get_details('codeforces ')
369401
370402 print(ans)
0 commit comments