-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlast_three_opponents.py
More file actions
28 lines (21 loc) · 875 Bytes
/
last_three_opponents.py
File metadata and controls
28 lines (21 loc) · 875 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import requests
import json
def get_last_three_opponents(username):
url = f"https://lichess.org/api/games/user/{username}?max=3"
response = requests.get(url, headers={"Accept": "application/x-ndjson"})
if response.status_code != 200:
print("Failed to retrieve data.")
return
games = response.text.strip().split("\n")
opponents = []
for game in games:
game_data = json.loads(game)
if game_data["players"]["white"]["user"]["name"].lower() == username.lower():
opponent = game_data["players"]["black"]["user"]["name"]
else:
opponent = game_data["players"]["white"]["user"]["name"]
opponents.append(opponent)
return opponents
username = "your_lichess_username"
last_three_opponents = get_last_three_opponents(username)
print("Last three opponents:", last_three_opponents)