Added more checks due to us disconnecting from OpenSubtitles

Signed-off-by: Louis Vallat <louis@louis-vallat.xyz>
This commit is contained in:
Louis Vallat 2020-10-30 09:59:52 +01:00
parent a53ac01ae0
commit d427c73090
4 changed files with 33 additions and 11 deletions

View File

@ -14,6 +14,7 @@ import reactor.core.publisher.Mono;
import xyz.vallat.louis.omdb.OMDBClient; import xyz.vallat.louis.omdb.OMDBClient;
import xyz.vallat.louis.omdb.objects.Movie; import xyz.vallat.louis.omdb.objects.Movie;
import xyz.vallat.louis.subtitles.OpenSubtitles; import xyz.vallat.louis.subtitles.OpenSubtitles;
import xyz.vallat.louis.subtitles.exceptions.UnauthorizedException;
import java.io.IOException; import java.io.IOException;
import java.util.List; import java.util.List;
@ -24,7 +25,7 @@ import java.util.function.Predicate;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.Stream; import java.util.stream.Stream;
import static xyz.vallat.louis.subtitles.OpenSubtitles.getSubtitleStream; import static xyz.vallat.louis.subtitles.OpenSubtitles.*;
public abstract class Command { public abstract class Command {
protected static final ReactionEmoji WAITING = ReactionEmoji.unicode(""); protected static final ReactionEmoji WAITING = ReactionEmoji.unicode("");
@ -90,7 +91,7 @@ public abstract class Command {
.then(); .then();
} }
protected void createEmbedListLang(String arg, EmbedCreateSpec embed, boolean isId) { protected void createEmbedListLang(String arg, EmbedCreateSpec embed, boolean isId) throws UnauthorizedException {
try { try {
Movie movie = OMDBClient.getMovie(arg, isId); Movie movie = OMDBClient.getMovie(arg, isId);
if (movie != null) { if (movie != null) {
@ -102,6 +103,8 @@ public abstract class Command {
} else if (subtitles.getStatus().equals(ResponseStatus.INVALID_IMDB_ID)) { } else if (subtitles.getStatus().equals(ResponseStatus.INVALID_IMDB_ID)) {
embed.setColor(Color.ORANGE).setDescription("It looks like this IMDB Identifier either isn't valid " + embed.setColor(Color.ORANGE).setDescription("It looks like this IMDB Identifier either isn't valid " +
"or is not in the database."); "or is not in the database.");
} else if (subtitles.getStatus().equals(ResponseStatus.UNAUTHORIZED)) {
throw new UnauthorizedException("OpenSubtitles replied with: " + subtitles.getStatus());
} else { } else {
logger.error("Cannot get subtitle list. Code returned: {}", subtitles.getStatus()); logger.error("Cannot get subtitle list. Code returned: {}", subtitles.getStatus());
embed.setDescription("I cannot get the subtitles right now, sorry! " + embed.setDescription("I cannot get the subtitles right now, sorry! " +

View File

@ -6,6 +6,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
import xyz.vallat.louis.subtitles.OpenSubtitles; import xyz.vallat.louis.subtitles.OpenSubtitles;
import xyz.vallat.louis.subtitles.exceptions.UnauthorizedException;
import java.util.List; import java.util.List;
@ -33,10 +34,11 @@ public class ListLangMovie extends Command {
.flatMap(channel -> event.getMessage().addReaction(WAITING).then(event.getMessage().getChannel() .flatMap(channel -> event.getMessage().addReaction(WAITING).then(event.getMessage().getChannel()
.flatMap(messageChannel -> messageChannel.createEmbed(embed -> { .flatMap(messageChannel -> messageChannel.createEmbed(embed -> {
embed.setTitle("Subtitle languages").setColor(Color.RED); embed.setTitle("Subtitle languages").setColor(Color.RED);
if (OpenSubtitles.isLoggedIn()) { try {
createEmbedListLang(arg, embed, isId); if (OpenSubtitles.isLoggedIn()) createEmbedListLang(arg, embed, isId);
} else { else throw new UnauthorizedException("");
logger.error("Not logged in on OpenSubtitles!"); } 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" + logger.warn("It may be due to a website being down. If it's not" +
"the case, please fix this issue quickly."); "the case, please fix this issue quickly.");
embed.setDescription("I cannot search for subtitle languages right now. Sorry."); embed.setDescription("I cannot search for subtitle languages right now. Sorry.");

View File

@ -2,15 +2,18 @@ package xyz.vallat.louis.subtitles;
import com.github.wtekiela.opensub4j.api.OpenSubtitlesClient; import com.github.wtekiela.opensub4j.api.OpenSubtitlesClient;
import com.github.wtekiela.opensub4j.impl.OpenSubtitlesClientImpl; 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.apache.xmlrpc.XmlRpcException;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import xyz.vallat.louis.env.EnvironmentVariables; import xyz.vallat.louis.env.EnvironmentVariables;
import xyz.vallat.louis.omdb.OMDBClient;
import xyz.vallat.louis.omdb.objects.Movie; 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.MalformedURLException;
import java.net.URL; import java.net.URL;
import java.util.Comparator; import java.util.Comparator;
@ -79,13 +82,19 @@ public final class OpenSubtitles {
public static Stream<SubtitleInfo> getSubtitleStreamFromMovie(Movie movie) { public static Stream<SubtitleInfo> getSubtitleStreamFromMovie(Movie movie) {
Stream<SubtitleInfo> subtitles = null; Stream<SubtitleInfo> subtitles = null;
try { try {
if (movie != null) { if (movie != null && isLoggedIn()) {
ListResponse<SubtitleInfo> subtitleInfoList = ListResponse<SubtitleInfo> subtitleInfoList =
OpenSubtitles.searchSubtitles("", String.valueOf(movie.getNumericImdbId())); OpenSubtitles.searchSubtitles("", String.valueOf(movie.getNumericImdbId()));
if (subtitleInfoList.getStatus().equals(ResponseStatus.OK)) if (subtitleInfoList.getStatus().equals(ResponseStatus.OK))
subtitles = getSubtitleStream(subtitleInfoList.getData()); 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.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."); logger.warn("Please fix this issue as soon as possible, or else people won't be able to get subtitles.");
} }

View File

@ -0,0 +1,8 @@
package xyz.vallat.louis.subtitles.exceptions;
public class UnauthorizedException extends Exception {
public UnauthorizedException(String message) {
super(message);
}
}