From 6231130d462980401a8e636c9b07cfc813a54f60 Mon Sep 17 00:00:00 2001 From: Stuart Boston Date: Wed, 25 Feb 2015 21:35:02 +0000 Subject: [PATCH] Genres --- .../themoviedbapi/methods/TmdbGenres.java | 57 +++++++++++-------- .../omertron/themoviedbapi/tools/Param.java | 1 + .../themoviedbapi/methods/TmdbGenresTest.java | 44 ++++++++++---- 3 files changed, 67 insertions(+), 35 deletions(-) diff --git a/src/main/java/com/omertron/themoviedbapi/methods/TmdbGenres.java b/src/main/java/com/omertron/themoviedbapi/methods/TmdbGenres.java index d714ef499..3937ec2cf 100644 --- a/src/main/java/com/omertron/themoviedbapi/methods/TmdbGenres.java +++ b/src/main/java/com/omertron/themoviedbapi/methods/TmdbGenres.java @@ -21,7 +21,7 @@ package com.omertron.themoviedbapi.methods; import com.omertron.themoviedbapi.MovieDbException; import com.omertron.themoviedbapi.model.Genre; -import com.omertron.themoviedbapi.model.MovieDb; +import com.omertron.themoviedbapi.model2.movie.MovieBasic; import com.omertron.themoviedbapi.results.TmdbResultsList; import com.omertron.themoviedbapi.tools.ApiUrl; import com.omertron.themoviedbapi.tools.HttpTools; @@ -30,9 +30,9 @@ import com.omertron.themoviedbapi.tools.MethodSub; import com.omertron.themoviedbapi.tools.Param; import com.omertron.themoviedbapi.tools.TmdbParameters; import com.omertron.themoviedbapi.wrapper.WrapperGenres; -import com.omertron.themoviedbapi.wrapper.WrapperMovie; import java.io.IOException; import java.net.URL; +import java.util.List; import org.yamj.api.common.exception.ApiExceptionType; /** @@ -53,19 +53,39 @@ public class TmdbGenres extends AbstractMethod { } /** - * You can use this method to retrieve the list of genres used on TMDb. - * - * These IDs will correspond to those found in movie calls. + * Get the list of movie genres. * * @param language * @return * @throws MovieDbException */ - public TmdbResultsList getGenreList(String language) throws MovieDbException { + public TmdbResultsList getGenreMovieList(String language) throws MovieDbException { + return getGenreList(language, MethodSub.MOVIE_LIST); + } + + /** + * Get the list of TV genres. + * + * @param language + * @return + * @throws MovieDbException + */ + public TmdbResultsList getGenreTVList(String language) throws MovieDbException { + return getGenreList(language, MethodSub.TV_LIST); + } + + /** + * Get the list of genres for movies or TV + * @param language + * @param sub + * @return + * @throws MovieDbException + */ + private TmdbResultsList getGenreList(String language, MethodSub sub) throws MovieDbException { TmdbParameters parameters = new TmdbParameters(); parameters.add(Param.LANGUAGE, language); - URL url = new ApiUrl(apiKey, MethodBase.GENRE).setSubMethod(MethodSub.LIST).buildUrl(parameters); + URL url = new ApiUrl(apiKey, MethodBase.GENRE).setSubMethod(sub).buildUrl(parameters); String webpage = httpTools.getRequest(url); try { @@ -74,43 +94,34 @@ public class TmdbGenres extends AbstractMethod { results.copyWrapper(wrapper); return results; } catch (IOException ex) { - throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, "Failed to get genre list", url, ex); + throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, "Failed to get genre " + sub.toString(), url, ex); } } /** - * Get a list of movies per genre. + * Get the list of movies for a particular genre by id. * - * It is important to understand that only movies with more than 10 votes - * get listed. - * - * This prevents movies from 1 10/10 rating from being listed first and for - * the first 5 pages. + * By default, only movies with 10 or more votes are included. * * @param genreId * @param language * @param page * @param includeAllMovies + * @param includeAdult * @return * @throws MovieDbException */ - public TmdbResultsList getGenreMovies(int genreId, String language, int page, boolean includeAllMovies) throws MovieDbException { + public List getGenreMovies(int genreId, String language, Integer page, Boolean includeAllMovies, Boolean includeAdult) throws MovieDbException { TmdbParameters parameters = new TmdbParameters(); parameters.add(Param.ID, genreId); parameters.add(Param.LANGUAGE, language); parameters.add(Param.PAGE, page); parameters.add(Param.INCLUDE_ALL_MOVIES, includeAllMovies); + parameters.add(Param.INCLUDE_ADULT, includeAdult); URL url = new ApiUrl(apiKey, MethodBase.GENRE).setSubMethod(MethodSub.MOVIES).buildUrl(parameters); String webpage = httpTools.getRequest(url); - try { - WrapperMovie wrapper = MAPPER.readValue(webpage, WrapperMovie.class); - TmdbResultsList results = new TmdbResultsList(wrapper.getMovies()); - results.copyWrapper(wrapper); - return results; - } catch (IOException ex) { - throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, "Failed to get genre movie list", url, ex); - } + return processWrapperList(TR_MOVIE_BASIC, url, webpage); } } diff --git a/src/main/java/com/omertron/themoviedbapi/tools/Param.java b/src/main/java/com/omertron/themoviedbapi/tools/Param.java index b84683035..4ee819bfc 100644 --- a/src/main/java/com/omertron/themoviedbapi/tools/Param.java +++ b/src/main/java/com/omertron/themoviedbapi/tools/Param.java @@ -38,6 +38,7 @@ public enum Param { FAVORITE("favorite="), ID("id="), INCLUDE_ALL_MOVIES("include_all_movies="), + INCLUDE_ADULT("include_adult="), LANGUAGE("language="), MOVIE_ID("movie_id="), MOVIE_WATCHLIST("movie_watchlist="), diff --git a/src/test/java/com/omertron/themoviedbapi/methods/TmdbGenresTest.java b/src/test/java/com/omertron/themoviedbapi/methods/TmdbGenresTest.java index 0300efce9..1e3c25634 100644 --- a/src/test/java/com/omertron/themoviedbapi/methods/TmdbGenresTest.java +++ b/src/test/java/com/omertron/themoviedbapi/methods/TmdbGenresTest.java @@ -22,11 +22,13 @@ package com.omertron.themoviedbapi.methods; import com.omertron.themoviedbapi.AbstractTests; import com.omertron.themoviedbapi.MovieDbException; import com.omertron.themoviedbapi.model.Genre; -import com.omertron.themoviedbapi.model.MovieDb; +import com.omertron.themoviedbapi.model2.movie.MovieBasic; import com.omertron.themoviedbapi.results.TmdbResultsList; +import java.util.List; import org.junit.After; import org.junit.AfterClass; -import static org.junit.Assert.assertTrue; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; @@ -62,27 +64,45 @@ public class TmdbGenresTest extends AbstractTests { } /** - * Test of getGenreList method, of class TheMovieDbApi. + * Test of getGenreMovieList method, of class TmdbGenres. * - * @throws MovieDbException + * @throws com.omertron.themoviedbapi.MovieDbException */ @Test - public void testGetGenreList() throws MovieDbException { - LOG.info("getGenreList"); - TmdbResultsList result = instance.getGenreList(LANGUAGE_DEFAULT); - assertTrue("No genres found", !result.getResults().isEmpty()); + public void testGetGenreMovieList() throws MovieDbException { + LOG.info("getGenreMovieList"); + TmdbResultsList result = instance.getGenreMovieList(LANGUAGE_DEFAULT); + assertNotNull("List is null", result.getResults()); + assertFalse("List is empty", result.getResults().isEmpty()); } /** - * Test of getGenreMovies method, of class TheMovieDbApi. + * Test of getGenreTVList method, of class TmdbGenres. * - * @throws MovieDbException + * @throws com.omertron.themoviedbapi.MovieDbException + */ + @Test + public void testGetGenreTVList() throws MovieDbException { + LOG.info("getGenreTVList"); + TmdbResultsList result = instance.getGenreTVList(LANGUAGE_DEFAULT); + assertNotNull("List is null", result.getResults()); + assertFalse("List is empty", result.getResults().isEmpty()); + } + + /** + * Test of getGenreMovies method, of class TmdbGenres. + * + * @throws com.omertron.themoviedbapi.MovieDbException */ @Test public void testGetGenreMovies() throws MovieDbException { LOG.info("getGenreMovies"); - TmdbResultsList result = instance.getGenreMovies(ID_GENRE_ACTION, LANGUAGE_DEFAULT, 0, Boolean.TRUE); - assertTrue("No genre movies found", !result.isEmpty()); + Integer page = null; + Boolean includeAllMovies = null; + Boolean includeAdult = null; + List result = instance.getGenreMovies(ID_GENRE_ACTION, LANGUAGE_DEFAULT, page, includeAllMovies, includeAdult); + assertNotNull("List is null", result); + assertFalse("List is empty", result.isEmpty()); } }