File size: 4,369 Bytes
9580dce | 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 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 | /**
* 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;
}
|