Added a way to import films in database

Signed-off-by: Louis Vallat <louis@louis-vallat.xyz>
This commit is contained in:
Louis Vallat 2020-10-28 14:13:53 +01:00
parent 59472b9b24
commit 9644cfcf0c
9 changed files with 74 additions and 30 deletions

View File

@ -21,7 +21,7 @@ CREATE TABLE IF NOT EXISTS films
imdb_id varchar(10) NOT NULL, imdb_id varchar(10) NOT NULL,
title text NOT NULL, title text NOT NULL,
release_date date, release_date date,
type text NOT NULL, film_type text NOT NULL,
season int, season int,
episode int, episode int,
poster_link text, poster_link text,

View File

@ -12,10 +12,12 @@ import org.slf4j.LoggerFactory;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
import xyz.vallat.louis.commands.*; import xyz.vallat.louis.commands.*;
import xyz.vallat.louis.database.DBManager; import xyz.vallat.louis.database.DBManager;
import xyz.vallat.louis.database.exceptions.ImportationException;
import xyz.vallat.louis.env.EnvironmentVariables; import xyz.vallat.louis.env.EnvironmentVariables;
import xyz.vallat.louis.subtitles.OpenSubtitles; import xyz.vallat.louis.subtitles.OpenSubtitles;
import java.io.IOException; import java.io.IOException;
import java.text.ParseException;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;

View File

