Added more checks because if you search for a movie with an imdb id of a series, then it gives you... a series.

Signed-off-by: Louis Vallat <louis@louis-vallat.xyz>
This commit is contained in:
Louis Vallat 2020-11-05 21:50:02 +01:00
parent af1c4e4cc3
commit a7c4b8c997
2 changed files with 3 additions and 28 deletions

View File

@ -11,7 +11,7 @@ public class MoviesQuoteBot {
public static final String PREFIX = "!"; public static final String PREFIX = "!";
public static final String NAME = "Movies Quote Bot"; public static final String NAME = "Movies Quote Bot";
public static final String DESCRIPTION = "I may know some quotes from some movies."; public static final String DESCRIPTION = "I may know some quotes from some movies.";
public static final String VERSION = "0.3.2-SNAPSHOT"; public static final String VERSION = "0.3.3-SNAPSHOT";
private static final Logger logger = LoggerFactory.getLogger(MoviesQuoteBot.class.getCanonicalName()); private static final Logger logger = LoggerFactory.getLogger(MoviesQuoteBot.class.getCanonicalName());
public static void main(String[] args) { public static void main(String[] args) {

View File

@ -34,32 +34,6 @@ public final class OMDBClient {
private OMDBClient() { private OMDBClient() {
} }
public static List<Movie> searchMoviesByTitle(String name) throws IOException, InterruptedException {
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();
logger.debug("Calling url '{}'.", request.uri());
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;
Movie movie = getMovieFromJson(jsonMovie);
if (movie == null) continue;
movies.add(movie);
}
} 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 value, boolean isId) throws IOException { public static Movie getMovie(String value, boolean isId) throws IOException {
logger.debug("Getting movie by title '{}'.", value); logger.debug("Getting movie by title '{}'.", value);
Movie movie = FilmManager.getMovieFromTitleOrImdbId(value, isId); Movie movie = FilmManager.getMovieFromTitleOrImdbId(value, isId);
@ -90,7 +64,8 @@ public final class OMDBClient {
private static Movie getMovieFromJson(JsonObject jsonMovie) { private static Movie getMovieFromJson(JsonObject jsonMovie) {
Movie movie = null; Movie movie = null;
if (jsonMovie.get(IMDB_KEY) != null && jsonMovie.get(TITLE_KEY) != null) { if (jsonMovie.get(IMDB_KEY) != null && jsonMovie.get(TITLE_KEY) != null &&
jsonMovie.get(TYPE_KEY) != null && jsonMovie.get(TYPE_KEY).getAsString().equalsIgnoreCase("movie")) {
movie = new Movie( movie = new Movie(
jsonMovie.get(TITLE_KEY).getAsString(), jsonMovie.get(TITLE_KEY).getAsString(),
jsonMovie.get(IMDB_KEY).getAsString(), jsonMovie.get(IMDB_KEY).getAsString(),