-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchess_com.py
73 lines (59 loc) · 2.5 KB
/
chess_com.py
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import requests
import json
import csv
# Set up API endpoint and headers
username = "Hsouna1"
games_endpoint = f"https://api.chess.com/pub/player/{username}/games"
stats_endpoint = f"https://api.chess.com/pub/player/{username}/stats"
headers = {
"Accept": "application/json"
}
# Make API request for games
response = requests.get(games_endpoint, headers=headers)
# Check if the request was successful
if response.status_code == 200:
# Retrieve the game data from the response
game_data = response.json()
# Filter blitz games
blitz_games = [game for game in game_data["games"] if game["time_class"] == "blitz"]
# Make API request for stats
response = requests.get(stats_endpoint, headers=headers)
# Check if the request was successful
if response.status_code == 200:
# Retrieve the stats data from the response
stats_data = response.json()
# Retrieve blitz game stats
blitz_stats = stats_data.get("chess_blitz", {})
# Retrieve blitz game record
blitz_record = blitz_stats.get("record", {})
# Retrieve blitz game best
blitz_best = blitz_stats.get("best", {})
# Retrieve blitz game last
blitz_last = blitz_stats.get("last", {})
# Create CSV file
with open("blitz_games.csv", mode="w", newline="") as file:
writer = csv.DictWriter(file, fieldnames=blitz_games[0].keys())
writer.writeheader()
for game in blitz_games:
writer.writerow(game)
# Print some stats
print("Blitz Game Record:")
print("Wins:", blitz_record.get("win"))
print("Losses:", blitz_record.get("loss"))
print("Draws:", blitz_record.get("draw"))
print("Timeout percentage in last 90 days:", blitz_record.get("timeout_percent"))
print()
print("Blitz Game Best:")
print("Date:", blitz_best.get("date"))
print("Rating:", blitz_best.get("rating"))
print("Game URL:", blitz_best.get("game"))
print()
print("Blitz Game Last:")
print("Date:", blitz_last.get("date"))
print("Rating:", blitz_last.get("rating"))
print("RD:", blitz_last.get("rd"))
print()
else:
print("Error retrieving stats. Status code:", response.status_code)
else:
print("Error retrieving game history. Status code:", response.status_code)