@ -10,10 +10,11 @@ import java.sql.DriverManager;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Statement; import java.sql.Statement;
import static xyz.vallat.louis.database.FilmManager.initializeFilm;
import static xyz.vallat.louis.database.LanguageManager.importLanguageIfNeeded; import static xyz.vallat.louis.database.LanguageManager.importLanguageIfNeeded;
import static xyz.vallat.louis.database.LanguageManager.initializeLanguages; import static xyz.vallat.louis.database.LanguageManager.initializeLanguages;
import static xyz.vallat.louis.database.PropertyManager.initializeProperties; import static xyz.vallat.louis.database.PropertyManager.initializeProperties;
import static xyz.vallat.louis.database.SubtitleLine.initializeSubtitleLine; import static xyz.vallat.louis.database.SubtitleLineManager.initializeSubtitleLine;
public final class DBManager { public final class DBManager {
@ -61,25 +62,6 @@ public final class DBManager {
} }
} }
private static void initializeFilm(Connection connection) throws SQLException {
logger.debug("Creating film table.");
try (Statement stmt = connection.createStatement()) {
String query = "CREATE TABLE IF NOT EXISTS films\n" +
"(\n" +
" id int GENERATED ALWAYS AS IDENTITY,\n" +
" imdb_id varchar(10) NOT NULL,\n" +
" title text NOT NULL,\n" +
" release_date date,\n" +
" type text NOT NULL,\n" +
" season int,\n" +
" episode int,\n" +
" poster_link text,\n" +
" PRIMARY KEY (id)\n" +
");";
stmt.executeUpdate(query);
}
}
private static void initializeSubtitle(Connection connection) throws SQLException { private static void initializeSubtitle(Connection connection) throws SQLException {
logger.debug("Creating subtitle table."); logger.debug("Creating subtitle table.");
try (Statement stmt = connection.createStatement()) { try (Statement stmt = connection.createStatement()) {

View File

@ -0,0 +1,55 @@
package xyz.vallat.louis.database;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import xyz.vallat.louis.codes.ExitCodes;
import xyz.vallat.louis.database.exceptions.ImportationException;
import xyz.vallat.louis.omdb.objects.Movie;
import java.sql.*;
import java.time.ZoneId;
public final class FilmManager {
private static final Logger logger = LoggerFactory.getLogger(FilmManager.class.getCanonicalName());
static void initializeFilm(Connection connection) throws SQLException {
logger.debug("Creating film table.");
try (Statement stmt = connection.createStatement()) {
String query = "CREATE TABLE IF NOT EXISTS films\n" +
"(\n" +
" id int GENERATED ALWAYS AS IDENTITY,\n" +
" imdb_id varchar(10) NOT NULL,\n" +
" title text NOT NULL,\n" +
" release_date date,\n" +
" film_type text NOT NULL,\n" +
" season int,\n" +
" episode int,\n" +
" poster_link text,\n" +
" PRIMARY KEY (id)\n" +
");";
stmt.executeUpdate(query);
}
}
public static void importFilm(Movie movie) throws ImportationException {
try (Connection connection = DBManager.getConnection()) {
String insert = "INSERT INTO films(imdb_id, title, release_date, film_type, poster_link) VALUES(?, ?, ?, ?, ?);";
try (PreparedStatement stmt = connection.prepareStatement(insert)) {
stmt.setString(1, movie.getImdbID());
stmt.setString(2, movie.getTitle());
stmt.setDate(3, Date.valueOf(movie.getReleased().atZone(ZoneId.of("UTC")).toLocalDate()));
stmt.setString(4, movie.getType());
stmt.setString(5, movie.getPoster());
stmt.executeUpdate();
} catch (SQLException e) {
logger.error("Error while importing movie: {}", e.getMessage());
throw new ImportationException();
}
} catch (SQLException e) {
logger.error("Cannot connect to database right now. Reason: {}", e.getMessage());
System.exit(ExitCodes.CANNOT_CONNECT_TO_DB.getValue());
}
}
}

View File

@ -29,7 +29,7 @@ public final class LanguageManager {
private LanguageManager() { private LanguageManager() {
} }
public static void importLanguageIfNeeded(Connection connection) { static void importLanguageIfNeeded(Connection connection) {
String storedHash = null; String storedHash = null;
try { try {
logger.debug("Checking if we need to import languages again."); logger.debug("Checking if we need to import languages again.");

View File

@ -7,11 +7,11 @@ import java.sql.Connection;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Statement; import java.sql.Statement;
public final class SubtitleLine { public final class SubtitleLineManager {
private static final Logger logger = LoggerFactory.getLogger(SubtitleLine.class.getCanonicalName()); private static final Logger logger = LoggerFactory.getLogger(SubtitleLineManager.class.getCanonicalName());
private SubtitleLine() { private SubtitleLineManager() {
} }
static void initializeSubtitleLine(Connection connection) throws SQLException { static void initializeSubtitleLine(Connection connection) throws SQLException {

View File

@ -0,0 +1,4 @@
package xyz.vallat.louis.database.exceptions;
public class ImportationException extends Exception {
}

View File

@ -51,7 +51,7 @@ public final class OMDBClient {
if (jsonMovie.get(IMDB_KEY) == null || jsonMovie.get(TITLE_KEY) == null) continue; if (jsonMovie.get(IMDB_KEY) == null || jsonMovie.get(TITLE_KEY) == null) continue;
movies.add(new Movie( movies.add(new Movie(
jsonMovie.get(TITLE_KEY).getAsString(), jsonMovie.get(TITLE_KEY).getAsString(),
DATE_FORMAT.parse(jsonMovie.get(RELEASED_KEY).getAsString()), DATE_FORMAT.parse(jsonMovie.get(RELEASED_KEY).getAsString()).toInstant(),
jsonMovie.get(IMDB_KEY).getAsString(), jsonMovie.get(IMDB_KEY).getAsString(),
jsonMovie.get(TYPE_KEY).getAsString(), jsonMovie.get(TYPE_KEY).getAsString(),
jsonMovie.get(POSTER_KEY).getAsString().equals("N/A") ? null : jsonMovie.get(POSTER_KEY).getAsString() jsonMovie.get(POSTER_KEY).getAsString().equals("N/A") ? null : jsonMovie.get(POSTER_KEY).getAsString()
@ -79,7 +79,7 @@ public final class OMDBClient {
if (jsonMovie.get(IMDB_KEY) != null && jsonMovie.get(TITLE_KEY) != null) { if (jsonMovie.get(IMDB_KEY) != null && jsonMovie.get(TITLE_KEY) != null) {
movie = new Movie( movie = new Movie(
jsonMovie.get(TITLE_KEY).getAsString(), jsonMovie.get(TITLE_KEY).getAsString(),
DATE_FORMAT.parse(jsonMovie.get(RELEASED_KEY).getAsString()), DATE_FORMAT.parse(jsonMovie.get(RELEASED_KEY).getAsString()).toInstant(),
jsonMovie.get(IMDB_KEY).getAsString(), jsonMovie.get(IMDB_KEY).getAsString(),
jsonMovie.get(TYPE_KEY).getAsString(), jsonMovie.get(TYPE_KEY).getAsString(),
jsonMovie.get(POSTER_KEY).getAsString().equals("N/A") ? null : jsonMovie.get(POSTER_KEY).getAsString() jsonMovie.get(POSTER_KEY).getAsString().equals("N/A") ? null : jsonMovie.get(POSTER_KEY).getAsString()

View File

@ -1,16 +1,17 @@
package xyz.vallat.louis.omdb.objects; package xyz.vallat.louis.omdb.objects;
import java.time.Instant;
import java.util.Date; import java.util.Date;
public class Movie { public class Movie {
private final String title; private final String title;
private final Date released; private final Instant released;
private final String imdbID; private final String imdbID;
private final String type; private final String type;
private final String poster; private final String poster;
public Movie(String title, Date released, String imdbID, String type, String poster) { public Movie(String title, Instant released, String imdbID, String type, String poster) {
this.title = title; this.title = title;
this.released = released; this.released = released;
this.imdbID = imdbID; this.imdbID = imdbID;
@ -22,7 +23,7 @@ public class Movie {
return title; return title;
} }
public Date getReleased() { public Instant getReleased() {
return released; return released;
} }