diff --git a/src/main/java/xyz/vallat/louis/commands/Download.java b/src/main/java/xyz/vallat/louis/commands/Download.java index 083e02c..959647a 100644 --- a/src/main/java/xyz/vallat/louis/commands/Download.java +++ b/src/main/java/xyz/vallat/louis/commands/Download.java @@ -5,6 +5,7 @@ import com.github.wtekiela.opensub4j.response.ResponseStatus; import com.github.wtekiela.opensub4j.response.SubtitleFile; import com.github.wtekiela.opensub4j.response.SubtitleInfo; import discord4j.core.event.domain.message.MessageCreateEvent; +import discord4j.core.spec.EmbedCreateSpec; import discord4j.rest.util.Color; import org.apache.xmlrpc.XmlRpcException; import org.slf4j.Logger; @@ -52,31 +53,8 @@ public class Download extends Command { else if (SubtitleManager.getSubtitlesId(movie, languageId) > 0) embed.setDescription("This movie already has already this language imported.") .setColor(Color.ORANGE); - else { - Stream subtitleInfoStream = OpenSubtitles.getSubtitleStreamFromMovie(movie); - Optional subtitleInfo = subtitleInfoStream == null ? Optional.empty() : - subtitleInfoStream.filter(info -> - LanguageManager.getIdLanguage(info.getLanguage()) == languageId) - .findFirst(); - if (subtitleInfo.isEmpty()) - embed.setDescription("Nothing was found with these parameters."); - else { - ListResponse subs = OpenSubtitles.downloadSubtitle( - subtitleInfo.get().getSubtitleFileId()); - if (subs.getStatus().equals(ResponseStatus.OK)) { - List blocks = new SubtitleParser().parseSRT( - subs.getData().get(0).getContentAsString("cp1252")); - SubtitleLineManager.importSubtitleLines(blocks, languageId, movie, - event.getGuildId().isPresent() ? event.getGuildId().get() : null, - event.getMember().isPresent() ? event.getMessage().getId() : null); - embed.setColor(Color.MEDIUM_SEA_GREEN).setDescription("Everything went well. " + - "Congratulations and thank you for your contribution!"); - } else { - logger.error("Could not download subtitle '{}': {}", args.get(0), subs.getStatus()); - embed.setDescription("An error occurred. Please contact my administrator."); - } - } - } + else + computeImportation(event, args, languageId, embed, movie); } catch (XmlRpcException | IOException e) { logger.error("An error occurred while fetching the data from opensubtitles: {}", e.getMessage()); embed.setDescription("An error occurred, please contact my administrator."); @@ -85,4 +63,32 @@ public class Download extends Command { } ))).then(event.getMessage().removeSelfReaction(WAITING)).then(); } + + private void computeImportation(MessageCreateEvent event, List args, + int languageId, EmbedCreateSpec embed, Movie movie) throws XmlRpcException { + Stream subtitleInfoStream = OpenSubtitles.getSubtitleStreamFromMovie(movie); + Optional subtitleInfo = subtitleInfoStream == null ? Optional.empty() : + subtitleInfoStream.filter(info -> + LanguageManager.getIdLanguage(info.getLanguage()) == languageId) + .findFirst(); + if (subtitleInfo.isEmpty()) + embed.setDescription("Nothing was found with these parameters."); + else { + ListResponse subs = OpenSubtitles.downloadSubtitle( + subtitleInfo.get().getSubtitleFileId()); + if (subs.getStatus().equals(ResponseStatus.OK)) { + List blocks = new SubtitleParser().parseSRT( + subs.getData().get(0).getContentAsString("cp1252")); + SubtitleLineManager.importSubtitleLines(blocks, languageId, movie, + event.getGuildId().isPresent() ? event.getGuildId().get() : null, + event.getMember().isPresent() ? event.getMessage().getId() : null); + embed.setColor(Color.MEDIUM_SEA_GREEN).setDescription("Everything went well. " + + "Congratulations and thank you for your contribution!"); + embed.addField("You imported", blocks.size() + " lines into my database", false); + } else { + logger.error("Could not download subtitle '{}': {}", args.get(0), subs.getStatus()); + embed.setDescription("An error occurred. Please contact my administrator."); + } + } + } }