[LIB-13] Add function to Scanner API
[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     @Override
70     public PGNGame getGame(String gameId, String tournamentId) throws ChessHogScannerException {
71         return getGame(gameId);
72     }
73
74     private void assignTournamentGames(PGNTournament tournament) throws ChessHogScannerException {
75         tournament.clearGames();
76         List<String> pgnGames = split(
77                 assignUrl(
78                         tournament.id(),
79                         null
80                 ),
81                 TOURNAMENT_GAMES_SPLIT_REGEX
82         );
83
84         if (!pgnGames.isEmpty()) {
85             tournament.setName(
86                     find(TOURNAMENT_NAME_REGEX, pgnGames.get(0))
87             );
88         }
89
90         for (String pgn : pgnGames) {
91             String gameId = find(GAME_ID_REGEX, pgn);
92             tournament.addGame(
93                     new PGNGame(gameId, pgn)
94             );
95         }
96     }
97
98     private String find(String regex, String pgn) {
99         Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
100         Matcher matcher = pattern.matcher(pgn);
101         if (matcher.find()) {
102             return matcher.group(1);
103         }
104         return null;
105     }
106
107 }