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:
parent
af1c4e4cc3
commit
a7c4b8c997
@ -11,7 +11,7 @@ public class MoviesQuoteBot {
|
||||
public static final String PREFIX = "!";
|
||||
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 VERSION = "0.3.2-SNAPSHOT";
|
||||
public static final String VERSION = "0.3.3-SNAPSHOT";
|
||||
private static final Logger logger = LoggerFactory.getLogger(MoviesQuoteBot.class.getCanonicalName());
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
@ -34,32 +34,6 @@ public final class 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 {
|
||||
logger.debug("Getting movie by title '{}'.", value);
|
||||
Movie movie = FilmManager.getMovieFromTitleOrImdbId(value, isId);
|
||||
@ -90,7 +64,8 @@ public final class OMDBClient {
|
||||
|
||||
private static Movie getMovieFromJson(JsonObject jsonMovie) {
|
||||
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(
|
||||
jsonMovie.get(TITLE_KEY).getAsString(),
|
||||
jsonMovie.get(IMDB_KEY).getAsString(),
|
||||
|
Loading…
Reference in New Issue
Block a user