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.Scanner;
24 import org.hedgecode.chess.scanner.ScannerException;
25 import org.hedgecode.chess.scanner.entity.PGNGame;
26 import org.hedgecode.chess.scanner.entity.PGNTournament;
27 import org.hedgecode.chess.scanner.format.lichess.Format;
28 import org.hedgecode.chess.scanner.format.lichess.GameFormat;
30 import static org.hedgecode.chess.scanner.ScannerConstants.*;
35 * @author Dmitry Samoshin aka gotty
37 public class LiChessScanner extends AbstractSettingsScanner implements Scanner {
39 private static final String SETTINGS_FILENAME = "lichess.settings";
42 protected String getResourceName() {
43 return SETTINGS_FILENAME;
47 public PGNTournament scanTournament(String tournamentId) throws ScannerException {
48 PGNTournament tournament = new PGNTournament(tournamentId);
49 assignTournamentGames(tournament);
54 public PGNTournament findTournament(String tournamentName) throws ScannerException {
55 throw new ScannerException(
56 "Lichess does not support searching for a tournament by name!"
61 public PGNGame scanGame(String gameId) throws ScannerException {
65 if (!isPgnFormat(pgn)) {
66 throw new ScannerException(
67 String.format("Failed to get PGN for requesting game ID: %s", gameId)
76 public PGNGame scanGame(String gameId, String tournamentId) throws ScannerException {
77 return scanGame(gameId);
81 public PGNGame scanUrl(String gameUrl) throws ScannerException {
82 String gamePage = request(gameUrl);
85 getSettings().getGameUrlRegex()
90 getSettings().getGameJsonUrlRegex()
93 throw new ScannerException(
94 String.format("Failed to get source data for requesting URL: %s", gameUrl)
97 GameFormat gameFormat = Format.formatGame(pgn);
103 pgn = StringEscapeUtils.unescapeHtml4(pgn);
104 if (!isPgnFormat(pgn)) {
105 throw new ScannerException(
106 String.format("Failed to get PGN for requesting URL: %s", gameUrl)
110 regex(pgn, getSettings().getGameIdRegex()),
116 private void assignTournamentGames(PGNTournament tournament) throws ScannerException {
117 tournament.clearGames();
118 List<String> pgnGames = split(
126 if (!pgnGames.isEmpty()) {
130 getSettings().getTournamentNameRegex()
135 for (String pgn : pgnGames) {
136 String gameId = regex(
138 getSettings().getGameIdRegex()
141 new PGNGame(gameId, pgn)