From 3e0758a63439e739d8223e27353d5cb36f022919 Mon Sep 17 00:00:00 2001 From: Louis Vallat Date: Tue, 27 Oct 2020 11:43:10 +0100 Subject: [PATCH] Basic POC for cutting subtitles in their separate dialog lines Signed-off-by: Louis Vallat --- build.gradle | 1 + .../xyz/vallat/louis/commands/Download.java | 5 +++- .../subtitles/parser/SubtitleParser.java | 26 +++++++++++++++++++ 3 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 src/main/java/xyz/vallat/louis/subtitles/parser/SubtitleParser.java diff --git a/build.gradle b/build.gradle index 5c172fb..89fc9c6 100644 --- a/build.gradle +++ b/build.gradle @@ -34,5 +34,6 @@ dependencies { implementation 'com.discord4j:discord4j-core:3.1.1' implementation 'com.github.wtekiela:opensub4j:0.3.0' implementation 'org.apache.commons:commons-lang3:3.11' + implementation 'com.github.dnbn.submerge:submerge-api:1.9.2' implementation 'ch.qos.logback:logback-classic:1.2.3' } \ No newline at end of file diff --git a/src/main/java/xyz/vallat/louis/commands/Download.java b/src/main/java/xyz/vallat/louis/commands/Download.java index 84f29c3..350e1ed 100644 --- a/src/main/java/xyz/vallat/louis/commands/Download.java +++ b/src/main/java/xyz/vallat/louis/commands/Download.java @@ -8,6 +8,7 @@ import org.apache.commons.lang3.StringUtils; import org.apache.xmlrpc.XmlRpcException; import reactor.core.publisher.Mono; import xyz.vallat.louis.subtitles.OpenSubtitles; +import xyz.vallat.louis.subtitles.parser.SubtitleParser; public class Download extends Command { @@ -28,7 +29,9 @@ public class Download extends Command { return event.getMessage().getChannel() .flatMap(messageChannel -> { - System.out.println(subs.getData().get(0).getContentAsString("cp1252")); + String var = subs.getData().get(0).getContentAsString("cp1252"); + SubtitleParser subtitleParser = new SubtitleParser(); + subtitleParser.parseSRT(var); return messageChannel.createMessage("Good"); } ) diff --git a/src/main/java/xyz/vallat/louis/subtitles/parser/SubtitleParser.java b/src/main/java/xyz/vallat/louis/subtitles/parser/SubtitleParser.java new file mode 100644 index 0000000..b2ad23c --- /dev/null +++ b/src/main/java/xyz/vallat/louis/subtitles/parser/SubtitleParser.java @@ -0,0 +1,26 @@ +package xyz.vallat.louis.subtitles.parser; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + +public class SubtitleParser { + + private final List subtitleText = new ArrayList<>(); + + // TODO: FIND A WAY TO DO THIS THAT HAS A BETTER COMPLEXITY + public void parseSRT(String s) { + s = s.replace("\r\n", "\n"); + String[] blocks = s.split("\n\n"); + for (String block : blocks) { + List splittedBlock = Arrays.stream(block.split("\n")).collect(Collectors.toList()); + subtitleText.add(splittedBlock.subList(2, splittedBlock.size()).stream().collect(Collectors.joining("\n"))); + } + + for(String st : subtitleText) { + System.out.println("TEXT:\n" + st + "\n\n"); + } + } + +}