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.ScannerConstants.*;
32 import static org.hedgecode.chess.scanner.format.PGNConstants.*;
37 * @author Dmitry Samoshin aka gotty
39 public class LiChessScanner extends AbstractSettingsScanner implements Scanner {
41 private static final String SETTINGS_FILENAME = "lichess.settings";
44 protected String getResourceName() {
45 return SETTINGS_FILENAME;
49 public PGNTournament scanTournament(String tournamentId) throws ScannerException {
50 PGNTournament tournament = new PGNTournament(tournamentId);
51 assignTournamentGames(tournament);
56 public PGNTournament findTournament(String tournamentName) throws ScannerException {
57 throw new ScannerException(
58 "scanner.portal.search.unavailable,name", DOMAIN_LICHESS
63 public PGNGame scanGame(String gameId) throws ScannerException {
67 if (!StringUtils.isPgn(pgn)) {
68 throw new ScannerException(
69 "scanner.failed.request.game", gameId
78 public PGNGame scanGame(String gameId, String tournamentId) throws ScannerException {
79 return scanGame(gameId);
83 public PGNGame scanUrl(String gameUrl) throws ScannerException {
84 String gamePage = request(gameUrl);
85 String pgn = StringUtils.match(
87 getSettings().getGameUrlRegex()
90 pgn = StringUtils.match(
92 getSettings().getGameJsonUrlRegex()
95 throw new ScannerException(
96 "scanner.failed.source.data", gameUrl
99 GameFormat gameFormat = Format.formatGame(pgn);
105 pgn = StringEscapeUtils.unescapeHtml4(pgn);
106 if (!StringUtils.isPgn(pgn)) {
107 throw new ScannerException(
108 "scanner.failed.request.url", gameUrl
114 getSettings().getGameIdRegex()
121 private void assignTournamentGames(PGNTournament tournament) throws ScannerException {
122 tournament.clearGames();
123 List<String> pgnGames = split(
131 if (!pgnGames.isEmpty()) {
135 getSettings().getTournamentNameRegex()
140 for (String pgn : pgnGames) {
141 String gameId = StringUtils.match(
143 getSettings().getGameIdRegex()
146 new PGNGame(gameId, pgn)