diff --git a/src/main/java/xyz/vallat/louis/commands/Download.java b/src/main/java/xyz/vallat/louis/commands/Download.java index 96969cb..d72d427 100644 --- a/src/main/java/xyz/vallat/louis/commands/Download.java +++ b/src/main/java/xyz/vallat/louis/commands/Download.java @@ -17,6 +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.dao.Lang; import xyz.vallat.louis.subtitles.dao.SubtitleBlock; 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())); 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)); + Lang language = LanguageManager.getLangFromAny(args.get(0)); + if (language == null) 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); @@ -50,11 +51,11 @@ public class Download extends Command { 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) + else if (SubtitleManager.getSubtitlesId(movie, language.getId()) > 0) embed.setDescription("This movie already has already this language imported.") .setColor(Color.ORANGE); else - computeImportation(event, args, languageId, embed, movie); + computeImportation(event, args, language, embed, movie); } catch (XmlRpcException | IOException e) { logger.error("An error occurred while fetching the data from opensubtitles: {}", e.getMessage()); embed.setDescription("An error occurred, please contact my administrator."); @@ -65,12 +66,9 @@ public class Download extends Command { } private void computeImportation(MessageCreateEvent event, List args, - int languageId, EmbedCreateSpec embed, Movie movie) throws XmlRpcException { - Stream subtitleInfoStream = OpenSubtitles.getSubtitleStreamFromMovie(movie); - Optional subtitleInfo = subtitleInfoStream == null ? Optional.empty() : - subtitleInfoStream.filter(info -> - LanguageManager.getIdLanguage(info.getLanguage()) == languageId) - .findFirst(); + Lang language, EmbedCreateSpec embed, Movie movie) throws XmlRpcException { + Stream subtitleInfoStream = OpenSubtitles.getSubtitleStreamFromMovie(movie, language.getAlpha3b()); + Optional subtitleInfo = subtitleInfoStream == null ? Optional.empty() : subtitleInfoStream.findFirst(); if (subtitleInfo.isEmpty()) embed.setDescription("Nothing was found with these parameters."); else { @@ -78,8 +76,8 @@ public class Download extends Command { subtitleInfo.get().getSubtitleFileId()); if (subs.getStatus().equals(ResponseStatus.OK)) { List blocks = new SubtitleParser().parseSRT( - subs.getData().get(0).getContentAsString("cp1252")); - SubtitleLineManager.importSubtitleLines(blocks, languageId, movie, + subs.getData().get(0).getContentAsString(subtitleInfo.get().getEncoding())); + SubtitleLineManager.importSubtitleLines(blocks, language, movie, event.getGuildId().isPresent() ? event.getGuildId().get() : null, event.getMember().isPresent() ? event.getMember().get().getId() : null); embed.setColor(Color.MEDIUM_SEA_GREEN).setDescription("Everything went well. " + diff --git a/src/main/java/xyz/vallat/louis/commands/Quote.java b/src/main/java/xyz/vallat/louis/commands/Quote.java index 6185432..fddd84e 100644 --- a/src/main/java/xyz/vallat/louis/commands/Quote.java +++ b/src/main/java/xyz/vallat/louis/commands/Quote.java @@ -6,6 +6,7 @@ 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 xyz.vallat.louis.subtitles.dao.Lang; import java.time.Instant; import java.util.List; @@ -20,13 +21,13 @@ public class Quote extends Command { @Override public Mono execute(MessageCreateEvent event) { List 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 -> { 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."); else { - FilmQuote quote = SubtitleLineManager.getRandomLine(langId); + FilmQuote quote = SubtitleLineManager.getRandomLine(language); 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/LanguageManager.java b/src/main/java/xyz/vallat/louis/database/LanguageManager.java index dcc94d8..d0c0387 100644 --- a/src/main/java/xyz/vallat/louis/database/LanguageManager.java +++ b/src/main/java/xyz/vallat/louis/database/LanguageManager.java @@ -6,6 +6,7 @@ import org.apache.commons.csv.CSVRecord; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import xyz.vallat.louis.codes.ExitCodes; +import xyz.vallat.louis.subtitles.dao.Lang; import java.io.IOException; 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(); 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 = ?;"; try (PreparedStatement stmt = connection.prepareStatement(query)){ for (int i = 1; i < 6; i++) stmt.setString(i, name); 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) { logger.error("Cannot connect to database: {}", e.getMessage()); System.exit(ExitCodes.CANNOT_CONNECT_TO_DB.getValue()); } - return -1; + return lang; } 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 { logger.debug("Computing the actual language source file's hash."); return getFileChecksum(MessageDigest.getInstance("MD5")); diff --git a/src/main/java/xyz/vallat/louis/database/SubtitleLineManager.java b/src/main/java/xyz/vallat/louis/database/SubtitleLineManager.java index 7711d89..8adb243 100644 --- a/src/main/java/xyz/vallat/louis/database/SubtitleLineManager.java +++ b/src/main/java/xyz/vallat/louis/database/SubtitleLineManager.java @@ -6,6 +6,7 @@ import org.slf4j.LoggerFactory; import xyz.vallat.louis.codes.ExitCodes; import xyz.vallat.louis.omdb.objects.Movie; import xyz.vallat.louis.subtitles.dao.FilmQuote; +import xyz.vallat.louis.subtitles.dao.Lang; import xyz.vallat.louis.subtitles.dao.SubtitleBlock; import java.sql.Connection; @@ -74,7 +75,7 @@ public final class SubtitleLineManager { } } - public static void importSubtitleLines(List subtitleBlocks, int language, Movie movie, + public static void importSubtitleLines(List subtitleBlocks, Lang language, Movie movie, Snowflake importer, Snowflake importer_guild) { try (Connection connection = DBManager.getConnection()) { connection.setAutoCommit(false); @@ -82,7 +83,7 @@ public final class SubtitleLineManager { "VALUES(?, ?, ?, ?);"; try (PreparedStatement stmt = connection.prepareStatement(querySubtitles, PreparedStatement.RETURN_GENERATED_KEYS)) { stmt.setInt(1, movie.getId()); - stmt.setInt(2, language); + stmt.setInt(2, language.getId()); stmt.setString(3, importer == null ? null : importer.asString()); stmt.setString(4, importer_guild == null ? null : importer_guild.asString()); 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; try (Connection connection = DBManager.getConnection()) { String query = "SELECT imdb_id, dialog_line, time_code FROM subtitle_lines " + @@ -124,7 +125,7 @@ public final class SubtitleLineManager { "ORDER BY RANDOM()" + "LIMIT 1;"; try (PreparedStatement stmt = connection.prepareStatement(query)) { - stmt.setInt(1, languageId); + stmt.setInt(1, language.getId()); stmt.execute(); if (stmt.getResultSet().next()) { Movie movie = FilmManager.getMovieFromTitleOrImdbId( diff --git a/src/main/java/xyz/vallat/louis/database/SubtitleManager.java b/src/main/java/xyz/vallat/louis/database/SubtitleManager.java index df49205..04df528 100644 --- a/src/main/java/xyz/vallat/louis/database/SubtitleManager.java +++ b/src/main/java/xyz/vallat/louis/database/SubtitleManager.java @@ -10,7 +10,7 @@ import java.sql.PreparedStatement; import java.sql.SQLException; import java.sql.Statement; -public class SubtitleManager { +public final class SubtitleManager { private static final Logger logger = LoggerFactory.getLogger(SubtitleManager.class.getCanonicalName()); diff --git a/src/main/java/xyz/vallat/louis/subtitles/OpenSubtitles.java b/src/main/java/xyz/vallat/louis/subtitles/OpenSubtitles.java index e6aa897..3551ade 100644 --- a/src/main/java/xyz/vallat/louis/subtitles/OpenSubtitles.java +++ b/src/main/java/xyz/vallat/louis/subtitles/OpenSubtitles.java @@ -6,7 +6,6 @@ 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 org.apache.commons.lang3.StringUtils; import org.apache.xmlrpc.XmlRpcException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -72,25 +71,22 @@ public final class OpenSubtitles { public static Stream getSubtitleStream(List subtitles) { return subtitles.stream() - .sorted(Comparator.comparingInt(SubtitleInfo::getDownloadsNo)) - .filter(distinctByKey(SubtitleInfo::getLanguage)) - .filter(subtitleInfo -> - (subtitleInfo.getEncoding().equalsIgnoreCase("ascii") - || subtitleInfo.getEncoding().equalsIgnoreCase("cp1252"))); + .sorted(Comparator.comparingInt(SubtitleInfo::getDownloadsNo).reversed()) + .filter(distinctByKey(SubtitleInfo::getLanguage)); } - public static Stream getSubtitleStreamFromMovie(Movie movie) { + public static Stream getSubtitleStreamFromMovie(Movie movie, String lang) { Stream subtitles = null; try { if (movie != null && isLoggedIn()) { ListResponse subtitleInfoList = - OpenSubtitles.searchSubtitles("", String.valueOf(movie.getNumericImdbId())); + OpenSubtitles.searchSubtitles(lang, String.valueOf(movie.getNumericImdbId())); if (subtitleInfoList.getStatus().equals(ResponseStatus.OK)) subtitles = getSubtitleStream(subtitleInfoList.getData()); else if (subtitleInfoList.getStatus().equals(ResponseStatus.UNAUTHORIZED)) { logout(); login(); - if (isLoggedIn()) subtitles = getSubtitleStreamFromMovie(movie); + if (isLoggedIn()) subtitles = getSubtitleStreamFromMovie(movie, lang); else throw new UnauthorizedException("Cannot login right now."); } } @@ -100,4 +96,8 @@ public final class OpenSubtitles { } return subtitles; } + + public static Stream getSubtitleStreamFromMovie(Movie movie) { + return getSubtitleStreamFromMovie(movie, ""); + } } diff --git a/src/main/java/xyz/vallat/louis/subtitles/dao/Lang.java b/src/main/java/xyz/vallat/louis/subtitles/dao/Lang.java new file mode 100644 index 0000000..df4dcaa --- /dev/null +++ b/src/main/java/xyz/vallat/louis/subtitles/dao/Lang.java @@ -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; + } +}