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.Base64;
21 import org.hedgecode.chess.scanner.ChessHogScannerException;
22 import org.hedgecode.chess.scanner.entity.PGNGame;
23 import org.hedgecode.chess.scanner.entity.PGNTournament;
24 import org.hedgecode.chess.scanner.format.ArenaFormat;
25 import org.hedgecode.chess.scanner.format.Format;
26 import org.hedgecode.chess.scanner.format.GameFormat;
27 import org.hedgecode.chess.scanner.format.TournamentFormat;
32 * @author Dmitry Samoshin aka gotty
34 public class ChessBombScanner extends AbstractSettingsScanner {
36 private static final String SETTINGS_FILENAME = "chessbomb.settings";
39 protected String getResourceName() {
40 return SETTINGS_FILENAME;
44 public PGNTournament scanTournament(String tournamentId) throws ChessHogScannerException {
45 String decodeTournament = decodeUrlByRegex(
46 assignUrl(tournamentId, null),
47 getSettings().getTournamentGamesUrlRegex()
50 TournamentFormat tournamentFormat = Format.formatTournament(decodeTournament);
52 PGNTournament tournament = new PGNTournament(
53 tournamentFormat.id(), tournamentFormat.name()
56 for (String gameId : tournamentFormat.gameUrls()) {
58 scanGame(gameId, tournamentId)
66 public PGNTournament findTournament(String tournamentName) throws ChessHogScannerException {
67 String decodeArena = decodeUrlByRegex(
68 assignUrl(tournamentName, false),
69 getSettings().getTournamentQueryUrlRegex()
72 ArenaFormat arenaFormat = Format.formatArena(decodeArena);
74 String tournamentId = arenaFormat.findTournament(tournamentName);
76 return tournamentId != null
77 ? scanTournament(tournamentId)
82 public PGNGame scanGame(String gameId) throws ChessHogScannerException {
83 throw new ChessHogScannerException(
84 "ChessBomb does not support searching game without a tournament name!"
89 public PGNGame scanGame(String gameId, String tournamentId) throws ChessHogScannerException {
90 String decodeGame = decodeUrlByRegex(
91 assignUrl(gameId, tournamentId, true),
92 getSettings().getGameUrlRegex()
95 GameFormat gameFormat = Format.formatGame(decodeGame);
104 public PGNGame scanUrl(String gameUrl) throws ChessHogScannerException {
108 private String decodeUrlByRegex(String url, String regex) throws ChessHogScannerException {
109 String encodeString = match(
114 Base64.getDecoder().decode(encodeString)