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