[LIB-13] Method scanUrl implementation for several scanners
[chesshog-scanner.git] / src / main / java / org / hedgecode / chess / scanner / portal / ChessGamesScanner.java
index 7658ba1..28493cf 100644 (file)
@@ -47,6 +47,7 @@ public class ChessGamesScanner extends AbstractSettingsScanner {
 
     @Override
     public PGNTournament findTournament(String tournamentName) throws ChessHogScannerException {
+        PGNTournament tournament = null;
         Map<String, String> result = matchMap(
                 assignUrl(
                         tournamentName, true
@@ -54,7 +55,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(
@@ -74,6 +74,11 @@ public class ChessGamesScanner extends AbstractSettingsScanner {
         String pgn = request(
                 assignUrl(gameId)
         );
+        if (!isPgnFormat(pgn)) {
+            throw new ChessHogScannerException(
+                    String.format("Failed to get PGN for requesting game ID: %s", gameId)
+            );
+        }
         return new PGNGame(
                 gameId, pgn
         );
@@ -86,7 +91,21 @@ public class ChessGamesScanner extends AbstractSettingsScanner {
 
     @Override
     public PGNGame scanUrl(String gameUrl) throws ChessHogScannerException {
-        return null;
+        String pgn = regex(
+                request(
+                        gameUrl
+                ),
+                getSettings().getGameUrlRegex()
+        );
+        if (pgn == null || !isPgnFormat(pgn)) {
+            throw new ChessHogScannerException(
+                    String.format("Failed to get PGN for requesting URL: %s", gameUrl)
+            );
+        }
+        return new PGNGame(
+                null,
+                pgn
+        );
     }
 
     private void assignTournamentGames(PGNTournament tournament) throws ChessHogScannerException {