From 585033d78b8115b4a4f98acb59481c9addf66f80 Mon Sep 17 00:00:00 2001 From: Louis Vallat Date: Thu, 29 Oct 2020 14:44:46 +0100 Subject: [PATCH] Getting a quote now should be working Signed-off-by: Louis Vallat --- .../xyz/vallat/louis/commands/Command.java | 5 ++- .../xyz/vallat/louis/commands/Download.java | 12 +++--- .../vallat/louis/commands/ListLangMovie.java | 10 ++--- .../java/xyz/vallat/louis/commands/Quote.java | 41 +++++++++++++++++++ .../louis/database/SubtitleLineManager.java | 30 +++++++++++++- .../vallat/louis/discord/DiscordManager.java | 1 + .../xyz/vallat/louis/omdb/OMDBClient.java | 11 ----- .../vallat/louis/subtitles/dao/FilmQuote.java | 22 ++++++++++ .../{parser => dao}/SubtitleBlock.java | 2 +- .../subtitles/parser/SubtitleParser.java | 2 + 10 files changed, 110 insertions(+), 26 deletions(-) create mode 100644 src/main/java/xyz/vallat/louis/commands/Quote.java create mode 100644 src/main/java/xyz/vallat/louis/subtitles/dao/FilmQuote.java rename src/main/java/xyz/vallat/louis/subtitles/{parser => dao}/SubtitleBlock.java (91%) diff --git a/src/main/java/xyz/vallat/louis/commands/Command.java b/src/main/java/xyz/vallat/louis/commands/Command.java index 23a8cc6..87fbdab 100644 --- a/src/main/java/xyz/vallat/louis/commands/Command.java +++ b/src/main/java/xyz/vallat/louis/commands/Command.java @@ -118,7 +118,7 @@ public abstract class Command { } private void handleOk(String arg, EmbedCreateSpec embed, boolean isId, Movie movie, ListResponse subtitles) { - embed.setColor(Color.BISMARK); + embed.setColor(Color.MEDIUM_SEA_GREEN); Stream subtitlesStream = getSubtitleStream(subtitles.getData()) .map(subtitleInfo -> subtitleInfo.getSubtitleFileId() + " - " + subtitleInfo.getLanguage()); String formattedSubtitles = subtitlesStream.limit(20).collect(Collectors.joining("\n")); @@ -132,6 +132,7 @@ public abstract class Command { } protected List getArguments(MessageCreateEvent event) { - return List.of(event.getMessage().getContent().substring(Math.min(name.length() + 1, name.length())).split(" ")); + String[] arguments = event.getMessage().getContent().split(" "); + return List.of(arguments).subList(1, arguments.length); } } diff --git a/src/main/java/xyz/vallat/louis/commands/Download.java b/src/main/java/xyz/vallat/louis/commands/Download.java index 959647a..ab7e1ca 100644 --- a/src/main/java/xyz/vallat/louis/commands/Download.java +++ b/src/main/java/xyz/vallat/louis/commands/Download.java @@ -17,7 +17,7 @@ import xyz.vallat.louis.database.SubtitleManager; import xyz.vallat.louis.omdb.OMDBClient; import xyz.vallat.louis.omdb.objects.Movie; import xyz.vallat.louis.subtitles.OpenSubtitles; -import xyz.vallat.louis.subtitles.parser.SubtitleBlock; +import xyz.vallat.louis.subtitles.dao.SubtitleBlock; import xyz.vallat.louis.subtitles.parser.SubtitleParser; import java.io.IOException; @@ -39,15 +39,15 @@ public class Download extends Command { List args = getArguments(event); if (args.size() < minArgs) return notEnoughArguments(event); String value = String.join(" ", args.subList(3, args.size())); - if (!args.get(2).equalsIgnoreCase("imdb") && !args.get(2).equalsIgnoreCase("title")) - return badArgument(event, args.get(2)); - int languageId = LanguageManager.getIdLanguage(args.get(1)); - if (languageId < 0) return unknownLanguage(event, args.get(1)); + if (!args.get(1).equalsIgnoreCase("imdb") && !args.get(1).equalsIgnoreCase("title")) + return badArgument(event, args.get(1)); + int languageId = LanguageManager.getIdLanguage(args.get(0)); + if (languageId < 0) return unknownLanguage(event, args.get(0)); return event.getMessage().getChannel().flatMap(channel -> event.getMessage().addReaction(WAITING).then( channel.createEmbed(embed -> { embed.setTitle("Importation").setColor(Color.RED); try { - Movie movie = OMDBClient.getMovie(value, args.get(2).equalsIgnoreCase("imdb")); + Movie movie = OMDBClient.getMovie(value, args.get(1).equalsIgnoreCase("imdb")); if (movie == null || movie.getId() <= 0) embed.setDescription("We couldn't find any movie with these information. Sorry!"); else if (SubtitleManager.getSubtitlesId(movie, languageId) > 0) diff --git a/src/main/java/xyz/vallat/louis/commands/ListLangMovie.java b/src/main/java/xyz/vallat/louis/commands/ListLangMovie.java index 579270a..7f66903 100644 --- a/src/main/java/xyz/vallat/louis/commands/ListLangMovie.java +++ b/src/main/java/xyz/vallat/louis/commands/ListLangMovie.java @@ -22,11 +22,11 @@ public class ListLangMovie extends Command { public Mono execute(MessageCreateEvent event) { List arguments = getArguments(event); if (arguments.size() < minArgs) return notEnoughArguments(event); - else if (!arguments.get(1).equalsIgnoreCase("imdb") && - !arguments.get(1).equalsIgnoreCase("title")) - return badArgument(event, arguments.get(1)); - boolean isId = arguments.get(1).equalsIgnoreCase("imdb"); - String arg = String.join(" ", arguments.subList(2, arguments.size())); + else if (!arguments.get(0).equalsIgnoreCase("imdb") && + !arguments.get(0).equalsIgnoreCase("title")) + return badArgument(event, arguments.get(0)); + boolean isId = arguments.get(0).equalsIgnoreCase("imdb"); + String arg = String.join(" ", arguments.subList(1, arguments.size())); if (arg.isBlank()) return notEnoughArguments(event); logger.debug("Executing command with argument '{}'.", arg); return event.getMessage().getChannel() diff --git a/src/main/java/xyz/vallat/louis/commands/Quote.java b/src/main/java/xyz/vallat/louis/commands/Quote.java new file mode 100644 index 0000000..fd9aa00 --- /dev/null +++ b/src/main/java/xyz/vallat/louis/commands/Quote.java @@ -0,0 +1,41 @@ +package xyz.vallat.louis.commands; + +import discord4j.core.event.domain.message.MessageCreateEvent; +import discord4j.rest.util.Color; +import reactor.core.publisher.Mono; +import xyz.vallat.louis.database.LanguageManager; +import xyz.vallat.louis.database.SubtitleLineManager; +import xyz.vallat.louis.subtitles.dao.FilmQuote; + +import java.time.Instant; +import java.util.List; + +public class Quote extends Command { + + + public Quote(String name) { + super(name, "Get a random quote from any movie.", name + " [lang]", 0, 1); + } + + @Override + public Mono execute(MessageCreateEvent event) { + List args = getArguments(event); + int langId = LanguageManager.getIdLanguage(args.isEmpty() ? "english" : args.get(0)); + return event.getMessage().getChannel().flatMap(messageChannel -> messageChannel.createEmbed(embed -> { + embed.setTitle("Quote").setColor(Color.RED); + if (langId < 0) + embed.setDescription("This language is unknown. Try again with another one. Or don't try at all."); + else { + FilmQuote quote = SubtitleLineManager.getRandomLine(langId); + if (quote == null) + embed.setDescription("We don't have any quote in that language right now! Sorry!").setColor(Color.ORANGE); + else { + embed.setDescription(quote.getSubtitleBlock().getDialogue()); + embed.setFooter(quote.getMovie().toString(), null); + embed.setColor(Color.MEDIUM_SEA_GREEN); + } + } + embed.setTimestamp(Instant.now()); + })).then(); + } +} diff --git a/src/main/java/xyz/vallat/louis/database/SubtitleLineManager.java b/src/main/java/xyz/vallat/louis/database/SubtitleLineManager.java index 4c0936c..7711d89 100644 --- a/src/main/java/xyz/vallat/louis/database/SubtitleLineManager.java +++ b/src/main/java/xyz/vallat/louis/database/SubtitleLineManager.java @@ -5,7 +5,8 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import xyz.vallat.louis.codes.ExitCodes; import xyz.vallat.louis.omdb.objects.Movie; -import xyz.vallat.louis.subtitles.parser.SubtitleBlock; +import xyz.vallat.louis.subtitles.dao.FilmQuote; +import xyz.vallat.louis.subtitles.dao.SubtitleBlock; import java.sql.Connection; import java.sql.PreparedStatement; @@ -113,4 +114,31 @@ public final class SubtitleLineManager { } } + public static FilmQuote getRandomLine(int languageId) { + FilmQuote filmQuote = null; + try (Connection connection = DBManager.getConnection()) { + String query = "SELECT imdb_id, dialog_line, time_code FROM subtitle_lines " + + "INNER JOIN subtitles ON subtitles.id = subtitle_lines.subtitle_id " + + "INNER JOIN films ON subtitles.film_id = films.id " + + "WHERE subtitles.language_id = ?" + + "ORDER BY RANDOM()" + + "LIMIT 1;"; + try (PreparedStatement stmt = connection.prepareStatement(query)) { + stmt.setInt(1, languageId); + stmt.execute(); + if (stmt.getResultSet().next()) { + Movie movie = FilmManager.getMovieFromTitleOrImdbId( + stmt.getResultSet().getString("imdb_id"), true); + SubtitleBlock block = new SubtitleBlock(null, + stmt.getResultSet().getString("time_code"), + stmt.getResultSet().getString("dialog_line")); + filmQuote = new FilmQuote(movie, block); + } + } + } catch (SQLException e) { + logger.error("Cannot connect to database right now: {}", e.getMessage()); + System.exit(ExitCodes.CANNOT_CONNECT_TO_DB.getValue()); + } + return filmQuote; + } } diff --git a/src/main/java/xyz/vallat/louis/discord/DiscordManager.java b/src/main/java/xyz/vallat/louis/discord/DiscordManager.java index 2be93b3..1ca8472 100644 --- a/src/main/java/xyz/vallat/louis/discord/DiscordManager.java +++ b/src/main/java/xyz/vallat/louis/discord/DiscordManager.java @@ -28,6 +28,7 @@ public final class DiscordManager { commands.put("version", new Version(PREFIX + "version")); commands.put("listLangMovie", new ListLangMovie(PREFIX + "listLangMovie")); commands.put("download", new Download(PREFIX + "download")); + commands.put("quote", new Quote(PREFIX + "quote")); } private DiscordManager() { diff --git a/src/main/java/xyz/vallat/louis/omdb/OMDBClient.java b/src/main/java/xyz/vallat/louis/omdb/OMDBClient.java index 7a6c149..edb49a7 100644 --- a/src/main/java/xyz/vallat/louis/omdb/OMDBClient.java +++ b/src/main/java/xyz/vallat/louis/omdb/OMDBClient.java @@ -1,21 +1,14 @@ package xyz.vallat.louis.omdb; -import com.github.wtekiela.opensub4j.response.ListResponse; -import com.github.wtekiela.opensub4j.response.ResponseStatus; -import com.github.wtekiela.opensub4j.response.SubtitleFile; -import com.github.wtekiela.opensub4j.response.SubtitleInfo; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonParser; -import org.apache.xmlrpc.XmlRpcException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import xyz.vallat.louis.database.FilmManager; import xyz.vallat.louis.database.exceptions.ImportationException; import xyz.vallat.louis.env.EnvironmentVariables; import xyz.vallat.louis.omdb.objects.Movie; -import xyz.vallat.louis.subtitles.OpenSubtitles; -import xyz.vallat.louis.subtitles.parser.SubtitleBlock; import java.io.IOException; import java.net.URI; @@ -25,11 +18,7 @@ import java.net.http.HttpRequest; import java.net.http.HttpResponse; import java.nio.charset.StandardCharsets; import java.util.ArrayList; -import java.util.Comparator; import java.util.List; -import java.util.stream.Stream; - -import static xyz.vallat.louis.commands.Command.distinctByKey; public final class OMDBClient { diff --git a/src/main/java/xyz/vallat/louis/subtitles/dao/FilmQuote.java b/src/main/java/xyz/vallat/louis/subtitles/dao/FilmQuote.java new file mode 100644 index 0000000..08e4979 --- /dev/null +++ b/src/main/java/xyz/vallat/louis/subtitles/dao/FilmQuote.java @@ -0,0 +1,22 @@ +package xyz.vallat.louis.subtitles.dao; + +import xyz.vallat.louis.omdb.objects.Movie; + +public class FilmQuote { + + private final Movie movie; + private final SubtitleBlock subtitleBlock; + + public FilmQuote(Movie movie, SubtitleBlock subtitleBlock) { + this.movie = movie; + this.subtitleBlock = subtitleBlock; + } + + public Movie getMovie() { + return movie; + } + + public SubtitleBlock getSubtitleBlock() { + return subtitleBlock; + } +} diff --git a/src/main/java/xyz/vallat/louis/subtitles/parser/SubtitleBlock.java b/src/main/java/xyz/vallat/louis/subtitles/dao/SubtitleBlock.java similarity index 91% rename from src/main/java/xyz/vallat/louis/subtitles/parser/SubtitleBlock.java rename to src/main/java/xyz/vallat/louis/subtitles/dao/SubtitleBlock.java index 8c799fe..7ebaba4 100644 --- a/src/main/java/xyz/vallat/louis/subtitles/parser/SubtitleBlock.java +++ b/src/main/java/xyz/vallat/louis/subtitles/dao/SubtitleBlock.java @@ -1,4 +1,4 @@ -package xyz.vallat.louis.subtitles.parser; +package xyz.vallat.louis.subtitles.dao; public class SubtitleBlock { diff --git a/src/main/java/xyz/vallat/louis/subtitles/parser/SubtitleParser.java b/src/main/java/xyz/vallat/louis/subtitles/parser/SubtitleParser.java index f01c56b..0f89862 100644 --- a/src/main/java/xyz/vallat/louis/subtitles/parser/SubtitleParser.java +++ b/src/main/java/xyz/vallat/louis/subtitles/parser/SubtitleParser.java @@ -1,5 +1,7 @@ package xyz.vallat.louis.subtitles.parser; +import xyz.vallat.louis.subtitles.dao.SubtitleBlock; + import java.util.ArrayList; import java.util.Arrays; import java.util.List;