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.Scanner;
22 import org.hedgecode.chess.scanner.ScannerException;
23 import org.hedgecode.chess.scanner.entity.PGNGame;
24 import org.hedgecode.chess.scanner.entity.PGNTournament;
25 import org.hedgecode.chess.scanner.format.chessbomb.ArenaFormat;
26 import org.hedgecode.chess.scanner.format.chessbomb.Format;
27 import org.hedgecode.chess.scanner.format.chessbomb.GameFormat;
28 import org.hedgecode.chess.scanner.format.chessbomb.TournamentFormat;
33 * @author Dmitry Samoshin aka gotty
35 public class ChessBombScanner extends AbstractSettingsScanner implements Scanner {
37 private static final String SETTINGS_FILENAME = "chessbomb.settings";
40 protected String getResourceName() {
41 return SETTINGS_FILENAME;
45 public PGNTournament scanTournament(String tournamentId) throws ScannerException {
46 String decodeTournament = decodeUrlByRegex(
47 assignUrl(tournamentId, null),
48 getSettings().getTournamentJsonUrlRegex()
51 TournamentFormat tournamentFormat = Format.formatTournament(decodeTournament);
53 PGNTournament tournament = new PGNTournament(
54 tournamentFormat.id(), tournamentFormat.name()
57 for (String gameId : tournamentFormat.gameUrls()) {
59 scanGame(gameId, tournamentId)
67 public PGNTournament findTournament(String tournamentName) throws ScannerException {
68 String decodeArena = decodeUrlByRegex(
69 assignUrl(tournamentName, false),
70 getSettings().getTournamentQueryUrlRegex()
73 ArenaFormat arenaFormat = Format.formatArena(decodeArena);
75 String tournamentId = arenaFormat.findTournament(tournamentName);
77 return tournamentId != null
78 ? scanTournament(tournamentId)
83 public PGNGame scanGame(String gameId) throws ScannerException {
84 throw new ScannerException(
85 "ChessBomb does not support searching game without a tournament name!"
90 public PGNGame scanGame(String gameId, String tournamentId) throws ScannerException {
91 return scanGameByRegex(
92 assignUrl(gameId, tournamentId, true),
93 getSettings().getGameJsonUrlRegex()
98 public PGNGame scanUrl(String gameUrl) throws ScannerException {
99 return scanGameByRegex(
101 getSettings().getGameJsonUrlRegex()
105 private PGNGame scanGameByRegex(String gameUrl, String regex) throws ScannerException {
106 String decodeGame = decodeUrlByRegex(
111 GameFormat gameFormat = Format.formatGame(decodeGame);
120 private String decodeUrlByRegex(String url, String regex) throws ScannerException {
121 String encodeString = match(
125 if (encodeString == null) {
126 throw new ScannerException(
127 String.format("Failed to decode source data for requesting URL: %s", url)
131 Base64.getDecoder().decode(