[WIP] Cleaned code a bit and added a better check to removed subtitle ads
Signed-off-by: Louis Vallat <louis@louis-vallat.xyz>
This commit is contained in:
parent
245c9091b7
commit
5446e6af38
@ -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<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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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<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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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<SubtitleBlock> 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<String> splitBlock = Arrays.stream(block.split("\n")).collect(Collectors.toList());
|
||||
subtitleBlocks.add(new SubtitleBlock(splitBlock.get(0),
|
||||
splitBlock.get(1), String.join("\n", splitBlock.subList(2, splitBlock.size()))));
|
||||
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;
|
||||
}
|
||||
if (!subtitleBlocks.isEmpty()) {
|
||||
subtitleBlocks.remove(0);
|
||||
subtitleBlocks.remove(Math.max(0, subtitleBlocks.size() - 1));
|
||||
subtitleBlocks.add(new SubtitleBlock(splitBlock.get(0),
|
||||
splitBlock.get(1), dialogLine));
|
||||
}
|
||||
return subtitleBlocks;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user