1

I want to create a React component that fetches data from Stack Overflow of a specific user. I haven't had any issues to get: reputation, answers, badges, etc. using Stack Exchange API v2.3 but I'm having a hard time trying to implement an API that will return the current ranking as shown in this Stack Exchange Data Explorer query and in the Stack Exchange reputation leagues.

Almost 4 years ago a user asked this question without any response. Why is that? Is it impossible?

double-beep
  • 5,031
  • 17
  • 33
  • 41
Gass
  • 7,536
  • 3
  • 37
  • 41

1 Answers1

1

This is not possible via the API. You can use two other "tricks", though (both must be done server-side because the sites return CORS headers):

  1. Scrape the /leagues page. Call the endpoint:

    GET /leagues/1/<type>/<site>/<YYYY-MM-DD>/<userId>
    

    (e.g. /leagues/1/quarter/stackoverflow/2021-01-01/10607772), look for the .highlight .number selector and get the inner text. If you want just the number, remember to remove # in the front of the result.

    However, the probem is that all league URLs exist in robots.txt, so if you make too many requests, you might be rate-limited.

  2. Fetch the information from Data Explorer. Call:

    POST /query/run/<siteId>/<queryId>/97298
    

    (for the query you've linked, the site id is 1 and the query id 6772).

    • If running exists in the returned JSON and is true, then using job_id call:

      POST /query/job/<job_id>
      

      every 1500 milliseconds (that's what Stack Exchange uses) until resultSets is present in the response.

    • If not, then look directly for resultSets.

    Here's a sample value of that property:

    [
        {
            "columns": [
                {
                    "name": "Id",
                    "type": "Number"
                },
                {
                    "name": "Ranking",
                    "type": "Number"
                },
                {
                    "name": "Percentile",
                    "type": "Number"
                }
            ],
            "rows": [
                [
                    10607772,
                    65137,
                    0.0586909706892046
                ]
            ],
            "messagePosition": 0,
            "truncated": false
        }
    ]
    

    Note that the results are only updated every Sunday. You might get rate-limited as well if you make too many requests in a short time period.

double-beep
  • 5,031
  • 17
  • 33
  • 41