[LIB-13] Package restructuring
[chesshog-scanner.git] / src / main / java / org / hedgecode / chess / scanner / portal / ChessBombScanner.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.Base64;
20
21 import org.hedgecode.chess.scanner.ChessHogScannerException;
22 import org.hedgecode.chess.scanner.entity.PGNGame;
23 import org.hedgecode.chess.scanner.entity.PGNTournament;
24 import org.hedgecode.chess.scanner.format.chessbomb.ArenaFormat;
25 import org.hedgecode.chess.scanner.format.chessbomb.Format;
26 import org.hedgecode.chess.scanner.format.chessbomb.GameFormat;
27 import org.hedgecode.chess.scanner.format.chessbomb.TournamentFormat;
28
29 /**
30  * ChessBombScanner
31  *
32  * @author Dmitry Samoshin aka gotty
33  */
34 public class ChessBombScanner extends AbstractSettingsScanner {
35
36     private static final String SETTINGS_FILENAME = "chessbomb.settings";
37
38     @Override
39     protected String getResourceName() {
40         return SETTINGS_FILENAME;
41     }
42
43     @Override
44     public PGNTournament scanTournament(String tournamentId) throws ChessHogScannerException {
45         String decodeTournament = decodeUrlByRegex(
46                 assignUrl(tournamentId, null),
47                 getSettings().getTournamentJsonUrlRegex()
48         );
49
50         TournamentFormat tournamentFormat = Format.formatTournament(decodeTournament);
51
52         PGNTournament tournament = new PGNTournament(
53                 tournamentFormat.id(), tournamentFormat.name()
54         );
55
56         for (String gameId : tournamentFormat.gameUrls()) {
57             tournament.addGame(
58                     scanGame(gameId, tournamentId)
59             );
60         }
61
62         return tournament;
63     }
64
65     @Override
66     public PGNTournament findTournament(String tournamentName) throws ChessHogScannerException {
67         String decodeArena = decodeUrlByRegex(
68                 assignUrl(tournamentName, false),
69                 getSettings().getTournamentQueryUrlRegex()
70         );
71
72         ArenaFormat arenaFormat = Format.formatArena(decodeArena);
73
74         String tournamentId = arenaFormat.findTournament(tournamentName);
75
76         return tournamentId != null
77                 ? scanTournament(tournamentId)
78                 : null;
79     }
80
81     @Override
82     public PGNGame scanGame(String gameId) throws ChessHogScannerException {
83         throw new ChessHogScannerException(
84                 "ChessBomb does not support searching game without a tournament name!"
85         );
86     }
87
88     @Override
89     public PGNGame scanGame(String gameId, String tournamentId) throws ChessHogScannerException {
90         return scanGameByRegex(
91                 assignUrl(gameId, tournamentId, true),
92                 getSettings().getGameJsonUrlRegex()
93         );
94     }
95
96     @Override
97     public PGNGame scanUrl(String gameUrl) throws ChessHogScannerException {
98         return scanGameByRegex(
99                 gameUrl,
100                 getSettings().getGameJsonUrlRegex()
101         );
102     }
103
104     private PGNGame scanGameByRegex(String gameUrl, String regex) throws ChessHogScannerException {
105         String decodeGame = decodeUrlByRegex(
106                 gameUrl,
107                 regex
108         );
109
110         GameFormat gameFormat = Format.formatGame(decodeGame);
111
112         return new PGNGame(
113                 gameFormat.id(),
114                 gameFormat.pgn()
115         );
116     }
117
118
119     private String decodeUrlByRegex(String url, String regex) throws ChessHogScannerException {
120         String encodeString = match(
121                 url,
122                 regex
123         );
124         if (encodeString == null) {
125             throw new ChessHogScannerException(
126                     String.format("Failed to decode source data for requesting URL: %s", url)
127             );
128         }
129         return new String(
130                 Base64.getDecoder().decode(
131                         encodeString
132                 )
133         );
134     }
135
136 }