2025-10-26 20:23:26 +03:00
|
|
|
import aiohttp
|
|
|
|
|
import logging
|
|
|
|
|
from typing import Optional, Dict, Any
|
|
|
|
|
from config import LICHESS_API_BASE_URL, LICHESS_STATS_API_BASE_URL
|
2025-11-18 15:10:19 +03:00
|
|
|
from rate_limiter import get_rate_limiter
|
2025-10-26 20:23:26 +03:00
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
class LichessAPI:
|
|
|
|
|
def __init__(self):
|
|
|
|
|
self.lichess_base_url = LICHESS_API_BASE_URL
|
|
|
|
|
self.stats_base_url = LICHESS_STATS_API_BASE_URL
|
2025-11-18 15:10:19 +03:00
|
|
|
self.rate_limiter = get_rate_limiter()
|
2025-10-26 20:23:26 +03:00
|
|
|
|
|
|
|
|
async def get_user_profile(self, token: str) -> Optional[Dict[str, Any]]:
|
|
|
|
|
"""Get user profile from Lichess API using token"""
|
2025-11-18 15:10:19 +03:00
|
|
|
await self.rate_limiter.wait_if_needed()
|
2025-10-26 20:23:26 +03:00
|
|
|
headers = {"Authorization": f"Bearer {token}"}
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
async with aiohttp.ClientSession() as session:
|
|
|
|
|
async with session.get(
|
|
|
|
|
f"{self.lichess_base_url}/account",
|
|
|
|
|
headers=headers
|
|
|
|
|
) as response:
|
|
|
|
|
if response.status == 200:
|
|
|
|
|
return await response.json()
|
|
|
|
|
else:
|
|
|
|
|
logger.error(f"Failed to get user profile: {response.status}")
|
|
|
|
|
return None
|
|
|
|
|
except Exception as e:
|
|
|
|
|
logger.error(f"Error getting user profile: {e}")
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
async def get_today_stats(self, username: str) -> Optional[Dict[str, Any]]:
|
|
|
|
|
"""Get today's statistics from our stats API"""
|
2025-11-18 15:10:19 +03:00
|
|
|
await self.rate_limiter.wait_if_needed()
|
2025-10-26 20:23:26 +03:00
|
|
|
try:
|
|
|
|
|
async with aiohttp.ClientSession() as session:
|
|
|
|
|
async with session.get(
|
|
|
|
|
f"{self.stats_base_url}/stats/{username}/today"
|
|
|
|
|
) as response:
|
|
|
|
|
if response.status == 200:
|
|
|
|
|
return await response.json()
|
|
|
|
|
else:
|
|
|
|
|
logger.error(f"Failed to get today stats: {response.status}")
|
|
|
|
|
return None
|
|
|
|
|
except Exception as e:
|
|
|
|
|
logger.error(f"Error getting today stats: {e}")
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
async def get_yesterday_stats(self, username: str) -> Optional[Dict[str, Any]]:
|
|
|
|
|
"""Get yesterday's statistics from our stats API"""
|
2025-11-18 15:10:19 +03:00
|
|
|
await self.rate_limiter.wait_if_needed()
|
2025-10-26 20:23:26 +03:00
|
|
|
try:
|
|
|
|
|
async with aiohttp.ClientSession() as session:
|
|
|
|
|
async with session.get(
|
|
|
|
|
f"{self.stats_base_url}/stats/{username}/yesterday"
|
|
|
|
|
) as response:
|
|
|
|
|
if response.status == 200:
|
|
|
|
|
return await response.json()
|
|
|
|
|
else:
|
|
|
|
|
logger.error(f"Failed to get yesterday stats: {response.status}")
|
|
|
|
|
return None
|
|
|
|
|
except Exception as e:
|
|
|
|
|
logger.error(f"Error getting yesterday stats: {e}")
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
async def get_week_stats(self, username: str) -> Optional[Dict[str, Any]]:
|
|
|
|
|
"""Get week's statistics from our stats API"""
|
2025-11-18 15:10:19 +03:00
|
|
|
await self.rate_limiter.wait_if_needed()
|
2025-10-26 20:23:26 +03:00
|
|
|
try:
|
|
|
|
|
async with aiohttp.ClientSession() as session:
|
|
|
|
|
async with session.get(
|
|
|
|
|
f"{self.stats_base_url}/stats/{username}/week"
|
|
|
|
|
) as response:
|
|
|
|
|
if response.status == 200:
|
|
|
|
|
return await response.json()
|
|
|
|
|
else:
|
|
|
|
|
logger.error(f"Failed to get week stats: {response.status}")
|
|
|
|
|
return None
|
|
|
|
|
except Exception as e:
|
|
|
|
|
logger.error(f"Error getting week stats: {e}")
|
|
|
|
|
return None
|
|
|
|
|
|
2025-11-16 12:48:23 +03:00
|
|
|
async def get_games_period(self, username: str, since: int, until: int, rated_only: Optional[bool] = None) -> Optional[Dict[str, Any]]:
|
2025-10-26 20:23:26 +03:00
|
|
|
"""Get games for a specific period"""
|
2025-11-18 15:10:19 +03:00
|
|
|
await self.rate_limiter.wait_if_needed()
|
2025-10-26 20:23:26 +03:00
|
|
|
try:
|
|
|
|
|
url = f"{self.stats_base_url}/games/{username}/period"
|
|
|
|
|
params = {"since": since, "until": until}
|
2025-11-16 12:48:23 +03:00
|
|
|
if rated_only is not None:
|
|
|
|
|
params["rated_only"] = "true" if rated_only else "false"
|
2025-10-26 20:23:26 +03:00
|
|
|
logger.info(f"🔍 LichessAPI.get_games_period: URL={url}, params={params}")
|
|
|
|
|
|
|
|
|
|
async with aiohttp.ClientSession() as session:
|
|
|
|
|
async with session.get(url, params=params) as response:
|
|
|
|
|
logger.info(f"🔍 LichessAPI.get_games_period: response.status={response.status}")
|
|
|
|
|
if response.status == 200:
|
|
|
|
|
result = await response.json()
|
|
|
|
|
logger.info(f"🔍 LichessAPI.get_games_period: result={result}")
|
|
|
|
|
return result
|
|
|
|
|
else:
|
|
|
|
|
logger.error(f"Failed to get games period: {response.status}")
|
|
|
|
|
return None
|
|
|
|
|
except Exception as e:
|
|
|
|
|
logger.error(f"Error getting games period: {e}")
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
async def get_puzzles_period(self, token: str, since: int, until: int, max_puzzles: int = 150) -> Optional[Dict[str, Any]]:
|
|
|
|
|
"""Get puzzles for a specific period"""
|
2025-11-18 15:10:19 +03:00
|
|
|
await self.rate_limiter.wait_if_needed()
|
2025-10-26 20:23:26 +03:00
|
|
|
headers = {"Authorization": f"Bearer {token}"}
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
async with aiohttp.ClientSession() as session:
|
|
|
|
|
async with session.get(
|
|
|
|
|
f"{self.stats_base_url}/puzzle/period",
|
|
|
|
|
headers=headers,
|
|
|
|
|
params={"since": since, "until": until, "max": max_puzzles}
|
|
|
|
|
) as response:
|
|
|
|
|
if response.status == 200:
|
|
|
|
|
return await response.json()
|
|
|
|
|
else:
|
|
|
|
|
logger.error(f"Failed to get puzzles period: {response.status}")
|
|
|
|
|
return None
|
|
|
|
|
except Exception as e:
|
|
|
|
|
logger.error(f"Error getting puzzles period: {e}")
|
|
|
|
|
return None
|
|
|
|
|
|
2025-11-07 22:54:49 +03:00
|
|
|
async def check_user_exists(self, username: str) -> bool:
|
|
|
|
|
"""Check if user exists on Lichess"""
|
2025-11-18 15:10:19 +03:00
|
|
|
await self.rate_limiter.wait_if_needed()
|
2025-11-07 22:54:49 +03:00
|
|
|
try:
|
|
|
|
|
async with aiohttp.ClientSession() as session:
|
|
|
|
|
async with session.get(
|
|
|
|
|
f"{self.lichess_base_url}/user/{username}"
|
|
|
|
|
) as response:
|
|
|
|
|
if response.status == 200:
|
|
|
|
|
return True
|
|
|
|
|
elif response.status == 404:
|
|
|
|
|
logger.warning(f"User {username} not found on Lichess (404)")
|
|
|
|
|
return False
|
|
|
|
|
else:
|
|
|
|
|
logger.error(f"Failed to check user existence: {response.status}")
|
|
|
|
|
return False
|
|
|
|
|
except Exception as e:
|
|
|
|
|
logger.error(f"Error checking user existence: {e}")
|
|
|
|
|
return False
|
|
|
|
|
|
2025-10-26 20:23:26 +03:00
|
|
|
async def get_user_ratings(self, username: str) -> Optional[Dict[str, Any]]:
|
|
|
|
|
"""Get user ratings from Lichess API"""
|
2025-11-18 15:10:19 +03:00
|
|
|
await self.rate_limiter.wait_if_needed()
|
2025-10-26 20:23:26 +03:00
|
|
|
try:
|
|
|
|
|
async with aiohttp.ClientSession() as session:
|
|
|
|
|
async with session.get(
|
|
|
|
|
f"{self.lichess_base_url}/user/{username}"
|
|
|
|
|
) as response:
|
|
|
|
|
if response.status == 200:
|
|
|
|
|
return await response.json()
|
|
|
|
|
else:
|
|
|
|
|
logger.error(f"Failed to get user ratings: {response.status}")
|
|
|
|
|
return None
|
|
|
|
|
except Exception as e:
|
|
|
|
|
logger.error(f"Error getting user ratings: {e}")
|
|
|
|
|
return None
|