[LIB-13] Add chessgames scanner
authorgotty <gotty@hedgecode.org>
Sun, 12 Jan 2020 00:39:11 +0000 (03:39 +0300)
committergotty <gotty@hedgecode.org>
Sun, 12 Jan 2020 00:39:11 +0000 (03:39 +0300)
src/main/java/org/hedgecode/chess/scanner/ChessHogScanner.java
src/main/java/org/hedgecode/chess/scanner/portal/ChessGamesScanner.java [new file with mode: 0644]
src/main/resources/settings/chessgames.settings [new file with mode: 0644]

index 45a8064..1b05efe 100644 (file)
@@ -19,6 +19,8 @@ package org.hedgecode.chess.scanner;
 import java.util.HashMap;
 import java.util.Map;
 
+import org.hedgecode.chess.scanner.portal.ChessGamesScanner;
+
 /**
  * ChessHogScanner
  *
@@ -28,7 +30,11 @@ public final class ChessHogScanner {
 
     private static ChessHogScanner _instance;
 
-    private final Map<ScannerType, Scanner> SCANNERS = new HashMap<ScannerType, Scanner>();
+    private final Map<ScannerType, Scanner> SCANNERS = new HashMap<ScannerType, Scanner>() {
+        {
+            put(ScannerType.CHESSGAMES, new ChessGamesScanner());
+        }
+    };
 
     public static Scanner get(ScannerType type) {
         return getInstance().scanner(type);
diff --git a/src/main/java/org/hedgecode/chess/scanner/portal/ChessGamesScanner.java b/src/main/java/org/hedgecode/chess/scanner/portal/ChessGamesScanner.java
new file mode 100644 (file)
index 0000000..c07e392
--- /dev/null
@@ -0,0 +1,114 @@
+/*
+ * 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.ArrayList;
+import java.util.List;
+import java.util.Map;
+
+import org.hedgecode.chess.scanner.ChessHogScannerException;
+import org.hedgecode.chess.scanner.entity.PGNGame;
+import org.hedgecode.chess.scanner.entity.PGNTournament;
+import org.hedgecode.chess.scanner.regex.RegexMatcherResult;
+import org.hedgecode.chess.scanner.regex.RegexResult;
+
+/**
+ * ChessGamesScanner
+ *
+ * @author Dmitry Samoshin aka gotty
+ */
+public class ChessGamesScanner extends AbstractSettingsScanner {
+
+    private static final String SETTINGS_FILENAME = "chessgames.settings";
+
+    @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 {
+        RegexResult result = matchRequest(
+                assignUrl(
+                        tournamentName, true
+                ),
+                new RegexMatcherResult(
+                        getSettings().getTournamentQueryUrlRegex(),
+                        true,
+                        true
+                )
+        );
+        PGNTournament tournament = null;
+        for (Map.Entry<String, String> entry : result.resultMap().entrySet()) {
+            if (entry.getValue().contains(tournamentName)) { // todo: contains?
+                tournament = new PGNTournament(
+                        entry.getKey(),
+                        entry.getValue()
+                );
+            }
+        }
+        if (tournament != null) {
+            assignTournamentGames(tournament);
+        }
+        return tournament;
+    }
+
+    @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> gamesId = new ArrayList<>();
+
+        List<String> pageGamesId;
+        int pageId = 0;
+        do {
+            pageGamesId = matchRequest(
+                    assignUrl(
+                            tournament.id(),
+                            Integer.toString(++pageId)
+                    ),
+                    getSettings().getTournamentGamesUrlRegex(),
+                    false
+            );
+            gamesId.addAll(
+                    pageGamesId
+            );
+        } while (!pageGamesId.isEmpty());
+
+        for (String gameId : gamesId) {
+            tournament.addGame(
+                    getGame(gameId)
+            );
+        }
+    }
+
+}
diff --git a/src/main/resources/settings/chessgames.settings b/src/main/resources/settings/chessgames.settings
new file mode 100644 (file)
index 0000000..452a981
--- /dev/null
@@ -0,0 +1,9 @@
+{
+  "tournamentUrl": "https://www.chessgames.com/perl/chess.pl?tid=[tournamentId]&page=[pageId]",
+  "tournamentIsMultiPage": true,
+  "tournamentGamesUrlRegex": "<a href=\"/perl/chessgame\\?gid=([0-9]+)\">",
+  "tournamentQuery": "https://www.chessgames.com/perl/tournaments?query=[query]",
+  "tournamentQueryUrlRegex": "<a href=\"/perl/chess.pl\\?tid=([0-9]+)\">([^<]+)</a>",
+  "gameUrl": "https://www.chessgames.com/perl/chessgame?gid=[gameId]",
+  "gamePgnUrl": "https://www.chessgames.com/perl/nph-chesspgn?gid=[gameId]&text=1"
+}
\ No newline at end of file