feat : add lichess user games import
This commit is contained in:
@@ -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;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user