[LIB-13] Add lichess scanner
[chesshog-scanner.git] / src / main / java / org / hedgecode / chess / scanner / portal / LiChessScanner.java
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;
+    }
+
+}