diff --git a/src/main/java/xyz/vallat/louis/commands/Download.java b/src/main/java/xyz/vallat/louis/commands/Download.java index 350e1ed..a51e14f 100644 --- a/src/main/java/xyz/vallat/louis/commands/Download.java +++ b/src/main/java/xyz/vallat/louis/commands/Download.java @@ -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(); diff --git a/src/main/java/xyz/vallat/louis/database/DBManager.java b/src/main/java/xyz/vallat/louis/database/DBManager.java index 23fe42f..d4e4abd 100644 --- a/src/main/java/xyz/vallat/louis/database/DBManager.java +++ b/src/main/java/xyz/vallat/louis/database/DBManager.java @@ -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); - } - } } diff --git a/src/main/java/xyz/vallat/louis/database/LanguageManager.java b/src/main/java/xyz/vallat/louis/database/LanguageManager.java index 387dc66..47fa8ae 100644 --- a/src/main/java/xyz/vallat/louis/database/LanguageManager.java +++ b/src/main/java/xyz/vallat/louis/database/LanguageManager.java @@ -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" + diff --git a/src/main/java/xyz/vallat/louis/database/PropertyManager.java b/src/main/java/xyz/vallat/louis/database/PropertyManager.java index 7dfdcc9..e2139f2 100644 --- a/src/main/java/xyz/vallat/louis/database/PropertyManager.java +++ b/src/main/java/xyz/vallat/louis/database/PropertyManager.java @@ -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" + diff --git a/src/main/java/xyz/vallat/louis/database/SubtitleLine.java b/src/main/java/xyz/vallat/louis/database/SubtitleLine.java new file mode 100644 index 0000000..a3bc698 --- /dev/null +++ b/src/main/java/xyz/vallat/louis/database/SubtitleLine.java @@ -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); + } + } + +} diff --git a/src/main/java/xyz/vallat/louis/subtitles/parser/SubtitleBlock.java b/src/main/java/xyz/vallat/louis/subtitles/parser/SubtitleBlock.java new file mode 100644 index 0000000..8c799fe --- /dev/null +++ b/src/main/java/xyz/vallat/louis/subtitles/parser/SubtitleBlock.java @@ -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; + } +} 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 1f1ba4c..f01c56b 100644 --- a/src/main/java/xyz/vallat/louis/subtitles/parser/SubtitleParser.java +++ b/src/main/java/xyz/vallat/louis/subtitles/parser/SubtitleParser.java @@ -7,20 +7,20 @@ import java.util.stream.Collectors; public class SubtitleParser { - private final List subtitleText = new ArrayList<>(); + private final List subtitleBlocks = new ArrayList<>(); // TODO: FIND A WAY TO DO THIS THAT HAS A BETTER COMPLEXITY - public List parseSRT(String s) { + public List parseSRT(String s) { s = s.replace("\r\n", "\n"); String[] blocks = s.split("\n\n"); for (String block : blocks) { List 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; } }