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
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 NAME = "Movies Quote Bot";
@ -44,13 +41,7 @@ public class MovieQuoteBot {
public static void main(String[] args) {
DBManager.testConnection();
DBManager.initDatabase();
// TODO: FIX CRASH ON LOGIN IF OS IS IN MAINTENANCE OR BROKEN
OpenSubtitles.login(
System.getenv(OS_USERNAME_ENVIRONMENT),
System.getenv(OS_PASSWORD_ENVIRONMENT),
"en",
System.getenv(OS_UA_ENVIRONMENT)
);
OpenSubtitles.login();
discordLogin(System.getenv(DISCORD_TOKEN_ENVIRONMENT));
registerDiscordCommands();

View File

@ -26,6 +26,7 @@ public final class PropertyManager {
stmt.setString(1, value);
stmt.setString(2, key);
stmt.execute();
logger.debug("Property '{}' saved.", key);
}
} catch (SQLException e) {
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 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 String username;
private static String password;
private static String lang;
private static String userAgent;
private static final String USERNAME = System.getenv(OS_USERNAME_ENVIRONMENT);
private static final String PASSWORD = System.getenv(OS_PASSWORD_ENVIRONMENT);
private static final String LANG = "en";
private static final String USER_AGENT = System.getenv(OS_UA_ENVIRONMENT);
private static OpenSubtitlesClient client;
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() {
try {
URL serverUrl = new URL("https", "api.opensubtitles.org", 443, "/xml-rpc");
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()) {
logger.error("Cannot login on OpenSubtitles.org.");
logger.error("Message: [{}] - {}", status.getCode(), status.getMessage());
} else logger.info("Logged in on OpenSubtitles.org.");
} catch (MalformedURLException | XmlRpcException e) {
logger.error(e.getMessage());
System.exit(3);
logger.error("Cannot login on OpenSubtitles.org. Reason: {}", e.getMessage());
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");
for (String block : blocks) {
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.remove(0);