From 43c9660a74b03477e33b67cf5960ac0f929afa77 Mon Sep 17 00:00:00 2001 From: Omertron Date: Sun, 1 Apr 2012 10:11:23 +0000 Subject: [PATCH] fixes issue 13 --- .../moviejukebox/themoviedb/TheMovieDb.java | 109 +++++++++--------- .../themoviedb/TheMovieDbTest.java | 16 ++- 2 files changed, 68 insertions(+), 57 deletions(-) diff --git a/themoviedbapi/src/main/java/com/moviejukebox/themoviedb/TheMovieDb.java b/themoviedbapi/src/main/java/com/moviejukebox/themoviedb/TheMovieDb.java index 6f100e49c..54c3bf0d4 100644 --- a/themoviedbapi/src/main/java/com/moviejukebox/themoviedb/TheMovieDb.java +++ b/themoviedbapi/src/main/java/com/moviejukebox/themoviedb/TheMovieDb.java @@ -39,8 +39,8 @@ public class TheMovieDb { private String apiKey; private TmdbConfiguration tmdbConfig; /* - * API Methods These are not set to static so that multiple instances of the - * API can co-exist + * API Methods: These are not set to static so that multiple instances of + * the API can co-exist */ private static final String BASE_MOVIE = "movie/"; private static final String BASE_PERSON = "person/"; @@ -69,7 +69,7 @@ public class TheMovieDb { */ private final ApiUrl tmdbLatestMovie = new ApiUrl(this, "latest/movie"); private final ApiUrl tmdbNowPlaying = new ApiUrl(this, "movie/now-playing"); - // - Populate Movie List + private final ApiUrl tmdbPopularMovieList = new ApiUrl(this, "movie/popular"); // - Top Rated Movies // Company Info // - Company Info @@ -134,6 +134,45 @@ public class TheMovieDb { WebBrowser.setWebTimeoutRead(read); } + /** + * Compare the MovieDB object with a title & year + * + * @param moviedb The moviedb object to compare too + * @param title The title of the movie to compare + * @param year The year of the movie to compare + * @return True if there is a match, False otherwise. + */ + public static boolean compareMovies(MovieDb moviedb, String title, String year) { + if ((moviedb == null) || (StringUtils.isBlank(title))) { + return false; + } + + if (StringUtils.isNotBlank(year) && !year.equalsIgnoreCase("UNKNOWN") && StringUtils.isNotBlank(moviedb.getReleaseDate())) { + // Compare with year + String movieYear = moviedb.getReleaseDate().substring(0, 4); + if (movieYear.equals(year)) { + if (moviedb.getOriginalTitle().equalsIgnoreCase(title)) { + return true; + } + + if (moviedb.getTitle().equalsIgnoreCase(title)) { + return true; + } + } + } + + // Compare without year + if (moviedb.getOriginalTitle().equalsIgnoreCase(title)) { + return true; + } + + if (moviedb.getTitle().equalsIgnoreCase(title)) { + return true; + } + + return false; + } + /** * 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 @@ -488,7 +527,7 @@ public class TheMovieDb { * * @param personId * @return - * @throws MovieDbException + * @throws MovieDbException */ public Person getPersonInfo(int personId) throws MovieDbException { @@ -510,7 +549,7 @@ public class TheMovieDb { * * @param personId * @return - * @throws MovieDbException + * @throws MovieDbException */ public List getPersonCredits(int personId) throws MovieDbException { @@ -544,7 +583,7 @@ public class TheMovieDb { * * @param personId * @return - * @throws MovieDbException + * @throws MovieDbException */ public List getPersonImages(int personId) throws MovieDbException { @@ -603,59 +642,21 @@ public class TheMovieDb { WrapperResultList resultList = mapper.readValue(webPage, WrapperResultList.class); return resultList.getResults(); } catch (IOException ex) { - LOGGER.warn("Failed to get latest movie: " + ex.getMessage()); + LOGGER.warn("Failed to get now playing movies: " + ex.getMessage()); throw new MovieDbException(MovieDbExceptionType.MAPPING_FAILED, webPage, ex); } } - /** - * This method is used to retrieve the movies currently in theatres. This is - * a curated list that will normally contain 100 movies. The default - * response will return 20 movies. - * - * @return - * @throws MovieDbException - */ - public List getNowPlayingMovies() throws MovieDbException { - return getNowPlayingMovies(""); - } - - /** - * Compare the MovieDB object with a title & year - * - * @param moviedb The moviedb object to compare too - * @param title The title of the movie to compare - * @param year The year of the movie to compare - * @return True if there is a match, False otherwise. - */ - public static boolean compareMovies(MovieDb moviedb, String title, String year) { - if ((moviedb == null) || (StringUtils.isBlank(title))) { - return false; - } - - if (StringUtils.isNotBlank(year) && !year.equalsIgnoreCase("UNKNOWN") && StringUtils.isNotBlank(moviedb.getReleaseDate())) { - // Compare with year - String movieYear = moviedb.getReleaseDate().substring(0, 4); - if (movieYear.equals(year)) { - if (moviedb.getOriginalTitle().equalsIgnoreCase(title)) { - return true; - } - - if (moviedb.getTitle().equalsIgnoreCase(title)) { - return true; - } - } - } - - // Compare without year - if (moviedb.getOriginalTitle().equalsIgnoreCase(title)) { - return true; - } + public List getPopularMovieList(String language) throws MovieDbException { + URL url = tmdbPopularMovieList.getIdUrl("", language); + String webPage = WebBrowser.request(url); - if (moviedb.getTitle().equalsIgnoreCase(title)) { - return true; + try { + WrapperResultList resultList = mapper.readValue(webPage, WrapperResultList.class); + return resultList.getResults(); + } catch (IOException ex) { + LOGGER.warn("Failed to get popular movie list: " + ex.getMessage()); + throw new MovieDbException(MovieDbExceptionType.MAPPING_FAILED, webPage, ex); } - - return false; } } diff --git a/themoviedbapi/src/test/java/com/moviejukebox/themoviedb/TheMovieDbTest.java b/themoviedbapi/src/test/java/com/moviejukebox/themoviedb/TheMovieDbTest.java index f89df73ce..20d835b3f 100644 --- a/themoviedbapi/src/test/java/com/moviejukebox/themoviedb/TheMovieDbTest.java +++ b/themoviedbapi/src/test/java/com/moviejukebox/themoviedb/TheMovieDbTest.java @@ -334,9 +334,19 @@ public class TheMovieDbTest { * Test of getNowPlayingMovies method, of class TheMovieDb. */ @Test - public void testGetNowPlayingMovies() throws Exception { + public void testGetNowPlayingMovies() throws MovieDbException { LOGGER.info("getNowPlayingMovies"); - List results = tmdb.getNowPlayingMovies(); - assertTrue("No now playing movies foind", !results.isEmpty()); + List results = tmdb.getNowPlayingMovies(""); + assertTrue("No now playing movies found", !results.isEmpty()); + } + + /** + * Test of getPopularMovieList method, of class TheMovieDb. + */ + @Test + public void testGetPopularMovieList() throws MovieDbException { + LOGGER.info("getPopularMovieList"); + List results = tmdb.getPopularMovieList(""); + assertTrue("No popular movies found", !results.isEmpty()); } }