X-Git-Url: https://git.hedgecode.org/?p=chesshog-scanner.git;a=blobdiff_plain;f=src%2Fmain%2Fjava%2Forg%2Fhedgecode%2Fchess%2Fscanner%2Fportal%2FLiChessScanner.java;fp=src%2Fmain%2Fjava%2Forg%2Fhedgecode%2Fchess%2Fscanner%2Fportal%2FLiChessScanner.java;h=f54e373dc29a69932bc80e900c073de3fb316c2e;hp=0000000000000000000000000000000000000000;hb=2f45a4a456c2d23642a91668276fc31211975711;hpb=607aedc60436c739f0a0fce23b4830c7e1561a53 diff --git a/src/main/java/org/hedgecode/chess/scanner/portal/LiChessScanner.java b/src/main/java/org/hedgecode/chess/scanner/portal/LiChessScanner.java new file mode 100644 index 0000000..f54e373 --- /dev/null +++ b/src/main/java/org/hedgecode/chess/scanner/portal/LiChessScanner.java @@ -0,0 +1,102 @@ +/* + * Copyright (c) 2019-2020. Developed by Hedgecode. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.hedgecode.chess.scanner.portal; + +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import org.hedgecode.chess.scanner.ChessHogScannerException; +import org.hedgecode.chess.scanner.entity.PGNGame; +import org.hedgecode.chess.scanner.entity.PGNTournament; + +/** + * LiChessScanner + * + * @author Dmitry Samoshin aka gotty + */ +public class LiChessScanner extends AbstractSettingsScanner { + + private static final String SETTINGS_FILENAME = "lichess.settings"; + + private static final String TOURNAMENT_GAMES_SPLIT_REGEX = "\\[Event \"[^\"]+\"\\]"; + private static final String TOURNAMENT_NAME_REGEX = "\\[Event \"([^\"]+)\"\\]"; + private static final String GAME_ID_REGEX = "\\[Site \"https://lichess.org/([^\"]+)\"\\]"; + + @Override + protected String getResourceName() { + return SETTINGS_FILENAME; + } + + @Override + public PGNTournament getTournament(String tournamentId) throws ChessHogScannerException { + PGNTournament tournament = new PGNTournament(tournamentId); + assignTournamentGames(tournament); + return tournament; + } + + @Override + public PGNTournament findTournament(String tournamentName) throws ChessHogScannerException { + throw new ChessHogScannerException( + "Lichess does not support searching for a tournament by name!" + ); + } + + @Override + public PGNGame getGame(String gameId) throws ChessHogScannerException { + String pgn = request( + assignUrl(gameId) + ); + return new PGNGame( + gameId, pgn + ); + } + + private void assignTournamentGames(PGNTournament tournament) throws ChessHogScannerException { + tournament.clearGames(); + List pgnGames = split( + assignUrl( + tournament.id(), + null + ), + TOURNAMENT_GAMES_SPLIT_REGEX + ); + + if (!pgnGames.isEmpty()) { + tournament.setName( + find(TOURNAMENT_NAME_REGEX, pgnGames.get(0)) + ); + } + + for (String pgn : pgnGames) { + String gameId = find(GAME_ID_REGEX, pgn); + tournament.addGame( + new PGNGame(gameId, pgn) + ); + } + } + + private String find(String regex, String pgn) { + Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE); + Matcher matcher = pattern.matcher(pgn); + if (matcher.find()) { + return matcher.group(1); + } + return null; + } + +}