diff --git a/src/main/java/xyz/vallat/louis/commands/Command.java b/src/main/java/xyz/vallat/louis/commands/Command.java index b57d2e4..cb377ce 100644 --- a/src/main/java/xyz/vallat/louis/commands/Command.java +++ b/src/main/java/xyz/vallat/louis/commands/Command.java @@ -1,20 +1,16 @@ 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 discord4j.core.event.domain.message.MessageCreateEvent; import discord4j.core.object.reaction.ReactionEmoji; import discord4j.core.spec.EmbedCreateSpec; import discord4j.rest.util.Color; -import org.apache.xmlrpc.XmlRpcException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; 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; @@ -25,8 +21,6 @@ import java.util.function.Predicate; import java.util.stream.Collectors; import java.util.stream.Stream; -import static xyz.vallat.louis.subtitles.OpenSubtitles.*; - public abstract class Command { protected static final ReactionEmoji WAITING = ReactionEmoji.unicode("⌛"); private static final Logger logger = LoggerFactory.getLogger(ListLangMovie.class.getCanonicalName()); @@ -91,30 +85,31 @@ public abstract class Command { .then(); } - protected void createEmbedListLang(String arg, EmbedCreateSpec embed, boolean isId) throws UnauthorizedException { + protected void createEmbedListLang(String arg, EmbedCreateSpec embed, boolean isId) { try { Movie movie = OMDBClient.getMovie(arg, isId); if (movie != null) { - ListResponse subtitles = - OpenSubtitles.searchSubtitles("", - String.valueOf(movie.getNumericImdbId())); - if (subtitles.getStatus().equals(ResponseStatus.OK)) { - handleOk(arg, embed, isId, movie, subtitles); - } 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()); + Stream subtitles = OpenSubtitles.getSubtitleStreamFromMovie(movie); + if (subtitles != null) { + embed.setColor(Color.MEDIUM_SEA_GREEN); + String formattedSubtitles = subtitles.limit(20).map(SubtitleInfo::getLanguage) + .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); } else { - logger.error("Cannot get subtitle list. Code returned: {}", subtitles.getStatus()); - embed.setDescription("I cannot get the subtitles right now, sorry! " + - "Try again later or contact my administrator."); + embed.setDescription("We couldn't find any subtitle right now. Sorry. Try again later or contact my" + + " administrator, please!"); } } else { embed.setDescription("I didn't find any correspondence, sorry! If you know this is an error, please " + "contact my administrator!"); } - } catch (IOException | XmlRpcException e) { + } catch (IOException e) { logger.error("A network error happened: {}", e.getMessage()); logger.warn("It may be due to a website being down. If it's not" + "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 subtitles) { - embed.setColor(Color.MEDIUM_SEA_GREEN); - Stream 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 getArguments(MessageCreateEvent event) { String[] arguments = event.getMessage().getContent().split(" "); return List.of(arguments).subList(1, arguments.length); diff --git a/src/main/java/xyz/vallat/louis/commands/ListLangMovie.java b/src/main/java/xyz/vallat/louis/commands/ListLangMovie.java index 4defd84..1d96077 100644 --- a/src/main/java/xyz/vallat/louis/commands/ListLangMovie.java +++ b/src/main/java/xyz/vallat/louis/commands/ListLangMovie.java @@ -8,6 +8,7 @@ import reactor.core.publisher.Mono; import xyz.vallat.louis.subtitles.OpenSubtitles; import xyz.vallat.louis.subtitles.exceptions.UnauthorizedException; +import java.time.Instant; import java.util.List; public class ListLangMovie extends Command { @@ -36,13 +37,14 @@ public class ListLangMovie extends Command { embed.setTitle("Subtitle languages").setColor(Color.RED); try { if (OpenSubtitles.isLoggedIn()) createEmbedListLang(arg, embed, isId); - else throw new UnauthorizedException(""); + else throw new UnauthorizedException("isLoggedIn returned false."); } 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."); } + embed.setTimestamp(Instant.now()); })))).then(event.getMessage().removeSelfReaction(WAITING)).then(); } } diff --git a/src/main/java/xyz/vallat/louis/database/LanguageManager.java b/src/main/java/xyz/vallat/louis/database/LanguageManager.java index d0c0387..ffcf5c5 100644 --- a/src/main/java/xyz/vallat/louis/database/LanguageManager.java +++ b/src/main/java/xyz/vallat/louis/database/LanguageManager.java @@ -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 { logger.debug("Computing the actual language source file's hash."); return getFileChecksum(MessageDigest.getInstance("MD5"));