From 8f4eb5349db29a566249a04c01bb5b899547a324 Mon Sep 17 00:00:00 2001 From: Louis Vallat Date: Wed, 4 Nov 2020 10:08:47 +0100 Subject: [PATCH] Implemented a search option to specify a movie's title or imdb identifier Signed-off-by: Louis Vallat --- src/main/java/xyz/vallat/louis/commands/Quote.java | 11 +++++++++-- .../vallat/louis/database/SubtitleLineManager.java | 8 ++++++-- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/main/java/xyz/vallat/louis/commands/Quote.java b/src/main/java/xyz/vallat/louis/commands/Quote.java index 7ab6e95..fd8172a 100644 --- a/src/main/java/xyz/vallat/louis/commands/Quote.java +++ b/src/main/java/xyz/vallat/louis/commands/Quote.java @@ -15,14 +15,19 @@ import xyz.vallat.louis.subtitles.dao.FilmQuote; import xyz.vallat.louis.subtitles.dao.Lang; import java.time.Instant; +import java.util.stream.Collectors; public class Quote extends Command { private static final Logger logger = LoggerFactory.getLogger(Quote.class.getCanonicalName()); public Quote(String name) { - super(name, "Get a random quote from any movie.", name + " [-l|--lang ]", 0, 1); + super(name, "Get a random quote from any movie.", name + " [-l|--lang ] " + + "[-i|--imdb imdb_identifier]|[-t|--title title]", 0, 1); options.addOption(Option.builder("l").longOpt("lang").desc("specify a language").hasArg().build()); + options.addOption(Option.builder("i").longOpt("imdb").desc("specify an IMDB identifier").hasArg().build()); + options.addOption(Option.builder("t").longOpt("title").desc("specify a title").hasArgs() + .numberOfArgs(Option.UNLIMITED_VALUES).build()); } @Override @@ -31,12 +36,14 @@ public class Quote extends Command { CommandLine cmd = new DefaultParser().parse(options, event.getMessage().getContent().substring(name.length()).split(" ")); Lang language = LanguageManager.getLangFromAny(cmd.hasOption("l") ? cmd.getOptionValue("l") : "english"); + String imdb = cmd.hasOption("i") ? cmd.getOptionValue("i") : null; + String title = cmd.hasOption("t") ? String.join(" ", cmd.getOptionValues("t")) : null; return event.getMessage().getChannel().flatMap(messageChannel -> messageChannel.createEmbed(embed -> { embed.setTitle("Quote").setColor(Color.RED); if (language == null) embed.setDescription("This language is unknown. Try again with another one. Or don't try at all."); else { - FilmQuote quote = SubtitleLineManager.getRandomLine(language); + FilmQuote quote = SubtitleLineManager.getRandomLine(language, imdb, title); if (quote == null) embed.setDescription("We don't have any quote in that language right now! Sorry!").setColor(Color.ORANGE); else { diff --git a/src/main/java/xyz/vallat/louis/database/SubtitleLineManager.java b/src/main/java/xyz/vallat/louis/database/SubtitleLineManager.java index 8adb243..132949d 100644 --- a/src/main/java/xyz/vallat/louis/database/SubtitleLineManager.java +++ b/src/main/java/xyz/vallat/louis/database/SubtitleLineManager.java @@ -115,17 +115,21 @@ public final class SubtitleLineManager { } } - public static FilmQuote getRandomLine(Lang language) { + public static FilmQuote getRandomLine(Lang language, String imdb, String title) { 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 = ?" + + "WHERE subtitles.language_id = ? " + + (imdb != null ? "AND films.imdb_id = ? " : "") + + (title != null ? "AND UPPER(films.title) = UPPER(?) " : "") + "ORDER BY RANDOM()" + "LIMIT 1;"; try (PreparedStatement stmt = connection.prepareStatement(query)) { stmt.setInt(1, language.getId()); + if (imdb != null) stmt.setString(2, imdb); + if (title != null) stmt.setString((imdb != null ? 3 : 2), title); stmt.execute(); if (stmt.getResultSet().next()) { Movie movie = FilmManager.getMovieFromTitleOrImdbId(