Extracting games from lichess public api

768 views Asked by At

I'm trying to download and analyse my chess games that i played on lichess.org for a project, and as per their public api they state that the response should be returned in ndjson format, however using the .json() method on the response body yields an a JSONDecode error

I want to be able to extract game data for each game and so far that is how I have reached:

params = {
"perfType": "blitz,rapid,classical"
}
response = requests.get('https://lichess.org/api/games/user/RAMEZESSAM1', params=params)

And when iterating over the content:

for item in response.iter_lines():
   print(item.decode())

The output looks like this: enter image description here

Just the decoded response byte text for all of my games

Any help would be appreciated to extract the game one by one and maybe store them as a json object

2

There are 2 answers

0
Nick Papadiochos On

Just like you set the perfType query parameter, you can also set the pgnInJson parameter to true. This one defaults to false, that's why you get back just the pgn formatted response.

https://lichess.org/api#operation/apiGamesUser

Give it a try !

enter image description here

0
AAL On

To get the data in json format, you need to specify the header 'Accept': 'application/x-ndjson' in the request ( How to specify requests to return data as JSON?):

response = requests.get('https://lichess.org/api/games/user/RAMEZESSAM1', params=params, headers={"Accept": "application/x-ndjson"})

Each line in the response is a json object.