Removed the encoding limitation that I had, and also added a lang object to limit the interactions with the database
Signed-off-by: Louis Vallat <louis@louis-vallat.xyz>
This commit is contained in:
parent
d4d6f4a228
commit
2f3ebf0509
@ -17,6 +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.dao.Lang;
|
||||||
import xyz.vallat.louis.subtitles.dao.SubtitleBlock;
|
import xyz.vallat.louis.subtitles.dao.SubtitleBlock;
|
||||||
import xyz.vallat.louis.subtitles.parser.SubtitleParser;
|
import xyz.vallat.louis.subtitles.parser.SubtitleParser;
|
||||||
|
|
||||||
@ -41,8 +42,8 @@ public class Download extends Command {
|
|||||||
String value = String.join(" ", args.subList(2, args.size()));
|
String value = String.join(" ", args.subList(2, args.size()));
|
||||||
if (!args.get(1).equalsIgnoreCase("imdb") && !args.get(1).equalsIgnoreCase("title"))
|
if (!args.get(1).equalsIgnoreCase("imdb") && !args.get(1).equalsIgnoreCase("title"))
|
||||||
return badArgument(event, args.get(1));
|
return badArgument(event, args.get(1));
|
||||||
int languageId = LanguageManager.getIdLanguage(args.get(0));
|
Lang language = LanguageManager.getLangFromAny(args.get(0));
|
||||||
if (languageId < 0) return unknownLanguage(event, args.get(0));
|
if (language == null) 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);
|
||||||
@ -50,11 +51,11 @@ public class Download extends Command {
|
|||||||
Movie movie = OMDBClient.getMovie(value, args.get(1).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, language.getId()) > 0)
|
||||||
embed.setDescription("This movie already has already this language imported.")
|
embed.setDescription("This movie already has already this language imported.")
|
||||||
.setColor(Color.ORANGE);
|
.setColor(Color.ORANGE);
|
||||||
else
|
else
|
||||||
computeImportation(event, args, languageId, embed, movie);
|
computeImportation(event, args, language, embed, movie);
|
||||||
} catch (XmlRpcException | IOException e) {
|
} catch (XmlRpcException | IOException e) {
|
||||||
logger.error("An error occurred while fetching the data from opensubtitles: {}", e.getMessage());
|
logger.error("An error occurred while fetching the data from opensubtitles: {}", e.getMessage());
|
||||||
embed.setDescription("An error occurred, please contact my administrator.");
|
embed.setDescription("An error occurred, please contact my administrator.");
|
||||||
@ -65,12 +66,9 @@ public class Download extends Command {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void computeImportation(MessageCreateEvent event, List<String> args,
|
private void computeImportation(MessageCreateEvent event, List<String> args,
|
||||||
int languageId, EmbedCreateSpec embed, Movie movie) throws XmlRpcException {
|
Lang language, EmbedCreateSpec embed, Movie movie) throws XmlRpcException {
|
||||||
Stream<SubtitleInfo> subtitleInfoStream = OpenSubtitles.getSubtitleStreamFromMovie(movie);
|
Stream<SubtitleInfo> subtitleInfoStream = OpenSubtitles.getSubtitleStreamFromMovie(movie, language.getAlpha3b());
|
||||||
Optional<SubtitleInfo> subtitleInfo = subtitleInfoStream == null ? Optional.empty() :
|
Optional<SubtitleInfo> subtitleInfo = subtitleInfoStream == null ? Optional.empty() : subtitleInfoStream.findFirst();
|
||||||
subtitleInfoStream.filter(info ->
|
|
||||||
LanguageManager.getIdLanguage(info.getLanguage()) == languageId)
|
|
||||||
.findFirst();
|
|
||||||
if (subtitleInfo.isEmpty())
|
if (subtitleInfo.isEmpty())
|
||||||
embed.setDescription("Nothing was found with these parameters.");
|
embed.setDescription("Nothing was found with these parameters.");
|
||||||
else {
|
else {
|
||||||
@ -78,8 +76,8 @@ public class Download extends Command {
|
|||||||
subtitleInfo.get().getSubtitleFileId());
|
subtitleInfo.get().getSubtitleFileId());
|
||||||
if (subs.getStatus().equals(ResponseStatus.OK)) {
|
if (subs.getStatus().equals(ResponseStatus.OK)) {
|
||||||
List<SubtitleBlock> blocks = new SubtitleParser().parseSRT(
|
List<SubtitleBlock> blocks = new SubtitleParser().parseSRT(
|
||||||
subs.getData().get(0).getContentAsString("cp1252"));
|
subs.getData().get(0).getContentAsString(subtitleInfo.get().getEncoding()));
|
||||||
SubtitleLineManager.importSubtitleLines(blocks, languageId, movie,
|
SubtitleLineManager.importSubtitleLines(blocks, language, movie,
|
||||||
event.getGuildId().isPresent() ? event.getGuildId().get() : null,
|
event.getGuildId().isPresent() ? event.getGuildId().get() : null,
|
||||||
event.getMember().isPresent() ? event.getMember().get().getId() : null);
|
event.getMember().isPresent() ? event.getMember().get().getId() : null);
|
||||||
embed.setColor(Color.MEDIUM_SEA_GREEN).setDescription("Everything went well. " +
|
embed.setColor(Color.MEDIUM_SEA_GREEN).setDescription("Everything went well. " +
|
||||||
|
@ -6,6 +6,7 @@ import reactor.core.publisher.Mono;
|
|||||||
import xyz.vallat.louis.database.LanguageManager;
|
import xyz.vallat.louis.database.LanguageManager;
|
||||||
import xyz.vallat.louis.database.SubtitleLineManager;
|
import xyz.vallat.louis.database.SubtitleLineManager;
|
||||||
import xyz.vallat.louis.subtitles.dao.FilmQuote;
|
import xyz.vallat.louis.subtitles.dao.FilmQuote;
|
||||||
|
import xyz.vallat.louis.subtitles.dao.Lang;
|
||||||
|
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@ -20,13 +21,13 @@ public class Quote extends Command {
|
|||||||
@Override
|
@Override
|
||||||
public Mono<Void> execute(MessageCreateEvent event) {
|
public Mono<Void> execute(MessageCreateEvent event) {
|
||||||
List<String> args = getArguments(event);
|
List<String> args = getArguments(event);
|
||||||
int langId = LanguageManager.getIdLanguage(args.isEmpty() ? "english" : args.get(0));
|
Lang language = LanguageManager.getLangFromAny(args.isEmpty() ? "english" : args.get(0));
|
||||||
return event.getMessage().getChannel().flatMap(messageChannel -> messageChannel.createEmbed(embed -> {
|
return event.getMessage().getChannel().flatMap(messageChannel -> messageChannel.createEmbed(embed -> {
|
||||||
embed.setTitle("Quote").setColor(Color.RED);
|
embed.setTitle("Quote").setColor(Color.RED);
|
||||||
if (langId < 0)
|
if (language == null)
|
||||||
embed.setDescription("This language is unknown. Try again with another one. Or don't try at all.");
|
embed.setDescription("This language is unknown. Try again with another one. Or don't try at all.");
|
||||||
else {
|
else {
|
||||||
FilmQuote quote = SubtitleLineManager.getRandomLine(langId);
|
FilmQuote quote = SubtitleLineManager.getRandomLine(language);
|
||||||
if (quote == null)
|
if (quote == null)
|
||||||
embed.setDescription("We don't have any quote in that language right now! Sorry!").setColor(Color.ORANGE);
|
embed.setDescription("We don't have any quote in that language right now! Sorry!").setColor(Color.ORANGE);
|
||||||
else {
|
else {
|
||||||
|
@ -6,6 +6,7 @@ import org.apache.commons.csv.CSVRecord;
|
|||||||
import org.slf4j.Logger;
|
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.subtitles.dao.Lang;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
@ -102,21 +103,31 @@ public final class LanguageManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static int getIdLanguage(String name) {
|
public static Lang getLangFromAny(String name) {
|
||||||
|
Lang lang = null;
|
||||||
name = name.toLowerCase();
|
name = name.toLowerCase();
|
||||||
try (Connection connection = DBManager.getConnection()) {
|
try (Connection connection = DBManager.getConnection()) {
|
||||||
String query = "SELECT id FROM languages WHERE alpha3_b = ? " +
|
String query = "SELECT * FROM languages WHERE alpha3_b = ? " +
|
||||||
"OR alpha3_t = ? OR alpha2 = ? OR english = ? OR french = ?;";
|
"OR alpha3_t = ? OR alpha2 = ? OR english = ? OR french = ?;";
|
||||||
try (PreparedStatement stmt = connection.prepareStatement(query)){
|
try (PreparedStatement stmt = connection.prepareStatement(query)){
|
||||||
for (int i = 1; i < 6; i++) stmt.setString(i, name);
|
for (int i = 1; i < 6; i++) stmt.setString(i, name);
|
||||||
stmt.execute();
|
stmt.execute();
|
||||||
if (stmt.getResultSet().next()) return stmt.getResultSet().getInt("id");
|
if (stmt.getResultSet().next()) {
|
||||||
|
lang = new Lang(
|
||||||
|
stmt.getResultSet().getInt("id"),
|
||||||
|
stmt.getResultSet().getString("alpha3_b"),
|
||||||
|
stmt.getResultSet().getString("alpha3_t"),
|
||||||
|
stmt.getResultSet().getString("alpha2"),
|
||||||
|
stmt.getResultSet().getString("english"),
|
||||||
|
stmt.getResultSet().getString("french")
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
logger.error("Cannot connect to database: {}", e.getMessage());
|
logger.error("Cannot connect to database: {}", e.getMessage());
|
||||||
System.exit(ExitCodes.CANNOT_CONNECT_TO_DB.getValue());
|
System.exit(ExitCodes.CANNOT_CONNECT_TO_DB.getValue());
|
||||||
}
|
}
|
||||||
return -1;
|
return lang;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void insertLang(Connection connection, String alpha3b, String alpha3t,
|
private static void insertLang(Connection connection, String alpha3b, String alpha3t,
|
||||||
@ -132,6 +143,12 @@ public final class LanguageManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static Lang getLanguageFromId(int id) {
|
||||||
|
Lang language = null;
|
||||||
|
|
||||||
|
return language;
|
||||||
|
}
|
||||||
|
|
||||||
private static String getActualHash() throws NoSuchAlgorithmException, IOException {
|
private static String getActualHash() throws NoSuchAlgorithmException, IOException {
|
||||||
logger.debug("Computing the actual language source file's hash.");
|
logger.debug("Computing the actual language source file's hash.");
|
||||||
return getFileChecksum(MessageDigest.getInstance("MD5"));
|
return getFileChecksum(MessageDigest.getInstance("MD5"));
|
||||||
|
@ -6,6 +6,7 @@ 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.dao.FilmQuote;
|
import xyz.vallat.louis.subtitles.dao.FilmQuote;
|
||||||
|
import xyz.vallat.louis.subtitles.dao.Lang;
|
||||||
import xyz.vallat.louis.subtitles.dao.SubtitleBlock;
|
import xyz.vallat.louis.subtitles.dao.SubtitleBlock;
|
||||||
|
|
||||||
import java.sql.Connection;
|
import java.sql.Connection;
|
||||||
@ -74,7 +75,7 @@ public final class SubtitleLineManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void importSubtitleLines(List<SubtitleBlock> subtitleBlocks, int language, Movie movie,
|
public static void importSubtitleLines(List<SubtitleBlock> subtitleBlocks, Lang language, Movie movie,
|
||||||
Snowflake importer, Snowflake importer_guild) {
|
Snowflake importer, Snowflake importer_guild) {
|
||||||
try (Connection connection = DBManager.getConnection()) {
|
try (Connection connection = DBManager.getConnection()) {
|
||||||
connection.setAutoCommit(false);
|
connection.setAutoCommit(false);
|
||||||
@ -82,7 +83,7 @@ public final class SubtitleLineManager {
|
|||||||
"VALUES(?, ?, ?, ?);";
|
"VALUES(?, ?, ?, ?);";
|
||||||
try (PreparedStatement stmt = connection.prepareStatement(querySubtitles, PreparedStatement.RETURN_GENERATED_KEYS)) {
|
try (PreparedStatement stmt = connection.prepareStatement(querySubtitles, PreparedStatement.RETURN_GENERATED_KEYS)) {
|
||||||
stmt.setInt(1, movie.getId());
|
stmt.setInt(1, movie.getId());
|
||||||
stmt.setInt(2, language);
|
stmt.setInt(2, language.getId());
|
||||||
stmt.setString(3, importer == null ? null : importer.asString());
|
stmt.setString(3, importer == null ? null : importer.asString());
|
||||||
stmt.setString(4, importer_guild == null ? null : importer_guild.asString());
|
stmt.setString(4, importer_guild == null ? null : importer_guild.asString());
|
||||||
stmt.executeUpdate();
|
stmt.executeUpdate();
|
||||||
@ -114,7 +115,7 @@ public final class SubtitleLineManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static FilmQuote getRandomLine(int languageId) {
|
public static FilmQuote getRandomLine(Lang language) {
|
||||||
FilmQuote filmQuote = null;
|
FilmQuote filmQuote = null;
|
||||||
try (Connection connection = DBManager.getConnection()) {
|
try (Connection connection = DBManager.getConnection()) {
|
||||||
String query = "SELECT imdb_id, dialog_line, time_code FROM subtitle_lines " +
|
String query = "SELECT imdb_id, dialog_line, time_code FROM subtitle_lines " +
|
||||||
@ -124,7 +125,7 @@ public final class SubtitleLineManager {
|
|||||||
"ORDER BY RANDOM()" +
|
"ORDER BY RANDOM()" +
|
||||||
"LIMIT 1;";
|
"LIMIT 1;";
|
||||||
try (PreparedStatement stmt = connection.prepareStatement(query)) {
|
try (PreparedStatement stmt = connection.prepareStatement(query)) {
|
||||||
stmt.setInt(1, languageId);
|
stmt.setInt(1, language.getId());
|
||||||
stmt.execute();
|
stmt.execute();
|
||||||
if (stmt.getResultSet().next()) {
|
if (stmt.getResultSet().next()) {
|
||||||
Movie movie = FilmManager.getMovieFromTitleOrImdbId(
|
Movie movie = FilmManager.getMovieFromTitleOrImdbId(
|
||||||
|
@ -10,7 +10,7 @@ import java.sql.PreparedStatement;
|
|||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.sql.Statement;
|
import java.sql.Statement;
|
||||||
|
|
||||||
public class SubtitleManager {
|
public final class SubtitleManager {
|
||||||
|
|
||||||
private static final Logger logger = LoggerFactory.getLogger(SubtitleManager.class.getCanonicalName());
|
private static final Logger logger = LoggerFactory.getLogger(SubtitleManager.class.getCanonicalName());
|
||||||
|
|
||||||
|
@ -6,7 +6,6 @@ import com.github.wtekiela.opensub4j.response.ListResponse;
|
|||||||
import com.github.wtekiela.opensub4j.response.ResponseStatus;
|
import com.github.wtekiela.opensub4j.response.ResponseStatus;
|
||||||
import com.github.wtekiela.opensub4j.response.SubtitleFile;
|
import com.github.wtekiela.opensub4j.response.SubtitleFile;
|
||||||
import com.github.wtekiela.opensub4j.response.SubtitleInfo;
|
import com.github.wtekiela.opensub4j.response.SubtitleInfo;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
|
||||||
import org.apache.xmlrpc.XmlRpcException;
|
import org.apache.xmlrpc.XmlRpcException;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
@ -72,25 +71,22 @@ public final class OpenSubtitles {
|
|||||||
|
|
||||||
public static Stream<SubtitleInfo> getSubtitleStream(List<SubtitleInfo> subtitles) {
|
public static Stream<SubtitleInfo> getSubtitleStream(List<SubtitleInfo> subtitles) {
|
||||||
return subtitles.stream()
|
return subtitles.stream()
|
||||||
.sorted(Comparator.comparingInt(SubtitleInfo::getDownloadsNo))
|
.sorted(Comparator.comparingInt(SubtitleInfo::getDownloadsNo).reversed())
|
||||||
.filter(distinctByKey(SubtitleInfo::getLanguage))
|
.filter(distinctByKey(SubtitleInfo::getLanguage));
|
||||||
.filter(subtitleInfo ->
|
|
||||||
(subtitleInfo.getEncoding().equalsIgnoreCase("ascii")
|
|
||||||
|| subtitleInfo.getEncoding().equalsIgnoreCase("cp1252")));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Stream<SubtitleInfo> getSubtitleStreamFromMovie(Movie movie) {
|
public static Stream<SubtitleInfo> getSubtitleStreamFromMovie(Movie movie, String lang) {
|
||||||
Stream<SubtitleInfo> subtitles = null;
|
Stream<SubtitleInfo> subtitles = null;
|
||||||
try {
|
try {
|
||||||
if (movie != null && isLoggedIn()) {
|
if (movie != null && isLoggedIn()) {
|
||||||
ListResponse<SubtitleInfo> subtitleInfoList =
|
ListResponse<SubtitleInfo> subtitleInfoList =
|
||||||
OpenSubtitles.searchSubtitles("", String.valueOf(movie.getNumericImdbId()));
|
OpenSubtitles.searchSubtitles(lang, String.valueOf(movie.getNumericImdbId()));
|
||||||
if (subtitleInfoList.getStatus().equals(ResponseStatus.OK))
|
if (subtitleInfoList.getStatus().equals(ResponseStatus.OK))
|
||||||
subtitles = getSubtitleStream(subtitleInfoList.getData());
|
subtitles = getSubtitleStream(subtitleInfoList.getData());
|
||||||
else if (subtitleInfoList.getStatus().equals(ResponseStatus.UNAUTHORIZED)) {
|
else if (subtitleInfoList.getStatus().equals(ResponseStatus.UNAUTHORIZED)) {
|
||||||
logout();
|
logout();
|
||||||
login();
|
login();
|
||||||
if (isLoggedIn()) subtitles = getSubtitleStreamFromMovie(movie);
|
if (isLoggedIn()) subtitles = getSubtitleStreamFromMovie(movie, lang);
|
||||||
else throw new UnauthorizedException("Cannot login right now.");
|
else throw new UnauthorizedException("Cannot login right now.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -100,4 +96,8 @@ public final class OpenSubtitles {
|
|||||||
}
|
}
|
||||||
return subtitles;
|
return subtitles;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static Stream<SubtitleInfo> getSubtitleStreamFromMovie(Movie movie) {
|
||||||
|
return getSubtitleStreamFromMovie(movie, "");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
44
src/main/java/xyz/vallat/louis/subtitles/dao/Lang.java
Normal file
44
src/main/java/xyz/vallat/louis/subtitles/dao/Lang.java
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
package xyz.vallat.louis.subtitles.dao;
|
||||||
|
|
||||||
|
public class Lang {
|
||||||
|
|
||||||
|
private final int id;
|
||||||
|
private final String alpha3b;
|
||||||
|
private final String alpha3t;
|
||||||
|
private final String alpha2;
|
||||||
|
private final String english;
|
||||||
|
private final String french;
|
||||||
|
|
||||||
|
public Lang(int id, String alpha3b, String alpha3t, String alpha2, String english, String french) {
|
||||||
|
this.id = id;
|
||||||
|
this.alpha3b = alpha3b;
|
||||||
|
this.alpha3t = alpha3t;
|
||||||
|
this.alpha2 = alpha2;
|
||||||
|
this.english = english;
|
||||||
|
this.french = french;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getAlpha3b() {
|
||||||
|
return alpha3b;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getAlpha3t() {
|
||||||
|
return alpha3t;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getAlpha2() {
|
||||||
|
return alpha2;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getEnglish() {
|
||||||
|
return english;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getFrench() {
|
||||||
|
return french;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user