import java.util.List;
import java.util.Map;
-import org.hedgecode.chess.scanner.ChessHogScannerException;
+import org.hedgecode.chess.scanner.Scanner;
+import org.hedgecode.chess.scanner.ScannerException;
+import org.hedgecode.chess.scanner.StringUtils;
import org.hedgecode.chess.scanner.entity.PGNGame;
import org.hedgecode.chess.scanner.entity.PGNTournament;
*
* @author Dmitry Samoshin aka gotty
*/
-public class ChessGamesScanner extends AbstractSettingsScanner {
+public class ChessGamesScanner extends AbstractSettingsScanner implements Scanner {
private static final String SETTINGS_FILENAME = "chessgames.settings";
}
@Override
- public PGNTournament getTournament(String tournamentId) throws ChessHogScannerException {
+ public PGNTournament scanTournament(String tournamentId) throws ScannerException {
PGNTournament tournament = new PGNTournament(tournamentId);
assignTournamentGames(tournament);
return tournament;
}
@Override
- public PGNTournament findTournament(String tournamentName) throws ChessHogScannerException {
+ public PGNTournament findTournament(String tournamentName) throws ScannerException {
+ PGNTournament tournament = null;
Map<String, String> result = matchMap(
assignUrl(
tournamentName, true
getSettings().getTournamentQueryUrlRegex(),
true
);
- PGNTournament tournament = null;
for (Map.Entry<String, String> entry : result.entrySet()) {
if (entry.getValue().contains(tournamentName)) { // todo: contains?
tournament = new PGNTournament(
}
@Override
- public PGNGame getGame(String gameId) throws ChessHogScannerException {
+ public PGNGame scanGame(String gameId) throws ScannerException {
String pgn = request(
assignUrl(gameId)
);
+ if (!StringUtils.isPgn(pgn)) {
+ throw new ScannerException(
+ String.format("Failed to get PGN for requesting game ID: %s", gameId)
+ );
+ }
return new PGNGame(
gameId, pgn
);
}
@Override
- public PGNGame getGame(String gameId, String tournamentId) throws ChessHogScannerException {
- return getGame(gameId);
+ public PGNGame scanGame(String gameId, String tournamentId) throws ScannerException {
+ return scanGame(gameId);
+ }
+
+ @Override
+ public PGNGame scanUrl(String gameUrl) throws ScannerException {
+ String gamePage = request(gameUrl);
+ String pgn = StringUtils.match(
+ gamePage,
+ getSettings().getGameUrlRegex()
+ );
+ if (pgn == null || !StringUtils.isPgn(pgn)) {
+ throw new ScannerException(
+ String.format("Failed to get PGN for requesting URL: %s", gameUrl)
+ );
+ }
+ return new PGNGame(
+ null,
+ pgn
+ );
}
- private void assignTournamentGames(PGNTournament tournament) throws ChessHogScannerException {
+ private void assignTournamentGames(PGNTournament tournament) throws ScannerException {
tournament.clearGames();
List<String> gamesId = new ArrayList<>();
for (String gameId : gamesId) {
tournament.addGame(
- getGame(gameId)
+ scanGame(gameId)
);
}
}