Getting a quote now should be working
Signed-off-by: Louis Vallat <louis@louis-vallat.xyz>
This commit is contained in:
parent
af35537353
commit
585033d78b
@ -118,7 +118,7 @@ public abstract class Command {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void handleOk(String arg, EmbedCreateSpec embed, boolean isId, Movie movie, ListResponse<SubtitleInfo> subtitles) {
|
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())
|
Stream<String> subtitlesStream = getSubtitleStream(subtitles.getData())
|
||||||
.map(subtitleInfo -> subtitleInfo.getSubtitleFileId() + " - " + subtitleInfo.getLanguage());
|
.map(subtitleInfo -> subtitleInfo.getSubtitleFileId() + " - " + subtitleInfo.getLanguage());
|
||||||
String formattedSubtitles = subtitlesStream.limit(20).collect(Collectors.joining("\n"));
|
String formattedSubtitles = subtitlesStream.limit(20).collect(Collectors.joining("\n"));
|
||||||
@ -132,6 +132,7 @@ public abstract class Command {
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected List<String> getArguments(MessageCreateEvent event) {
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -17,7 +17,7 @@ import xyz.vallat.louis.database.SubtitleManager;
|
|||||||
import xyz.vallat.louis.omdb.OMDBClient;
|
import xyz.vallat.louis.omdb.OMDBClient;
|
||||||
import xyz.vallat.louis.omdb.objects.Movie;
|
import xyz.vallat.louis.omdb.objects.Movie;
|
||||||
import xyz.vallat.louis.subtitles.OpenSubtitles;
|
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 xyz.vallat.louis.subtitles.parser.SubtitleParser;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@ -39,15 +39,15 @@ public class Download extends Command {
|
|||||||
List<String> args = getArguments(event);
|
List<String> args = getArguments(event);
|
||||||
if (args.size() < minArgs) return notEnoughArguments(event);
|
if (args.size() < minArgs) return notEnoughArguments(event);
|
||||||
String value = String.join(" ", args.subList(3, args.size()));
|
String value = String.join(" ", args.subList(3, args.size()));
|
||||||
if (!args.get(2).equalsIgnoreCase("imdb") && !args.get(2).equalsIgnoreCase("title"))
|
if (!args.get(1).equalsIgnoreCase("imdb") && !args.get(1).equalsIgnoreCase("title"))
|
||||||
return badArgument(event, args.get(2));
|
return badArgument(event, args.get(1));
|
||||||
int languageId = LanguageManager.getIdLanguage(args.get(1));
|
int languageId = LanguageManager.getIdLanguage(args.get(0));
|
||||||
if (languageId < 0) return unknownLanguage(event, args.get(1));
|
if (languageId < 0) return unknownLanguage(event, args.get(0));
|
||||||
return event.getMessage().getChannel().flatMap(channel -> event.getMessage().addReaction(WAITING).then(
|
return event.getMessage().getChannel().flatMap(channel -> event.getMessage().addReaction(WAITING).then(
|
||||||
channel.createEmbed(embed -> {
|
channel.createEmbed(embed -> {
|
||||||
embed.setTitle("Importation").setColor(Color.RED);
|
embed.setTitle("Importation").setColor(Color.RED);
|
||||||
try {
|
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)
|
if (movie == null || movie.getId() <= 0)
|
||||||
embed.setDescription("We couldn't find any movie with these information. Sorry!");
|
embed.setDescription("We couldn't find any movie with these information. Sorry!");
|
||||||
else if (SubtitleManager.getSubtitlesId(movie, languageId) > 0)
|
else if (SubtitleManager.getSubtitlesId(movie, languageId) > 0)
|
||||||
|
@ -22,11 +22,11 @@ public class ListLangMovie extends Command {
|
|||||||
public Mono<Void> execute(MessageCreateEvent event) {
|
public Mono<Void> execute(MessageCreateEvent event) {
|
||||||
List<String> arguments = getArguments(event);
|
List<String> arguments = getArguments(event);
|
||||||
if (arguments.size() < minArgs) return notEnoughArguments(event);
|
if (arguments.size() < minArgs) return notEnoughArguments(event);
|
||||||
else if (!arguments.get(1).equalsIgnoreCase("imdb") &&
|
else if (!arguments.get(0).equalsIgnoreCase("imdb") &&
|
||||||
!arguments.get(1).equalsIgnoreCase("title"))
|
!arguments.get(0).equalsIgnoreCase("title"))
|
||||||
return badArgument(event, arguments.get(1));
|
return badArgument(event, arguments.get(0));
|
||||||
boolean isId = arguments.get(1).equalsIgnoreCase("imdb");
|
boolean isId = arguments.get(0).equalsIgnoreCase("imdb");
|
||||||
String arg = String.join(" ", arguments.subList(2, arguments.size()));
|
String arg = String.join(" ", arguments.subList(1, arguments.size()));
|
||||||
if (arg.isBlank()) return notEnoughArguments(event);
|
if (arg.isBlank()) return notEnoughArguments(event);
|
||||||
logger.debug("Executing command with argument '{}'.", arg);
|
logger.debug("Executing command with argument '{}'.", arg);
|
||||||
return event.getMessage().getChannel()
|
return event.getMessage().getChannel()
|
||||||
|
41
src/main/java/xyz/vallat/louis/commands/Quote.java
Normal file
41
src/main/java/xyz/vallat/louis/commands/Quote.java
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
@ -5,7 +5,8 @@ import org.slf4j.Logger;
|
|||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import xyz.vallat.louis.codes.ExitCodes;
|
import xyz.vallat.louis.codes.ExitCodes;
|
||||||
import xyz.vallat.louis.omdb.objects.Movie;
|
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.Connection;
|
||||||
import java.sql.PreparedStatement;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -28,6 +28,7 @@ public final class DiscordManager {
|
|||||||
commands.put("version", new Version(PREFIX + "version"));
|
commands.put("version", new Version(PREFIX + "version"));
|
||||||
commands.put("listLangMovie", new ListLangMovie(PREFIX + "listLangMovie"));
|
commands.put("listLangMovie", new ListLangMovie(PREFIX + "listLangMovie"));
|
||||||
commands.put("download", new Download(PREFIX + "download"));
|
commands.put("download", new Download(PREFIX + "download"));
|
||||||
|
commands.put("quote", new Quote(PREFIX + "quote"));
|
||||||
}
|
}
|
||||||
|
|
||||||
private DiscordManager() {
|
private DiscordManager() {
|
||||||
|
@ -1,21 +1,14 @@
|
|||||||
package xyz.vallat.louis.omdb;
|
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.JsonElement;
|
||||||
import com.google.gson.JsonObject;
|
import com.google.gson.JsonObject;
|
||||||
import com.google.gson.JsonParser;
|
import com.google.gson.JsonParser;
|
||||||
import org.apache.xmlrpc.XmlRpcException;
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import xyz.vallat.louis.database.FilmManager;
|
import xyz.vallat.louis.database.FilmManager;
|
||||||
import xyz.vallat.louis.database.exceptions.ImportationException;
|
import xyz.vallat.louis.database.exceptions.ImportationException;
|
||||||
import xyz.vallat.louis.env.EnvironmentVariables;
|
import xyz.vallat.louis.env.EnvironmentVariables;
|
||||||
import xyz.vallat.louis.omdb.objects.Movie;
|
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.io.IOException;
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
@ -25,11 +18,7 @@ import java.net.http.HttpRequest;
|
|||||||
import java.net.http.HttpResponse;
|
import java.net.http.HttpResponse;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Comparator;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.stream.Stream;
|
|
||||||
|
|
||||||
import static xyz.vallat.louis.commands.Command.distinctByKey;
|
|
||||||
|
|
||||||
public final class OMDBClient {
|
public final class OMDBClient {
|
||||||
|
|
||||||
|
22
src/main/java/xyz/vallat/louis/subtitles/dao/FilmQuote.java
Normal file
22
src/main/java/xyz/vallat/louis/subtitles/dao/FilmQuote.java
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
@ -1,4 +1,4 @@
|
|||||||
package xyz.vallat.louis.subtitles.parser;
|
package xyz.vallat.louis.subtitles.dao;
|
||||||
|
|
||||||
public class SubtitleBlock {
|
public class SubtitleBlock {
|
||||||
|
|
@ -1,5 +1,7 @@
|
|||||||
package xyz.vallat.louis.subtitles.parser;
|
package xyz.vallat.louis.subtitles.parser;
|
||||||
|
|
||||||
|
import xyz.vallat.louis.subtitles.dao.SubtitleBlock;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
Loading…
Reference in New Issue
Block a user