[LIB-13] Functional for working with strings moved to StringUtils
[chesshog-scanner.git] / src / main / java / org / hedgecode / chess / scanner / portal / ChessGamesScanner.java
index 6346929..56fc032 100644 (file)
@@ -20,7 +20,9 @@ import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
 
-import org.hedgecode.chess.scanner.ChessHogScannerException;
+import org.hedgecode.chess.scanner.Scanner;
+import org.hedgecode.chess.scanner.ScannerException;
+import org.hedgecode.chess.scanner.StringUtils;
 import org.hedgecode.chess.scanner.entity.PGNGame;
 import org.hedgecode.chess.scanner.entity.PGNTournament;
 
@@ -29,7 +31,7 @@ import org.hedgecode.chess.scanner.entity.PGNTournament;
  *
  * @author Dmitry Samoshin aka gotty
  */
-public class ChessGamesScanner extends AbstractSettingsScanner {
+public class ChessGamesScanner extends AbstractSettingsScanner implements Scanner {
 
     private static final String SETTINGS_FILENAME = "chessgames.settings";
 
@@ -39,14 +41,15 @@ public class ChessGamesScanner extends AbstractSettingsScanner {
     }
 
     @Override
-    public PGNTournament getTournament(String tournamentId) throws ChessHogScannerException {
+    public PGNTournament scanTournament(String tournamentId) throws ScannerException {
         PGNTournament tournament = new PGNTournament(tournamentId);
         assignTournamentGames(tournament);
         return tournament;
     }
 
     @Override
-    public PGNTournament findTournament(String tournamentName) throws ChessHogScannerException {
+    public PGNTournament findTournament(String tournamentName) throws ScannerException {
+        PGNTournament tournament = null;
         Map<String, String> result = matchMap(
                 assignUrl(
                         tournamentName, true
@@ -54,7 +57,6 @@ public class ChessGamesScanner extends AbstractSettingsScanner {
                 getSettings().getTournamentQueryUrlRegex(),
                 true
         );
-        PGNTournament tournament = null;
         for (Map.Entry<String, String> entry : result.entrySet()) {
             if (entry.getValue().contains(tournamentName)) { // todo: contains?
                 tournament = new PGNTournament(
@@ -70,21 +72,44 @@ public class ChessGamesScanner extends AbstractSettingsScanner {
     }
 
     @Override
-    public PGNGame getGame(String gameId) throws ChessHogScannerException {
+    public PGNGame scanGame(String gameId) throws ScannerException {
         String pgn = request(
                 assignUrl(gameId)
         );
+        if (!StringUtils.isPgn(pgn)) {
+            throw new ScannerException(
+                    String.format("Failed to get PGN for requesting game ID: %s", gameId)
+            );
+        }
         return new PGNGame(
                 gameId, pgn
         );
     }
 
     @Override
-    public PGNGame getGame(String gameId, String tournamentId) throws ChessHogScannerException {
-        return getGame(gameId);
+    public PGNGame scanGame(String gameId, String tournamentId) throws ScannerException {
+        return scanGame(gameId);
+    }
+
+    @Override
+    public PGNGame scanUrl(String gameUrl) throws ScannerException {
+        String gamePage = request(gameUrl);
+        String pgn = StringUtils.match(
+                gamePage,
+                getSettings().getGameUrlRegex()
+        );
+        if (pgn == null || !StringUtils.isPgn(pgn)) {
+            throw new ScannerException(
+                    String.format("Failed to get PGN for requesting URL: %s", gameUrl)
+            );
+        }
+        return new PGNGame(
+                null,
+                pgn
+        );
     }
 
-    private void assignTournamentGames(PGNTournament tournament) throws ChessHogScannerException {
+    private void assignTournamentGames(PGNTournament tournament) throws ScannerException {
         tournament.clearGames();
         List<String> gamesId = new ArrayList<>();
 
@@ -106,7 +131,7 @@ public class ChessGamesScanner extends AbstractSettingsScanner {
 
         for (String gameId : gamesId) {
             tournament.addGame(
-                    getGame(gameId)
+                    scanGame(gameId)
             );
         }
     }