master
Stuart Boston 11 years ago
parent a129692ae3
commit 6231130d46

@ -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.
* Get the list of movie genres.
*
* These IDs will correspond to those found in movie calls.
* @param language
* @return
* @throws MovieDbException
*/
public TmdbResultsList<Genre> getGenreMovieList(String language) throws MovieDbException {
return getGenreList(language, MethodSub.MOVIE_LIST);
}
/**
* Get the list of TV genres.
*
* @param language
* @return
* @throws MovieDbException
*/
public TmdbResultsList<Genre> getGenreList(String language) throws MovieDbException {
public TmdbResultsList<Genre> 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<Genre> 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.
*
* It is important to understand that only movies with more than 10 votes
* get listed.
* Get the list of movies for a particular genre by id.
*
* 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<MovieDb> getGenreMovies(int genreId, String language, int page, boolean includeAllMovies) throws MovieDbException {
public List<MovieBasic> 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<MovieDb> results = new TmdbResultsList<MovieDb>(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);
}
}

@ -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="),

@ -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<Genre> result = instance.getGenreList(LANGUAGE_DEFAULT);
assertTrue("No genres found", !result.getResults().isEmpty());
public void testGetGenreMovieList() throws MovieDbException {
LOG.info("getGenreMovieList");
TmdbResultsList<Genre> 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<Genre> 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<MovieDb> 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<MovieBasic> result = instance.getGenreMovies(ID_GENRE_ACTION, LANGUAGE_DEFAULT, page, includeAllMovies, includeAdult);
assertNotNull("List is null", result);
assertFalse("List is empty", result.isEmpty());
}
}

Loading…
Cancel
Save