[LIB-13] Add chessgames scanner
[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 import org.hedgecode.chess.scanner.regex.RegexMatcherResult;
27 import org.hedgecode.chess.scanner.regex.RegexResult;
28
29 /**
30  * ChessGamesScanner
31  *
32  * @author Dmitry Samoshin aka gotty
33  */
34 public class ChessGamesScanner extends AbstractSettingsScanner {
35
36     private static final String SETTINGS_FILENAME = "chessgames.settings";
37
38     @Override
39     protected String getResourceName() {
40         return SETTINGS_FILENAME;
41     }
42
43     @Override
44     public PGNTournament getTournament(String tournamentId) throws ChessHogScannerException {
45         PGNTournament tournament = new PGNTournament(tournamentId);
46         assignTournamentGames(tournament);
47         return tournament;
48     }
49
50     @Override
51     public PGNTournament findTournament(String tournamentName) throws ChessHogScannerException {
52         RegexResult result = matchRequest(
53                 assignUrl(
54                         tournamentName, true
55                 ),
56                 new RegexMatcherResult(
57                         getSettings().getTournamentQueryUrlRegex(),
58                         true,
59                         true
60                 )
61         );
62         PGNTournament tournament = null;
63         for (Map.Entry<String, String> entry : result.resultMap().entrySet()) {
64             if (entry.getValue().contains(tournamentName)) { // todo: contains?
65                 tournament = new PGNTournament(
66                         entry.getKey(),
67                         entry.getValue()
68                 );
69             }
70         }
71         if (tournament != null) {
72             assignTournamentGames(tournament);
73         }
74         return tournament;
75     }
76
77     @Override
78     public PGNGame getGame(String gameId) throws ChessHogScannerException {
79         String pgn = request(
80                 assignUrl(gameId)
81         );
82         return new PGNGame(
83                 gameId, pgn
84         );
85     }
86
87     private void assignTournamentGames(PGNTournament tournament) throws ChessHogScannerException {
88         tournament.clearGames();
89         List<String> gamesId = new ArrayList<>();
90
91         List<String> pageGamesId;
92         int pageId = 0;
93         do {
94             pageGamesId = matchRequest(
95                     assignUrl(
96                             tournament.id(),
97                             Integer.toString(++pageId)
98                     ),
99                     getSettings().getTournamentGamesUrlRegex(),
100                     false
101             );
102             gamesId.addAll(
103                     pageGamesId
104             );
105         } while (!pageGamesId.isEmpty());
106
107         for (String gameId : gamesId) {
108             tournament.addGame(
109                     getGame(gameId)
110             );
111         }
112     }
113
114 }