[LIB-13] Add lichess scanner
authorgotty <gotty@hedgecode.org>
Mon, 13 Jan 2020 21:58:26 +0000 (00:58 +0300)
committergotty <gotty@hedgecode.org>
Mon, 13 Jan 2020 21:58:26 +0000 (00:58 +0300)
src/main/java/org/hedgecode/chess/scanner/ChessHogScanner.java
src/main/java/org/hedgecode/chess/scanner/portal/LiChessScanner.java [new file with mode: 0644]
src/main/resources/settings/lichess.settings [new file with mode: 0644]

index 1b05efe..0191cff 100644 (file)
@@ -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<ScannerType, Scanner> SCANNERS = new HashMap<ScannerType, Scanner>() {
         {
-            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 (file)
index 0000000..f54e373
--- /dev/null
@@ -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<String> 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 (file)
index 0000000..4f44ba1
--- /dev/null
@@ -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