[LIB-13] Functional for working with strings moved to StringUtils
[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.Scanner;
24 import org.hedgecode.chess.scanner.ScannerException;
25 import org.hedgecode.chess.scanner.StringUtils;
26 import org.hedgecode.chess.scanner.entity.PGNGame;
27 import org.hedgecode.chess.scanner.entity.PGNTournament;
28
29 /**
30  * ChessGamesScanner
31  *
32  * @author Dmitry Samoshin aka gotty
33  */
34 public class ChessGamesScanner extends AbstractSettingsScanner implements Scanner {
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 scanTournament(String tournamentId) throws ScannerException {
45         PGNTournament tournament = new PGNTournament(tournamentId);
46         assignTournamentGames(tournament);
47         return tournament;
48     }
49
50     @Override
51     public PGNTournament findTournament(String tournamentName) throws ScannerException {
52         PGNTournament tournament = null;
53         Map<String, String> result = matchMap(
54                 assignUrl(
55                         tournamentName, true
56                 ),
57                 getSettings().getTournamentQueryUrlRegex(),
58                 true
59         );
60         for (Map.Entry<String, String> entry : result.entrySet()) {
61             if (entry.getValue().contains(tournamentName)) { // todo: contains?
62                 tournament = new PGNTournament(
63                         entry.getKey(),
64                         entry.getValue()
65                 );
66             }
67         }
68         if (tournament != null) {
69             assignTournamentGames(tournament);
70         }
71         return tournament;
72     }
73
74     @Override
75     public PGNGame scanGame(String gameId) throws ScannerException {
76         String pgn = request(
77                 assignUrl(gameId)
78         );
79         if (!StringUtils.isPgn(pgn)) {
80             throw new ScannerException(
81                     String.format("Failed to get PGN for requesting game ID: %s", gameId)
82             );
83         }
84         return new PGNGame(
85                 gameId, pgn
86         );
87     }
88
89     @Override
90     public PGNGame scanGame(String gameId, String tournamentId) throws ScannerException {
91         return scanGame(gameId);
92     }
93
94     @Override
95     public PGNGame scanUrl(String gameUrl) throws ScannerException {
96         String gamePage = request(gameUrl);
97         String pgn = StringUtils.match(
98                 gamePage,
99                 getSettings().getGameUrlRegex()
100         );
101         if (pgn == null || !StringUtils.isPgn(pgn)) {
102             throw new ScannerException(
103                     String.format("Failed to get PGN for requesting URL: %s", gameUrl)
104             );
105         }
106         return new PGNGame(
107                 null,
108                 pgn
109         );
110     }
111
112     private void assignTournamentGames(PGNTournament tournament) throws ScannerException {
113         tournament.clearGames();
114         List<String> gamesId = new ArrayList<>();
115
116         List<String> pageGamesId;
117         int pageId = 0;
118         do {
119             pageGamesId = match(
120                     assignUrl(
121                             tournament.id(),
122                             Integer.toString(++pageId)
123                     ),
124                     getSettings().getTournamentGamesUrlRegex(),
125                     false
126             );
127             gamesId.addAll(
128                     pageGamesId
129             );
130         } while (!pageGamesId.isEmpty());
131
132         for (String gameId : gamesId) {
133             tournament.addGame(
134                     scanGame(gameId)
135             );
136         }
137     }
138
139 }