*
* @param url URL string for scan.
* @return PGN game.
- * @throws ChessHogScannerException Incorrect URL or unknown chess portal.
+ * @throws ScannerException Incorrect URL or unknown chess portal.
*/
- public static PGNGame scan(String url) throws ChessHogScannerException {
+ public static PGNGame scan(String url) throws ScannerException {
String hostName;
try {
hostName = new URL(url).getHost();
} catch (MalformedURLException cause) {
- throw new ChessHogScannerException(
+ throw new ScannerException(
String.format("Incorrect URL: %s", url), cause
);
}
ScannerType type = ScannerType.byHost(hostName);
if (type == null) {
- throw new ChessHogScannerException(
+ throw new ScannerException(
String.format("Host %s is not among the known for the scanner", hostName)
);
}
initiableScanner.init();
}
initScanners.add(type);
- } catch (ChessHogScannerException e) {
+ } catch (ScannerException e) {
throw new RuntimeException(e);
}
}
}
private ChessHogScanner() {
- if (!ChessHogScannerProperties.is("scanner.lazy.init")) {
+ if (!ScannerProperties.is("scanner.lazy.init")) {
init();
}
}
*/
public final class ChessHogScannerApp {
- public static void main(String[] args) throws ChessHogScannerException {
+ public static void main(String[] args) throws ScannerException {
- ChessHogScannerConsole.init();
+ ScannerConsole.init();
- boolean useProxy = Boolean.parseBoolean(
- ChessHogScannerProperties.get("scanner.use.proxy")
- );
+ boolean useProxy = ScannerProperties.is("scanner.use.proxy");
if (useProxy) {
ProxyParams proxyParams = new ProxyParams(
- ChessHogScannerProperties.get("scanner.proxy.server"),
- ChessHogScannerProperties.get("scanner.proxy.auth")
+ ScannerProperties.get("scanner.proxy.server"),
+ ScannerProperties.get("scanner.proxy.auth")
);
- ChessHogScannerConsole.console(proxyParams.toString());
Proxy.setProxy(proxyParams);
}
*/
public interface Initiable {
- void init() throws ChessHogScannerException;
+ void init() throws ScannerException;
}
*/
public interface Scanner {
- PGNTournament scanTournament(String tournamentId) throws ChessHogScannerException;
+ PGNTournament scanTournament(String tournamentId) throws ScannerException;
- PGNTournament findTournament(String tournamentName) throws ChessHogScannerException;
+ PGNTournament findTournament(String tournamentName) throws ScannerException;
- PGNGame scanGame(String gameId) throws ChessHogScannerException;
+ PGNGame scanGame(String gameId) throws ScannerException;
- PGNGame scanGame(String gameId, String tournamentId) throws ChessHogScannerException;
+ PGNGame scanGame(String gameId, String tournamentId) throws ScannerException;
- PGNGame scanUrl(String gameUrl) throws ChessHogScannerException;
+ PGNGame scanUrl(String gameUrl) throws ScannerException;
}
*
* @author Dmitry Samoshin aka gotty
*/
-public class ChessHogScannerConsole {
+public class ScannerConsole {
private static final String[] ASCII_LOGO = {
" _______ _ _ ",
private static boolean logMode = false;
- private ChessHogScannerConsole() {
+ private ScannerConsole() {
}
public static void setLogMode(boolean isLogMode) {
}
private static void copyright() {
- int inceptionYear = ChessHogScannerProperties.getInt("scanner.inception.year");
+ int inceptionYear = ScannerProperties.getInt("scanner.inception.year");
int currentYear = Calendar.getInstance().get(Calendar.YEAR);
String copyright = currentYear > inceptionYear
? String.format("%s-%s %s", inceptionYear, currentYear, COPYRIGHT)
console(
alignCenter(
String.format(
- "v. %s", ChessHogScannerProperties.get("scanner.version")
+ "v. %s", ScannerProperties.get("scanner.version")
)
)
);
*
* @author Dmitry Samoshin aka gotty
*/
-public final class ChessHogScannerConstants {
+public final class ScannerConstants {
public static final Charset CHARSET = StandardCharsets.UTF_8;
public static final String PROXY_HTTP = "http";
public static final String PROXY_SOCKS = "socks";
- private ChessHogScannerConstants() {
+ private ScannerConstants() {
throw new AssertionError(
String.format("No %s instances!", getClass().getName())
);
package org.hedgecode.chess.scanner;
/**
- * ChessHogScannerException
+ * ScannerException
*
* @author Dmitry Samoshin aka gotty
*/
-public class ChessHogScannerException extends Exception {
+public class ScannerException extends Exception {
- public ChessHogScannerException(String message) {
+ public ScannerException(String message) {
super(message);
}
- public ChessHogScannerException(Throwable cause) {
+ public ScannerException(Throwable cause) {
super(cause);
}
- public ChessHogScannerException(String message, Throwable cause) {
+ public ScannerException(String message, Throwable cause) {
super(message, cause);
}
*
* @author Dmitry Samoshin aka gotty
*/
-public class ChessHogScannerProperties {
+public class ScannerProperties {
/** Properties file. */
private static final String PROPERTIES_FILE = "scanner.properties";
* @param throwExc Flag indicating whether to throw an exception or not.
*/
public static void readProperties(String propsFile, Properties props, boolean throwExc) {
- try (InputStream is = ChessHogScannerProperties.class.getClassLoader().getResourceAsStream(propsFile))
+ try (InputStream is = ScannerProperties.class.getClassLoader().getResourceAsStream(propsFile))
{
if (is == null) {
if (throwExc) {
/**
* Private constructor.
*/
- private ChessHogScannerProperties() {
+ private ScannerProperties() {
}
}
package org.hedgecode.chess.scanner;
-import static org.hedgecode.chess.scanner.ChessHogScannerConstants.*;
+import static org.hedgecode.chess.scanner.ScannerConstants.*;
/**
* ScannerType
*/
public interface SettingsBuilder {
- Settings build(String name) throws ChessHogScannerException;
+ Settings build(String name) throws ScannerException;
}
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
-import org.hedgecode.chess.scanner.ChessHogScannerException;
+import org.hedgecode.chess.scanner.ScannerException;
import org.hedgecode.chess.scanner.Settings;
import org.hedgecode.chess.scanner.SettingsBuilder;
private static final Gson GSON = new GsonBuilder().create();
@Override
- public Settings build(String jsonFileName) throws ChessHogScannerException {
+ public Settings build(String jsonFileName) throws ScannerException {
Settings settings;
try (BufferedReader jsonReader = new BufferedReader(
new InputStreamReader(
JSONSettings.class
);
} catch (IOException cause) {
- throw new ChessHogScannerException(
+ throw new ScannerException(
String.format("Failed to get settings from resource file: %s", jsonFileName), cause
);
}
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
-import org.hedgecode.chess.scanner.ChessHogScannerException;
import org.hedgecode.chess.scanner.Scanner;
+import org.hedgecode.chess.scanner.ScannerException;
import org.hedgecode.chess.scanner.regex.RegexMatcher;
import org.hedgecode.chess.scanner.regex.RegexResult;
import org.hedgecode.chess.scanner.regex.RegexType;
import org.hedgecode.chess.scanner.regex.RegexTypeMatcher;
-import static org.hedgecode.chess.scanner.ChessHogScannerConstants.*;
+import static org.hedgecode.chess.scanner.ScannerConstants.*;
/**
* AbstractRequestScanner
*/
public abstract class AbstractRequestScanner implements Scanner {
- protected String request(String url) throws ChessHogScannerException {
+ protected String request(String url) throws ScannerException {
return request(url, true);
}
- protected String request(String url, boolean withCrlf) throws ChessHogScannerException {
+ protected String request(String url, boolean withCrlf) throws ScannerException {
HttpGet request = new HttpGet(url);
CloseableHttpResponse response = null;
StringBuilder sb = new StringBuilder();
}
}
} catch (IOException cause) {
- throw new ChessHogScannerException(
+ throw new ScannerException(
String.format("Error occurred for requesting URL: %s", url), cause
);
} finally {
return sb.toString();
}
- protected List<String> split(String url, String splitMatch) throws ChessHogScannerException {
+ protected List<String> split(String url, String splitMatch) throws ScannerException {
RegexResult result = splitRequest(url, splitMatch);
return result.resultList();
}
- protected Map<String, String> splitMap(String url, String splitMatch) throws ChessHogScannerException {
+ protected Map<String, String> splitMap(String url, String splitMatch) throws ScannerException {
RegexResult result = splitRequest(url, splitMatch);
return result.resultMap();
}
- protected RegexResult splitRequest(String url, String splitMatch) throws ChessHogScannerException {
+ protected RegexResult splitRequest(String url, String splitMatch) throws ScannerException {
RegexMatcher matcher = new RegexTypeMatcher(
RegexType.SPLIT, splitMatch
);
return matchRequest(url, matcher, false);
}
- protected String match(String url, String match) throws ChessHogScannerException {
+ protected String match(String url, String match) throws ScannerException {
List<String> result = match(url, match, true);
return result.isEmpty() ? null : result.get(0);
}
protected List<String> match(String url, String match, boolean isFirst)
- throws ChessHogScannerException
+ throws ScannerException
{
RegexMatcher matcher = new RegexTypeMatcher(
RegexType.FIND, match
}
protected Map<String, String> matchMap(String url, String match, boolean isFirst)
- throws ChessHogScannerException
+ throws ScannerException
{
RegexMatcher matcher = new RegexTypeMatcher(
RegexType.FIND, match
}
protected String match(String url, String startMatch, String endMatch)
- throws ChessHogScannerException
+ throws ScannerException
{
List<String> result = match(url, startMatch, endMatch, true);
return result.isEmpty() ? null : result.get(0);
}
protected List<String> match(String url, String startMatch, String endMatch, boolean isFirst)
- throws ChessHogScannerException
+ throws ScannerException
{
RegexMatcher matcher = new RegexTypeMatcher(
startMatch, endMatch
}
protected Map<String, String> matchMap(String url, String startMatch, String endMatch, boolean isFirst)
- throws ChessHogScannerException
+ throws ScannerException
{
RegexMatcher matcher = new RegexTypeMatcher(
startMatch, endMatch
}
protected RegexResult matchRequest(String url, RegexMatcher matcher, boolean isFirst)
- throws ChessHogScannerException
+ throws ScannerException
{
HttpGet request = new HttpGet(url);
CloseableHttpResponse response = null;
}
}
} catch (IOException cause) {
- throw new ChessHogScannerException(
+ throw new ScannerException(
String.format("Error occurred for requesting URL: %s", url), cause
);
} finally {
import java.util.regex.Matcher;
import java.util.regex.Pattern;
-import org.hedgecode.chess.scanner.ChessHogScannerException;
import org.hedgecode.chess.scanner.Initiable;
+import org.hedgecode.chess.scanner.ScannerException;
import org.hedgecode.chess.scanner.Settings;
import org.hedgecode.chess.scanner.SettingsBuilder;
import org.hedgecode.chess.scanner.regex.RegexBuilder;
import org.hedgecode.chess.scanner.regex.RegexParams;
import org.hedgecode.chess.scanner.spi.ServiceRegistry;
-import static org.hedgecode.chess.scanner.ChessHogScannerConstants.*;
+import static org.hedgecode.chess.scanner.ScannerConstants.*;
import static org.hedgecode.chess.scanner.regex.RegexBuilder.Type;
/**
protected abstract String getResourceName();
@Override
- public void init() throws ChessHogScannerException {
+ public void init() throws ScannerException {
SettingsBuilder settingsBuilder = ServiceRegistry.singleProvider(
SettingsBuilder.class
);
package org.hedgecode.chess.scanner.portal;
-import org.hedgecode.chess.scanner.ChessHogScannerException;
import org.hedgecode.chess.scanner.Scanner;
+import org.hedgecode.chess.scanner.ScannerException;
import org.hedgecode.chess.scanner.entity.PGNGame;
import org.hedgecode.chess.scanner.entity.PGNTournament;
}
@Override
- public PGNTournament scanTournament(String tournamentId) throws ChessHogScannerException {
- throw new ChessHogScannerException(
+ public PGNTournament scanTournament(String tournamentId) throws ScannerException {
+ throw new ScannerException(
"The scanner functional of portal chess24.com is under development!"
);
}
@Override
- public PGNTournament findTournament(String tournamentName) throws ChessHogScannerException {
- throw new ChessHogScannerException(
+ public PGNTournament findTournament(String tournamentName) throws ScannerException {
+ throw new ScannerException(
"The scanner functional of portal chess24.com is under development!"
);
}
@Override
- public PGNGame scanGame(String gameId) throws ChessHogScannerException {
+ public PGNGame scanGame(String gameId) throws ScannerException {
return scanGame(gameId, null);
}
@Override
- public PGNGame scanGame(String gameId, String tournamentId) throws ChessHogScannerException {
- throw new ChessHogScannerException(
+ public PGNGame scanGame(String gameId, String tournamentId) throws ScannerException {
+ throw new ScannerException(
"The scanner functional of portal chess24.com is under development!"
);
}
@Override
- public PGNGame scanUrl(String gameUrl) throws ChessHogScannerException {
- throw new ChessHogScannerException(
+ public PGNGame scanUrl(String gameUrl) throws ScannerException {
+ throw new ScannerException(
"The scanner functional of portal chess24.com is under development!"
);
}
import java.util.Base64;
-import org.hedgecode.chess.scanner.ChessHogScannerException;
import org.hedgecode.chess.scanner.Scanner;
+import org.hedgecode.chess.scanner.ScannerException;
import org.hedgecode.chess.scanner.entity.PGNGame;
import org.hedgecode.chess.scanner.entity.PGNTournament;
import org.hedgecode.chess.scanner.format.chessbomb.ArenaFormat;
}
@Override
- public PGNTournament scanTournament(String tournamentId) throws ChessHogScannerException {
+ public PGNTournament scanTournament(String tournamentId) throws ScannerException {
String decodeTournament = decodeUrlByRegex(
assignUrl(tournamentId, null),
getSettings().getTournamentJsonUrlRegex()
}
@Override
- public PGNTournament findTournament(String tournamentName) throws ChessHogScannerException {
+ public PGNTournament findTournament(String tournamentName) throws ScannerException {
String decodeArena = decodeUrlByRegex(
assignUrl(tournamentName, false),
getSettings().getTournamentQueryUrlRegex()
}
@Override
- public PGNGame scanGame(String gameId) throws ChessHogScannerException {
- throw new ChessHogScannerException(
+ public PGNGame scanGame(String gameId) throws ScannerException {
+ throw new ScannerException(
"ChessBomb does not support searching game without a tournament name!"
);
}
@Override
- public PGNGame scanGame(String gameId, String tournamentId) throws ChessHogScannerException {
+ public PGNGame scanGame(String gameId, String tournamentId) throws ScannerException {
return scanGameByRegex(
assignUrl(gameId, tournamentId, true),
getSettings().getGameJsonUrlRegex()
}
@Override
- public PGNGame scanUrl(String gameUrl) throws ChessHogScannerException {
+ public PGNGame scanUrl(String gameUrl) throws ScannerException {
return scanGameByRegex(
gameUrl,
getSettings().getGameJsonUrlRegex()
);
}
- private PGNGame scanGameByRegex(String gameUrl, String regex) throws ChessHogScannerException {
+ private PGNGame scanGameByRegex(String gameUrl, String regex) throws ScannerException {
String decodeGame = decodeUrlByRegex(
gameUrl,
regex
}
- private String decodeUrlByRegex(String url, String regex) throws ChessHogScannerException {
+ private String decodeUrlByRegex(String url, String regex) throws ScannerException {
String encodeString = match(
url,
regex
);
if (encodeString == null) {
- throw new ChessHogScannerException(
+ throw new ScannerException(
String.format("Failed to decode source data for requesting URL: %s", url)
);
}
package org.hedgecode.chess.scanner.portal;
-import org.hedgecode.chess.scanner.ChessHogScannerException;
import org.hedgecode.chess.scanner.Scanner;
+import org.hedgecode.chess.scanner.ScannerException;
import org.hedgecode.chess.scanner.entity.PGNGame;
import org.hedgecode.chess.scanner.entity.PGNTournament;
}
@Override
- public PGNTournament scanTournament(String tournamentId) throws ChessHogScannerException {
- throw new ChessHogScannerException(
+ public PGNTournament scanTournament(String tournamentId) throws ScannerException {
+ throw new ScannerException(
"The scanner functional of portal chess.com is under development!"
);
}
@Override
- public PGNTournament findTournament(String tournamentName) throws ChessHogScannerException {
- throw new ChessHogScannerException(
+ public PGNTournament findTournament(String tournamentName) throws ScannerException {
+ throw new ScannerException(
"The scanner functional of portal chess.com is under development!"
);
}
@Override
- public PGNGame scanGame(String gameId) throws ChessHogScannerException {
+ public PGNGame scanGame(String gameId) throws ScannerException {
return scanGame(gameId, null);
}
@Override
- public PGNGame scanGame(String gameId, String tournamentId) throws ChessHogScannerException {
- throw new ChessHogScannerException(
+ public PGNGame scanGame(String gameId, String tournamentId) throws ScannerException {
+ throw new ScannerException(
"The scanner functional of portal chess.com is under development!"
);
}
@Override
- public PGNGame scanUrl(String gameUrl) throws ChessHogScannerException {
- throw new ChessHogScannerException(
+ public PGNGame scanUrl(String gameUrl) throws ScannerException {
+ throw new ScannerException(
"The scanner functional of portal chess.com is under development!"
);
}
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.entity.PGNGame;
import org.hedgecode.chess.scanner.entity.PGNTournament;
}
@Override
- public PGNTournament scanTournament(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(
}
@Override
- public PGNGame scanGame(String gameId) throws ChessHogScannerException {
+ public PGNGame scanGame(String gameId) throws ScannerException {
String pgn = request(
assignUrl(gameId)
);
if (!isPgnFormat(pgn)) {
- throw new ChessHogScannerException(
+ throw new ScannerException(
String.format("Failed to get PGN for requesting game ID: %s", gameId)
);
}
}
@Override
- public PGNGame scanGame(String gameId, String tournamentId) throws ChessHogScannerException {
+ public PGNGame scanGame(String gameId, String tournamentId) throws ScannerException {
return scanGame(gameId);
}
@Override
- public PGNGame scanUrl(String gameUrl) throws ChessHogScannerException {
+ public PGNGame scanUrl(String gameUrl) throws ScannerException {
String pgn = regex(
request(
gameUrl
getSettings().getGameUrlRegex()
);
if (pgn == null || !isPgnFormat(pgn)) {
- throw new ChessHogScannerException(
+ throw new ScannerException(
String.format("Failed to get PGN for requesting URL: %s", gameUrl)
);
}
);
}
- private void assignTournamentGames(PGNTournament tournament) throws ChessHogScannerException {
+ private void assignTournamentGames(PGNTournament tournament) throws ScannerException {
tournament.clearGames();
List<String> gamesId = new ArrayList<>();
import org.apache.commons.text.StringEscapeUtils;
-import org.hedgecode.chess.scanner.ChessHogScannerException;
import org.hedgecode.chess.scanner.Scanner;
+import org.hedgecode.chess.scanner.ScannerException;
import org.hedgecode.chess.scanner.entity.PGNGame;
import org.hedgecode.chess.scanner.entity.PGNTournament;
import org.hedgecode.chess.scanner.format.lichess.Format;
import org.hedgecode.chess.scanner.format.lichess.GameFormat;
-import static org.hedgecode.chess.scanner.ChessHogScannerConstants.*;
+import static org.hedgecode.chess.scanner.ScannerConstants.*;
/**
* LiChessScanner
}
@Override
- public PGNTournament scanTournament(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 {
- throw new ChessHogScannerException(
+ public PGNTournament findTournament(String tournamentName) throws ScannerException {
+ throw new ScannerException(
"Lichess does not support searching for a tournament by name!"
);
}
@Override
- public PGNGame scanGame(String gameId) throws ChessHogScannerException {
+ public PGNGame scanGame(String gameId) throws ScannerException {
String pgn = request(
assignUrl(gameId)
);
if (!isPgnFormat(pgn)) {
- throw new ChessHogScannerException(
+ throw new ScannerException(
String.format("Failed to get PGN for requesting game ID: %s", gameId)
);
}
}
@Override
- public PGNGame scanGame(String gameId, String tournamentId) throws ChessHogScannerException {
+ public PGNGame scanGame(String gameId, String tournamentId) throws ScannerException {
return scanGame(gameId);
}
@Override
- public PGNGame scanUrl(String gameUrl) throws ChessHogScannerException {
+ public PGNGame scanUrl(String gameUrl) throws ScannerException {
String gamePage = request(gameUrl);
String pgn = regex(
gamePage,
getSettings().getGameJsonUrlRegex()
);
if (pgn == null) {
- throw new ChessHogScannerException(
+ throw new ScannerException(
String.format("Failed to get source data for requesting URL: %s", gameUrl)
);
}
} else {
pgn = StringEscapeUtils.unescapeHtml4(pgn);
if (!isPgnFormat(pgn)) {
- throw new ChessHogScannerException(
+ throw new ScannerException(
String.format("Failed to get PGN for requesting URL: %s", gameUrl)
);
}
}
}
- private void assignTournamentGames(PGNTournament tournament) throws ChessHogScannerException {
+ private void assignTournamentGames(PGNTournament tournament) throws ScannerException {
tournament.clearGames();
List<String> pgnGames = split(
assignUrl(
import org.hedgecode.chess.scanner.proxy.type.HTTPSetter;
import org.hedgecode.chess.scanner.proxy.type.SOCKSSetter;
-import static org.hedgecode.chess.scanner.ChessHogScannerConstants.*;
+import static org.hedgecode.chess.scanner.ScannerConstants.*;
/**
* Setter for proxy settings.
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
-import static org.hedgecode.chess.scanner.ChessHogScannerConstants.*;
+import static org.hedgecode.chess.scanner.ScannerConstants.*;
/**
* RegexBuilder
import org.hedgecode.chess.scanner.regex.RegexMatcher;
import org.hedgecode.chess.scanner.regex.RegexResult;
-import static org.hedgecode.chess.scanner.ChessHogScannerConstants.*;
+import static org.hedgecode.chess.scanner.ScannerConstants.*;
/**
* RegexBlockFinder
import org.hedgecode.chess.scanner.regex.RegexMatcher;
import org.hedgecode.chess.scanner.regex.RegexResult;
-import static org.hedgecode.chess.scanner.ChessHogScannerConstants.*;
+import static org.hedgecode.chess.scanner.ScannerConstants.*;
/**
* RegexSplitter