Skip to content

Commit

Permalink
Merge pull request #170 from c-eg/genres-api
Browse files Browse the repository at this point in the history
Genres api
  • Loading branch information
c-eg authored Feb 12, 2024
2 parents 475ba9b + 0570a68 commit 180abc8
Show file tree
Hide file tree
Showing 4 changed files with 224 additions and 49 deletions.
64 changes: 15 additions & 49 deletions src/main/java/info/movito/themoviedbapi/TmdbGenre.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.fasterxml.jackson.annotation.JsonProperty;
import info.movito.themoviedbapi.model.Genre;
import info.movito.themoviedbapi.model.core.AbstractJsonMapping;
import info.movito.themoviedbapi.model.core.MovieDbResultsPage;
import info.movito.themoviedbapi.tools.ApiUrl;
import info.movito.themoviedbapi.tools.TmdbException;

Expand All @@ -14,9 +13,7 @@
* <a href="https://developer.themoviedb.org/reference/genre-movie-list">documentation</a> for more info.
*/
public class TmdbGenre extends AbstractTmdbApi {
public static final String PARAM_INCLUDE_ALL_MOVIES = "include_all_movies";

public static final String TMDB_METHOD_GENRE = "genre";
protected static final String TMDB_METHOD_GENRE = "genre";

/**
* Create a new TmdbGenre instance to call the genre related TMDb API methods.
Expand All @@ -26,64 +23,33 @@ public TmdbGenre(TmdbApi tmdbApi) {
}

/**
* You can use this method to retrieve the list of genres used on TMDb.
* These IDs will correspond to those found in movie calls.
*
* @deprecated use {@code getMovieGenreList} as TV shows Genres was added.
*/
@Deprecated
public List<Genre> getGenreList(String language) throws TmdbException {
return getMovieGenreList(language);
}

/**
* You can use this method to retrieve the list of official genres for
* movies used on TMDb.
* <p>Get the list of official genres for movies.</p>
* <p>See the <a href="https://developer.themoviedb.org/reference/genre-movie-list">documentation</a> for more info.</p>
*
* These IDs will correspond to those found in movie calls.
* @param language optional - The language, e.g. "en".
* @return The list of official genres for movies.
* @throws TmdbException If there was an error making the request or mapping the response.
*/
public List<Genre> getMovieGenreList(String language) throws TmdbException {
ApiUrl apiUrl = new ApiUrl(TMDB_METHOD_GENRE, "movie", "list");

public List<Genre> getMovieList(String language) throws TmdbException {
ApiUrl apiUrl = new ApiUrl(TMDB_METHOD_GENRE, "movie/list");
apiUrl.addLanguage(language);

return mapJsonResult(apiUrl, Genres.class).genres;
}

/**
* You can use this method to retrieve the list of of official genres for
* TV shows used on TMDb.
* <p>Get the list of official genres for TV shows.</p>
* <p>See the <a href="https://developer.themoviedb.org/reference/genre-tv-list">documentation</a> for more info.</p>
*
* These IDs will correspond to those found in TV shows calls.
* @param language optional - The language, e.g. "en".
* @return The list of official genres for movies.
* @throws TmdbException If there was an error making the request or mapping the response.
*/
public List<Genre> getTvGenreList(String language) throws TmdbException {
ApiUrl apiUrl = new ApiUrl(TMDB_METHOD_GENRE, "tv", "list");

public List<Genre> getTvList(String language) throws TmdbException {
ApiUrl apiUrl = new ApiUrl(TMDB_METHOD_GENRE, "tv/list");
apiUrl.addLanguage(language);

return mapJsonResult(apiUrl, Genres.class).genres;
}

/**
* Get a list of movies per genre.
*
* 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.
*/
public MovieDbResultsPage getGenreMovies(int genreId, String language, Integer page, boolean includeAllMovies)
throws TmdbException {
ApiUrl apiUrl = new ApiUrl(TMDB_METHOD_GENRE, genreId, "movies");

apiUrl.addLanguage(language);

apiUrl.addPage(page);

apiUrl.addPathParam(PARAM_INCLUDE_ALL_MOVIES, includeAllMovies);

return mapJsonResult(apiUrl, MovieDbResultsPage.class);
}

private static class Genres extends AbstractJsonMapping {
@JsonProperty("genres")
private List<Genre> genres;
Expand Down
61 changes: 61 additions & 0 deletions src/test/java/info/movito/themoviedbapi/TmdbGenresTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package info.movito.themoviedbapi;

import info.movito.themoviedbapi.model.Genre;
import info.movito.themoviedbapi.tools.RequestType;
import info.movito.themoviedbapi.tools.TmdbException;
import info.movito.themoviedbapi.util.TestUtils;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.net.URL;
import java.util.List;

import static info.movito.themoviedbapi.TmdbGenre.TMDB_METHOD_GENRE;
import static info.movito.themoviedbapi.tools.ApiUrl.TMDB_API_BASE_URL;
import static info.movito.themoviedbapi.util.TestUtils.testForNullFieldsAndNewItems;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.mockito.Mockito.when;

/**
* Tests for {@link TmdbGenre}.
*/
public class TmdbGenresTest extends AbstractTmdbApiTest {
/**
* Tests {@link TmdbGenre#getMovieList(String)} with an expected result.
*/
@Test
public void testGetMovieList() throws IOException, TmdbException {
String body = TestUtils.readTestFile("api_responses/genres/movie_list.json");
URL url = new URL(TMDB_API_BASE_URL + TMDB_METHOD_GENRE + "/movie/list?language=en");
when(getTmdbUrlReader().readUrl(url, null, RequestType.GET)).thenReturn(body);

TmdbGenre tmdbGenre = new TmdbGenre(getTmdbApi());
List<Genre> genres = tmdbGenre.getMovieList("en");
assertNotNull(genres);
assertFalse(genres.isEmpty());

Genre genre = genres.get(0);
assertNotNull(genre);
testForNullFieldsAndNewItems(genre);
}

/**
* Tests {@link TmdbGenre#getTvList(String)} with an expected result.
*/
@Test
public void testGetTvList() throws IOException, TmdbException {
String body = TestUtils.readTestFile("api_responses/genres/tv_list.json");
URL url = new URL(TMDB_API_BASE_URL + TMDB_METHOD_GENRE + "/tv/list?language=en");
when(getTmdbUrlReader().readUrl(url, null, RequestType.GET)).thenReturn(body);

TmdbGenre tmdbGenre = new TmdbGenre(getTmdbApi());
List<Genre> genres = tmdbGenre.getTvList("en");
assertNotNull(genres);
assertFalse(genres.isEmpty());

Genre genre = genres.get(0);
assertNotNull(genre);
testForNullFieldsAndNewItems(genre);
}
}
80 changes: 80 additions & 0 deletions src/test/resources/api_responses/genres/movie_list.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
{
"genres": [
{
"id": 28,
"name": "Action"
},
{
"id": 12,
"name": "Abenteuer"
},
{
"id": 16,
"name": "Animation"
},
{
"id": 35,
"name": "Komödie"
},
{
"id": 80,
"name": "Krimi"
},
{
"id": 99,
"name": "Dokumentarfilm"
},
{
"id": 18,
"name": "Drama"
},
{
"id": 10751,
"name": "Familie"
},
{
"id": 14,
"name": "Fantasy"
},
{
"id": 36,
"name": "Historie"
},
{
"id": 27,
"name": "Horror"
},
{
"id": 10402,
"name": "Musik"
},
{
"id": 9648,
"name": "Mystery"
},
{
"id": 10749,
"name": "Liebesfilm"
},
{
"id": 878,
"name": "Science Fiction"
},
{
"id": 10770,
"name": "TV-Film"
},
{
"id": 53,
"name": "Thriller"
},
{
"id": 10752,
"name": "Kriegsfilm"
},
{
"id": 37,
"name": "Western"
}
]
}
68 changes: 68 additions & 0 deletions src/test/resources/api_responses/genres/tv_list.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
{
"genres": [
{
"id": 10759,
"name": "Action & Adventure"
},
{
"id": 16,
"name": "Animation"
},
{
"id": 35,
"name": "Komödie"
},
{
"id": 80,
"name": "Krimi"
},
{
"id": 99,
"name": "Dokumentarfilm"
},
{
"id": 18,
"name": "Drama"
},
{
"id": 10751,
"name": "Familie"
},
{
"id": 10762,
"name": "Kids"
},
{
"id": 9648,
"name": "Mystery"
},
{
"id": 10763,
"name": "News"
},
{
"id": 10764,
"name": "Reality"
},
{
"id": 10765,
"name": "Sci-Fi & Fantasy"
},
{
"id": 10766,
"name": "Soap"
},
{
"id": 10767,
"name": "Talk"
},
{
"id": 10768,
"name": "War & Politics"
},
{
"id": 37,
"name": "Western"
}
]
}

0 comments on commit 180abc8

Please sign in to comment.