In this article, we will make a list of all the users of Codeforces and store their information in a JSON file. We will use the json and urlib.request library of python for the purpose.
We will use Codeforces API to access the data and store the required data in a list. We will finally sort it according to their maxRating to get a list of all users of Codeforces in descending order.
First, let us learn about JSON and API.
What is API?
API stands for Application Programming Interface. An API is a set of routines, protocols, and tools for building software applications. It is a software piece that can be used by another piece of software to allow applications to talk to each other.
What is JSON?
Let us learn about the libraries used in the program.
To support JSON in python, we need the in-built library json. The json module provides an API similar to pickle for converting in-memory Python objects to a JSON object.
Urllib module is the in-built URL handling module for python. It is used to fetch URLs. It uses the urlopen function and can fetch URLs using a variety of different protocols.
Urllib is a package that collects several modules for working with URLs, such as:
- urllib.request for opening and reading.
- urllib.parse for parsing URLs
- urllib.error for the exceptions raised
- urllib.robotparser for parsing robot.txt files
We will make use of urllib.request package in this program.
Looking for API
Codeforces presents several API to work with and to access public data. To make a list of all the users, we will use the method user.ratedList.
It returns the list of users who have participated in at least one contest and their information in decreasing order of their rating.
To only choose users who have been active in the past month, the parameter activeOnly can be passed as true with the query. Otherwise, all users are returned.
The API returns JSON string with keys status and result. If successful, status is OK; the result contains a dictionary containing the handle and other information about users.
Note If the status is FAILED, then the comment contains why the request failed. If the status is OK, then there is no comment. If the status is FAILED, then there is no result.
The API used, looks like the image below, when opened in the browser.
Reading the JSON string
We will use the json and urllib.request to open and load the API.
First, import the libraries.
import json import urllib.request
Then, load the URL from which the API is accessed.
query = "https://codeforces.com/api/user.ratedList?activeOnly=false" with urllib.request.urlopen(query) as url: data = json.loads(url.read().decode())
Note Language-depended fields like names or descriptions will be returned in the default language. So, you can pass additional parameter lang with values en and ru to select the result’s language.
The code reads the URL’s API and stores it in a Python object, a dict object.
Extracting the user data
data is the JSON string that contains two keys status and result.
Next, extract the user’s information from the data and save it in a list, in the given code-named users.
We are only keeping the users handle, country, and maxRating. To store different parameters from the one selected, the respective keys can be chosen.
For the user entries that do not have countries specified, the respective key does not exist in the dict object, hence to avoid key error, "if-else" statements are used. The country for such entries is written as "Unknown".
users = data["result"] result =  for i in range(len(users)): if 'country' in users[i]: result.append([users[i]['handle'], users[i]['country'], users[i]['maxRating']]) else: result.append([ users[i]['handle'], "Unknown", users[i]['maxRating']])
For example, one of the entries in the list looks like
["MiFaFaOvO", "China", 3681]
If the country is not available, then the entry would be appended in the list as
["Um_nik", "Unknown", 3567]
- Further, to select users with specific constraints, additional conditions can be added, such as to select users from one particular country, add an additional condition in if statement specifying the value of the dict key.
- While selecting another key-value pair from user entries, you should ensure that the key appears in all list items or take necessary measures, else the program might result in a key error.
Finally, sort the result according to the maxRating to get a list of top-rated users.
We will use the lambda feature in python to sort the list according to the parameter with index 2, which is maxRating. The negative sign is added to sort the list in descending order.
result = sorted(result, key=lambda x: -x)
Writing JSON to a file
To store this data in a JSON file, we can use Python's built-in JSON package to transform Python list object into a JSON array.
We use with statement to open the file in which we want to store the data and then use json.dump to write the data object to the outfile file.
Refer to the following code.
with open('user_list.json', 'w') as outfile: json.dumps(result, outfile)
The entire output is list that contains all the users who have participated in atleast one contest.
[["tourist", "Belarus", 3739],
["MiFaFaOvO", "China", 3681],
["Petr", "Switzerland", 3597],
["Um_nik", "Unknown", 3567],
["Benq", "United States", 3539],
["Radewoosh", "Poland", 3527],
["mnbvmar", "Poland", 3509],
["ecnerwala", "United States", 3458],
["apiadu", "Samoa", 3438],
["maroonrk", "Japan", 3431],
["TLE", "China", 3374],
["LHiC", "Russia", 3355],...]
saved in a user_list.json file.
In this way, with this article at OpenGenus, you have made a list of top users from the Codeforces website. We hope this article helped you understand how to work with Codeforces API and manipulate the JSON string to make a list of top-rated users.