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.List;
21 import org.apache.commons.text.StringEscapeUtils;
23 import org.hedgecode.chess.scanner.ChessHogScannerException;
24 import org.hedgecode.chess.scanner.entity.PGNGame;
25 import org.hedgecode.chess.scanner.entity.PGNTournament;
26 import org.hedgecode.chess.scanner.format.lichess.Format;
27 import org.hedgecode.chess.scanner.format.lichess.GameFormat;
29 import static org.hedgecode.chess.scanner.ChessHogScannerConstants.*;
34 * @author Dmitry Samoshin aka gotty
36 public class LiChessScanner extends AbstractSettingsScanner {
38 private static final String SETTINGS_FILENAME = "lichess.settings";
41 protected String getResourceName() {
42 return SETTINGS_FILENAME;
46 public PGNTournament scanTournament(String tournamentId) throws ChessHogScannerException {
47 PGNTournament tournament = new PGNTournament(tournamentId);
48 assignTournamentGames(tournament);
53 public PGNTournament findTournament(String tournamentName) throws ChessHogScannerException {
54 throw new ChessHogScannerException(
55 "Lichess does not support searching for a tournament by name!"
60 public PGNGame scanGame(String gameId) throws ChessHogScannerException {
64 if (!isPgnFormat(pgn)) {
65 throw new ChessHogScannerException(
66 String.format("Failed to get PGN for requesting game ID: %s", gameId)
75 public PGNGame scanGame(String gameId, String tournamentId) throws ChessHogScannerException {
76 return scanGame(gameId);
80 public PGNGame scanUrl(String gameUrl) throws ChessHogScannerException {
81 String gamePage = request(gameUrl);
84 getSettings().getGameUrlRegex()
89 getSettings().getGameJsonUrlRegex()
92 throw new ChessHogScannerException(
93 String.format("Failed to get source data for requesting URL: %s", gameUrl)
96 GameFormat gameFormat = Format.formatGame(pgn);
102 pgn = StringEscapeUtils.unescapeHtml4(pgn);
103 if (!isPgnFormat(pgn)) {
104 throw new ChessHogScannerException(
105 String.format("Failed to get PGN for requesting URL: %s", gameUrl)
109 regex(pgn, getSettings().getGameIdRegex()),
115 private void assignTournamentGames(PGNTournament tournament) throws ChessHogScannerException {
116 tournament.clearGames();
117 List<String> pgnGames = split(
125 if (!pgnGames.isEmpty()) {
129 getSettings().getTournamentNameRegex()
134 for (String pgn : pgnGames) {
135 String gameId = regex(
137 getSettings().getGameIdRegex()
140 new PGNGame(gameId, pgn)