From 6e8190a376e6ff4e4aa689f918a45135f9745c64 Mon Sep 17 00:00:00 2001 From: gotty Date: Wed, 15 Jan 2020 04:33:29 +0300 Subject: [PATCH] [LIB-13] Add function to Scanner API --- pom.xml | 4 ++-- .../java/org/hedgecode/chess/scanner/Scanner.java | 2 ++ .../scanner/portal/AbstractSettingsScanner.java | 22 ++++++++++++++++++++-- .../chess/scanner/portal/ChessGamesScanner.java | 5 +++++ .../chess/scanner/portal/LiChessScanner.java | 5 +++++ .../chess/scanner/regex/RegexBuilder.java | 6 ++++++ .../hedgecode/chess/scanner/regex/RegexParams.java | 7 +++++++ 7 files changed, 47 insertions(+), 4 deletions(-) diff --git a/pom.xml b/pom.xml index d423cdc..fb01884 100644 --- a/pom.xml +++ b/pom.xml @@ -24,14 +24,14 @@ org.hedgecode.maven lib-parent - 2 + 3 ../lib-parent/pom.xml org.hedgecode.chess chesshog-scanner 0.1-SNAPSHOT - pom + jar Hedgecode ChessHog Scanner diff --git a/src/main/java/org/hedgecode/chess/scanner/Scanner.java b/src/main/java/org/hedgecode/chess/scanner/Scanner.java index 8cc80eb..0192f2b 100644 --- a/src/main/java/org/hedgecode/chess/scanner/Scanner.java +++ b/src/main/java/org/hedgecode/chess/scanner/Scanner.java @@ -32,4 +32,6 @@ public interface Scanner { PGNGame getGame(String gameId) throws ChessHogScannerException; + PGNGame getGame(String gameId, String tournamentId) throws ChessHogScannerException; + } diff --git a/src/main/java/org/hedgecode/chess/scanner/portal/AbstractSettingsScanner.java b/src/main/java/org/hedgecode/chess/scanner/portal/AbstractSettingsScanner.java index 58ac5b0..0910047 100644 --- a/src/main/java/org/hedgecode/chess/scanner/portal/AbstractSettingsScanner.java +++ b/src/main/java/org/hedgecode/chess/scanner/portal/AbstractSettingsScanner.java @@ -53,7 +53,7 @@ public abstract class AbstractSettingsScanner extends AbstractRequestScanner imp protected String assignUrl(String tournamentId, String pageId) { String tournamentUrl = assignUrlWithParams( - settings.getTournamentUrl(), settings.getGameQueryParams() + settings.getTournamentUrl(), settings.getTournamentQueryParams() ); return RegexBuilder.build( Type.TOURNAMENT, @@ -76,11 +76,29 @@ public abstract class AbstractSettingsScanner extends AbstractRequestScanner imp ); } + protected String assignUrl(String gameId, String tournamentId, boolean withTournament) { + String gameUrl = settings.getGamePgnUrl() != null + ? settings.getGamePgnUrl() + : settings.getGameUrl(); + return RegexBuilder.build( + Type.GAME, + assignUrlWithParams(gameUrl, settings.getGameQueryParams()), + new RegexParams( + gameId, + tournamentId, + withTournament + ) + ); + } + protected String assignUrl(String query, boolean isUrlEncode) { return RegexBuilder.build( Type.QUERY, settings.getTournamentQuery(), - new RegexParams(query, isUrlEncode) + new RegexParams( + query, + isUrlEncode + ) ); } diff --git a/src/main/java/org/hedgecode/chess/scanner/portal/ChessGamesScanner.java b/src/main/java/org/hedgecode/chess/scanner/portal/ChessGamesScanner.java index b34e447..6346929 100644 --- a/src/main/java/org/hedgecode/chess/scanner/portal/ChessGamesScanner.java +++ b/src/main/java/org/hedgecode/chess/scanner/portal/ChessGamesScanner.java @@ -79,6 +79,11 @@ public class ChessGamesScanner extends AbstractSettingsScanner { ); } + @Override + public PGNGame getGame(String gameId, String tournamentId) throws ChessHogScannerException { + return getGame(gameId); + } + private void assignTournamentGames(PGNTournament tournament) throws ChessHogScannerException { tournament.clearGames(); List gamesId = new ArrayList<>(); diff --git a/src/main/java/org/hedgecode/chess/scanner/portal/LiChessScanner.java b/src/main/java/org/hedgecode/chess/scanner/portal/LiChessScanner.java index f54e373..1449392 100644 --- a/src/main/java/org/hedgecode/chess/scanner/portal/LiChessScanner.java +++ b/src/main/java/org/hedgecode/chess/scanner/portal/LiChessScanner.java @@ -66,6 +66,11 @@ public class LiChessScanner extends AbstractSettingsScanner { ); } + @Override + public PGNGame getGame(String gameId, String tournamentId) throws ChessHogScannerException { + return getGame(gameId); + } + private void assignTournamentGames(PGNTournament tournament) throws ChessHogScannerException { tournament.clearGames(); List pgnGames = split( diff --git a/src/main/java/org/hedgecode/chess/scanner/regex/RegexBuilder.java b/src/main/java/org/hedgecode/chess/scanner/regex/RegexBuilder.java index 14b4fba..70aa1f9 100644 --- a/src/main/java/org/hedgecode/chess/scanner/regex/RegexBuilder.java +++ b/src/main/java/org/hedgecode/chess/scanner/regex/RegexBuilder.java @@ -56,6 +56,12 @@ public class RegexBuilder { GAME { @Override public String format(String target, RegexParams params) { + if (params.getTournamentId() != null) { + target = target.replace( + RegexParams.TOURNAMENT_ID, + params.getTournamentId() + ); + } return target.replace( RegexParams.GAME_ID, params.getGameId() diff --git a/src/main/java/org/hedgecode/chess/scanner/regex/RegexParams.java b/src/main/java/org/hedgecode/chess/scanner/regex/RegexParams.java index f54243d..bd2779a 100644 --- a/src/main/java/org/hedgecode/chess/scanner/regex/RegexParams.java +++ b/src/main/java/org/hedgecode/chess/scanner/regex/RegexParams.java @@ -43,6 +43,13 @@ public final class RegexParams { this.gameId = gameId; } + public RegexParams(String gameId, String tournamentId, boolean withTournament) { + this.gameId = gameId; + if (withTournament) { + this.tournamentId = tournamentId; + } + } + public RegexParams(String query, boolean isUrlEncode) { this.query = query; this.isUrlEncode = isUrlEncode; -- 2.10.0