Getting a quote now should be working

Signed-off-by: Louis Vallat <louis@louis-vallat.xyz>
This commit is contained in:
Louis Vallat 2020-10-29 14:44:46 +01:00
parent af35537353
commit 585033d78b
10 changed files with 110 additions and 26 deletions

View File

@ -118,7 +118,7 @@ public abstract class Command {
}
private void handleOk(String arg, EmbedCreateSpec embed, boolean isId, Movie movie, ListResponse<SubtitleInfo> subtitles) {
embed.setColor(Color.BISMARK);
embed.setColor(Color.MEDIUM_SEA_GREEN);
Stream<String> 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<String> 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);
}
}

View File

@ -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<String> 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)

View File

@ -22,11 +22,11 @@ public class ListLangMovie extends Command {
public Mono<Void> execute(MessageCreateEvent event) {
List<String> 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()

View File

@ -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<Void> execute(MessageCreateEvent event) {
List<String> 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();
}
}

View File

@ -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;
}
}

View File

@ -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() {

View File

@ -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 {

View File

@ -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;
}
}

View File

@ -1,4 +1,4 @@
package xyz.vallat.louis.subtitles.parser;
package xyz.vallat.louis.subtitles.dao;
public class SubtitleBlock {

View File

@ -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;