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