diff --git a/MoviesQuoteBot.sql b/MoviesQuoteBot.sql index ba8e27a..7df2f0a 100644 --- a/MoviesQuoteBot.sql +++ b/MoviesQuoteBot.sql @@ -21,6 +21,9 @@ CREATE TABLE IF NOT EXISTS films imdb_id varchar(10) NOT NULL, title text NOT NULL, release_date date, + type text NOT NULL, + season int, + episode int, poster_link text, PRIMARY KEY (id) ); diff --git a/build.gradle b/build.gradle index 0b524be..99afd5c 100644 --- a/build.gradle +++ b/build.gradle @@ -37,4 +37,5 @@ dependencies { implementation 'org.apache.commons:commons-csv:1.8' implementation 'ch.qos.logback:logback-classic:1.2.3' implementation 'org.postgresql:postgresql:42.2.18.jre7' + implementation 'com.google.code.gson:gson:2.8.6' } \ No newline at end of file diff --git a/src/main/java/xyz/vallat/louis/MovieQuoteBot.java b/src/main/java/xyz/vallat/louis/MovieQuoteBot.java index a9010e5..0bb52b2 100644 --- a/src/main/java/xyz/vallat/louis/MovieQuoteBot.java +++ b/src/main/java/xyz/vallat/louis/MovieQuoteBot.java @@ -15,6 +15,7 @@ import xyz.vallat.louis.database.DBManager; import xyz.vallat.louis.env.EnvironmentVariables; import xyz.vallat.louis.subtitles.OpenSubtitles; +import java.io.IOException; import java.util.HashMap; import java.util.Map; diff --git a/src/main/java/xyz/vallat/louis/database/DBManager.java b/src/main/java/xyz/vallat/louis/database/DBManager.java index d4e4abd..a2461d5 100644 --- a/src/main/java/xyz/vallat/louis/database/DBManager.java +++ b/src/main/java/xyz/vallat/louis/database/DBManager.java @@ -70,6 +70,9 @@ public final class DBManager { " 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" + ");"; diff --git a/src/main/java/xyz/vallat/louis/env/EnvironmentVariables.java b/src/main/java/xyz/vallat/louis/env/EnvironmentVariables.java index 218d84d..18165ef 100644 --- a/src/main/java/xyz/vallat/louis/env/EnvironmentVariables.java +++ b/src/main/java/xyz/vallat/louis/env/EnvironmentVariables.java @@ -9,7 +9,8 @@ public enum EnvironmentVariables { DB_PORT("DB_PORT"), OS_USERNAME("OPEN_SUBTITLES_USERNAME"), OS_PASSWORD("OPEN_SUBTITLES_PASSWORD"), - OS_USER_AGENT("OPEN_SUBTITLES_USER_AGENT"); + OS_USER_AGENT("OPEN_SUBTITLES_USER_AGENT"), + OMDB_API_KEY("OMDB_API_KEY"); private final String value; diff --git a/src/main/java/xyz/vallat/louis/omdb/OMDBClient.java b/src/main/java/xyz/vallat/louis/omdb/OMDBClient.java new file mode 100644 index 0000000..dbef0d9 --- /dev/null +++ b/src/main/java/xyz/vallat/louis/omdb/OMDBClient.java @@ -0,0 +1,100 @@ +package xyz.vallat.louis.omdb; + +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import xyz.vallat.louis.env.EnvironmentVariables; +import xyz.vallat.louis.omdb.objects.Movie; + +import java.io.IOException; +import java.net.URI; +import java.net.URLEncoder; +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; +import java.nio.charset.StandardCharsets; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.List; + +public final class OMDBClient { + + private static final Logger logger = LoggerFactory.getLogger(OMDBClient.class.getCanonicalName()); + private static final String ENDPOINT = "https://www.omdbapi.com/"; + private static final String API_KEY = System.getenv(EnvironmentVariables.OMDB_API_KEY.getValue()); + private static final String IMDB_KEY = "imdbID"; + private static final String TITLE_KEY = "Title"; + private static final String RELEASED_KEY = "Released"; + private static final String POSTER_KEY = "Poster"; + private static final String TYPE_KEY = "Type"; + private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("dd MMM yyyy"); + + private OMDBClient() { + } + + public static List searchMoviesByTitle(String name) throws IOException, InterruptedException, ParseException { + logger.debug("Searching for '{}'.", name); + List movies = new ArrayList<>(); + HttpRequest request = HttpRequest.newBuilder() + .GET() + .uri(URI.create(ENDPOINT + "?apikey=" + API_KEY + "&type=movie&s=" + URLEncoder.encode(name, StandardCharsets.UTF_8))) + .build(); + HttpResponse response = getHttpClient().send(request, HttpResponse.BodyHandlers.ofString()); + if (response.statusCode() == 200) { + JsonObject object = JsonParser.parseString(response.body()).getAsJsonObject(); + if (object.get("Response").getAsBoolean()) { + for (JsonElement jsonElement : object.get("Search").getAsJsonArray()) { + JsonObject jsonMovie = (JsonObject) jsonElement; + if (jsonMovie.get(IMDB_KEY) == null || jsonMovie.get(TITLE_KEY) == null) continue; + movies.add(new Movie( + jsonMovie.get(TITLE_KEY).getAsString(), + DATE_FORMAT.parse(jsonMovie.get(RELEASED_KEY).getAsString()), + jsonMovie.get(IMDB_KEY).getAsString(), + jsonMovie.get(TYPE_KEY).getAsString(), + jsonMovie.get(POSTER_KEY).getAsString().equals("N/A") ? null : jsonMovie.get(POSTER_KEY).getAsString() + )); + } + } else + logger.error("OMDB API returned an error: {}", object.get("Error").getAsString()); + } else + logger.error("Querying the results gave this code: {}.", response.statusCode()); + return movies; + } + + public static Movie getMovie(String name, boolean id) throws IOException, InterruptedException, ParseException { + logger.debug("Getting movie by title '{}'.", name); + Movie movie = null; + HttpRequest request = HttpRequest.newBuilder() + .GET() + .uri(URI.create(ENDPOINT + "?apikey=" + API_KEY + "&type=movie" + + "&" + (id ? "i" : "t") + "=" + URLEncoder.encode(name, StandardCharsets.UTF_8))) + .build(); + HttpResponse response = getHttpClient().send(request, HttpResponse.BodyHandlers.ofString()); + if (response.statusCode() == 200) { + JsonObject jsonMovie = JsonParser.parseString(response.body()).getAsJsonObject(); + if (jsonMovie.get("Response").getAsBoolean()) { + if (jsonMovie.get(IMDB_KEY) != null && jsonMovie.get(TITLE_KEY) != null) { + movie = new Movie( + jsonMovie.get(TITLE_KEY).getAsString(), + DATE_FORMAT.parse(jsonMovie.get(RELEASED_KEY).getAsString()), + jsonMovie.get(IMDB_KEY).getAsString(), + jsonMovie.get(TYPE_KEY).getAsString(), + jsonMovie.get(POSTER_KEY).getAsString().equals("N/A") ? null : jsonMovie.get(POSTER_KEY).getAsString() + ); + } + } else + logger.error("OMDB API returned an error: {}", jsonMovie.get("Error").getAsString()); + } else + logger.error("Querying the results gave this code: {}.", response.statusCode()); + return movie; + } + + private static HttpClient getHttpClient() { + return HttpClient.newBuilder() + .version(HttpClient.Version.HTTP_2) + .build(); + } +} diff --git a/src/main/java/xyz/vallat/louis/omdb/objects/Movie.java b/src/main/java/xyz/vallat/louis/omdb/objects/Movie.java new file mode 100644 index 0000000..cffc39b --- /dev/null +++ b/src/main/java/xyz/vallat/louis/omdb/objects/Movie.java @@ -0,0 +1,40 @@ +package xyz.vallat.louis.omdb.objects; + +import java.util.Date; + +public class Movie { + + private final String title; + private final Date released; + private final String imdbID; + private final String type; + private final String poster; + + public Movie(String title, Date released, String imdbID, String type, String poster) { + this.title = title; + this.released = released; + this.imdbID = imdbID; + this.type = type; + this.poster = poster; + } + + public String getTitle() { + return title; + } + + public Date getReleased() { + return released; + } + + public String getImdbID() { + return imdbID; + } + + public String getType() { + return type; + } + + public String getPoster() { + return poster; + } +}