From 412ff2a5b5855bf769e2b767896bfa59f927dde2 Mon Sep 17 00:00:00 2001 From: gotty Date: Sun, 12 Jan 2020 03:39:11 +0300 Subject: [PATCH] [LIB-13] Add chessgames scanner --- .../hedgecode/chess/scanner/ChessHogScanner.java | 8 +- .../chess/scanner/portal/ChessGamesScanner.java | 114 +++++++++++++++++++++ src/main/resources/settings/chessgames.settings | 9 ++ 3 files changed, 130 insertions(+), 1 deletion(-) create mode 100644 src/main/java/org/hedgecode/chess/scanner/portal/ChessGamesScanner.java create mode 100644 src/main/resources/settings/chessgames.settings diff --git a/src/main/java/org/hedgecode/chess/scanner/ChessHogScanner.java b/src/main/java/org/hedgecode/chess/scanner/ChessHogScanner.java index 45a8064..1b05efe 100644 --- a/src/main/java/org/hedgecode/chess/scanner/ChessHogScanner.java +++ b/src/main/java/org/hedgecode/chess/scanner/ChessHogScanner.java @@ -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 SCANNERS = new HashMap(); + private final Map SCANNERS = new HashMap() { + { + 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 index 0000000..c07e392 --- /dev/null +++ b/src/main/java/org/hedgecode/chess/scanner/portal/ChessGamesScanner.java @@ -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 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 gamesId = new ArrayList<>(); + + List 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 index 0000000..452a981 --- /dev/null +++ b/src/main/resources/settings/chessgames.settings @@ -0,0 +1,9 @@ +{ + "tournamentUrl": "https://www.chessgames.com/perl/chess.pl?tid=[tournamentId]&page=[pageId]", + "tournamentIsMultiPage": true, + "tournamentGamesUrlRegex": "", + "tournamentQuery": "https://www.chessgames.com/perl/tournaments?query=[query]", + "tournamentQueryUrlRegex": "([^<]+)", + "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 -- 2.10.0