Competitive_Programming_Score_API icon indicating copy to clipboard operation
Competitive_Programming_Score_API copied to clipboard

feature : list of Top Coder

Open cs20m002 opened this issue 4 years ago • 6 comments

any API to get top coder list from Codechef

cs20m002 avatar Mar 24 '21 20:03 cs20m002

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:

  1. x-csrf-token: value is returned as a window.csrfToken in html response of codechef ratings/homepage
  2. cookie: 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.

saurabh-prakash avatar Mar 30 '21 18:03 saurabh-prakash

Another method is parsing html table on this page https://www.codechef.com/ratings/all. I, personally, don't like this method though.

saurabh-prakash avatar Mar 30 '21 19:03 saurabh-prakash

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:

  1. x-csrf-token: value is returned as a window.csrfToken in html response of codechef ratings/homepage
  2. cookie: 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?

Abhijeet-AR avatar Apr 09 '21 14:04 Abhijeet-AR

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.

saurabh-prakash avatar Apr 10 '21 08:04 saurabh-prakash

Sounds good 🙂

Abhijeet-AR avatar Apr 10 '21 10:04 Abhijeet-AR

+1

nisarg0 avatar May 28 '21 09:05 nisarg0