From 2f45a4a456c2d23642a91668276fc31211975711 Mon Sep 17 00:00:00 2001 From: gotty Date: Tue, 14 Jan 2020 00:58:26 +0300 Subject: [PATCH] [LIB-13] Add lichess scanner --- .../hedgecode/chess/scanner/ChessHogScanner.java | 4 +- .../chess/scanner/portal/LiChessScanner.java | 102 +++++++++++++++++++++ src/main/resources/settings/lichess.settings | 8 ++ 3 files changed, 113 insertions(+), 1 deletion(-) create mode 100644 src/main/java/org/hedgecode/chess/scanner/portal/LiChessScanner.java create mode 100644 src/main/resources/settings/lichess.settings diff --git a/src/main/java/org/hedgecode/chess/scanner/ChessHogScanner.java b/src/main/java/org/hedgecode/chess/scanner/ChessHogScanner.java index 1b05efe..0191cff 100644 --- a/src/main/java/org/hedgecode/chess/scanner/ChessHogScanner.java +++ b/src/main/java/org/hedgecode/chess/scanner/ChessHogScanner.java @@ -20,6 +20,7 @@ import java.util.HashMap; import java.util.Map; import org.hedgecode.chess.scanner.portal.ChessGamesScanner; +import org.hedgecode.chess.scanner.portal.LiChessScanner; /** * ChessHogScanner @@ -32,7 +33,8 @@ public final class ChessHogScanner { private final Map SCANNERS = new HashMap() { { - put(ScannerType.CHESSGAMES, new ChessGamesScanner()); + put( ScannerType.CHESSGAMES, new ChessGamesScanner() ); + put( ScannerType.LICHESS, new LiChessScanner() ); } }; diff --git a/src/main/java/org/hedgecode/chess/scanner/portal/LiChessScanner.java b/src/main/java/org/hedgecode/chess/scanner/portal/LiChessScanner.java new file mode 100644 index 0000000..f54e373 --- /dev/null +++ b/src/main/java/org/hedgecode/chess/scanner/portal/LiChessScanner.java @@ -0,0 +1,102 @@ +/* + * Copyright (c) 2019-2020. Developed by Hedgecode. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.hedgecode.chess.scanner.portal; + +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import org.hedgecode.chess.scanner.ChessHogScannerException; +import org.hedgecode.chess.scanner.entity.PGNGame; +import org.hedgecode.chess.scanner.entity.PGNTournament; + +/** + * LiChessScanner + * + * @author Dmitry Samoshin aka gotty + */ +public class LiChessScanner extends AbstractSettingsScanner { + + private static final String SETTINGS_FILENAME = "lichess.settings"; + + private static final String TOURNAMENT_GAMES_SPLIT_REGEX = "\\[Event \"[^\"]+\"\\]"; + private static final String TOURNAMENT_NAME_REGEX = "\\[Event \"([^\"]+)\"\\]"; + private static final String GAME_ID_REGEX = "\\[Site \"https://lichess.org/([^\"]+)\"\\]"; + + @Override + protected String getResourceName() { + return SETTINGS_FILENAME; + } + + @Override + public PGNTournament getTournament(String tournamentId) throws ChessHogScannerException { + PGNTournament tournament = new PGNTournament(tournamentId); + assignTournamentGames(tournament); + return tournament; + } + + @Override + public PGNTournament findTournament(String tournamentName) throws ChessHogScannerException { + throw new ChessHogScannerException( + "Lichess does not support searching for a tournament by name!" + ); + } + + @Override + public PGNGame getGame(String gameId) throws ChessHogScannerException { + String pgn = request( + assignUrl(gameId) + ); + return new PGNGame( + gameId, pgn + ); + } + + private void assignTournamentGames(PGNTournament tournament) throws ChessHogScannerException { + tournament.clearGames(); + List pgnGames = split( + assignUrl( + tournament.id(), + null + ), + TOURNAMENT_GAMES_SPLIT_REGEX + ); + + if (!pgnGames.isEmpty()) { + tournament.setName( + find(TOURNAMENT_NAME_REGEX, pgnGames.get(0)) + ); + } + + for (String pgn : pgnGames) { + String gameId = find(GAME_ID_REGEX, pgn); + tournament.addGame( + new PGNGame(gameId, pgn) + ); + } + } + + private String find(String regex, String pgn) { + Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE); + Matcher matcher = pattern.matcher(pgn); + if (matcher.find()) { + return matcher.group(1); + } + return null; + } + +} diff --git a/src/main/resources/settings/lichess.settings b/src/main/resources/settings/lichess.settings new file mode 100644 index 0000000..4f44ba1 --- /dev/null +++ b/src/main/resources/settings/lichess.settings @@ -0,0 +1,8 @@ +{ + "tournamentUrl": "https://lichess.org/api/tournament/[tournamentId]/games", + "tournamentIsMultiPage": false, + "tournamentQueryParams": "?clocks=false&evals=false&opening=true", + "gameUrl": "https://lichess.org/[gameId]", + "gamePgnUrl": "https://lichess.org/game/export/[gameId]", + "gameQueryParams": "?clocks=false&evals=false&literate=true" +} \ No newline at end of file -- 2.10.0