From 2fa8b18a0ab7608f1cc768a7f91a5847ca913d9f Mon Sep 17 00:00:00 2001 From: Louis Vallat Date: Tue, 27 Oct 2020 09:52:14 +0100 Subject: [PATCH] Basic POC for the subtitles downloading feature Signed-off-by: Louis Vallat --- .../java/xyz/vallat/louis/MovieQuoteBot.java | 1 + .../xyz/vallat/louis/commands/Download.java | 43 +++++++++++++++++++ .../xyz/vallat/louis/commands/ListLang.java | 2 +- .../vallat/louis/subtitles/OpenSubtitles.java | 9 ++-- 4 files changed, 50 insertions(+), 5 deletions(-) create mode 100644 src/main/java/xyz/vallat/louis/commands/Download.java diff --git a/src/main/java/xyz/vallat/louis/MovieQuoteBot.java b/src/main/java/xyz/vallat/louis/MovieQuoteBot.java index 48d8a86..c3dacdd 100644 --- a/src/main/java/xyz/vallat/louis/MovieQuoteBot.java +++ b/src/main/java/xyz/vallat/louis/MovieQuoteBot.java @@ -35,6 +35,7 @@ public class MovieQuoteBot { commands.put("ping", new Ping(PREFIX + "ping")); commands.put("version", new Version(PREFIX + "version")); commands.put("listLang", new ListLang(PREFIX + "listLang")); + commands.put("download", new Download(PREFIX + "download")); } public static void main(String[] args) { diff --git a/src/main/java/xyz/vallat/louis/commands/Download.java b/src/main/java/xyz/vallat/louis/commands/Download.java new file mode 100644 index 0000000..84f29c3 --- /dev/null +++ b/src/main/java/xyz/vallat/louis/commands/Download.java @@ -0,0 +1,43 @@ +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.SubtitleFile; +import discord4j.core.event.domain.message.MessageCreateEvent; +import org.apache.commons.lang3.StringUtils; +import org.apache.xmlrpc.XmlRpcException; +import reactor.core.publisher.Mono; +import xyz.vallat.louis.subtitles.OpenSubtitles; + +public class Download extends Command { + + public Download(String name) { + super(name, "Download a subtitle.", " subtitleId", 1, 1); + } + + @Override + public Mono execute(MessageCreateEvent event) { + String[] args = event.getMessage().getContent().split(" "); + if (args.length < minArgs || (args.length >= 1 && !StringUtils.isNumeric(args[1]))) + return event.getMessage().getChannel() + .flatMap(messageChannel -> messageChannel.createMessage("Error: malformed arguments. " + + "Usage:" + usage)).then(); + try { + ListResponse subs = OpenSubtitles.downloadSubtitle(Integer.parseInt(args[1])); + if (subs.getStatus().getCode() == ResponseStatus.OK.getCode()) { + return event.getMessage().getChannel() + .flatMap(messageChannel -> + { + System.out.println(subs.getData().get(0).getContentAsString("cp1252")); + return messageChannel.createMessage("Good"); + } + ) + .then(); + } else + return event.getMessage().getChannel().flatMap(channel -> channel.createMessage("An error occurred.")).then(); + } catch (XmlRpcException ignored) { + return event.getMessage().getChannel().flatMap(messageChannel -> messageChannel.createMessage("Error.")).then(); + } + + } +} diff --git a/src/main/java/xyz/vallat/louis/commands/ListLang.java b/src/main/java/xyz/vallat/louis/commands/ListLang.java index dda9757..dd13d9b 100644 --- a/src/main/java/xyz/vallat/louis/commands/ListLang.java +++ b/src/main/java/xyz/vallat/louis/commands/ListLang.java @@ -57,7 +57,7 @@ public class ListLang extends Command { if (!subtitles.getData().isEmpty()) { StringBuilder s = new StringBuilder(); for (SubtitleInfo info : subtitles.getData().subList(0, Math.min(10, subtitles.getData().size()))) - s.append(info.getId()).append(" - ").append(info.getLanguage()).append("\n"); + s.append(info.getSubtitleFileId()).append(" - ").append(info.getLanguage()).append("\n"); embed.addField("Languages found", s.toString(), false); } }); diff --git a/src/main/java/xyz/vallat/louis/subtitles/OpenSubtitles.java b/src/main/java/xyz/vallat/louis/subtitles/OpenSubtitles.java index a2d54de..1934bb1 100644 --- a/src/main/java/xyz/vallat/louis/subtitles/OpenSubtitles.java +++ b/src/main/java/xyz/vallat/louis/subtitles/OpenSubtitles.java @@ -2,10 +2,7 @@ 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.ListResponse; -import com.github.wtekiela.opensub4j.response.MovieInfo; -import com.github.wtekiela.opensub4j.response.ResponseStatus; -import com.github.wtekiela.opensub4j.response.SubtitleInfo; +import com.github.wtekiela.opensub4j.response.*; import org.apache.xmlrpc.XmlRpcException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -66,6 +63,10 @@ public final class OpenSubtitles { return client.searchSubtitles(lang, imdbId); } + public static ListResponse downloadSubtitle(int id) throws XmlRpcException { + return client.downloadSubtitles(id); + } + public static boolean isLoggedIn() { return client != null && client.isLoggedIn(); }