Fixed issue where a disconnection from the OpenSubtitles.org system or a failure in login would exit the program

Signed-off-by: Louis Vallat <louis@louis-vallat.xyz>
This commit is contained in:
Louis Vallat 2020-10-27 23:17:41 +01:00
parent 4ba3d48d3c
commit afe16f9eb1
4 changed files with 13 additions and 26 deletions

View File

@ -21,9 +21,6 @@ public class MovieQuoteBot {
// TODO: MOVE TO ENUM // TODO: MOVE TO ENUM
public static final String DISCORD_TOKEN_ENVIRONMENT = "DISCORD_TOKEN"; public static final String DISCORD_TOKEN_ENVIRONMENT = "DISCORD_TOKEN";
public static final String OS_USERNAME_ENVIRONMENT = "OPEN_SUBTITLES_USERNAME";
public static final String OS_PASSWORD_ENVIRONMENT = "OPEN_SUBTITLES_PASSWORD";
public static final String OS_UA_ENVIRONMENT = "OPEN_SUBTITLES_USER_AGENT";
public static final String PREFIX = "!"; public static final String PREFIX = "!";
public static final String NAME = "Movies Quote Bot"; public static final String NAME = "Movies Quote Bot";
@ -44,13 +41,7 @@ public class MovieQuoteBot {
public static void main(String[] args) { public static void main(String[] args) {
DBManager.testConnection(); DBManager.testConnection();
DBManager.initDatabase(); DBManager.initDatabase();
// TODO: FIX CRASH ON LOGIN IF OS IS IN MAINTENANCE OR BROKEN OpenSubtitles.login();
OpenSubtitles.login(
System.getenv(OS_USERNAME_ENVIRONMENT),
System.getenv(OS_PASSWORD_ENVIRONMENT),
"en",
System.getenv(OS_UA_ENVIRONMENT)
);
discordLogin(System.getenv(DISCORD_TOKEN_ENVIRONMENT)); discordLogin(System.getenv(DISCORD_TOKEN_ENVIRONMENT));
registerDiscordCommands(); registerDiscordCommands();

View File

@ -26,6 +26,7 @@ public final class PropertyManager {
stmt.setString(1, value); stmt.setString(1, value);
stmt.setString(2, key); stmt.setString(2, key);
stmt.execute(); stmt.execute();
logger.debug("Property '{}' saved.", key);
} }
} catch (SQLException e) { } catch (SQLException e) {
logger.error("Cannot save properties right now. Reason: {}", e.getMessage()); logger.error("Cannot save properties right now. Reason: {}", e.getMessage());

View File

@ -12,37 +12,31 @@ import java.net.URL;
public final class OpenSubtitles { public final class OpenSubtitles {
public static final String OS_USERNAME_ENVIRONMENT = "OPEN_SUBTITLES_USERNAME";
public static final String OS_PASSWORD_ENVIRONMENT = "OPEN_SUBTITLES_PASSWORD";
public static final String OS_UA_ENVIRONMENT = "OPEN_SUBTITLES_USER_AGENT";
private static final Logger logger = LoggerFactory.getLogger(OpenSubtitles.class.getCanonicalName()); private static final Logger logger = LoggerFactory.getLogger(OpenSubtitles.class.getCanonicalName());
private static final String USERNAME = System.getenv(OS_USERNAME_ENVIRONMENT);
private static String username; private static final String PASSWORD = System.getenv(OS_PASSWORD_ENVIRONMENT);
private static String password; private static final String LANG = "en";
private static String lang; private static final String USER_AGENT = System.getenv(OS_UA_ENVIRONMENT);
private static String userAgent;
private static OpenSubtitlesClient client; private static OpenSubtitlesClient client;
private OpenSubtitles() { private OpenSubtitles() {
} }
public static void login(String username, String password, String lang, String userAgent) {
OpenSubtitles.username = username;
OpenSubtitles.password = password;
OpenSubtitles.lang = lang;
OpenSubtitles.userAgent = userAgent;
login();
}
public static void login() { public static void login() {
try { try {
URL serverUrl = new URL("https", "api.opensubtitles.org", 443, "/xml-rpc"); URL serverUrl = new URL("https", "api.opensubtitles.org", 443, "/xml-rpc");
client = new OpenSubtitlesClientImpl(serverUrl); client = new OpenSubtitlesClientImpl(serverUrl);
ResponseStatus status = client.login(username, password, lang, userAgent).getStatus(); ResponseStatus status = client.login(USERNAME, PASSWORD, LANG, USER_AGENT).getStatus();
if (!client.isLoggedIn()) { if (!client.isLoggedIn()) {
logger.error("Cannot login on OpenSubtitles.org."); logger.error("Cannot login on OpenSubtitles.org.");
logger.error("Message: [{}] - {}", status.getCode(), status.getMessage()); logger.error("Message: [{}] - {}", status.getCode(), status.getMessage());
} else logger.info("Logged in on OpenSubtitles.org."); } else logger.info("Logged in on OpenSubtitles.org.");
} catch (MalformedURLException | XmlRpcException e) { } catch (MalformedURLException | XmlRpcException e) {
logger.error(e.getMessage()); logger.error("Cannot login on OpenSubtitles.org. Reason: {}", e.getMessage());
System.exit(3); logger.warn("You won't be able to do anything related to searching & importing subtitles until this is fixed.");
} }
} }

View File

@ -15,6 +15,7 @@ public class SubtitleParser {
String[] blocks = s.split("\n\n"); String[] blocks = s.split("\n\n");
for (String block : blocks) { for (String block : blocks) {
List<String> splitBlock = Arrays.stream(block.split("\n")).collect(Collectors.toList()); List<String> splitBlock = Arrays.stream(block.split("\n")).collect(Collectors.toList());
subtitleText.addAll(splitBlock.subList(0, 1));
subtitleText.add(String.join("\n", splitBlock.subList(2, splitBlock.size()))); subtitleText.add(String.join("\n", splitBlock.subList(2, splitBlock.size())));
} }
subtitleText.remove(0); subtitleText.remove(0);