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;
30 import static org.hedgecode.chess.scanner.ScannerConstants.*;
35 * @author Dmitry Samoshin aka gotty
37 public class ChessBombScanner extends AbstractSettingsScanner implements Scanner {
39 private static final String SETTINGS_FILENAME = "chessbomb.settings";
42 protected String getResourceName() {
43 return SETTINGS_FILENAME;
47 public PGNTournament scanTournament(String tournamentId) throws ScannerException {
48 String decodeTournament = decodeUrlByRegex(
49 assignUrl(tournamentId, null),
50 getSettings().getTournamentJsonUrlRegex()
53 TournamentFormat tournamentFormat = Format.formatTournament(decodeTournament);
55 PGNTournament tournament = new PGNTournament(
56 tournamentFormat.id(), tournamentFormat.name()
59 for (String gameId : tournamentFormat.gameUrls()) {
61 scanGame(gameId, tournamentId)
69 public PGNTournament findTournament(String tournamentName) throws ScannerException {
70 String decodeArena = decodeUrlByRegex(
71 assignUrl(tournamentName, false),
72 getSettings().getTournamentQueryUrlRegex()
75 ArenaFormat arenaFormat = Format.formatArena(decodeArena);
77 String tournamentId = arenaFormat.findTournament(tournamentName);
79 return tournamentId != null
80 ? scanTournament(tournamentId)
85 public PGNGame scanGame(String gameId) throws ScannerException {
86 throw new ScannerException(
87 "scanner.portal.search.without.tournament.name", DOMAIN_CHESSBOMB
92 public PGNGame scanGame(String gameId, String tournamentId) throws ScannerException {
93 return scanGameByRegex(
94 assignUrl(gameId, tournamentId, true),
95 getSettings().getGameJsonUrlRegex()
100 public PGNGame scanUrl(String gameUrl) throws ScannerException {
101 return scanGameByRegex(
103 getSettings().getGameJsonUrlRegex()
107 private PGNGame scanGameByRegex(String gameUrl, String regex) throws ScannerException {
108 String decodeGame = decodeUrlByRegex(
113 GameFormat gameFormat = Format.formatGame(decodeGame);
122 private String decodeUrlByRegex(String url, String regex) throws ScannerException {
123 String encodeString = match(
127 if (encodeString == null) {
128 throw new ScannerException(
129 "scanner.failed.decode.url", url
133 Base64.getDecoder().decode(