Basic POC for the subtitles downloading feature

Signed-off-by: Louis Vallat <louis@louis-vallat.xyz>
This commit is contained in:
Louis Vallat 2020-10-27 09:52:14 +01:00
parent 2746c81466
commit 2fa8b18a0a
4 changed files with 50 additions and 5 deletions

View File

@ -35,6 +35,7 @@ public class MovieQuoteBot {
commands.put("ping", new Ping(PREFIX + "ping")); commands.put("ping", new Ping(PREFIX + "ping"));
commands.put("version", new Version(PREFIX + "version")); commands.put("version", new Version(PREFIX + "version"));
commands.put("listLang", new ListLang(PREFIX + "listLang")); commands.put("listLang", new ListLang(PREFIX + "listLang"));
commands.put("download", new Download(PREFIX + "download"));
} }
public static void main(String[] args) { public static void main(String[] args) {

View File

@ -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<Void> 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<SubtitleFile> 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();
}
}
}

View File

@ -57,7 +57,7 @@ public class ListLang extends Command {
if (!subtitles.getData().isEmpty()) { if (!subtitles.getData().isEmpty()) {
StringBuilder s = new StringBuilder(); StringBuilder s = new StringBuilder();
for (SubtitleInfo info : subtitles.getData().subList(0, Math.min(10, subtitles.getData().size()))) 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); embed.addField("Languages found", s.toString(), false);
} }
}); });

View File

@ -2,10 +2,7 @@ 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.ListResponse; import com.github.wtekiela.opensub4j.response.*;
import com.github.wtekiela.opensub4j.response.MovieInfo;
import com.github.wtekiela.opensub4j.response.ResponseStatus;
import com.github.wtekiela.opensub4j.response.SubtitleInfo;
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;
@ -66,6 +63,10 @@ public final class OpenSubtitles {
return client.searchSubtitles(lang, imdbId); return client.searchSubtitles(lang, imdbId);
} }
public static ListResponse<SubtitleFile> downloadSubtitle(int id) throws XmlRpcException {
return client.downloadSubtitles(id);
}
public static boolean isLoggedIn() { public static boolean isLoggedIn() {
return client != null && client.isLoggedIn(); return client != null && client.isLoggedIn();
} }