From c830abcb4d7906687bc7c52f527d2e6cdd06874e Mon Sep 17 00:00:00 2001 From: Louis Vallat Date: Wed, 4 Nov 2020 13:37:31 +0100 Subject: [PATCH] Added search capability in the quote command Signed-off-by: Louis Vallat --- src/main/java/xyz/vallat/louis/commands/Quote.java | 10 +++++++--- .../vallat/louis/database/SubtitleLineManager.java | 11 +++++++---- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/src/main/java/xyz/vallat/louis/commands/Quote.java b/src/main/java/xyz/vallat/louis/commands/Quote.java index cb21ea9..19edd62 100644 --- a/src/main/java/xyz/vallat/louis/commands/Quote.java +++ b/src/main/java/xyz/vallat/louis/commands/Quote.java @@ -19,12 +19,15 @@ public class Quote extends Command { public Quote(String name) { super(name, "Get a random quote from any movie.", name + " [-l|--lang ] " + - "[-i|--imdb imdb_identifier]|[-t|--title title]", 0, 1); + "[-s|--search ] [-i|--imdb imdb_identifier]|[-t|--title title]", 0, 1); OptionGroup iOrT = new OptionGroup() .addOption(Option.builder("i").longOpt("imdb").desc("specify an IMDB identifier").hasArg().build()) .addOption(Option.builder("t").longOpt("title").desc("specify a title").hasArgs() .numberOfArgs(Option.UNLIMITED_VALUES).build()); - options.addOption(Option.builder("l").longOpt("lang").desc("specify a language (by default, english)").hasArg().build()); + options.addOption(Option.builder("l").longOpt("lang").desc("specify a language (by default, english)") + .hasArg().build()); + options.addOption(Option.builder("s").longOpt("search").desc("specify a part of the quote").hasArgs() + .numberOfArgs(Option.UNLIMITED_VALUES).build()); options.addOptionGroup(iOrT); } @@ -36,12 +39,13 @@ public class Quote extends Command { 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; + String search = cmd.hasOption("s") ? String.join(" ", cmd.getOptionValues("s")) : 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, imdb, title); + FilmQuote quote = SubtitleLineManager.getRandomLine(language, imdb, title, search); 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 132949d..4138393 100644 --- a/src/main/java/xyz/vallat/louis/database/SubtitleLineManager.java +++ b/src/main/java/xyz/vallat/louis/database/SubtitleLineManager.java @@ -115,21 +115,24 @@ public final class SubtitleLineManager { } } - public static FilmQuote getRandomLine(Lang language, String imdb, String title) { + public static FilmQuote getRandomLine(Lang language, String imdb, String title, String search) { 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 = ? " + + (search != null ? "AND regexp_replace(dialog_line, E'[\\\\n\\\\r]+', ' ', 'g' ) LIKE ? " : "") + (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); + int position = 0; + stmt.setInt(++position, language.getId()); + if (search != null) stmt.setString(++position, "%" + search + "%"); + if (imdb != null) stmt.setString(++position, imdb); + if (title != null) stmt.setString(++position, title); stmt.execute(); if (stmt.getResultSet().next()) { Movie movie = FilmManager.getMovieFromTitleOrImdbId(