feat : add lichess user games import

This commit is contained in:
GuillaumeSD
2024-03-17 18:57:22 +01:00
parent f9cb5acc1f
commit 46198f2a47
7 changed files with 147 additions and 3 deletions

View File

@@ -3,6 +3,7 @@ import { sortLines } from "./engine/helpers/parseResults";
import {
LichessError,
LichessEvalBody,
LichessGame,
LichessResponse,
} from "@/types/lichess";
@@ -54,3 +55,22 @@ export const getLichessEval = async (
};
}
};
export const getLichessUserRecentGames = async (
username: string
): Promise<LichessGame[]> => {
const res = await fetch(
`https://lichess.org/api/games/user/${username}?until=${Date.now()}&max=50&pgnInJson=true&sort=dateDesc`,
{ method: "GET", headers: { accept: "application/x-ndjson" } }
);
if (res.status === 404) return [];
const rawData = await res.text();
const games: LichessGame[] = rawData
.split("\n")
.filter((game) => game.length > 0)
.map((game) => JSON.parse(game));
return games;
};