From 83279129bfc015ee6e283d218fd3a9703f8842b4 Mon Sep 17 00:00:00 2001 From: Louis Vallat Date: Mon, 26 Oct 2020 11:32:57 +0100 Subject: [PATCH] Added OpenSubtitles capability Signed-off-by: Louis Vallat --- build.gradle | 1 + .../java/xyz/vallat/louis/MovieQuoteBot.java | 101 ++++++++++++++---- 2 files changed, 81 insertions(+), 21 deletions(-) diff --git a/build.gradle b/build.gradle index 176d5c7..9a200c9 100644 --- a/build.gradle +++ b/build.gradle @@ -32,5 +32,6 @@ repositories { dependencies { implementation 'com.discord4j:discord4j-core:3.1.1' + implementation 'com.github.wtekiela:opensub4j:0.3.0' implementation 'ch.qos.logback:logback-classic:1.2.3' } \ No newline at end of file diff --git a/src/main/java/xyz/vallat/louis/MovieQuoteBot.java b/src/main/java/xyz/vallat/louis/MovieQuoteBot.java index bb20542..ec6f791 100644 --- a/src/main/java/xyz/vallat/louis/MovieQuoteBot.java +++ b/src/main/java/xyz/vallat/louis/MovieQuoteBot.java @@ -1,5 +1,8 @@ package xyz.vallat.louis; +import com.github.wtekiela.opensub4j.api.OpenSubtitlesClient; +import com.github.wtekiela.opensub4j.impl.OpenSubtitlesClientImpl; +import com.github.wtekiela.opensub4j.response.ResponseStatus; import discord4j.core.DiscordClientBuilder; import discord4j.core.GatewayDiscordClient; import discord4j.core.event.domain.lifecycle.ReadyEvent; @@ -7,21 +10,27 @@ import discord4j.core.event.domain.message.MessageCreateEvent; import discord4j.core.object.entity.User; import discord4j.core.object.presence.Activity; import discord4j.core.object.presence.Presence; -import discord4j.discordjson.json.gateway.StatusUpdate; +import org.apache.xmlrpc.XmlRpcException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; import xyz.vallat.louis.commands.Command; import xyz.vallat.louis.commands.Ping; +import xyz.vallat.louis.commands.Search; import xyz.vallat.louis.commands.Version; +import java.net.MalformedURLException; +import java.net.URL; import java.util.HashMap; import java.util.Map; public class MovieQuoteBot { public static final String DISCORD_TOKEN_ENVIRONMENT = "DISCORD_TOKEN"; + public static final String OS_USERNAME_ENVIRONMENT = "OPEN_SUBTITLES_USERNAME"; + public static final String OS_PASSWORD_ENVIRONMENT = "OPEN_SUBTITLES_PASSWORD"; + public static final String OS_UA_ENVIRONMENT = "OPEN_SUBTITLES_USER_AGENT"; public static final String PREFIX = "!"; public static final String NAME = "Movies Quote Bot"; public static final String DESCRIPTION = "I may know some quotes from some movies."; @@ -29,48 +38,98 @@ public class MovieQuoteBot { private static final Logger logger = LoggerFactory.getLogger(MovieQuoteBot.class.getCanonicalName()); private static final Map commands = new HashMap<>(); - private static GatewayDiscordClient client; + private static GatewayDiscordClient discordClient; + private static OpenSubtitlesClient openSubtitlesClient; + private static String discordToken; + private static String osUsername; + private static String osPassword; + private static String osUa; static { commands.put("ping", new Ping()); commands.put("version", new Version()); + commands.put("search", new Search()); } public static void main(String[] args) { - if (System.getenv(DISCORD_TOKEN_ENVIRONMENT) == null && args.length == 0) { - logger.error("Missing token. Exiting."); + if ((System.getenv(DISCORD_TOKEN_ENVIRONMENT) == null || + System.getenv(OS_USERNAME_ENVIRONMENT) == null || + System.getenv(OS_PASSWORD_ENVIRONMENT) == null || + System.getenv(OS_UA_ENVIRONMENT) == null + ) && args.length < 4) { + logger.error("Missing tokens. Exiting."); System.exit(1); } - client = DiscordClientBuilder.create( - System.getenv(DISCORD_TOKEN_ENVIRONMENT) == null ? args[0] : System.getenv(DISCORD_TOKEN_ENVIRONMENT) - ).build().login().block(); + setVariables(args); + osLogin(); + discordLogin(); + registerDiscordCommands(); - assert client != null; - client.updatePresence(Presence.online(Activity.watching("some more films"))).subscribe(); - client.getEventDispatcher().on(ReadyEvent.class) - .subscribe(event -> { - User self = event.getSelf(); - logger.info("Logged in as {}#{}.", self.getUsername(), self.getDiscriminator()); - }); + Runtime.getRuntime().addShutdownHook(new Thread(() -> { + logger.info("Received shut down signal. Bye!"); + discordClient.logout().block(); + })); - client.getEventDispatcher().on(MessageCreateEvent.class) + discordClient.onDisconnect().block(); + } + + private static void registerDiscordCommands() { + discordClient.getEventDispatcher().on(MessageCreateEvent.class) .flatMap(event -> Mono.just(event.getMessage().getContent()) .flatMap(content -> Flux.fromIterable(commands.entrySet()) .filter(entry -> content.split(" ")[0].equals(PREFIX + entry.getKey())) .flatMap(entry -> entry.getValue().execute(event)) .next())) .subscribe(); + } - Runtime.getRuntime().addShutdownHook(new Thread(() -> { - logger.info("Received shut down signal. Bye!"); - client.logout(); - })); + private static void discordLogin() { + discordClient = DiscordClientBuilder.create(discordToken).build().login().block(); + assert discordClient != null; + discordClient.updatePresence(Presence.online(Activity.watching("some more films"))).subscribe(); + discordClient.getEventDispatcher().on(ReadyEvent.class) + .subscribe(event -> { + User self = event.getSelf(); + logger.info("Logged in on Discord as {}#{}.", self.getUsername(), self.getDiscriminator()); + }); + } - client.onDisconnect().block(); + private static void setVariables(String[] args) { + discordToken = System.getenv(DISCORD_TOKEN_ENVIRONMENT) == null ? + args[0] : + System.getenv(DISCORD_TOKEN_ENVIRONMENT); + osUsername = System.getenv(OS_USERNAME_ENVIRONMENT) == null ? + args[1] : + System.getenv(OS_USERNAME_ENVIRONMENT); + osPassword = System.getenv(OS_PASSWORD_ENVIRONMENT) == null ? + args[2] : + System.getenv(OS_PASSWORD_ENVIRONMENT); + osUa = System.getenv(OS_UA_ENVIRONMENT) == null ? + args[3] : + System.getenv(OS_UA_ENVIRONMENT); + } + + public static void osLogin() { + try { + URL serverUrl = new URL("https", "api.opensubtitles.org", 443, "/xml-rpc"); + openSubtitlesClient = new OpenSubtitlesClientImpl(serverUrl); + ResponseStatus status = openSubtitlesClient.login(osUsername, osPassword, "en", osUa).getStatus(); + if (!openSubtitlesClient.isLoggedIn()) { + logger.error("Cannot login on OpenSubtitles.org."); + logger.error("Message: [{}] - {}", status.getCode(), status.getMessage()); + } else logger.info("Logged in on OpenSubtitles.org."); + } catch (MalformedURLException | XmlRpcException e) { + logger.error(e.getMessage()); + System.exit(3); + } + } + + public static boolean isOsLoggedIn() { + return openSubtitlesClient != null && openSubtitlesClient.isLoggedIn(); } public static Mono getGuilds() { - return client.getGuilds().count(); + return discordClient.getGuilds().count(); } }