[LIB-13] Add function to Scanner API
authorgotty <gotty@hedgecode.org>
Wed, 15 Jan 2020 01:33:29 +0000 (04:33 +0300)
committergotty <gotty@hedgecode.org>
Wed, 15 Jan 2020 01:33:29 +0000 (04:33 +0300)
pom.xml
src/main/java/org/hedgecode/chess/scanner/Scanner.java
src/main/java/org/hedgecode/chess/scanner/portal/AbstractSettingsScanner.java
src/main/java/org/hedgecode/chess/scanner/portal/ChessGamesScanner.java
src/main/java/org/hedgecode/chess/scanner/portal/LiChessScanner.java
src/main/java/org/hedgecode/chess/scanner/regex/RegexBuilder.java
src/main/java/org/hedgecode/chess/scanner/regex/RegexParams.java

diff --git a/pom.xml b/pom.xml
index d423cdc..fb01884 100644 (file)
--- a/pom.xml
+++ b/pom.xml
     <parent>
         <groupId>org.hedgecode.maven</groupId>
         <artifactId>lib-parent</artifactId>
-        <version>2</version>
+        <version>3</version>
         <relativePath>../lib-parent/pom.xml</relativePath>
     </parent>
 
     <groupId>org.hedgecode.chess</groupId>
     <artifactId>chesshog-scanner</artifactId>
     <version>0.1-SNAPSHOT</version>
-    <packaging>pom</packaging>
+    <packaging>jar</packaging>
 
     <name>Hedgecode ChessHog Scanner</name>
     <description>
index 8cc80eb..0192f2b 100644 (file)
@@ -32,4 +32,6 @@ public interface Scanner {
 
     PGNGame getGame(String gameId) throws ChessHogScannerException;
 
+    PGNGame getGame(String gameId, String tournamentId) throws ChessHogScannerException;
+
 }
index 58ac5b0..0910047 100644 (file)
@@ -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
+                )
         );
     }
 
index b34e447..6346929 100644 (file)
@@ -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<String> gamesId = new ArrayList<>();
index f54e373..1449392 100644 (file)
@@ -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<String> pgnGames = split(
index 14b4fba..70aa1f9 100644 (file)
@@ -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()
index f54243d..bd2779a 100644 (file)
@@ -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;