Skip to content

Commit 8a67648

Browse files
committed
endpoints & readme file updated + format & minor fixes
1 parent 81aae9a commit 8a67648

File tree

4 files changed

+60
-20
lines changed

4 files changed

+60
-20
lines changed

Readme.md

+49-7
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,25 @@
11
# CodeChefAPI
2-
The codechef API is for educational purposes only. [Heroku deployment](https://codechef-api-education-only.herokuapp.com)
2+
With CodeChef API you can get stats, contest details, all solved question links, and submission details for any user.
33

44
## Endpoints
5-
1. user-stats/<user_name>
6-
2. contest-details/<user_name>
7-
3. solved/<user_name>
8-
4. submission-details/<user_name>
5+
1. user-stats
6+
2. contest-details
7+
3. solved
8+
4. submission-details
99

1010
### user-stats
1111
Returns brief details about the user, such as the total number of contests participated, country, division, global rank, etc.
1212

13-
#### sample
13+
#### sample code
14+
```pycon
15+
import requests
16+
17+
url = "https://domain.com/user-stats"
18+
res = requests.post(url, headers={"username": "yash2003bisht"})
19+
print(res.json())
20+
```
21+
22+
#### sample response
1423
```json lines
1524
{
1625
"codechef_pro_plan": "No Active Plan",
@@ -32,6 +41,17 @@ Returns brief details about the user, such as the total number of contests parti
3241

3342
### contest-details
3443
Returns all details about user contests participated.
44+
45+
#### sample code
46+
```pycon
47+
import requests
48+
49+
url = "https://domain.com/contest-details"
50+
res = requests.post(url, headers={"username": "yash2003bisht"})
51+
print(res.json())
52+
```
53+
54+
#### sample response
3555
```json lines
3656
{
3757
"contest_details": [
@@ -64,6 +84,17 @@ Returns all details about user contests participated.
6484

6585
### solved
6686
Returns a list of links containing all questions solved by user.
87+
88+
#### sample code
89+
```pycon
90+
import requests
91+
92+
url = "https://domain.com/solved"
93+
res = requests.post(url, headers={"username": "yash2003bisht"})
94+
print(res.json())
95+
```
96+
97+
#### sample response
6798
```json lines
6899
{
69100
"solved_links": [
@@ -75,6 +106,17 @@ Returns a list of links containing all questions solved by user.
75106

76107
### submission-details
77108
Returns data from the submissions graph section.
109+
110+
#### sample code
111+
```pycon
112+
import requests
113+
114+
url = "https://domain.com/submission-details"
115+
res = requests.post(url, headers={"username": "yash2003bisht"})
116+
print(res.json())
117+
```
118+
119+
#### sample response
78120
```json lines
79121
{
80122
"compile_error": 1,
@@ -86,4 +128,4 @@ Returns data from the submissions graph section.
86128
}
87129
```
88130

89-
*Note*: The **submission-details** and **contest-details** endpoints may take longer to fetch the data, as the data are loaded dynamically, and we are using selenium to scrape it.
131+
*Note*: **submission-details** endpoints may take a longer time to fetch data, as there is some animation in data that delays loading graph details, so we are using **selenium** to scrape it.

src/API/endpoints.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ def root():
1010
data = {
1111
'message': 'Welcome to codechefAPI, this api is for educational purpose only',
1212
'endpoints': {
13-
'user-stats/<user_profile_name>': 'return user profile data details',
14-
'solved/<user_profile_name>': 'return user all solved question links',
15-
'submission-details/<user_profile_name>': 'return details from submission graph',
16-
'contest-details/<user_profile_name>': 'return user all contest participation details'
13+
'user-stats': 'return user profile data details',
14+
'solved': 'return user all solved question links',
15+
'submission-details': 'return details from submission graph',
16+
'contest-details': 'return user all contest participation details'
1717
}
1818
}
1919
return jsonify(data)

src/scraping_code/codechef_core_api_endpoints.py

+7-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from helper_functions import get_response
2-
2+
from typing import Any
33

44
headers = {
55
"authority": "www.codechef.com",
@@ -24,7 +24,7 @@
2424
}
2525

2626

27-
def contest_endpoint(contest_name: str, username: str) -> dict:
27+
def contest_endpoint(contest_name: str, username: str) -> Any:
2828
"""Codechef contest details endpoint
2929
3030
Args:
@@ -38,7 +38,8 @@ def contest_endpoint(contest_name: str, username: str) -> dict:
3838

3939
# add aditional details
4040
headers["path"] = f"/api/rankings/{contest_name}?itemsPerPage=100&order=asc&page=1&search={username}&sortBy=rank"
41-
headers["referer"] = f"https://www.codechef.com/rankings/{contest_name}?itemsPerPage=100&order=asc&page=1&search={username}&sortBy=rank"
41+
headers[
42+
"referer"] = f"https://www.codechef.com/rankings/{contest_name}?itemsPerPage=100&order=asc&page=1&search={username}&sortBy=rank"
4243

4344
res_data = get_response(url, "json", headers)
4445
details = {}
@@ -49,27 +50,25 @@ def contest_endpoint(contest_name: str, username: str) -> dict:
4950
details['rank'] = res_data['list'][0]['rank']
5051
details['total_score'] = res_data['list'][0]['score']
5152

52-
problems_solved = {}
53+
problems_solved = {}
5354
total_problems = []
5455
total_solved = 0
5556

5657
for key, value in res_data['list'][0]["problems_status"].items():
5758
problems_solved[key] = value
5859
problems_solved[key]['question_link'] = f"https://www.codechef.com/LP1TO201/problems/{key}"
5960
problems_solved[key]['submission_link'] = \
60-
f"https://www.codechef.com/rankings/{contest_name}/bestsolution/{key},{username}"
61+
f"https://www.codechef.com/rankings/{contest_name}/bestsolution/{key},{username}"
6162
total_solved += 1
62-
63+
6364
for problem in res_data['problems']:
6465
data = problem
6566
data['question_link'] = f"https://www.codechef.com/LP1TO201/problems/{data['code']}"
6667
total_problems.append(data)
6768

68-
6969
details['problems_solved'] = problems_solved
7070
details['total_problems'] = total_problems
7171
details['total_solved'] = total_solved
72-
7372

7473
return details
7574

src/scraping_code/profile_scraper.py

-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ def get_all_solved_links(username: str) -> dict:
2020
"""
2121
url = f'{BASE_URL}/users/{username}'
2222
soup = get_response(url)
23-
links = []
2423

2524
try:
2625
# get all solved questions links

0 commit comments

Comments
 (0)