ac0c8f33077f47b4aef16a0c0d9348fc4afd50be
[chesshog-scanner.git] / src / main / java / org / hedgecode / chess / scanner / portal / Chess2700Scanner.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 org.hedgecode.chess.scanner.Scanner;
20 import org.hedgecode.chess.scanner.ScannerException;
21 import org.hedgecode.chess.scanner.StringUtils;
22 import org.hedgecode.chess.scanner.entity.PGNGame;
23 import org.hedgecode.chess.scanner.entity.PGNTournament;
24
25 /**
26  * Chess2700Scanner
27  *
28  * @author Dmitry Samoshin aka gotty
29  */
30 public class Chess2700Scanner extends AbstractSettingsScanner implements Scanner {
31
32     private static final String SETTINGS_FILENAME = "2700chess.settings";
33
34     @Override
35     protected String getResourceName() {
36         return SETTINGS_FILENAME;
37     }
38
39     @Override
40     public PGNTournament scanTournament(String tournamentId) throws ScannerException {
41         throw new ScannerException(
42                 "2700Chess does not support searching for a tournament by ID!"
43         );
44     }
45
46     @Override
47     public PGNTournament findTournament(String tournamentName) throws ScannerException {
48         throw new ScannerException(
49                 "2700Chess does not support searching for a tournament by name!"
50         );
51     }
52
53     @Override
54     public PGNGame scanGame(String gameId) throws ScannerException {
55         return scanUrl(
56                 assignUrl(gameId)
57         );
58     }
59
60     @Override
61     public PGNGame scanGame(String gameId, String tournamentId) throws ScannerException {
62         return scanGame(gameId);
63     }
64
65     @Override
66     public PGNGame scanUrl(String gameUrl) throws ScannerException {
67         String pgn = match(
68                 gameUrl,
69                 getSettings().getGameUrlRegex()
70         );
71         if (pgn == null) {
72             throw new ScannerException(
73                     String.format("Failed to get PGN for requesting URL: %s", gameUrl)
74             );
75         }
76         pgn = StringUtils.formatCrlf(pgn);
77         if (!StringUtils.isPgn(pgn)) {
78             throw new ScannerException(
79                     String.format("Incorrect PGN for requesting URL: %s", gameUrl)
80             );
81         }
82         return new PGNGame(
83                 null,
84                 pgn
85         );
86     }
87
88 }