Removed useless function in the LanguageManager and also added a way to reconnect when fetching data from OpenSubtitles and getting 401 due to inactivity

Signed-off-by: Louis Vallat <louis@louis-vallat.xyz>
This commit is contained in:
Louis Vallat 2020-10-31 10:22:00 +01:00
parent 5d96a15113
commit bcf1bb766d
3 changed files with 19 additions and 42 deletions

View File

@ -1,20 +1,16 @@
package xyz.vallat.louis.commands; package xyz.vallat.louis.commands;
import com.github.wtekiela.opensub4j.response.ListResponse;
import com.github.wtekiela.opensub4j.response.ResponseStatus;
import com.github.wtekiela.opensub4j.response.SubtitleInfo; import com.github.wtekiela.opensub4j.response.SubtitleInfo;
import discord4j.core.event.domain.message.MessageCreateEvent; import discord4j.core.event.domain.message.MessageCreateEvent;
import discord4j.core.object.reaction.ReactionEmoji; import discord4j.core.object.reaction.ReactionEmoji;
import discord4j.core.spec.EmbedCreateSpec; import discord4j.core.spec.EmbedCreateSpec;
import discord4j.rest.util.Color; import discord4j.rest.util.Color;
import org.apache.xmlrpc.XmlRpcException;
import org.slf4j.Logger; 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.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;
@ -25,8 +21,6 @@ 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.*;
public abstract class Command { public abstract class Command {
protected static final ReactionEmoji WAITING = ReactionEmoji.unicode(""); protected static final ReactionEmoji WAITING = ReactionEmoji.unicode("");
private static final Logger logger = LoggerFactory.getLogger(ListLangMovie.class.getCanonicalName()); private static final Logger logger = LoggerFactory.getLogger(ListLangMovie.class.getCanonicalName());
@ -91,30 +85,31 @@ public abstract class Command {
.then(); .then();
} }
protected void createEmbedListLang(String arg, EmbedCreateSpec embed, boolean isId) throws UnauthorizedException { protected void createEmbedListLang(String arg, EmbedCreateSpec embed, boolean isId) {
try { try {
Movie movie = OMDBClient.getMovie(arg, isId); Movie movie = OMDBClient.getMovie(arg, isId);
if (movie != null) { if (movie != null) {
ListResponse<SubtitleInfo> subtitles = Stream<SubtitleInfo> subtitles = OpenSubtitles.getSubtitleStreamFromMovie(movie);
OpenSubtitles.searchSubtitles("", if (subtitles != null) {
String.valueOf(movie.getNumericImdbId())); embed.setColor(Color.MEDIUM_SEA_GREEN);
if (subtitles.getStatus().equals(ResponseStatus.OK)) { String formattedSubtitles = subtitles.limit(20).map(SubtitleInfo::getLanguage)
handleOk(arg, embed, isId, movie, subtitles); .collect(Collectors.joining("\n"));
} else if (subtitles.getStatus().equals(ResponseStatus.INVALID_IMDB_ID)) { embed.setDescription("You requested a list of the languages available for a movie with the " +
embed.setColor(Color.ORANGE).setDescription("It looks like this IMDB Identifier either isn't valid " + (isId ? "IMDB Identifier" : "title") + " '" + arg + "'. ");
"or is not in the database."); embed.addField("Movie", movie.toString(), true);
} else if (subtitles.getStatus().equals(ResponseStatus.UNAUTHORIZED)) { embed.addField("IMDB Link", "https://www.imdb.com/title/" + movie.getImdbID(), true);
throw new UnauthorizedException("OpenSubtitles replied with: " + subtitles.getStatus()); if (movie.getPoster() != null) embed.setThumbnail(movie.getPoster());
embed.addField("Top 20 distinct languages (based on downloads)",
formattedSubtitles.isBlank() ? "None." : formattedSubtitles, false);
} else { } else {
logger.error("Cannot get subtitle list. Code returned: {}", subtitles.getStatus()); embed.setDescription("We couldn't find any subtitle right now. Sorry. Try again later or contact my" +
embed.setDescription("I cannot get the subtitles right now, sorry! " + " administrator, please!");
"Try again later or contact my administrator.");
} }
} else { } else {
embed.setDescription("I didn't find any correspondence, sorry! If you know this is an error, please " + embed.setDescription("I didn't find any correspondence, sorry! If you know this is an error, please " +
"contact my administrator!"); "contact my administrator!");
} }
} catch (IOException | XmlRpcException e) { } catch (IOException e) {
logger.error("A network error happened: {}", e.getMessage()); logger.error("A network error happened: {}", 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.");
@ -123,20 +118,6 @@ public abstract class Command {
} }
} }
private void handleOk(String arg, EmbedCreateSpec embed, boolean isId, Movie movie, ListResponse<SubtitleInfo> subtitles) {
embed.setColor(Color.MEDIUM_SEA_GREEN);
Stream<String> subtitlesStream = getSubtitleStream(subtitles.getData())
.map(SubtitleInfo::getLanguage).sorted();
String formattedSubtitles = subtitlesStream.limit(20).collect(Collectors.joining("\n"));
embed.setDescription("You requested a list of the languages available for a movie with the " +
(isId ? "IMDB Identifier" : "title") + " '" + arg + "'. ");
embed.addField("Movie", movie.toString(), true);
embed.addField("IMDB Link", "https://www.imdb.com/title/" + movie.getImdbID(), true);
if (movie.getPoster() != null) embed.setThumbnail(movie.getPoster());
embed.addField("Top 20 distinct languages (based on downloads)",
formattedSubtitles.isBlank() ? "None." : formattedSubtitles, false);
}
protected List<String> getArguments(MessageCreateEvent event) { protected List<String> getArguments(MessageCreateEvent event) {
String[] arguments = event.getMessage().getContent().split(" "); String[] arguments = event.getMessage().getContent().split(" ");
return List.of(arguments).subList(1, arguments.length); return List.of(arguments).subList(1, arguments.length);

View File

@ -8,6 +8,7 @@ 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 xyz.vallat.louis.subtitles.exceptions.UnauthorizedException;
import java.time.Instant;
import java.util.List; import java.util.List;
public class ListLangMovie extends Command { public class ListLangMovie extends Command {
@ -36,13 +37,14 @@ public class ListLangMovie extends Command {
embed.setTitle("Subtitle languages").setColor(Color.RED); embed.setTitle("Subtitle languages").setColor(Color.RED);
try { try {
if (OpenSubtitles.isLoggedIn()) createEmbedListLang(arg, embed, isId); if (OpenSubtitles.isLoggedIn()) createEmbedListLang(arg, embed, isId);
else throw new UnauthorizedException(""); else throw new UnauthorizedException("isLoggedIn returned false.");
} catch (UnauthorizedException e) { } catch (UnauthorizedException e) {
logger.error("Not logged in on OpenSubtitles! {}", e.getMessage()); 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.");
} }
embed.setTimestamp(Instant.now());
})))).then(event.getMessage().removeSelfReaction(WAITING)).then(); })))).then(event.getMessage().removeSelfReaction(WAITING)).then();
} }
} }

View File

@ -143,12 +143,6 @@ public final class LanguageManager {
} }
} }
public static Lang getLanguageFromId(int id) {
Lang language = null;
return language;
}
private static String getActualHash() throws NoSuchAlgorithmException, IOException { private static String getActualHash() throws NoSuchAlgorithmException, IOException {
logger.debug("Computing the actual language source file's hash."); logger.debug("Computing the actual language source file's hash.");
return getFileChecksum(MessageDigest.getInstance("MD5")); return getFileChecksum(MessageDigest.getInstance("MD5"));