Skip to content

Instantly share code, notes, and snippets.

@replete
Created June 9, 2023 09:58
Show Gist options
  • Save replete/6ec14399898b1e156ff35a3ad957b891 to your computer and use it in GitHub Desktop.
Save replete/6ec14399898b1e156ff35a3ad957b891 to your computer and use it in GitHub Desktop.
get game data from chess.com API
//https://api.chess.com/pub/player/{username}/games/2023/02
(async function main () {
async function getChessGamesForMonth(username, year, month) {
const res = await fetch(`https://api.chess.com/pub/player/${username}/games/${year}/${month}`);
if (res.ok) {
const data = await res.json();
return data.games
} else {
console.error('Problem loading chess.com games from API', res);
}
}
function getChessGamesForDay(datestring, games) {
// expects YYYY-MM-DD format datestring
return games.filter(game => game.pgn.includes(`[Date "${datestring.replaceAll('-','.')}"]`))
}
let gamesForMonth = await getChessGamesForMonth('repletewithfish', '2023', '02');
let gamesForDay = getChessGamesForDay('2023-02-01', gamesForMonth);
console.log(gamesForDay.length);
let gameExample = {
url: 'https://www.chess.com/game/live/68978881803',
pgn: '[Event "Live Chess"]\n' +
'[Site "Chess.com"]\n' +
'[Date "2023.02.01"]\n' +
'[Round "-"]\n' +
'[White "repletewithfish"]\n' +
'[Black "LasseJ63"]\n' +
'[Result "0-1"]\n' +
'[CurrentPosition "r4rk1/pp3ppn/2p1q2P/3p4/5bP1/2PbPP2/PP6/R1B1K2R w KQ -"]\n' +
'[Timezone "UTC"]\n' +
'[ECO "A40"]\n' +
'[ECOUrl "https://www.chess.com/openings/Englund-Gambit-Declined-Reversed-French-Variation"]\n' +
'[UTCDate "2023.02.01"]\n' +
'[UTCTime "08:26:18"]\n' +
'[WhiteElo "922"]\n' +
'[BlackElo "893"]\n' +
'[TimeControl "180"]\n' +
'[Termination "LasseJ63 won by resignation"]\n' +
'[StartTime "08:26:18"]\n' +
'[EndDate "2023.02.01"]\n' +
'[EndTime "08:29:36"]\n' +
'[Link "https://www.chess.com/game/live/68978881803"]\n' +
'\n' +
'1. e3 {[%clk 0:03:00]} 1... e5 {[%clk 0:02:58.7]} 2. d4 {[%clk 0:02:59.9]} 2... exd4 {[%clk 0:02:56.6]} 3. Qxd4 {[%clk 0:02:59.8]} 3... Nc6 {[%clk 0:02:55.3]} 4. Qd2 {[%clk 0:02:59.4]} 4... Nf6 {[%clk 0:02:50.2]} 5. c3 {[%clk 0:02:57.2]} 5... Bc5 {[%clk 0:02:48]} 6. Bb5 {[%clk 0:02:55.1]} 6... Ne5 {[%clk 0:02:36.6]} 7. Qe2 {[%clk 0:02:49.1]} 7... O-O {[%clk 0:02:32.3]} 8. Nf3 {[%clk 0:02:47.4]} 8... Nxf3+ {[%clk 0:02:21.5]} 9. Qxf3 {[%clk 0:02:46]} 9... d6 {[%clk 0:02:14.8]} 10. Nd2 {[%clk 0:02:41.8]} 10... Bg4 {[%clk 0:02:11.9]} 11. Qg3 {[%clk 0:02:37]} 11... c6 {[%clk 0:02:08.7]} 12. Bd3 {[%clk 0:02:32.2]} 12... Qd7 {[%clk 0:02:01.3]} 13. h3 {[%clk 0:02:29.6]} 13... Bh5 {[%clk 0:01:56.7]} 14. Nf3 {[%clk 0:02:26.3]} 14... d5 {[%clk 0:01:49.3]} 15. Ng5 {[%clk 0:02:23.5]} 15... h6 {[%clk 0:01:44.1]} 16. f3 {[%clk 0:02:12.6]} 16... hxg5 {[%clk 0:01:35.4]} 17. Qxg5 {[%clk 0:02:10.4]} 17... Qe6 {[%clk 0:01:24]} 18. g4 {[%clk 0:02:07.1]} 18... Bg6 {[%clk 0:01:20.7]} 19. h4 {[%clk 0:02:03.8]} 19... Bxd3 {[%clk 0:01:17.8]} 20. h5 {[%clk 0:02:02.3]} 20... Nh7 {[%clk 0:01:09.4]} 21. Qf4 {[%clk 0:01:56.1]} 21... Bd6 {[%clk 0:01:05.7]} 22. h6 {[%clk 0:01:53.4]} 22... Bxf4 {[%clk 0:01:03.2]} 0-1\n',
time_control: '180',
end_time: 1675240176,
rated: true,
accuracies: { white: 68.53, black: 85.45 },
tcn: 'mu0KlBKBdB5QBl!Tks9IfHQKlm8!gvKvmvZRbl6EvwYQHt7ZpxENlvRJvM3VnvVMwMZSoENUxFUtFNT3MDIRNVRD',
uuid: '17061374-a20a-11ed-9668-78ac4409ff3c',
initial_setup: 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1',
fen: 'r4rk1/pp3ppn/2p1q2P/3p4/5bP1/2PbPP2/PP6/R1B1K2R w KQ -',
time_class: 'blitz',
rules: 'chess',
white: {
rating: 922,
result: 'resigned',
'@id': 'https://api.chess.com/pub/player/repletewithfish',
username: 'repletewithfish',
uuid: '1bbceb52-890c-11e9-9698-65fe799b7dc6'
},
black: {
rating: 893,
result: 'win',
'@id': 'https://api.chess.com/pub/player/lassej63',
username: 'LasseJ63',
uuid: 'dd02604a-8faa-11e9-819a-2ddebffdf990'
}
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment