[LIB-13] Method scanUrl implementation for several scanners
[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.ChessHogScannerException;
24 import org.hedgecode.chess.scanner.entity.PGNGame;
25 import org.hedgecode.chess.scanner.entity.PGNTournament;
26 import org.hedgecode.chess.scanner.format.lichess.Format;
27 import org.hedgecode.chess.scanner.format.lichess.GameFormat;
28
29 import static org.hedgecode.chess.scanner.ChessHogScannerConstants.*;
30
31 /**
32  * LiChessScanner
33  *
34  * @author Dmitry Samoshin aka gotty
35  */
36 public class LiChessScanner extends AbstractSettingsScanner {
37
38     private static final String SETTINGS_FILENAME = "lichess.settings";
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         if (!isPgnFormat(pgn)) {
65             throw new ChessHogScannerException(
66                     String.format("Failed to get PGN for requesting game ID: %s", gameId)
67             );
68         }
69         return new PGNGame(
70                 gameId, pgn
71         );
72     }
73
74     @Override
75     public PGNGame scanGame(String gameId, String tournamentId) throws ChessHogScannerException {
76         return scanGame(gameId);
77     }
78
79     @Override
80     public PGNGame scanUrl(String gameUrl) throws ChessHogScannerException {
81         String gamePage = request(gameUrl);
82         String pgn = regex(
83                 gamePage,
84                 getSettings().getGameUrlRegex()
85         );
86         if (pgn == null) {
87             pgn = regex(
88                     gamePage,
89                     getSettings().getGameJsonUrlRegex()
90             );
91             if (pgn == null) {
92                 throw new ChessHogScannerException(
93                         String.format("Failed to get source data for requesting URL: %s", gameUrl)
94                 );
95             }
96             GameFormat gameFormat = Format.formatGame(pgn);
97             return new PGNGame(
98                     gameFormat.id(),
99                     gameFormat.pgn()
100             );
101         } else {
102             pgn = StringEscapeUtils.unescapeHtml4(pgn);
103             if (!isPgnFormat(pgn)) {
104                 throw new ChessHogScannerException(
105                         String.format("Failed to get PGN for requesting URL: %s", gameUrl)
106                 );
107             }
108             return new PGNGame(
109                     regex(pgn, getSettings().getGameIdRegex()),
110                     pgn
111             );
112         }
113     }
114
115     private void assignTournamentGames(PGNTournament tournament) throws ChessHogScannerException {
116         tournament.clearGames();
117         List<String> pgnGames = split(
118                 assignUrl(
119                         tournament.id(),
120                         null
121                 ),
122                 PGN_DETECT_REGEX
123         );
124
125         if (!pgnGames.isEmpty()) {
126             tournament.setName(
127                     regex(
128                             pgnGames.get(0),
129                             getSettings().getTournamentNameRegex()
130                     )
131             );
132         }
133
134         for (String pgn : pgnGames) {
135             String gameId = regex(
136                     pgn,
137                     getSettings().getGameIdRegex()
138             );
139             tournament.addGame(
140                     new PGNGame(gameId, pgn)
141             );
142         }
143     }
144
145 }