[WIP] Reimporting now works
Signed-off-by: Louis Vallat <louis@louis-vallat.xyz>
This commit is contained in:
parent
4490eff40c
commit
245c9091b7
@ -1,20 +1,34 @@
|
|||||||
package xyz.vallat.louis;
|
package xyz.vallat.louis;
|
||||||
|
|
||||||
|
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.cli.*;
|
import org.apache.commons.cli.*;
|
||||||
|
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.codes.ExitCodes;
|
import xyz.vallat.louis.codes.ExitCodes;
|
||||||
import xyz.vallat.louis.database.DBManager;
|
import xyz.vallat.louis.database.DBManager;
|
||||||
|
import xyz.vallat.louis.database.SubtitleLineManager;
|
||||||
import xyz.vallat.louis.database.SubtitleManager;
|
import xyz.vallat.louis.database.SubtitleManager;
|
||||||
import xyz.vallat.louis.discord.DiscordManager;
|
import xyz.vallat.louis.discord.DiscordManager;
|
||||||
|
import xyz.vallat.louis.omdb.objects.Movie;
|
||||||
import xyz.vallat.louis.sockets.CommandsClient;
|
import xyz.vallat.louis.sockets.CommandsClient;
|
||||||
import xyz.vallat.louis.sockets.CommandsServer;
|
import xyz.vallat.louis.sockets.CommandsServer;
|
||||||
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.Subtitle;
|
||||||
|
import xyz.vallat.louis.subtitles.dao.SubtitleBlock;
|
||||||
|
import xyz.vallat.louis.subtitles.parser.SubtitleParser;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
public class MoviesQuoteBot {
|
public class MoviesQuoteBot {
|
||||||
|
|
||||||
@ -32,7 +46,7 @@ public class MoviesQuoteBot {
|
|||||||
options.addOption(Option.builder().longOpt("reimport").build());
|
options.addOption(Option.builder().longOpt("reimport").build());
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) throws ParseException {
|
public static void main(String[] args) throws ParseException, InterruptedException {
|
||||||
CommandLine cmd = new DefaultParser().parse(options, args);
|
CommandLine cmd = new DefaultParser().parse(options, args);
|
||||||
if (cmd.hasOption("reimport")) reimportAllSubtitleLines();
|
if (cmd.hasOption("reimport")) reimportAllSubtitleLines();
|
||||||
|
|
||||||
@ -94,14 +108,44 @@ public class MoviesQuoteBot {
|
|||||||
return isInMaintenanceMode;
|
return isInMaintenanceMode;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void reimportAllSubtitleLines() {
|
private static void reimportAllSubtitleLines() throws InterruptedException {
|
||||||
logger.info("Reimporting all subtitle lines.");
|
logger.info("Reimporting all subtitle lines.");
|
||||||
CommandsClient.checkVersions();
|
CommandsClient.checkVersions();
|
||||||
|
DBManager.testConnection();
|
||||||
|
OpenSubtitles.login();
|
||||||
CommandsClient.setMaintenanceMode(true);
|
CommandsClient.setMaintenanceMode(true);
|
||||||
SubtitleManager.getSubtitles().forEach(subtitle ->
|
SubtitleLineManager.truncateSubtitleLines();
|
||||||
logger.debug("[{}] - '{}' in {}.", subtitle.getId(), subtitle.getMovie(), subtitle.getLang().getEnglish())
|
for (Subtitle subtitle : SubtitleManager.getSubtitles()) {
|
||||||
);
|
importSubtitleLines(subtitle);
|
||||||
|
}
|
||||||
CommandsClient.setMaintenanceMode(false);
|
CommandsClient.setMaintenanceMode(false);
|
||||||
System.exit(0);
|
System.exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static void importSubtitleLines(Subtitle subtitle) throws InterruptedException {
|
||||||
|
try {
|
||||||
|
Stream<SubtitleInfo> subtitleInfoStream = OpenSubtitles.getSubtitleStreamFromMovie(subtitle.getMovie(), subtitle.getLang().getAlpha3b());
|
||||||
|
Optional<SubtitleInfo> subtitleInfo = subtitleInfoStream == null ? Optional.empty() : subtitleInfoStream.findFirst();
|
||||||
|
if (subtitleInfo.isEmpty()) {
|
||||||
|
logger.warn("Didn't find anything for the film '{}' in {}.", subtitle.getMovie(), subtitle.getLang().getEnglish());
|
||||||
|
} else {
|
||||||
|
ListResponse<SubtitleFile> subs = OpenSubtitles.downloadSubtitle(
|
||||||
|
subtitleInfo.get().getSubtitleFileId());
|
||||||
|
if (subs.getStatus().equals(ResponseStatus.OK)) {
|
||||||
|
List<SubtitleBlock> blocks = new SubtitleParser().parseSRT(
|
||||||
|
subs.getData().get(0).getContentAsString(subtitleInfo.get().getEncoding()));
|
||||||
|
SubtitleLineManager.importSubtitleLines(blocks, subtitle.getLang(), subtitle.getMovie(), null, null);
|
||||||
|
} else if (subs.getStatus().getCode() == 429) {
|
||||||
|
logger.warn("Reached requests limit. Retrying in 10 seconds...");
|
||||||
|
Thread.sleep(10000);
|
||||||
|
importSubtitleLines(subtitle);
|
||||||
|
} else {
|
||||||
|
logger.error("Server replied with status: '{}' while trying to download subtitle '{}'. Skipping.",
|
||||||
|
subs.getStatus(), subtitle.getId());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (XmlRpcException e) {
|
||||||
|
logger.error("The OpenSubtitles API had an issue.", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -56,7 +56,7 @@ public class Download extends Command {
|
|||||||
Movie movie = OMDBClient.getMovie(cmd.hasOption("i") ?
|
Movie movie = OMDBClient.getMovie(cmd.hasOption("i") ?
|
||||||
cmd.getOptionValue("i") : String.join(" ", cmd.getOptionValues("t")),
|
cmd.getOptionValue("i") : String.join(" ", cmd.getOptionValues("t")),
|
||||||
cmd.hasOption("i"));
|
cmd.hasOption("i"));
|
||||||
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, language.getId()) > 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.")
|
||||||
|
@ -1,19 +1,24 @@
|
|||||||
package xyz.vallat.louis.database;
|
package xyz.vallat.louis.database;
|
||||||
|
|
||||||
|
import com.github.wtekiela.opensub4j.response.SubtitleInfo;
|
||||||
import discord4j.common.util.Snowflake;
|
import discord4j.common.util.Snowflake;
|
||||||
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.omdb.objects.Movie;
|
import xyz.vallat.louis.omdb.objects.Movie;
|
||||||
|
import xyz.vallat.louis.subtitles.OpenSubtitles;
|
||||||
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.Lang;
|
||||||
import xyz.vallat.louis.subtitles.dao.SubtitleBlock;
|
import xyz.vallat.louis.subtitles.dao.SubtitleBlock;
|
||||||
|
|
||||||
|
import javax.swing.plaf.nimbus.State;
|
||||||
import java.sql.Connection;
|
import java.sql.Connection;
|
||||||
import java.sql.PreparedStatement;
|
import java.sql.PreparedStatement;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.sql.Statement;
|
import java.sql.Statement;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
public final class SubtitleLineManager {
|
public final class SubtitleLineManager {
|
||||||
|
|
||||||
@ -79,6 +84,8 @@ public final class SubtitleLineManager {
|
|||||||
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);
|
||||||
|
int subId = SubtitleManager.getSubtitlesId(movie, language.getId());
|
||||||
|
if (subId < 0) {
|
||||||
String querySubtitles = "INSERT INTO subtitles(film_id, language_id, importer_id, importer_guild_id) " +
|
String querySubtitles = "INSERT INTO subtitles(film_id, language_id, importer_id, importer_guild_id) " +
|
||||||
"VALUES(?, ?, ?, ?);";
|
"VALUES(?, ?, ?, ?);";
|
||||||
try (PreparedStatement stmt = connection.prepareStatement(querySubtitles, PreparedStatement.RETURN_GENERATED_KEYS)) {
|
try (PreparedStatement stmt = connection.prepareStatement(querySubtitles, PreparedStatement.RETURN_GENERATED_KEYS)) {
|
||||||
@ -88,14 +95,16 @@ public final class SubtitleLineManager {
|
|||||||
stmt.setString(4, importer_guild == null ? null : importer_guild.asString());
|
stmt.setString(4, importer_guild == null ? null : importer_guild.asString());
|
||||||
stmt.executeUpdate();
|
stmt.executeUpdate();
|
||||||
stmt.getGeneratedKeys().next();
|
stmt.getGeneratedKeys().next();
|
||||||
int insertedSub = stmt.getGeneratedKeys().getInt(1);
|
subId = stmt.getGeneratedKeys().getInt(1);
|
||||||
logger.debug("Inserted subtitle with id '{}'.", insertedSub);
|
logger.debug("Inserted subtitle with id '{}'.", subId);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
String querySubtitleLines = "INSERT INTO subtitle_lines(subtitle_id, dialog_line, time_code) " +
|
String querySubtitleLines = "INSERT INTO subtitle_lines(subtitle_id, dialog_line, time_code) " +
|
||||||
"VALUES(?, ?, ?);";
|
"VALUES(?, ?, ?);";
|
||||||
for (SubtitleBlock block : subtitleBlocks) {
|
for (SubtitleBlock block : subtitleBlocks) {
|
||||||
try (PreparedStatement stmtLines = connection.prepareStatement(querySubtitleLines, PreparedStatement.RETURN_GENERATED_KEYS)) {
|
try (PreparedStatement stmtLines = connection.prepareStatement(querySubtitleLines, PreparedStatement.RETURN_GENERATED_KEYS)) {
|
||||||
stmtLines.setInt(1, insertedSub);
|
stmtLines.setInt(1, subId);
|
||||||
stmtLines.setString(2, block.getDialogue());
|
stmtLines.setString(2, block.getDialogue());
|
||||||
stmtLines.setString(3, block.getTimecode());
|
stmtLines.setString(3, block.getTimecode());
|
||||||
stmtLines.executeUpdate();
|
stmtLines.executeUpdate();
|
||||||
@ -103,7 +112,6 @@ public final class SubtitleLineManager {
|
|||||||
logger.debug("Inserted subtitle line with id '{}'.", stmtLines.getGeneratedKeys().getInt(1));
|
logger.debug("Inserted subtitle line with id '{}'.", stmtLines.getGeneratedKeys().getInt(1));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
try {
|
try {
|
||||||
connection.commit();
|
connection.commit();
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
@ -115,6 +123,19 @@ public final class SubtitleLineManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void truncateSubtitleLines() {
|
||||||
|
logger.warn("Flushing subtitle lines.");
|
||||||
|
try (Connection connection = DBManager.getConnection()) {
|
||||||
|
String query = "TRUNCATE subtitle_lines RESTART IDENTITY;";
|
||||||
|
try (Statement stmt = connection.createStatement()){
|
||||||
|
stmt.executeUpdate(query);
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
logger.error("Cannot connect to database right now: {}", e.getMessage());
|
||||||
|
System.exit(ExitCodes.CANNOT_CONNECT_TO_DB.getValue());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public static FilmQuote getRandomLine(Lang language, String imdb, String title, String search) {
|
public static FilmQuote getRandomLine(Lang language, String imdb, String title, String search) {
|
||||||
FilmQuote filmQuote = null;
|
FilmQuote filmQuote = null;
|
||||||
try (Connection connection = DBManager.getConnection()) {
|
try (Connection connection = DBManager.getConnection()) {
|
||||||
|
@ -104,7 +104,7 @@ public final class SubtitleManager {
|
|||||||
if (stmt.getResultSet().next()) {
|
if (stmt.getResultSet().next()) {
|
||||||
logger.debug("Found subtitle with id '{}'.", stmt.getResultSet().getInt("id"));
|
logger.debug("Found subtitle with id '{}'.", stmt.getResultSet().getInt("id"));
|
||||||
return stmt.getResultSet().getInt("id");
|
return stmt.getResultSet().getInt("id");
|
||||||
}
|
} else return -1;
|
||||||
}
|
}
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
logger.error("Cannot connect to database right now: {}", e.getMessage());
|
logger.error("Cannot connect to database right now: {}", e.getMessage());
|
||||||
|
Loading…
Reference in New Issue
Block a user