Implemented a search option to specify a movie's title or imdb identifier
Signed-off-by: Louis Vallat <louis@louis-vallat.xyz>
This commit is contained in:
parent
0ea4385cf0
commit
8f4eb5349d
@ -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 <lang>]", 0, 1);
|
||||
super(name, "Get a random quote from any movie.", name + " [-l|--lang <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 {
|
||||
|
@ -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 = ? " +
|
||||
(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(
|
||||
|
Loading…
Reference in New Issue
Block a user