Abhijeet-AR / Competitive_Programming_Score_API

API to get user details for competitive coding platforms - Codeforces, Codechef, SPOJ, Interviewbit
MIT License
183 stars 59 forks source link

feature : list of Top Coder #16

Open cs20m002 opened 3 years ago

cs20m002 commented 3 years ago

any API to get top coder list from Codechef

saurabh-prakash commented 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:

  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 commented 3 years ago

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

Abhijeet-AR commented 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:

  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?

saurabh-prakash commented 3 years ago

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.

Abhijeet-AR commented 3 years ago

Sounds good 🙂

nisarg0 commented 3 years ago

+1