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