Skip to content

Commit 5e6da96

Browse files
authored
Added atcoder support (#17)
* Added atcoder support * Added Atcoder in readme
1 parent b866f38 commit 5e6da96

File tree

3 files changed

+52
-2
lines changed

3 files changed

+52
-2
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,6 @@ Use this [JSON Formatter Chrome Extension](https://chrome.google.com/webstore/de
3838
* SPOJ
3939
* Interviewbit
4040
* Leetcode (new)
41+
* Atcoder
4142

4243
If you would like to leave a feedback or request a feature, please [open an issue](https://github.com/Abhijeet-AR/Competitive_Programming_Score_API/issues) or feel free to PR. Do follow [these](https://help.github.com/articles/creating-a-pull-request/) instructions to make a valid PR.

details_soup.py

+46
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ class UsernameError(Exception):
1919
class PlatformError(Exception):
2020
pass
2121

22+
class BrokenChangesError(Exception):
23+
pass
2224

2325
class UserData:
2426
def __init__(self, username=None):
@@ -296,6 +298,47 @@ def __interviewbit(self):
296298

297299
return details
298300

301+
def __atcoder(self):
302+
url = "https://atcoder.jp/users/{}".format(self.__username)
303+
304+
page = requests.get(url)
305+
306+
if page.status_code != 200:
307+
raise UsernameError("User not Found")
308+
309+
soup = BeautifulSoup(page.text, "html.parser")
310+
tables = soup.find_all("table", class_="dl-table")
311+
if len(tables)<2:
312+
details = {
313+
"status": "Success",
314+
"username": self.__username,
315+
"platform": "Atcoder",
316+
"rating": "NA",
317+
"highest": "NA",
318+
"rank": "NA",
319+
"level": "NA",
320+
}
321+
return details
322+
rows = tables[1].find_all("td")
323+
try:
324+
rank = int(rows[0].text[:-2])
325+
current_rating = int(rows[1].text)
326+
spans = rows[2].find_all("span")
327+
highest_rating = int(spans[0].text)
328+
level = spans[2].text
329+
except Exception as E:
330+
raise BrokenChangesError(E)
331+
details = {
332+
"status": "Success",
333+
"username": self.__username,
334+
"platform": "Atcoder",
335+
"rating": current_rating,
336+
"highest": highest_rating,
337+
"rank": rank,
338+
"level": level,
339+
}
340+
return details
341+
299342
# DEPRECATED
300343
def __leetcode(self):
301344
url = 'https://leetcode.com/{}'.format(self.__username)
@@ -490,6 +533,9 @@ def get_details(self, platform):
490533
if platform == 'leetcode':
491534
return self.__leetcode_v2()
492535

536+
if platform == 'atcoder':
537+
return self.__atcoder()
538+
493539
raise PlatformError('Platform not Found')
494540

495541

main.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from flask_restful import Api, Resource
33
from flask_cors import CORS
44

5-
from details_soup import UserData, UsernameError, PlatformError
5+
from details_soup import UserData, UsernameError, PlatformError, BrokenChangesError
66
from send_mail import Mail
77

88
app = Flask(__name__)
@@ -23,6 +23,9 @@ def get(self, platform, username):
2323

2424
except PlatformError:
2525
return {'status': 'Failed', 'details': 'Invalid Platform'}
26+
27+
except BrokenChangesError:
28+
return {'status': 'Failed', 'details': 'API broken due to site changes'}
2629

2730

2831
api.add_resource(Details,'/api/<string:platform>/<string:username>')
@@ -34,4 +37,4 @@ def invalid_route(e):
3437

3538

3639
if __name__ == '__main__':
37-
app.run()
40+
app.run()

0 commit comments

Comments
 (0)