-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1b956a0
commit 3af80c5
Showing
4 changed files
with
88 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import {RateLimiter} from "../util/RateLimiter.js"; | ||
import axios from "axios"; | ||
|
||
interface ImportGameOutResponse { | ||
id: string | ||
} | ||
|
||
class LichessClient { | ||
private rateLimiter: RateLimiter = new RateLimiter(1_000) | ||
|
||
public async importGame(pgn: string): Promise<ImportGameOutResponse> { | ||
await this.rateLimiter.assertDelay() | ||
|
||
const url = "https://lichess.org/api/import" | ||
const config = { | ||
headers: { | ||
'Accept': 'application/json', | ||
"Content-Type": "application/x-www-form-urlencoded" | ||
} | ||
} | ||
const params = new URLSearchParams({ | ||
pgn: pgn | ||
}); | ||
const response = await axios.post(url, params, config) | ||
return { | ||
id: response.data.id | ||
} | ||
} | ||
} | ||
|
||
export const lichessClient = new LichessClient() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import {database, NAMESPACE_LICHESS_GAME_ID} from "../db.js"; | ||
Check failure on line 1 in build_scripts/lichess/LichessService.ts GitHub Actions / deploy
|
||
import {lichessClient} from "./LichessClient.js"; | ||
|
||
class LichessService { | ||
public async importGames(id: string, force: boolean = false) { | ||
if (!database.read(NAMESPACE_LICHESS_GAME_ID, id) || force) { | ||
const games = database.readDescriptionGames(id); | ||
if (games.length === 0) { | ||
return | ||
} | ||
|
||
const lichessGamesIds = [] | ||
for (let game of games) { | ||
if (game.pgn) { | ||
lichessGamesIds.push(await lichessClient.importGame(game.pgn)) | ||
} else { | ||
lichessGamesIds.push({}) | ||
} | ||
} | ||
|
||
database.save(NAMESPACE_LICHESS_GAME_ID, id, lichessGamesIds) | ||
} | ||
} | ||
} | ||
|
||
export const lichessService = new LichessService() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters