2 * Copyright (c) 2019-2020. Developed by Hedgecode.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.hedgecode.chess.scanner.portal;
19 import java.util.ArrayList;
20 import java.util.List;
23 import org.hedgecode.chess.scanner.Scanner;
24 import org.hedgecode.chess.scanner.ScannerException;
25 import org.hedgecode.chess.scanner.entity.PGNGame;
26 import org.hedgecode.chess.scanner.entity.PGNTournament;
31 * @author Dmitry Samoshin aka gotty
33 public class ChessGamesScanner extends AbstractSettingsScanner implements Scanner {
35 private static final String SETTINGS_FILENAME = "chessgames.settings";
38 protected String getResourceName() {
39 return SETTINGS_FILENAME;
43 public PGNTournament scanTournament(String tournamentId) throws ScannerException {
44 PGNTournament tournament = new PGNTournament(tournamentId);
45 assignTournamentGames(tournament);
50 public PGNTournament findTournament(String tournamentName) throws ScannerException {
51 PGNTournament tournament = null;
52 Map<String, String> result = matchMap(
56 getSettings().getTournamentQueryUrlRegex(),
59 for (Map.Entry<String, String> entry : result.entrySet()) {
60 if (entry.getValue().contains(tournamentName)) { // todo: contains?
61 tournament = new PGNTournament(
67 if (tournament != null) {
68 assignTournamentGames(tournament);
74 public PGNGame scanGame(String gameId) throws ScannerException {
78 if (!isPgnFormat(pgn)) {
79 throw new ScannerException(
80 String.format("Failed to get PGN for requesting game ID: %s", gameId)
89 public PGNGame scanGame(String gameId, String tournamentId) throws ScannerException {
90 return scanGame(gameId);
94 public PGNGame scanUrl(String gameUrl) throws ScannerException {
99 getSettings().getGameUrlRegex()
101 if (pgn == null || !isPgnFormat(pgn)) {
102 throw new ScannerException(
103 String.format("Failed to get PGN for requesting URL: %s", gameUrl)
112 private void assignTournamentGames(PGNTournament tournament) throws ScannerException {
113 tournament.clearGames();
114 List<String> gamesId = new ArrayList<>();
116 List<String> pageGamesId;
122 Integer.toString(++pageId)
124 getSettings().getTournamentGamesUrlRegex(),
130 } while (!pageGamesId.isEmpty());
132 for (String gameId : gamesId) {