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;
20 import java.util.regex.Matcher;
21 import java.util.regex.Pattern;
23 import org.hedgecode.chess.scanner.ChessHogScannerException;
24 import org.hedgecode.chess.scanner.entity.PGNGame;
25 import org.hedgecode.chess.scanner.entity.PGNTournament;
30 * @author Dmitry Samoshin aka gotty
32 public class LiChessScanner extends AbstractSettingsScanner {
34 private static final String SETTINGS_FILENAME = "lichess.settings";
36 private static final String TOURNAMENT_GAMES_SPLIT_REGEX = "\\[Event \"[^\"]+\"\\]";
37 private static final String TOURNAMENT_NAME_REGEX = "\\[Event \"([^\"]+)\"\\]";
38 private static final String GAME_ID_REGEX = "\\[Site \"https://lichess.org/([^\"]+)\"\\]";
41 protected String getResourceName() {
42 return SETTINGS_FILENAME;
46 public PGNTournament getTournament(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 getGame(String gameId) throws ChessHogScannerException {
69 private void assignTournamentGames(PGNTournament tournament) throws ChessHogScannerException {
70 tournament.clearGames();
71 List<String> pgnGames = split(
76 TOURNAMENT_GAMES_SPLIT_REGEX
79 if (!pgnGames.isEmpty()) {
81 find(TOURNAMENT_NAME_REGEX, pgnGames.get(0))
85 for (String pgn : pgnGames) {
86 String gameId = find(GAME_ID_REGEX, pgn);
88 new PGNGame(gameId, pgn)
93 private String find(String regex, String pgn) {
94 Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
95 Matcher matcher = pattern.matcher(pgn);
97 return matcher.group(1);