player_tools_simple.pyā¢2.34 kB
"""
Simplified Chess.com player tools without analytics.
"""
from typing import Dict, Any, List
import httpx
import asyncio
import json
import os
import sys
# Import configuration
sys.path.insert(0, os.path.dirname(os.path.dirname(__file__)))
from chess_config import API_CONFIG, CACHE_TTL
async def get_player_profile(username: str) -> Dict[str, Any]:
"""
Get a player's profile information from Chess.com.
Args:
username: The Chess.com username
Returns:
Player profile data including join date, status, followers, etc.
"""
base_url = API_CONFIG.get("base_url", "https://api.chess.com/pub")
endpoint = f"{base_url}/player/{username}"
try:
async with httpx.AsyncClient() as client:
response = await client.get(endpoint)
response.raise_for_status()
return response.json()
except Exception as e:
return {"error": f"Failed to fetch player profile: {str(e)}"}
async def get_player_stats(username: str) -> Dict[str, Any]:
"""
Get a player's chess statistics from Chess.com.
Args:
username: The Chess.com username
Returns:
Player statistics for different game types (chess_blitz, chess_rapid, etc.)
"""
base_url = API_CONFIG.get("base_url", "https://api.chess.com/pub")
endpoint = f"{base_url}/player/{username}/stats"
try:
async with httpx.AsyncClient() as client:
response = await client.get(endpoint)
response.raise_for_status()
return response.json()
except Exception as e:
return {"error": f"Failed to fetch player stats: {str(e)}"}
async def is_player_online(username: str) -> Dict[str, bool]:
"""
Check if a player is currently online on Chess.com.
Args:
username: The Chess.com username
Returns:
Dictionary with 'online' boolean value
"""
base_url = API_CONFIG.get("base_url", "https://api.chess.com/pub")
endpoint = f"{base_url}/player/{username}/is-online"
try:
async with httpx.AsyncClient() as client:
response = await client.get(endpoint)
response.raise_for_status()
return response.json()
except Exception as e:
return {"online": False, "error": str(e)}