0

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

  • always put code, data and full error message as text (not screenshot, not link) in question (not in comment). It will be more readable and easier to use in answer, and more people will see it - so more people can help you. – furas May 25 '22 at 14:14

2 Answers2

1

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

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.

AAL
  • 81
  • 3