From 613374276869a3271647794e9cc1fccb3edab220 Mon Sep 17 00:00:00 2001 From: Omertron Date: Thu, 26 Jan 2012 09:38:46 +0000 Subject: [PATCH] Added IMDB ID search --- .../moviejukebox/themoviedb/TheMovieDB.java | 71 ++++++++++++++----- .../moviejukebox/themoviedb/tools/ApiUrl.java | 23 +++++- .../themoviedb/TheMovieDBTest.java | 11 +++ 3 files changed, 83 insertions(+), 22 deletions(-) diff --git a/themoviedbapi/src/main/java/com/moviejukebox/themoviedb/TheMovieDB.java b/themoviedbapi/src/main/java/com/moviejukebox/themoviedb/TheMovieDB.java index 0c711e869..469614cf8 100644 --- a/themoviedbapi/src/main/java/com/moviejukebox/themoviedb/TheMovieDB.java +++ b/themoviedbapi/src/main/java/com/moviejukebox/themoviedb/TheMovieDB.java @@ -26,9 +26,9 @@ import org.codehaus.jackson.map.DeserializationConfig; import org.codehaus.jackson.map.ObjectMapper; /** - * The MovieDB API. - * This is for version 3 of the API as specified here: + * The MovieDB API. This is for version 3 of the API as specified here: * http://help.themoviedb.org/kb/api/about-3 + * * @author stuart.boston */ public class TheMovieDB { @@ -41,7 +41,7 @@ public class TheMovieDB { */ protected static final String TMDB_API_BASE = "http://api.themoviedb.org/3/"; /* - * API Methods + * API Methods */ protected static final ApiUrl TMDB_CONFIG_URL = new ApiUrl("configuration"); protected static final ApiUrl TMDB_SEARCH_MOVIE = new ApiUrl("search/movie"); @@ -83,10 +83,9 @@ public class TheMovieDB { } /** - * Search Movies - * This is a good starting point to start finding movies on TMDb. - * The idea is to be a quick and light method so you can iterate through movies quickly. - * http://help.themoviedb.org/kb/api/search-movies + * Search Movies This is a good starting point to start finding movies on + * TMDb. The idea is to be a quick and light method so you can iterate + * through movies quickly. http://help.themoviedb.org/kb/api/search-movies */ public List searchMovie(String movieName, String language, boolean allResults) { try { @@ -100,8 +99,9 @@ public class TheMovieDB { } /** - * This method is used to retrieve all of the basic movie information. - * It will return the single highest rated poster and backdrop. + * This method is used to retrieve all of the basic movie information. It + * will return the single highest rated poster and backdrop. + * * @param movieId * @param language * @return @@ -117,7 +117,27 @@ public class TheMovieDB { } /** - * This method is used to retrieve all of the alternative titles we have for a particular movie. + * This method is used to retrieve all of the basic movie information. It + * will return the single highest rated poster and backdrop. + * + * @param movieId + * @param language + * @return + */ + public MovieDB getMovieInfoImdb(String imdbId, String language) { + try { + URL url = TMDB_MOVIE_INFO.getIdUrl(imdbId, language); + return mapper.readValue(url, MovieDB.class); + } catch (IOException ex) { + LOGGER.warn("Failed to get movie info: " + ex.getMessage()); + } + return new MovieDB(); + } + + /** + * This method is used to retrieve all of the alternative titles we have for + * a particular movie. + * * @param movieId * @param country * @return @@ -135,6 +155,7 @@ public class TheMovieDB { /** * This method is used to retrieve all of the movie cast information. + * * @param movieId * @return */ @@ -167,7 +188,9 @@ public class TheMovieDB { } /** - * This method should be used when you’re wanting to retrieve all of the images for a particular movie. + * This method should be used when you’re wanting to retrieve all of the + * images for a particular movie. + * * @param movieId * @param language * @return @@ -198,8 +221,9 @@ public class TheMovieDB { } /** - * This method is used to retrieve all of the keywords that have been added to a particular movie. - * Currently, only English keywords exist. + * This method is used to retrieve all of the keywords that have been added + * to a particular movie. Currently, only English keywords exist. + * * @param movieId * @return */ @@ -215,7 +239,9 @@ public class TheMovieDB { } /** - * This method is used to retrieve all of the release and certification data we have for a specific movie. + * This method is used to retrieve all of the release and certification data + * we have for a specific movie. + * * @param movieId * @param language * @return @@ -232,8 +258,9 @@ public class TheMovieDB { } /** - * This method is used to retrieve all of the trailers for a particular movie. - * Supported sites are YouTube and QuickTime. + * This method is used to retrieve all of the trailers for a particular + * movie. Supported sites are YouTube and QuickTime. + * * @param movieId * @param language * @return @@ -263,7 +290,9 @@ public class TheMovieDB { } /** - * This method is used to retrieve a list of the available translations for a specific movie. + * This method is used to retrieve a list of the available translations for + * a specific movie. + * * @param movieId * @return */ @@ -279,8 +308,10 @@ public class TheMovieDB { } /** - * This method is used to retrieve all of the basic information about a movie collection. - * You can get the ID needed for this method by making a getMovieInfo request for the belongs_to_collection. + * This method is used to retrieve all of the basic information about a + * movie collection. You can get the ID needed for this method by making a + * getMovieInfo request for the belongs_to_collection. + * * @param movieId * @param language * @return @@ -296,6 +327,7 @@ public class TheMovieDB { /** * Get the configuration information + * * @return */ public TmdbConfiguration getConfiguration() { @@ -304,6 +336,7 @@ public class TheMovieDB { /** * Generate the full image URL from the size and image path + * * @param imagePath * @param requiredSize * @return diff --git a/themoviedbapi/src/main/java/com/moviejukebox/themoviedb/tools/ApiUrl.java b/themoviedbapi/src/main/java/com/moviejukebox/themoviedb/tools/ApiUrl.java index 0b4b3e1a3..a663c599e 100644 --- a/themoviedbapi/src/main/java/com/moviejukebox/themoviedb/tools/ApiUrl.java +++ b/themoviedbapi/src/main/java/com/moviejukebox/themoviedb/tools/ApiUrl.java @@ -43,6 +43,7 @@ public class ApiUrl { private static final String PARAMETER_PAGE = DELIMITER_SUBSEQUENT + "page="; private static final String DEFAULT_QUERY = ""; private static final int DEFAULT_ID = -1; + private static final int IMDB_ID_TRIGGER = 0; // Use to determine its an IMDB search private static final String DEFAULT_LANGUAGE = ""; private static final String DEFAULT_COUNTRY = ""; private static final int DEFAULT_PAGE = -1; @@ -74,7 +75,7 @@ public class ApiUrl { * @param page * @return */ - private URL getFullUrl(String query, int tmdbId, String language, String country, int page) { + private URL getFullUrl(String query, int tmdbId, String imdbId, String language, String country, int page) { StringBuilder urlString = new StringBuilder(TheMovieDB.getApiBase()); // Get the start of the URL @@ -97,6 +98,11 @@ public class ApiUrl { if (tmdbId > DEFAULT_ID) { urlString.append(tmdbId); } + + // Append the IMDB ID if provided + if (StringUtils.isNotBlank(imdbId)) { + urlString.append(imdbId); + } // Append the suffix of the API URL urlString.append(submethod); @@ -147,7 +153,7 @@ public class ApiUrl { * @return */ public URL getQueryUrl(String query, String language, int page) { - return getFullUrl(query, DEFAULT_ID, language, null, page); + return getFullUrl(query, DEFAULT_ID, DEFAULT_QUERY, language, null, page); } public URL getQueryUrl(String query) { @@ -166,7 +172,7 @@ public class ApiUrl { * @return */ public URL getIdUrl(int tmdbId, String language, String country) { - return getFullUrl(DEFAULT_QUERY, tmdbId, language, country, DEFAULT_PAGE); + return getFullUrl(DEFAULT_QUERY, tmdbId, DEFAULT_QUERY, language, country, DEFAULT_PAGE); } public URL getIdUrl(int tmdbId) { @@ -176,5 +182,16 @@ public class ApiUrl { public URL getIdUrl(int tmdbId, String language) { return getIdUrl(tmdbId, language, DEFAULT_COUNTRY); } + + /** + * Get the movie info for an IMDB ID. + * Note, this is a special case + * @param imdbId + * @param language + * @return + */ + public URL getIdUrl(String imdbId, String language) { + return getFullUrl(DEFAULT_QUERY, DEFAULT_ID, imdbId, language, DEFAULT_COUNTRY, DEFAULT_PAGE); + } } diff --git a/themoviedbapi/src/test/java/com/moviejukebox/themoviedb/TheMovieDBTest.java b/themoviedbapi/src/test/java/com/moviejukebox/themoviedb/TheMovieDBTest.java index 4a7a11944..ddce06de7 100644 --- a/themoviedbapi/src/test/java/com/moviejukebox/themoviedb/TheMovieDBTest.java +++ b/themoviedbapi/src/test/java/com/moviejukebox/themoviedb/TheMovieDBTest.java @@ -217,4 +217,15 @@ public class TheMovieDBTest { String result = tmdb.createImageUrl(movie.getPosterPath(), "original").toString(); assertTrue("Error compiling image URL", !result.isEmpty()); } + + /** + * Test of getMovieInfoImdb method, of class TheMovieDB. + */ + @Test + public void testGetMovieInfoImdb() { + LOGGER.info("getMovieInfoImdb"); + MovieDB result = tmdb.getMovieInfoImdb("tt0076759", "en-US"); + assertTrue("Error getting the movie from IMDB ID", result.getId() == 11); + } + }