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.StringUtils;
26 import org.hedgecode.chess.scanner.entity.PGNGame;
27 import org.hedgecode.chess.scanner.entity.PGNTournament;
28 import org.hedgecode.chess.scanner.format.lichess.Format;
29 import org.hedgecode.chess.scanner.format.lichess.GameFormat;
31 import static org.hedgecode.chess.scanner.format.PGNConstants.*;
36 * @author Dmitry Samoshin aka gotty
38 public class LiChessScanner extends AbstractSettingsScanner implements Scanner {
40 private static final String SETTINGS_FILENAME = "lichess.settings";
43 protected String getResourceName() {
44 return SETTINGS_FILENAME;
48 public PGNTournament scanTournament(String tournamentId) throws ScannerException {
49 PGNTournament tournament = new PGNTournament(tournamentId);
50 assignTournamentGames(tournament);
55 public PGNTournament findTournament(String tournamentName) throws ScannerException {
56 throw new ScannerException(
57 "Lichess does not support searching for a tournament by name!"
62 public PGNGame scanGame(String gameId) throws ScannerException {
66 if (!StringUtils.isPgn(pgn)) {
67 throw new ScannerException(
68 String.format("Failed to get PGN for requesting game ID: %s", gameId)
77 public PGNGame scanGame(String gameId, String tournamentId) throws ScannerException {
78 return scanGame(gameId);
82 public PGNGame scanUrl(String gameUrl) throws ScannerException {
83 String gamePage = request(gameUrl);
84 String pgn = StringUtils.match(
86 getSettings().getGameUrlRegex()
89 pgn = StringUtils.match(
91 getSettings().getGameJsonUrlRegex()
94 throw new ScannerException(
95 String.format("Failed to get source data for requesting URL: %s", gameUrl)
98 GameFormat gameFormat = Format.formatGame(pgn);
104 pgn = StringEscapeUtils.unescapeHtml4(pgn);
105 if (!StringUtils.isPgn(pgn)) {
106 throw new ScannerException(
107 String.format("Failed to get PGN for requesting URL: %s", gameUrl)
113 getSettings().getGameIdRegex()
120 private void assignTournamentGames(PGNTournament tournament) throws ScannerException {
121 tournament.clearGames();
122 List<String> pgnGames = split(
130 if (!pgnGames.isEmpty()) {
134 getSettings().getTournamentNameRegex()
139 for (String pgn : pgnGames) {
140 String gameId = StringUtils.match(
142 getSettings().getGameIdRegex()
145 new PGNGame(gameId, pgn)