Basic OMDB interaction

Signed-off-by: Louis Vallat <louis@louis-vallat.xyz>
This commit is contained in:
Louis Vallat 2020-10-28 13:34:13 +01:00
parent c589c70237
commit 59472b9b24
7 changed files with 150 additions and 1 deletions

View File

@ -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)
);

View File

@ -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'
}

View File

@ -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;

View File

@ -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" +
");";

View File

@ -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;

View File

@ -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<Movie> searchMoviesByTitle(String name) throws IOException, InterruptedException, ParseException {
logger.debug("Searching for '{}'.", name);
List<Movie> 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<String> 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<String> 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();
}
}

View File

@ -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;
}
}