Open cs20m002 opened 3 years ago
Well, codechef removed its APIs from public access some time back. However, if you're still adamant, you can inspect the ratings page and observe the APIs it hits. One interesting API is https://www.codechef.com/api/ratings/all?sortBy=global_rank&order=asc&page=1&itemsPerPage=20 but it requires x-csrf-token
and cookie
headers to be present to return you a response. You can get these 2 things:
x-csrf-token
: value is returned as a window.csrfToken
in html response of codechef ratings/homepagecookie
: use the cookie returned while visiting the above page (by reusing the session)Here's a Python snippet:
import re
import requests
import json
sess = requests.Session()
res = sess.get('https://www.codechef.com')
match_obj = re.search(r"window\.csrfToken\s*=\s*'(.*?)';", res.text)
token = ''
if match_obj is not None:
token = match_obj.group(1)
print(token)
res = sess.get('https://www.codechef.com/api/ratings/all?sortBy=global_rank&order=asc&page=1&itemsPerPage=20', headers={'x-csrf-token': token})
print(res.text)
@Abhijeet-AR can decide if we want to add/provide this functionality as a part of this repository.
Another method is parsing html table on this page https://www.codechef.com/ratings/all. I, personally, don't like this method though.
Well, codechef removed its APIs from public access some time back. However, if you're still adamant, you can inspect the ratings page and observe the APIs it hits. One interesting API is https://www.codechef.com/api/ratings/all?sortBy=global_rank&order=asc&page=1&itemsPerPage=20 but it requires
x-csrf-token
andcookie
headers to be present to return you a response. You can get these 2 things:
x-csrf-token
: value is returned as awindow.csrfToken
in html response of codechef ratings/homepagecookie
: use the cookie returned while visiting the above page (by reusing the session)Here's a Python snippet:
import re import requests import json sess = requests.Session() res = sess.get('https://www.codechef.com') match_obj = re.search(r"window\.csrfToken\s*=\s*'(.*?)';", res.text) token = '' if match_obj is not None: token = match_obj.group(1) print(token) res = sess.get('https://www.codechef.com/api/ratings/all?sortBy=global_rank&order=asc&page=1&itemsPerPage=20', headers={'x-csrf-token': token}) print(res.text)
@Abhijeet-AR can decide if we want to add/provide this functionality as a part of this repository.
That's a really good solution. This repo is to provide user details of competitive coding platforms through an API. But top coders list doesn't fit in the profile. But I'm open to pivot and add more features like this. What are your thoughts @saurabh-prakash? Should we add it as a feature?
Depends on demand. If there is not enough demand for this feature, there's always an overhead cost of maintenance.
Let's keep this issue open and observe the number of +1s we get for this feature. We'll take a call (on whether to add or not) at a later date.
Sounds good 🙂
+1
any API to get top coder list from Codechef