[WIP] Now it is possible to get all subtitles from the database (including language and films) and to print them through the logger.

Signed-off-by: Louis Vallat <louis@louis-vallat.xyz>
This commit is contained in:
Louis Vallat 2020-11-17 00:19:24 +01:00
parent 9890b005fd
commit 4490eff40c
3 changed files with 78 additions and 4 deletions

View File

@ -5,6 +5,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import xyz.vallat.louis.codes.ExitCodes; import xyz.vallat.louis.codes.ExitCodes;
import xyz.vallat.louis.database.DBManager; import xyz.vallat.louis.database.DBManager;
import xyz.vallat.louis.database.SubtitleManager;
import xyz.vallat.louis.discord.DiscordManager; import xyz.vallat.louis.discord.DiscordManager;
import xyz.vallat.louis.sockets.CommandsClient; import xyz.vallat.louis.sockets.CommandsClient;
import xyz.vallat.louis.sockets.CommandsServer; import xyz.vallat.louis.sockets.CommandsServer;
@ -94,8 +95,12 @@ public class MoviesQuoteBot {
} }
private static void reimportAllSubtitleLines() { private static void reimportAllSubtitleLines() {
logger.info("Reimporting all subtitle lines.");
CommandsClient.checkVersions(); CommandsClient.checkVersions();
CommandsClient.setMaintenanceMode(true); CommandsClient.setMaintenanceMode(true);
SubtitleManager.getSubtitles().forEach(subtitle ->
logger.debug("[{}] - '{}' in {}.", subtitle.getId(), subtitle.getMovie(), subtitle.getLang().getEnglish())
);
CommandsClient.setMaintenanceMode(false); CommandsClient.setMaintenanceMode(false);
System.exit(0); System.exit(0);
} }

View File

@ -4,11 +4,12 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import xyz.vallat.louis.codes.ExitCodes; import xyz.vallat.louis.codes.ExitCodes;
import xyz.vallat.louis.omdb.objects.Movie; import xyz.vallat.louis.omdb.objects.Movie;
import xyz.vallat.louis.subtitles.dao.Lang;
import xyz.vallat.louis.subtitles.dao.Subtitle;
import java.sql.Connection; import java.sql.*;
import java.sql.PreparedStatement; import java.util.ArrayList;
import java.sql.SQLException; import java.util.List;
import java.sql.Statement;
public final class SubtitleManager { public final class SubtitleManager {
@ -33,6 +34,46 @@ public final class SubtitleManager {
return -1; return -1;
} }
public static List<Subtitle> getSubtitles() {
logger.debug("Getting all subtitles in database.");
List<Subtitle> subtitles = new ArrayList<>();
try (Connection connection = DBManager.getConnection()) {
try (Statement stmt = connection.createStatement()) {
String query = """
SELECT films.id AS film_id, imdb_id, title, year, film_type, poster_link,
languages.id AS language_id, alpha3_b, alpha3_t, alpha2, english, french, subtitles.id AS subtitle_id
FROM subtitles INNER JOIN films ON subtitles.film_id = films.id
INNER JOIN languages ON subtitles.language_id = languages.id ORDER BY subtitles.id;""";
stmt.executeQuery(query);
while (stmt.getResultSet().next()) {
ResultSet set = stmt.getResultSet();
Movie movie = new Movie(
set.getString("title"),
set.getString("imdb_id"),
set.getString("film_type"),
set.getString("poster_link")
);
movie.setId(set.getInt("film_id"));
if (set.getObject("year") == null) movie.setYear(set.getInt("year"));
Lang lang = new Lang(
set.getInt("language_id"),
set.getString("alpha3_b"),
set.getString("alpha3_t"),
set.getString("alpha2"),
set.getString("english"),
set.getString("french")
);
subtitles.add(new Subtitle(set.getInt("subtitle_id"), movie, lang));
}
return subtitles;
}
} catch (SQLException e) {
logger.error("Could not connect to the database right now. Reason: {}", e.getMessage());
System.exit(ExitCodes.CANNOT_CONNECT_TO_DB.getValue());
}
return null;
}
public static int getNumberOfSubtitlesByGuild(String guild_id) { public static int getNumberOfSubtitlesByGuild(String guild_id) {
logger.debug("Getting the number of subtitles in database from guild '{}'.", guild_id); logger.debug("Getting the number of subtitles in database from guild '{}'.", guild_id);
try (Connection connection = DBManager.getConnection()) { try (Connection connection = DBManager.getConnection()) {

View File

@ -0,0 +1,28 @@
package xyz.vallat.louis.subtitles.dao;
import xyz.vallat.louis.omdb.objects.Movie;
public class Subtitle {
private final int id;
private final Movie movie;
private final Lang lang;
public Subtitle(int id, Movie movie, Lang lang) {
this.id = id;
this.movie = movie;
this.lang = lang;
}
public Movie getMovie() {
return movie;
}
public Lang getLang() {
return lang;
}
public int getId() {
return id;
}
}