From d427c73090916e7df7390d1c63b6378cd72d5478 Mon Sep 17 00:00:00 2001 From: Louis Vallat Date: Fri, 30 Oct 2020 09:59:52 +0100 Subject: [PATCH] Added more checks due to us disconnecting from OpenSubtitles Signed-off-by: Louis Vallat --- .../xyz/vallat/louis/commands/Command.java | 7 +++++-- .../vallat/louis/commands/ListLangMovie.java | 10 ++++++---- .../vallat/louis/subtitles/OpenSubtitles.java | 19 ++++++++++++++----- .../exceptions/UnauthorizedException.java | 8 ++++++++ 4 files changed, 33 insertions(+), 11 deletions(-) create mode 100644 src/main/java/xyz/vallat/louis/subtitles/exceptions/UnauthorizedException.java diff --git a/src/main/java/xyz/vallat/louis/commands/Command.java b/src/main/java/xyz/vallat/louis/commands/Command.java index 01a82d4..97526d5 100644 --- a/src/main/java/xyz/vallat/louis/commands/Command.java +++ b/src/main/java/xyz/vallat/louis/commands/Command.java @@ -14,6 +14,7 @@ import reactor.core.publisher.Mono; import xyz.vallat.louis.omdb.OMDBClient; import xyz.vallat.louis.omdb.objects.Movie; import xyz.vallat.louis.subtitles.OpenSubtitles; +import xyz.vallat.louis.subtitles.exceptions.UnauthorizedException; import java.io.IOException; import java.util.List; @@ -24,7 +25,7 @@ import java.util.function.Predicate; import java.util.stream.Collectors; import java.util.stream.Stream; -import static xyz.vallat.louis.subtitles.OpenSubtitles.getSubtitleStream; +import static xyz.vallat.louis.subtitles.OpenSubtitles.*; public abstract class Command { protected static final ReactionEmoji WAITING = ReactionEmoji.unicode("⌛"); @@ -90,7 +91,7 @@ public abstract class Command { .then(); } - protected void createEmbedListLang(String arg, EmbedCreateSpec embed, boolean isId) { + protected void createEmbedListLang(String arg, EmbedCreateSpec embed, boolean isId) throws UnauthorizedException { try { Movie movie = OMDBClient.getMovie(arg, isId); if (movie != null) { @@ -102,6 +103,8 @@ public abstract class Command { } else if (subtitles.getStatus().equals(ResponseStatus.INVALID_IMDB_ID)) { embed.setColor(Color.ORANGE).setDescription("It looks like this IMDB Identifier either isn't valid " + "or is not in the database."); + } else if (subtitles.getStatus().equals(ResponseStatus.UNAUTHORIZED)) { + throw new UnauthorizedException("OpenSubtitles replied with: " + subtitles.getStatus()); } else { logger.error("Cannot get subtitle list. Code returned: {}", subtitles.getStatus()); embed.setDescription("I cannot get the subtitles right now, sorry! " + diff --git a/src/main/java/xyz/vallat/louis/commands/ListLangMovie.java b/src/main/java/xyz/vallat/louis/commands/ListLangMovie.java index db7d05f..cbea752 100644 --- a/src/main/java/xyz/vallat/louis/commands/ListLangMovie.java +++ b/src/main/java/xyz/vallat/louis/commands/ListLangMovie.java @@ -6,6 +6,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import reactor.core.publisher.Mono; import xyz.vallat.louis.subtitles.OpenSubtitles; +import xyz.vallat.louis.subtitles.exceptions.UnauthorizedException; import java.util.List; @@ -33,10 +34,11 @@ public class ListLangMovie extends Command { .flatMap(channel -> event.getMessage().addReaction(WAITING).then(event.getMessage().getChannel() .flatMap(messageChannel -> messageChannel.createEmbed(embed -> { embed.setTitle("Subtitle languages").setColor(Color.RED); - if (OpenSubtitles.isLoggedIn()) { - createEmbedListLang(arg, embed, isId); - } else { - logger.error("Not logged in on OpenSubtitles!"); + try { + if (OpenSubtitles.isLoggedIn()) createEmbedListLang(arg, embed, isId); + else throw new UnauthorizedException(""); + } catch (UnauthorizedException e) { + logger.error("Not logged in on OpenSubtitles! {}", e.getMessage()); logger.warn("It may be due to a website being down. If it's not" + "the case, please fix this issue quickly."); embed.setDescription("I cannot search for subtitle languages right now. Sorry."); diff --git a/src/main/java/xyz/vallat/louis/subtitles/OpenSubtitles.java b/src/main/java/xyz/vallat/louis/subtitles/OpenSubtitles.java index a5c9c17..e6aa897 100644 --- a/src/main/java/xyz/vallat/louis/subtitles/OpenSubtitles.java +++ b/src/main/java/xyz/vallat/louis/subtitles/OpenSubtitles.java @@ -2,15 +2,18 @@ package xyz.vallat.louis.subtitles; import com.github.wtekiela.opensub4j.api.OpenSubtitlesClient; import com.github.wtekiela.opensub4j.impl.OpenSubtitlesClientImpl; -import com.github.wtekiela.opensub4j.response.*; +import com.github.wtekiela.opensub4j.response.ListResponse; +import com.github.wtekiela.opensub4j.response.ResponseStatus; +import com.github.wtekiela.opensub4j.response.SubtitleFile; +import com.github.wtekiela.opensub4j.response.SubtitleInfo; +import org.apache.commons.lang3.StringUtils; import org.apache.xmlrpc.XmlRpcException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import xyz.vallat.louis.env.EnvironmentVariables; -import xyz.vallat.louis.omdb.OMDBClient; import xyz.vallat.louis.omdb.objects.Movie; +import xyz.vallat.louis.subtitles.exceptions.UnauthorizedException; -import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; import java.util.Comparator; @@ -79,13 +82,19 @@ public final class OpenSubtitles { public static Stream getSubtitleStreamFromMovie(Movie movie) { Stream subtitles = null; try { - if (movie != null) { + if (movie != null && isLoggedIn()) { ListResponse subtitleInfoList = OpenSubtitles.searchSubtitles("", String.valueOf(movie.getNumericImdbId())); if (subtitleInfoList.getStatus().equals(ResponseStatus.OK)) subtitles = getSubtitleStream(subtitleInfoList.getData()); + else if (subtitleInfoList.getStatus().equals(ResponseStatus.UNAUTHORIZED)) { + logout(); + login(); + if (isLoggedIn()) subtitles = getSubtitleStreamFromMovie(movie); + else throw new UnauthorizedException("Cannot login right now."); + } } - } catch (XmlRpcException e) { + } catch (XmlRpcException | UnauthorizedException e) { logger.error("An error occurred: {}", e.getMessage()); logger.warn("Please fix this issue as soon as possible, or else people won't be able to get subtitles."); } diff --git a/src/main/java/xyz/vallat/louis/subtitles/exceptions/UnauthorizedException.java b/src/main/java/xyz/vallat/louis/subtitles/exceptions/UnauthorizedException.java new file mode 100644 index 0000000..89694b4 --- /dev/null +++ b/src/main/java/xyz/vallat/louis/subtitles/exceptions/UnauthorizedException.java @@ -0,0 +1,8 @@ +package xyz.vallat.louis.subtitles.exceptions; + +public class UnauthorizedException extends Exception { + + public UnauthorizedException(String message) { + super(message); + } +}