Created the SubtitleBlock type to contain all information extracted by the subtitles parser(s)

Signed-off-by: Louis Vallat <louis@louis-vallat.xyz>
This commit is contained in:
Louis Vallat 2020-10-28 00:36:26 +01:00
parent 8c217c47ee
commit 89a0d13983
7 changed files with 71 additions and 28 deletions

View File

@ -32,7 +32,7 @@ public class Download extends Command {
String var = subs.getData().get(0).getContentAsString("cp1252");
SubtitleParser subtitleParser = new SubtitleParser();
subtitleParser.parseSRT(var);
return messageChannel.createMessage("Good");
return messageChannel.createMessage("Imported.");
}
)
.then();

View File

@ -13,6 +13,7 @@ import java.sql.Statement;
import static xyz.vallat.louis.database.LanguageManager.importLanguageIfNeeded;
import static xyz.vallat.louis.database.LanguageManager.initializeLanguages;
import static xyz.vallat.louis.database.PropertyManager.initializeProperties;
import static xyz.vallat.louis.database.SubtitleLine.initializeSubtitleLine;
public final class DBManager {
@ -28,7 +29,6 @@ public final class DBManager {
private DBManager() {
}
// TODO: EXIT CODES AS ENUM
public static Connection getConnection() {
try {
return DriverManager.getConnection("jdbc:" + CONNECTION_PREFIX + "://" + HOST + ":" + PORT + "/"
@ -97,21 +97,4 @@ public final class DBManager {
stmt.executeUpdate(query);
}
}
private static void initializeSubtitleLine(Connection connection) throws SQLException {
logger.debug("Creating subtitle_line table.");
try (Statement stmt = connection.createStatement()) {
String query = "CREATE TABLE IF NOT EXISTS subtitle_lines\n" +
"(\n" +
" id int GENERATED ALWAYS AS IDENTITY,\n" +
" subtitle_id int NOT NULL,\n" +
" dialog_line text NOT NULL,\n" +
" time_code text NOT NULL,\n" +
" PRIMARY KEY (id),\n" +
" FOREIGN KEY (subtitle_id)\n" +
" REFERENCES subtitles (id)\n" +
");";
stmt.executeUpdate(query);
}
}
}

View File

@ -138,7 +138,7 @@ public final class LanguageManager {
}
}
public static void initializeLanguages(Connection connection) throws SQLException {
static void initializeLanguages(Connection connection) throws SQLException {
logger.debug("Creating language table.");
try (Statement stmt = connection.createStatement()) {
String query = "CREATE TABLE IF NOT EXISTS languages\n" +

View File

@ -51,7 +51,7 @@ public final class PropertyManager {
return null;
}
public static void initializeProperties(Connection connection) throws SQLException {
static void initializeProperties(Connection connection) throws SQLException {
logger.debug("Creating properties table.");
try (Statement stmt = connection.createStatement()) {
String query = "CREATE TABLE IF NOT EXISTS properties\n" +

View File

@ -0,0 +1,34 @@
package xyz.vallat.louis.database;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
public final class SubtitleLine {
private static final Logger logger = LoggerFactory.getLogger(SubtitleLine.class.getCanonicalName());
private SubtitleLine() {
}
static void initializeSubtitleLine(Connection connection) throws SQLException {
logger.debug("Creating subtitle_line table.");
try (Statement stmt = connection.createStatement()) {
String query = "CREATE TABLE IF NOT EXISTS subtitle_lines\n" +
"(\n" +
" id int GENERATED ALWAYS AS IDENTITY,\n" +
" subtitle_id int NOT NULL,\n" +
" dialog_line text NOT NULL,\n" +
" time_code text NOT NULL,\n" +
" PRIMARY KEY (id),\n" +
" FOREIGN KEY (subtitle_id)\n" +
" REFERENCES subtitles (id)\n" +
");";
stmt.executeUpdate(query);
}
}
}

View File

@ -0,0 +1,26 @@
package xyz.vallat.louis.subtitles.parser;
public class SubtitleBlock {
private final String id;
private final String timecode;
private final String dialogue;
public SubtitleBlock(String id, String timecode, String dialogue) {
this.id = id;
this.timecode = timecode;
this.dialogue = dialogue;
}
public String getId() {
return id;
}
public String getTimecode() {
return timecode;
}
public String getDialogue() {
return dialogue;
}
}

View File

@ -7,20 +7,20 @@ import java.util.stream.Collectors;
public class SubtitleParser {
private final List<String> subtitleText = new ArrayList<>();
private final List<SubtitleBlock> subtitleBlocks = new ArrayList<>();
// TODO: FIND A WAY TO DO THIS THAT HAS A BETTER COMPLEXITY
public List<String> parseSRT(String s) {
public List<SubtitleBlock> parseSRT(String s) {
s = s.replace("\r\n", "\n");
String[] blocks = s.split("\n\n");
for (String block : blocks) {
List<String> splitBlock = Arrays.stream(block.split("\n")).collect(Collectors.toList());
subtitleText.addAll(splitBlock.subList(0, 1));
subtitleText.add(String.join("\n", splitBlock.subList(2, splitBlock.size())));
subtitleBlocks.add(new SubtitleBlock(splitBlock.get(0),
splitBlock.get(1), String.join("\n", splitBlock.subList(2, splitBlock.size()))));
}
subtitleText.remove(0);
subtitleText.remove(subtitleText.size() - 1);
return subtitleText;
subtitleBlocks.remove(0);
subtitleBlocks.remove(subtitleBlocks.size() - 1);
return subtitleBlocks;
}
}