[LIB-13] Method scanUrl implementation for several scanners
[chesshog-scanner.git] / src / main / java / org / hedgecode / chess / scanner / portal / ChessGamesScanner.java
1 /*
2  * Copyright (c) 2019-2020. Developed by Hedgecode.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.hedgecode.chess.scanner.portal;
18
19 import java.util.ArrayList;
20 import java.util.List;
21 import java.util.Map;
22
23 import org.hedgecode.chess.scanner.ChessHogScannerException;
24 import org.hedgecode.chess.scanner.entity.PGNGame;
25 import org.hedgecode.chess.scanner.entity.PGNTournament;
26
27 /**
28  * ChessGamesScanner
29  *
30  * @author Dmitry Samoshin aka gotty
31  */
32 public class ChessGamesScanner extends AbstractSettingsScanner {
33
34     private static final String SETTINGS_FILENAME = "chessgames.settings";
35
36     @Override
37     protected String getResourceName() {
38         return SETTINGS_FILENAME;
39     }
40
41     @Override
42     public PGNTournament scanTournament(String tournamentId) throws ChessHogScannerException {
43         PGNTournament tournament = new PGNTournament(tournamentId);
44         assignTournamentGames(tournament);
45         return tournament;
46     }
47
48     @Override
49     public PGNTournament findTournament(String tournamentName) throws ChessHogScannerException {
50         PGNTournament tournament = null;
51         Map<String, String> result = matchMap(
52                 assignUrl(
53                         tournamentName, true
54                 ),
55                 getSettings().getTournamentQueryUrlRegex(),
56                 true
57         );
58         for (Map.Entry<String, String> entry : result.entrySet()) {
59             if (entry.getValue().contains(tournamentName)) { // todo: contains?
60                 tournament = new PGNTournament(
61                         entry.getKey(),
62                         entry.getValue()
63                 );
64             }
65         }
66         if (tournament != null) {
67             assignTournamentGames(tournament);
68         }
69         return tournament;
70     }
71
72     @Override
73     public PGNGame scanGame(String gameId) throws ChessHogScannerException {
74         String pgn = request(
75                 assignUrl(gameId)
76         );
77         if (!isPgnFormat(pgn)) {
78             throw new ChessHogScannerException(
79                     String.format("Failed to get PGN for requesting game ID: %s", gameId)
80             );
81         }
82         return new PGNGame(
83                 gameId, pgn
84         );
85     }
86
87     @Override
88     public PGNGame scanGame(String gameId, String tournamentId) throws ChessHogScannerException {
89         return scanGame(gameId);
90     }
91
92     @Override
93     public PGNGame scanUrl(String gameUrl) throws ChessHogScannerException {
94         String pgn = regex(
95                 request(
96                         gameUrl
97                 ),
98                 getSettings().getGameUrlRegex()
99         );
100         if (pgn == null || !isPgnFormat(pgn)) {
101             throw new ChessHogScannerException(
102                     String.format("Failed to get PGN for requesting URL: %s", gameUrl)
103             );
104         }
105         return new PGNGame(
106                 null,
107                 pgn
108         );
109     }
110
111     private void assignTournamentGames(PGNTournament tournament) throws ChessHogScannerException {
112         tournament.clearGames();
113         List<String> gamesId = new ArrayList<>();
114
115         List<String> pageGamesId;
116         int pageId = 0;
117         do {
118             pageGamesId = match(
119                     assignUrl(
120                             tournament.id(),
121                             Integer.toString(++pageId)
122                     ),
123                     getSettings().getTournamentGamesUrlRegex(),
124                     false
125             );
126             gamesId.addAll(
127                     pageGamesId
128             );
129         } while (!pageGamesId.isEmpty());
130
131         for (String gameId : gamesId) {
132             tournament.addGame(
133                     scanGame(gameId)
134             );
135         }
136     }
137
138 }