Contest tools and workflow
eulerscheZahl
24.2K views
Using the API with Python
Now that we know how to use the API, let's send some requests with Python!
The following code will download a replay:
Python
1
2
3
4
5
6
7
8
9
10
11
12
13
import json
import requests
game_id = 453253378
r = requests.post(
'https://www.codingame.com/services/gameResultRemoteService/findByGameId',
json = [str(game_id), None]
)
replay = r.json()
print(replay)
with open(f'{game_id}.json', 'w+') as f:
f.write(json.dumps(replay))
I recommend to save it to your disk before running a complex analysis. That way you don't have to download it multiple times when your analysis code is buggy at first.
Let's have a closer look at what we just downloaded: On the left you see the online version, on the right our downloaded json. The errorstream is missing. We need to login first in order to get it too.
Let's try again, this time with login. This code is intentionally not embedded in a runner, as it needs your login information in order to work properly. It's just for reference and offline execution.
import os, json, requests, sys
email = '[email protected]'
pw = 'YourSecretP4ssw0rd'
userID = 1500515 # that's my userID, you have to change it
game_id = 453253378
# the session object saves cookies
with requests.Session() as s:
# let's login first
p = s.post('https://www.codingame.com/services/CodingamerRemoteService/loginSiteV2', json = [email, pw, True])
# the same request as above, but with a session object
r = s.post('https://www.codingame.com/services/gameResultRemoteService/findByGameId', json = [str(game_id), userID])
replay = r.json()
# TODO save your replay
Create your playground on Tech.io
This playground was created on Tech.io, our hands-on, knowledge-sharing platform for developers.