-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetch.py
More file actions
96 lines (76 loc) · 3.1 KB
/
fetch.py
File metadata and controls
96 lines (76 loc) · 3.1 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
"""
Chess.com API integration for fetching user games.
"""
import requests
from typing import List, Dict, Any, Optional
import time
class ChessComAPI:
"""Handles interactions with the Chess.com API."""
BASE_URL = "https://api.chess.com/pub"
def __init__(self):
self.session = requests.Session()
self.session.headers.update({
'User-Agent': 'Chess-Insight-Analyzer/1.0'
})
def get_user_games(self, username: str, count: int = 5) -> List[Dict[str, Any]]:
"""
Fetch recent games for a user.
Args:
username: Chess.com username
count: Number of games to fetch (default: 5)
Returns:
List of game data dictionaries
"""
try:
# Get user's game archives
archives_url = f"{self.BASE_URL}/player/{username}/games/archives"
response = self.session.get(archives_url)
response.raise_for_status()
archives_data = response.json()
archives = archives_data.get('archives', [])
if not archives:
print(f"No games found for user {username}")
return []
# Get games from the most recent archives
all_games = []
for archive_url in archives[-3:]: # Last 3 months
try:
games_response = self.session.get(archive_url)
games_response.raise_for_status()
games_data = games_response.json()
games = games_data.get('games', [])
all_games.extend(games)
# Rate limiting
time.sleep(0.1)
except requests.RequestException as e:
print(f"Error fetching games from {archive_url}: {e}")
continue
# Sort by date and take the most recent
all_games.sort(key=lambda x: x.get('end_time', 0), reverse=True)
return all_games[:count]
except requests.RequestException as e:
print(f"Error fetching games for {username}: {e}")
return []
def get_game_pgn(self, game_data: Dict[str, Any]) -> Optional[str]:
"""
Extract PGN from game data.
Args:
game_data: Game data dictionary from Chess.com API
Returns:
PGN string or None if not available
"""
return game_data.get('pgn')
def validate_username(self, username: str) -> bool:
"""
Validate if a username exists on Chess.com.
Args:
username: Chess.com username to validate
Returns:
True if username exists, False otherwise
"""
try:
profile_url = f"{self.BASE_URL}/player/{username}"
response = self.session.get(profile_url)
return response.status_code == 200
except requests.RequestException:
return False