[LIB-13] Add lichess scanner
[chesshog-scanner.git] / src / main / java / org / hedgecode / chess / scanner / portal / LiChessScanner.java
1 /*
2  * Copyright (c) 2019-2020. Developed by Hedgecode.
3  *
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
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.hedgecode.chess.scanner.portal;
18
19 import java.util.List;
20 import java.util.regex.Matcher;
21 import java.util.regex.Pattern;
22
23 import org.hedgecode.chess.scanner.ChessHogScannerException;
24 import org.hedgecode.chess.scanner.entity.PGNGame;
25 import org.hedgecode.chess.scanner.entity.PGNTournament;
26
27 /**
28  * LiChessScanner
29  *
30  * @author Dmitry Samoshin aka gotty
31  */
32 public class LiChessScanner extends AbstractSettingsScanner {
33
34     private static final String SETTINGS_FILENAME = "lichess.settings";
35
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/([^\"]+)\"\\]";
39
40     @Override
41     protected String getResourceName() {
42         return SETTINGS_FILENAME;
43     }
44
45     @Override
46     public PGNTournament getTournament(String tournamentId) throws ChessHogScannerException {
47         PGNTournament tournament = new PGNTournament(tournamentId);
48         assignTournamentGames(tournament);
49         return tournament;
50     }
51
52     @Override
53     public PGNTournament findTournament(String tournamentName) throws ChessHogScannerException {
54         throw new ChessHogScannerException(
55                 "Lichess does not support searching for a tournament by name!"
56         );
57     }
58
59     @Override
60     public PGNGame getGame(String gameId) throws ChessHogScannerException {
61         String pgn = request(
62                 assignUrl(gameId)
63         );
64         return new PGNGame(
65                 gameId, pgn
66         );
67     }
68
69     private void assignTournamentGames(PGNTournament tournament) throws ChessHogScannerException {
70         tournament.clearGames();
71         List<String> pgnGames = split(
72                 assignUrl(
73                         tournament.id(),
74                         null
75                 ),
76                 TOURNAMENT_GAMES_SPLIT_REGEX
77         );
78
79         if (!pgnGames.isEmpty()) {
80             tournament.setName(
81                     find(TOURNAMENT_NAME_REGEX, pgnGames.get(0))
82             );
83         }
84
85         for (String pgn : pgnGames) {
86             String gameId = find(GAME_ID_REGEX, pgn);
87             tournament.addGame(
88                     new PGNGame(gameId, pgn)
89             );
90         }
91     }
92
93     private String find(String regex, String pgn) {
94         Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
95         Matcher matcher = pattern.matcher(pgn);
96         if (matcher.find()) {
97             return matcher.group(1);
98         }
99         return null;
100     }
101
102 }