diff --git a/src/main/java/xyz/vallat/louis/MoviesQuoteBot.java b/src/main/java/xyz/vallat/louis/MoviesQuoteBot.java index 070dfed..c073cea 100644 --- a/src/main/java/xyz/vallat/louis/MoviesQuoteBot.java +++ b/src/main/java/xyz/vallat/louis/MoviesQuoteBot.java @@ -116,36 +116,10 @@ public class MoviesQuoteBot { CommandsClient.setMaintenanceMode(true); SubtitleLineManager.truncateSubtitleLines(); for (Subtitle subtitle : SubtitleManager.getSubtitles()) { - importSubtitleLines(subtitle); + SubtitleLineManager.importSubtitleLines(subtitle); } CommandsClient.setMaintenanceMode(false); + OpenSubtitles.logout(); System.exit(0); } - - private static void importSubtitleLines(Subtitle subtitle) throws InterruptedException { - try { - Stream subtitleInfoStream = OpenSubtitles.getSubtitleStreamFromMovie(subtitle.getMovie(), subtitle.getLang().getAlpha3b()); - Optional 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 subs = OpenSubtitles.downloadSubtitle( - subtitleInfo.get().getSubtitleFileId()); - if (subs.getStatus().equals(ResponseStatus.OK)) { - List 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); - } - } } diff --git a/src/main/java/xyz/vallat/louis/database/SubtitleLineManager.java b/src/main/java/xyz/vallat/louis/database/SubtitleLineManager.java index 02bad8f..59312b0 100644 --- a/src/main/java/xyz/vallat/louis/database/SubtitleLineManager.java +++ b/src/main/java/xyz/vallat/louis/database/SubtitleLineManager.java @@ -1,7 +1,11 @@ package xyz.vallat.louis.database; +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 discord4j.common.util.Snowflake; +import org.apache.xmlrpc.XmlRpcException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import xyz.vallat.louis.codes.ExitCodes; @@ -9,7 +13,9 @@ 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.Lang; +import xyz.vallat.louis.subtitles.dao.Subtitle; import xyz.vallat.louis.subtitles.dao.SubtitleBlock; +import xyz.vallat.louis.subtitles.parser.SubtitleParser; import javax.swing.plaf.nimbus.State; import java.sql.Connection; @@ -170,4 +176,31 @@ public final class SubtitleLineManager { } return filmQuote; } + + public static void importSubtitleLines(Subtitle subtitle) throws InterruptedException { + try { + Stream subtitleInfoStream = OpenSubtitles.getSubtitleStreamFromMovie(subtitle.getMovie(), subtitle.getLang().getAlpha3b()); + Optional 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 subs = OpenSubtitles.downloadSubtitle( + subtitleInfo.get().getSubtitleFileId()); + if (subs.getStatus().equals(ResponseStatus.OK)) { + List 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); + } + } } diff --git a/src/main/java/xyz/vallat/louis/subtitles/parser/SubtitleParser.java b/src/main/java/xyz/vallat/louis/subtitles/parser/SubtitleParser.java index 9550394..3100d88 100644 --- a/src/main/java/xyz/vallat/louis/subtitles/parser/SubtitleParser.java +++ b/src/main/java/xyz/vallat/louis/subtitles/parser/SubtitleParser.java @@ -1,5 +1,7 @@ package xyz.vallat.louis.subtitles.parser; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import xyz.vallat.louis.subtitles.dao.SubtitleBlock; import java.util.ArrayList; @@ -9,6 +11,7 @@ import java.util.stream.Collectors; public class SubtitleParser { + private static final Logger logger = LoggerFactory.getLogger(SubtitleParser.class.getCanonicalName()); private final List subtitleBlocks = new ArrayList<>(); // TODO: FIND A WAY TO DO THIS THAT HAS A BETTER COMPLEXITY @@ -17,12 +20,13 @@ public class SubtitleParser { String[] blocks = s.split("\n\n"); for (String block : blocks) { List splitBlock = Arrays.stream(block.split("\n")).collect(Collectors.toList()); + String dialogLine = String.join("\n", splitBlock.subList(2, splitBlock.size())); + if (dialogLine.toLowerCase().contains("subtitle")) { + logger.warn("Skipping line due to word 'subtitle' found in it. '{}'", dialogLine); + continue; + } subtitleBlocks.add(new SubtitleBlock(splitBlock.get(0), - splitBlock.get(1), String.join("\n", splitBlock.subList(2, splitBlock.size())))); - } - if (!subtitleBlocks.isEmpty()) { - subtitleBlocks.remove(0); - subtitleBlocks.remove(Math.max(0, subtitleBlocks.size() - 1)); + splitBlock.get(1), dialogLine)); } return subtitleBlocks; }