[LIB-13] Locale resource settings
[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
21 import org.apache.commons.text.StringEscapeUtils;
22
23 import org.hedgecode.chess.scanner.Scanner;
24 import org.hedgecode.chess.scanner.ScannerException;
25 import org.hedgecode.chess.scanner.StringUtils;
26 import org.hedgecode.chess.scanner.entity.PGNGame;
27 import org.hedgecode.chess.scanner.entity.PGNTournament;
28 import org.hedgecode.chess.scanner.format.lichess.Format;
29 import org.hedgecode.chess.scanner.format.lichess.GameFormat;
30
31 import static org.hedgecode.chess.scanner.ScannerConstants.*;
32 import static org.hedgecode.chess.scanner.format.PGNConstants.*;
33
34 /**
35  * LiChessScanner
36  *
37  * @author Dmitry Samoshin aka gotty
38  */
39 public class LiChessScanner extends AbstractSettingsScanner implements Scanner {
40
41     private static final String SETTINGS_FILENAME = "lichess.settings";
42
43     @Override
44     protected String getResourceName() {
45         return SETTINGS_FILENAME;
46     }
47
48     @Override
49     public PGNTournament scanTournament(String tournamentId) throws ScannerException {
50         PGNTournament tournament = new PGNTournament(tournamentId);
51         assignTournamentGames(tournament);
52         return tournament;
53     }
54
55     @Override
56     public PGNTournament findTournament(String tournamentName) throws ScannerException {
57         throw new ScannerException(
58                 "scanner.portal.search.unavailable,name", DOMAIN_LICHESS
59         );
60     }
61
62     @Override
63     public PGNGame scanGame(String gameId) throws ScannerException {
64         String pgn = request(
65                 assignUrl(gameId)
66         );
67         if (!StringUtils.isPgn(pgn)) {
68             throw new ScannerException(
69                     "scanner.failed.request.game", gameId
70             );
71         }
72         return new PGNGame(
73                 gameId, pgn
74         );
75     }
76
77     @Override
78     public PGNGame scanGame(String gameId, String tournamentId) throws ScannerException {
79         return scanGame(gameId);
80     }
81
82     @Override
83     public PGNGame scanUrl(String gameUrl) throws ScannerException {
84         String gamePage = request(gameUrl);
85         String pgn = StringUtils.match(
86                 gamePage,
87                 getSettings().getGameUrlRegex()
88         );
89         if (pgn == null) {
90             pgn = StringUtils.match(
91                     gamePage,
92                     getSettings().getGameJsonUrlRegex()
93             );
94             if (pgn == null) {
95                 throw new ScannerException(
96                         "scanner.failed.source.data", gameUrl
97                 );
98             }
99             GameFormat gameFormat = Format.formatGame(pgn);
100             return new PGNGame(
101                     gameFormat.id(),
102                     gameFormat.pgn()
103             );
104         } else {
105             pgn = StringEscapeUtils.unescapeHtml4(pgn);
106             if (!StringUtils.isPgn(pgn)) {
107                 throw new ScannerException(
108                         "scanner.failed.request.url", gameUrl
109                 );
110             }
111             return new PGNGame(
112                     StringUtils.match(
113                             pgn,
114                             getSettings().getGameIdRegex()
115                     ),
116                     pgn
117             );
118         }
119     }
120
121     private void assignTournamentGames(PGNTournament tournament) throws ScannerException {
122         tournament.clearGames();
123         List<String> pgnGames = split(
124                 assignUrl(
125                         tournament.id(),
126                         null
127                 ),
128                 PGN_DETECT_REGEX
129         );
130
131         if (!pgnGames.isEmpty()) {
132             tournament.setName(
133                     StringUtils.match(
134                             pgnGames.get(0),
135                             getSettings().getTournamentNameRegex()
136                     )
137             );
138         }
139
140         for (String pgn : pgnGames) {
141             String gameId = StringUtils.match(
142                     pgn,
143                     getSettings().getGameIdRegex()
144             );
145             tournament.addGame(
146                     new PGNGame(gameId, pgn)
147             );
148         }
149     }
150
151 }