Download src/lib/chesscom.ts from NAP1320/RotNot: direct link, hf CLI and curl.
- Browser
- Download file 4.37 kB
-
https://huggingface.co/spaces/NAP1320/RotNot/resolve/main/src/lib/chesscom.ts
- Command line
-
hf download hf://spaces/NAP1320/RotNot/src/lib/chesscom.ts
-
curl -L -o chesscom.ts https://huggingface.co/spaces/NAP1320/RotNot/resolve/main/src/lib/chesscom.ts
4.37 kB
| /** | |
| * Chess.com Public API client | |
| * | |
| * Docs: https://www.chess.com/news/view/published-data-api | |
| * No authentication required. | |
| */ | |
| const CHESSCOM_BASE = "https://api.chess.com/pub"; | |
| const CHESSCOM_HEADERS: HeadersInit = { | |
| Accept: "application/json", | |
| "User-Agent": "rotnot/1.0 (https://github.com/CuriosityQuantified/rotnot)", | |
| }; | |
| // -------------------------------------------------------------------------- | |
| // Types | |
| // -------------------------------------------------------------------------- | |
| export interface ChessComRating { | |
| rating: number; | |
| date: number; // Unix timestamp | |
| rd?: number; // Rating deviation | |
| } | |
| export interface ChessComTimeClassStats { | |
| last: ChessComRating; | |
| best: ChessComRating; | |
| record: { | |
| win: number; | |
| loss: number; | |
| draw: number; | |
| time_per_move?: number; | |
| timeout_percent?: number; | |
| }; | |
| } | |
| export interface ChessComPlayerStats { | |
| chess_bullet?: ChessComTimeClassStats; | |
| chess_blitz?: ChessComTimeClassStats; | |
| chess_rapid?: ChessComTimeClassStats; | |
| chess_daily?: ChessComTimeClassStats; | |
| tactics?: { | |
| highest: ChessComRating; | |
| lowest: ChessComRating; | |
| }; | |
| puzzle_rush?: { | |
| best: { | |
| total_attempts: number; | |
| score: number; | |
| }; | |
| }; | |
| } | |
| export interface ChessComGame { | |
| url: string; | |
| pgn: string; | |
| time_control: string; | |
| end_time: number; | |
| rated: boolean; | |
| time_class: "bullet" | "blitz" | "rapid" | "daily"; | |
| rules: string; | |
| white: { | |
| rating: number; | |
| result: string; | |
| username: string; | |
| }; | |
| black: { | |
| rating: number; | |
| result: string; | |
| username: string; | |
| }; | |
| } | |
| export interface ChessComGamesArchive { | |
| games: ChessComGame[]; | |
| } | |
| // -------------------------------------------------------------------------- | |
| // getPlayerStats | |
| // -------------------------------------------------------------------------- | |
| /** | |
| * Fetches rating and game stats for a Chess.com player. | |
| * Returns null if the username doesn't exist or on network error. | |
| */ | |
| export async function getPlayerStats( | |
| username: string, | |
| ): Promise<ChessComPlayerStats | null> { | |
| if (!username?.trim()) return null; | |
| try { | |
| const res = await fetch( | |
| `${CHESSCOM_BASE}/player/${encodeURIComponent(username.toLowerCase())}/stats`, | |
| { | |
| headers: CHESSCOM_HEADERS, | |
| next: { revalidate: 300 }, // cache 5 minutes | |
| }, | |
| ); | |
| if (res.status === 404) return null; // Unknown username | |
| if (!res.ok) { | |
| console.error(`Chess.com stats fetch failed: ${res.status} for "${username}"`); | |
| return null; | |
| } | |
| return (await res.json()) as ChessComPlayerStats; | |
| } catch (err) { | |
| console.error("getPlayerStats error:", err); | |
| return null; | |
| } | |
| } | |
| // -------------------------------------------------------------------------- | |
| // getPlayerGames | |
| // -------------------------------------------------------------------------- | |
| /** | |
| * Fetches a player's game archive for a given year and month (1-based month). | |
| * Returns an empty array on error. | |
| */ | |
| export async function getPlayerGames( | |
| username: string, | |
| year: number, | |
| month: number, | |
| ): Promise<ChessComGame[]> { | |
| if (!username?.trim()) return []; | |
| const mm = String(month).padStart(2, "0"); | |
| try { | |
| const res = await fetch( | |
| `${CHESSCOM_BASE}/player/${encodeURIComponent(username.toLowerCase())}/games/${year}/${mm}`, | |
| { | |
| headers: CHESSCOM_HEADERS, | |
| next: { revalidate: 300 }, | |
| }, | |
| ); | |
| if (!res.ok) { | |
| console.error( | |
| `Chess.com games fetch failed: ${res.status} for "${username}" ${year}/${mm}`, | |
| ); | |
| return []; | |
| } | |
| const data = (await res.json()) as ChessComGamesArchive; | |
| return data.games ?? []; | |
| } catch (err) { | |
| console.error("getPlayerGames error:", err); | |
| return []; | |
| } | |
| } | |
| // -------------------------------------------------------------------------- | |
| // Helpers | |
| // -------------------------------------------------------------------------- | |
| /** | |
| * Returns the best available rating across bullet/blitz/rapid/daily. | |
| */ | |
| export function getBestRating(stats: ChessComPlayerStats): number | null { | |
| const timeClasses: (keyof ChessComPlayerStats)[] = [ | |
| "chess_rapid", | |
| "chess_blitz", | |
| "chess_bullet", | |
| "chess_daily", | |
| ]; | |
| for (const tc of timeClasses) { | |
| const entry = stats[tc] as ChessComTimeClassStats | undefined; | |
| if (entry?.last?.rating) return entry.last.rating; | |
| } | |
| return null; | |
| } | |