diff --git a/src/main/java/com/omertron/themoviedbapi/TheMovieDbApi.java b/src/main/java/com/omertron/themoviedbapi/TheMovieDbApi.java index 080d838e0..e6e75d73f 100644 --- a/src/main/java/com/omertron/themoviedbapi/TheMovieDbApi.java +++ b/src/main/java/com/omertron/themoviedbapi/TheMovieDbApi.java @@ -1,1670 +1,1675 @@ -/* - * Copyright (c) 2004-2013 Stuart Boston - * - * This file is part of TheMovieDB API. - * - * TheMovieDB API is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * any later version. - * - * TheMovieDB API is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with TheMovieDB API. If not, see . - * - */ -package com.omertron.themoviedbapi; - -import com.fasterxml.jackson.databind.ObjectMapper; -import com.omertron.themoviedbapi.MovieDbException.MovieDbExceptionType; -import com.omertron.themoviedbapi.model.*; -import com.omertron.themoviedbapi.results.TmdbResultsList; -import com.omertron.themoviedbapi.tools.ApiUrl; -import static com.omertron.themoviedbapi.tools.ApiUrl.*; -import com.omertron.themoviedbapi.tools.WebBrowser; -import com.omertron.themoviedbapi.wrapper.*; -import java.io.IOException; -import java.net.MalformedURLException; -import java.net.URL; -import org.apache.commons.lang3.StringUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * 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 TheMovieDbApi { - - private static final Logger LOG = LoggerFactory.getLogger(TheMovieDbApi.class); - private String apiKey; - private TmdbConfiguration tmdbConfig; - // API Methods - private static final String BASE_MOVIE = "movie/"; - private static final String BASE_PERSON = "person/"; - private static final String BASE_COMPANY = "company/"; - private static final String BASE_GENRE = "genre/"; - private static final String BASE_AUTH = "authentication/"; - private static final String BASE_COLLECTION = "collection/"; -// private static final String BASE_ACCOUNT = "account/"; - private static final String BASE_SEARCH = "search/"; - private static final String BASE_LIST = "list/"; - private static final String BASE_KEYWORD = "keyword/"; - private static final String BASE_JOB = "job/"; - private static final String BASE_DISCOVER = "discover/"; - // Jackson JSON configuration - private static ObjectMapper mapper = new ObjectMapper(); - - /** - * API for The Movie Db. - * - * @param apiKey - * @throws MovieDbException - */ - public TheMovieDbApi(String apiKey) throws MovieDbException { - this.apiKey = apiKey; - ApiUrl apiUrl = new ApiUrl(apiKey, "configuration"); - URL configUrl = apiUrl.buildUrl(); - String webpage = WebBrowser.request(configUrl); - - try { - WrapperConfig wc = mapper.readValue(webpage, WrapperConfig.class); - tmdbConfig = wc.getTmdbConfiguration(); - } catch (IOException ex) { - throw new MovieDbException(MovieDbExceptionType.MAPPING_FAILED, "Failed to read configuration", ex); - } - } - - /** - * Get the API key that is to be used - * - */ - public String getApiKey() { - return apiKey; - } - - /** - * Set the proxy information - * - * @param host - * @param port - * @param username - * @param password - */ - public void setProxy(String host, String port, String username, String password) { - WebBrowser.setProxyHost(host); - WebBrowser.setProxyPort(port); - WebBrowser.setProxyUsername(username); - WebBrowser.setProxyPassword(password); - } - - /** - * Set the connection and read time out values - * - * @param connect - * @param read - */ - public void setTimeout(int connect, int read) { - WebBrowser.setWebTimeoutConnect(connect); - 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 exact match - * @return True if there is a match, False otherwise. - */ - public static boolean compareMovies(MovieDb moviedb, String title, String year) { - return compareMovies(moviedb, title, year, 0); - } - - /** - * 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 - * @param maxDistance The Levenshtein Distance between the two titles. 0 = exact match - * @return True if there is a match, False otherwise. - */ - public static boolean compareMovies(MovieDb moviedb, String title, String year, int maxDistance) { - if ((moviedb == null) || (StringUtils.isBlank(title))) { - return Boolean.FALSE; - } - - if (isValidYear(year) && isValidYear(moviedb.getReleaseDate())) { - // Compare with year - String movieYear = moviedb.getReleaseDate().substring(0, 4); - if (movieYear.equals(year)) { - if (compareDistance(moviedb.getOriginalTitle(), title, maxDistance)) { - return Boolean.TRUE; - } - - if (compareDistance(moviedb.getTitle(), title, maxDistance)) { - return Boolean.TRUE; - } - } - } - - // Compare without year - if (compareDistance(moviedb.getOriginalTitle(), title, maxDistance)) { - return Boolean.TRUE; - } - - if (compareDistance(moviedb.getTitle(), title, maxDistance)) { - return Boolean.TRUE; - } - - return Boolean.FALSE; - } - - /** - * Compare the Levenshtein Distance between the two strings - * - * @param title1 - * @param title2 - * @param distance - */ - private static boolean compareDistance(String title1, String title2, int distance) { - return (StringUtils.getLevenshteinDistance(title1, title2) <= distance); - } - - /** - * Check the year is not blank or UNKNOWN - * - * @param year - */ - private static boolean isValidYear(String year) { - return (StringUtils.isNotBlank(year) && !year.equals("UNKNOWN")); - } - - // - /** - * Get the configuration information - */ - public TmdbConfiguration getConfiguration() { - return tmdbConfig; - } - - /** - * Generate the full image URL from the size and image path - * - * @param imagePath - * @param requiredSize - * @throws MovieDbException - */ - public URL createImageUrl(String imagePath, String requiredSize) throws MovieDbException { - if (!tmdbConfig.isValidSize(requiredSize)) { - throw new MovieDbException(MovieDbExceptionType.INVALID_IMAGE, requiredSize); - } - - StringBuilder sb = new StringBuilder(tmdbConfig.getBaseUrl()); - sb.append(requiredSize); - sb.append(imagePath); - try { - return (new URL(sb.toString())); - } catch (MalformedURLException ex) { - LOG.warn("Failed to create image URL: {}", ex.getMessage()); - throw new MovieDbException(MovieDbExceptionType.INVALID_URL, sb.toString(), ex); - } - } - - // - // - // - /** - * This method is used to generate a valid request token for user based authentication. - * - * A request token is required in order to request a session id. - * - * You can generate any number of request tokens but they will expire after 60 minutes. - * - * As soon as a valid session id has been created the token will be destroyed. - * - * @throws MovieDbException - */ - public TokenAuthorisation getAuthorisationToken() throws MovieDbException { - ApiUrl apiUrl = new ApiUrl(apiKey, BASE_AUTH, "token/new"); - - URL url = apiUrl.buildUrl(); - String webpage = WebBrowser.request(url); - - try { - return mapper.readValue(webpage, TokenAuthorisation.class); - } catch (IOException ex) { - LOG.warn("Failed to get Authorisation Token: {}", ex.getMessage()); - throw new MovieDbException(MovieDbExceptionType.AUTHORISATION_FAILURE, webpage, ex); - } - } - - /** - * This method is used to generate a session id for user based authentication. - * - * A session id is required in order to use any of the write methods. - * - * @param token - * @throws MovieDbException - */ - public TokenSession getSessionToken(TokenAuthorisation token) throws MovieDbException { - ApiUrl apiUrl = new ApiUrl(apiKey, BASE_AUTH, "session/new"); - - if (!token.getSuccess()) { - LOG.warn("Authorisation token was not successful!"); - throw new MovieDbException(MovieDbExceptionType.AUTHORISATION_FAILURE, "Authorisation token was not successful!"); - } - - apiUrl.addArgument(PARAM_TOKEN, token.getRequestToken()); - URL url = apiUrl.buildUrl(); - String webpage = WebBrowser.request(url); - - try { - return mapper.readValue(webpage, TokenSession.class); - } catch (IOException ex) { - LOG.warn("Failed to get Session Token: {}", ex.getMessage()); - throw new MovieDbException(MovieDbExceptionType.MAPPING_FAILED, webpage, ex); - } - } - - /** - * This method is used to generate a guest session id. - * - * A guest session can be used to rate movies without having a registered TMDb user account. - * - * You should only generate a single guest session per user (or device) as you will be able to attach the ratings to a TMDb user - * account in the future. - * - * There are also IP limits in place so you should always make sure it's the end user doing the guest session actions. - * - * If a guest session is not used for the first time within 24 hours, it will be automatically discarded. - * - * @throws MovieDbException - */ - public TokenSession getGuestSessionToken() throws MovieDbException { - ApiUrl apiUrl = new ApiUrl(apiKey, BASE_AUTH, "guest_session/new"); - - URL url = apiUrl.buildUrl(); - String webpage = WebBrowser.request(url); - - try { - return mapper.readValue(webpage, TokenSession.class); - } catch (IOException ex) { - LOG.warn("Failed to get Session Token: {}", ex.getMessage()); - throw new MovieDbException(MovieDbExceptionType.MAPPING_FAILED, webpage, ex); - } - } - - // - // - // - // - // - // - /** - * 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 - * @throws MovieDbException - */ - public MovieDb getMovieInfo(int movieId, String language, String... appendToResponse) throws MovieDbException { - ApiUrl apiUrl = new ApiUrl(apiKey, BASE_MOVIE); - - apiUrl.addArgument(PARAM_ID, movieId); - - if (StringUtils.isNotBlank(language)) { - apiUrl.addArgument(PARAM_LANGUAGE, language); - } - - apiUrl.appendToResponse(appendToResponse); - - URL url = apiUrl.buildUrl(); - String webpage = WebBrowser.request(url); - try { - return mapper.readValue(webpage, MovieDb.class); - } catch (IOException ex) { - LOG.warn("Failed to get movie info: {}", ex.getMessage()); - throw new MovieDbException(MovieDbExceptionType.MAPPING_FAILED, webpage, ex); - } - } - - /** - * This method is used to retrieve all of the basic movie information. - * - * It will return the single highest rated poster and backdrop. - * - * @param imdbId - * @param language - * @throws MovieDbException - */ - public MovieDb getMovieInfoImdb(String imdbId, String language, String... appendToResponse) throws MovieDbException { - ApiUrl apiUrl = new ApiUrl(apiKey, BASE_MOVIE); - - apiUrl.addArgument(PARAM_ID, imdbId); - - if (StringUtils.isNotBlank(language)) { - apiUrl.addArgument(PARAM_LANGUAGE, language); - } - - apiUrl.appendToResponse(appendToResponse); - - URL url = apiUrl.buildUrl(); - String webpage = WebBrowser.request(url); - try { - return mapper.readValue(webpage, MovieDb.class); - } catch (IOException ex) { - LOG.warn("Failed to get movie info: {}", ex.getMessage()); - throw new MovieDbException(MovieDbExceptionType.MAPPING_FAILED, webpage, ex); - } - } - - /** - * This method is used to retrieve all of the alternative titles we have for a particular movie. - * - * @param movieId - * @param country - * @throws MovieDbException - */ - public TmdbResultsList getMovieAlternativeTitles(int movieId, String country, String... appendToResponse) throws MovieDbException { - ApiUrl apiUrl = new ApiUrl(apiKey, BASE_MOVIE, "/alternative_titles"); - apiUrl.addArgument(PARAM_ID, movieId); - - if (StringUtils.isNotBlank(country)) { - apiUrl.addArgument(PARAM_COUNTRY, country); - } - - apiUrl.appendToResponse(appendToResponse); - - URL url = apiUrl.buildUrl(); - String webpage = WebBrowser.request(url); - try { - WrapperAlternativeTitles wrapper = mapper.readValue(webpage, WrapperAlternativeTitles.class); - TmdbResultsList results = new TmdbResultsList(wrapper.getTitles()); - results.copyWrapper(wrapper); - return results; - } catch (IOException ex) { - LOG.warn("Failed to get movie alternative titles: {}", ex.getMessage()); - throw new MovieDbException(MovieDbExceptionType.MAPPING_FAILED, webpage, ex); - } - } - - /** - * Get the cast information for a specific movie id. - * - * TODO: Add a function to enrich the data with the people methods - * - * @param movieId - * @throws MovieDbException - */ - public TmdbResultsList getMovieCasts(int movieId, String... appendToResponse) throws MovieDbException { - ApiUrl apiUrl = new ApiUrl(apiKey, BASE_MOVIE, "/casts"); - apiUrl.addArgument(PARAM_ID, movieId); - - apiUrl.appendToResponse(appendToResponse); - - URL url = apiUrl.buildUrl(); - String webpage = WebBrowser.request(url); - - try { - WrapperMovieCasts wrapper = mapper.readValue(webpage, WrapperMovieCasts.class); - TmdbResultsList results = new TmdbResultsList(wrapper.getAll()); - results.copyWrapper(wrapper); - return results; - } catch (IOException ex) { - LOG.warn("Failed to get movie casts: {}", ex.getMessage()); - throw new MovieDbException(MovieDbExceptionType.MAPPING_FAILED, webpage, ex); - } - } - - /** - * This method should be used when you’re wanting to retrieve all of the images for a particular movie. - * - * @param movieId - * @param language - * @throws MovieDbException - */ - public TmdbResultsList getMovieImages(int movieId, String language, String... appendToResponse) throws MovieDbException { - ApiUrl apiUrl = new ApiUrl(apiKey, BASE_MOVIE, "/images"); - apiUrl.addArgument(PARAM_ID, movieId); - - if (StringUtils.isNotBlank(language)) { - apiUrl.addArgument(PARAM_LANGUAGE, language); - } - - apiUrl.appendToResponse(appendToResponse); - - URL url = apiUrl.buildUrl(); - String webpage = WebBrowser.request(url); - - try { - WrapperImages wrapper = mapper.readValue(webpage, WrapperImages.class); - TmdbResultsList results = new TmdbResultsList(wrapper.getAll()); - results.copyWrapper(wrapper); - return results; - } catch (IOException ex) { - LOG.warn("Failed to get movie images: {}", ex.getMessage()); - throw new MovieDbException(MovieDbExceptionType.MAPPING_FAILED, webpage, ex); - } - } - - /** - * 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 - * @throws MovieDbException - */ - public TmdbResultsList getMovieKeywords(int movieId, String... appendToResponse) throws MovieDbException { - ApiUrl apiUrl = new ApiUrl(apiKey, BASE_MOVIE, "/keywords"); - apiUrl.addArgument(PARAM_ID, movieId); - - apiUrl.appendToResponse(appendToResponse); - - URL url = apiUrl.buildUrl(); - String webpage = WebBrowser.request(url); - - try { - WrapperMovieKeywords wrapper = mapper.readValue(webpage, WrapperMovieKeywords.class); - TmdbResultsList results = new TmdbResultsList(wrapper.getKeywords()); - results.copyWrapper(wrapper); - return results; - } catch (IOException ex) { - LOG.warn("Failed to get movie keywords: {}", ex.getMessage()); - throw new MovieDbException(MovieDbExceptionType.MAPPING_FAILED, webpage, ex); - } - } - - /** - * This method is used to retrieve all of the release and certification data we have for a specific movie. - * - * @param movieId - * @param language - * @throws MovieDbException - */ - public TmdbResultsList getMovieReleaseInfo(int movieId, String language, String... appendToResponse) throws MovieDbException { - ApiUrl apiUrl = new ApiUrl(apiKey, BASE_MOVIE, "/releases"); - apiUrl.addArgument(PARAM_ID, movieId); - apiUrl.addArgument(PARAM_LANGUAGE, language); - - apiUrl.appendToResponse(appendToResponse); - - URL url = apiUrl.buildUrl(); - String webpage = WebBrowser.request(url); - - try { - WrapperReleaseInfo wrapper = mapper.readValue(webpage, WrapperReleaseInfo.class); - TmdbResultsList results = new TmdbResultsList(wrapper.getCountries()); - results.copyWrapper(wrapper); - return results; - } catch (IOException ex) { - LOG.warn("Failed to get movie release information: {}", ex.getMessage()); - throw new MovieDbException(MovieDbExceptionType.MAPPING_FAILED, webpage, ex); - } - } - - /** - * This method is used to retrieve all of the trailers for a particular movie. - * - * Supported sites are YouTube and QuickTime. - * - * @param movieId - * @param language - * @throws MovieDbException - */ - public TmdbResultsList getMovieTrailers(int movieId, String language, String... appendToResponse) throws MovieDbException { - ApiUrl apiUrl = new ApiUrl(apiKey, BASE_MOVIE, "/trailers"); - apiUrl.addArgument(PARAM_ID, movieId); - - if (StringUtils.isNotBlank(language)) { - apiUrl.addArgument(PARAM_LANGUAGE, language); - } - - apiUrl.appendToResponse(appendToResponse); - - URL url = apiUrl.buildUrl(); - String webpage = WebBrowser.request(url); - - try { - WrapperTrailers wrapper = mapper.readValue(webpage, WrapperTrailers.class); - TmdbResultsList results = new TmdbResultsList(wrapper.getAll()); - results.copyWrapper(wrapper); - return results; - } catch (IOException ex) { - LOG.warn("Failed to get movie trailers: {}", ex.getMessage()); - throw new MovieDbException(MovieDbExceptionType.MAPPING_FAILED, webpage, ex); - } - } - - /** - * This method is used to retrieve a list of the available translations for a specific movie. - * - * @param movieId - * @throws MovieDbException - */ - public TmdbResultsList getMovieTranslations(int movieId, String... appendToResponse) throws MovieDbException { - ApiUrl apiUrl = new ApiUrl(apiKey, BASE_MOVIE, "/translations"); - apiUrl.addArgument(PARAM_ID, movieId); - - apiUrl.appendToResponse(appendToResponse); - - URL url = apiUrl.buildUrl(); - String webpage = WebBrowser.request(url); - - try { - WrapperTranslations wrapper = mapper.readValue(webpage, WrapperTranslations.class); - TmdbResultsList results = new TmdbResultsList(wrapper.getTranslations()); - results.copyWrapper(wrapper); - return results; - } catch (IOException ex) { - LOG.warn("Failed to get movie tranlations: {}", ex.getMessage()); - throw new MovieDbException(MovieDbExceptionType.MAPPING_FAILED, webpage, ex); - } - } - - /** - * The similar movies method will let you retrieve the similar movies for a particular movie. - * - * This data is created dynamically but with the help of users votes on TMDb. - * - * The data is much better with movies that have more keywords - * - * @param movieId - * @param language - * @param page - * @throws MovieDbException - */ - public TmdbResultsList getSimilarMovies(int movieId, String language, int page, String... appendToResponse) throws MovieDbException { - ApiUrl apiUrl = new ApiUrl(apiKey, BASE_MOVIE, "/similar_movies"); - apiUrl.addArgument(PARAM_ID, movieId); - - if (StringUtils.isNotBlank(language)) { - apiUrl.addArgument(PARAM_LANGUAGE, language); - } - - if (page > 0) { - apiUrl.addArgument(PARAM_PAGE, page); - } - - apiUrl.appendToResponse(appendToResponse); - - URL url = apiUrl.buildUrl(); - String webpage = WebBrowser.request(url); - - try { - WrapperMovie wrapper = mapper.readValue(webpage, WrapperMovie.class); - TmdbResultsList results = new TmdbResultsList(wrapper.getMovies()); - results.copyWrapper(wrapper); - return results; - } catch (IOException ex) { - LOG.warn("Failed to get similar movies: {}", ex.getMessage()); - throw new MovieDbException(MovieDbExceptionType.MAPPING_FAILED, webpage, ex); - } - } - - public TmdbResultsList getReviews(int movieId, String language, int page, String... appendToResponse) throws MovieDbException { - ApiUrl apiUrl = new ApiUrl(apiKey, BASE_MOVIE, "/reviews"); - apiUrl.addArgument(PARAM_ID, movieId); - - if (StringUtils.isNotBlank(language)) { - apiUrl.addArgument(PARAM_LANGUAGE, language); - } - - if (page > 0) { - apiUrl.addArgument(PARAM_PAGE, page); - } - - apiUrl.appendToResponse(appendToResponse); - - URL url = apiUrl.buildUrl(); - String webpage = WebBrowser.request(url); - - try { - WrapperReviews wrapper = mapper.readValue(webpage, WrapperReviews.class); - TmdbResultsList results = new TmdbResultsList(wrapper.getReviews()); - results.copyWrapper(wrapper); - return results; - } catch (IOException ex) { - LOG.warn("Failed to get reviews: {}", ex.getMessage()); - throw new MovieDbException(MovieDbExceptionType.MAPPING_FAILED, webpage, ex); - } - } - - /** - * Get the lists that the movie belongs to - * - * @param movieId - * @param language - * @param page - * @throws MovieDbException - */ - public TmdbResultsList getMovieLists(int movieId, String language, int page, String... appendToResponse) throws MovieDbException { - ApiUrl apiUrl = new ApiUrl(apiKey, BASE_MOVIE, "/lists"); - apiUrl.addArgument(PARAM_ID, movieId); - - if (StringUtils.isNotBlank(language)) { - apiUrl.addArgument(PARAM_LANGUAGE, language); - } - - if (page > 0) { - apiUrl.addArgument(PARAM_PAGE, page); - } - - apiUrl.appendToResponse(appendToResponse); - - URL url = apiUrl.buildUrl(); - String webpage = WebBrowser.request(url); - - try { - WrapperMovieList wrapper = mapper.readValue(webpage, WrapperMovieList.class); - TmdbResultsList results = new TmdbResultsList(wrapper.getMovieList()); - results.copyWrapper(wrapper); - return results; - } catch (IOException ex) { - LOG.warn("Failed to get movie lists: {}", ex.getMessage()); - throw new MovieDbException(MovieDbExceptionType.MAPPING_FAILED, webpage, ex); - } - } - - /** - * Get the changes for a specific movie id. - * - * Changes are grouped by key, and ordered by date in descending order. - * - * By default, only the last 24 hours of changes are returned. - * - * The maximum number of days that can be returned in a single request is 14. - * - * The language is present on fields that are translatable. - * - * TODO: DOES NOT WORK AT THE MOMENT. This is due to the "value" item changing type in the ChangeItem - * - * @param movieId - * @param startDate the start date of the changes, optional - * @param endDate the end date of the changes, optional - * @throws MovieDbException - */ - @Deprecated - public TmdbResultsList getMovieChanges(int movieId, String startDate, String endDate) throws MovieDbException { - ApiUrl apiUrl = new ApiUrl(apiKey, BASE_MOVIE, "/changes"); - apiUrl.addArgument(PARAM_ID, movieId); - - if (StringUtils.isNotBlank(startDate)) { - apiUrl.addArgument("start_date", startDate); - } - - if (StringUtils.isNotBlank(endDate)) { - apiUrl.addArgument("end_date", endDate); - } - - URL url = apiUrl.buildUrl(); - String webpage = WebBrowser.request(url); - - try { - WrapperChanges wrapper = mapper.readValue(webpage, WrapperChanges.class); - TmdbResultsList results = new TmdbResultsList(wrapper.getChanges()); - results.copyWrapper(wrapper); - return results; - } catch (IOException ex) { - LOG.warn("Failed to get movie changes: {}", ex.getMessage()); - throw new MovieDbException(MovieDbExceptionType.MAPPING_FAILED, webpage, ex); - } - - } - - /** - * This method is used to retrieve the newest movie that was added to TMDb. - * - */ - public MovieDb getLatestMovie() throws MovieDbException { - ApiUrl apiUrl = new ApiUrl(apiKey, BASE_MOVIE, "/latest"); - URL url = apiUrl.buildUrl(); - String webpage = WebBrowser.request(url); - - try { - return mapper.readValue(webpage, MovieDb.class); - } catch (IOException ex) { - LOG.warn("Failed to get latest movie: {}", ex.getMessage()); - throw new MovieDbException(MovieDbExceptionType.MAPPING_FAILED, webpage, ex); - } - } - - /** - * Get the list of upcoming movies. - * - * This list refreshes every day. - * - * The maximum number of items this list will include is 100. - * - * @throws MovieDbException - */ - public TmdbResultsList getUpcoming(String language, int page) throws MovieDbException { - ApiUrl apiUrl = new ApiUrl(apiKey, BASE_MOVIE, "upcoming"); - - if (StringUtils.isNotBlank(language)) { - apiUrl.addArgument(PARAM_LANGUAGE, language); - } - - if (page > 0) { - apiUrl.addArgument(PARAM_PAGE, page); - } - - URL url = apiUrl.buildUrl(); - String webpage = WebBrowser.request(url); - - try { - WrapperMovie wrapper = mapper.readValue(webpage, WrapperMovie.class); - TmdbResultsList results = new TmdbResultsList(wrapper.getMovies()); - results.copyWrapper(wrapper); - return results; - } catch (IOException ex) { - LOG.warn("Failed to get upcoming 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. - * - * TODO: Implement more than 20 movies - * - * @param language - * @param page - * @throws MovieDbException - */ - public TmdbResultsList getNowPlayingMovies(String language, int page) throws MovieDbException { - ApiUrl apiUrl = new ApiUrl(apiKey, BASE_MOVIE, "now-playing"); - - if (StringUtils.isNotBlank(language)) { - apiUrl.addArgument(PARAM_LANGUAGE, language); - } - - if (page > 0) { - apiUrl.addArgument(PARAM_PAGE, page); - } - - URL url = apiUrl.buildUrl(); - String webpage = WebBrowser.request(url); - - try { - WrapperMovie wrapper = mapper.readValue(webpage, WrapperMovie.class); - TmdbResultsList results = new TmdbResultsList(wrapper.getMovies()); - results.copyWrapper(wrapper); - return results; - } catch (IOException ex) { - LOG.warn("Failed to get now playing movies: {}", ex.getMessage()); - throw new MovieDbException(MovieDbExceptionType.MAPPING_FAILED, webpage, ex); - } - } - - /** - * This method is used to retrieve the daily movie popularity list. - * - * This list is updated daily. The default response will return 20 movies. - * - * TODO: Implement more than 20 movies - * - * @param language - * @param page - * @throws MovieDbException - */ - public TmdbResultsList getPopularMovieList(String language, int page) throws MovieDbException { - ApiUrl apiUrl = new ApiUrl(apiKey, BASE_MOVIE, "popular"); - - if (StringUtils.isNotBlank(language)) { - apiUrl.addArgument(PARAM_LANGUAGE, language); - } - - if (page > 0) { - apiUrl.addArgument(PARAM_PAGE, page); - } - - URL url = apiUrl.buildUrl(); - String webpage = WebBrowser.request(url); - - try { - WrapperMovie wrapper = mapper.readValue(webpage, WrapperMovie.class); - TmdbResultsList results = new TmdbResultsList(wrapper.getMovies()); - results.copyWrapper(wrapper); - return results; - } catch (IOException ex) { - LOG.warn("Failed to get popular movie list: {}", ex.getMessage()); - throw new MovieDbException(MovieDbExceptionType.MAPPING_FAILED, webpage, ex); - } - } - - /** - * This method is used to retrieve the top rated movies that have over 10 votes on TMDb. - * - * The default response will return 20 movies. - * - * TODO: Implement more than 20 movies - * - * @param language - * @param page - * @throws MovieDbException - */ - public TmdbResultsList getTopRatedMovies(String language, int page) throws MovieDbException { - ApiUrl apiUrl = new ApiUrl(apiKey, BASE_MOVIE, "top-rated"); - - if (StringUtils.isNotBlank(language)) { - apiUrl.addArgument(PARAM_LANGUAGE, language); - } - - if (page > 0) { - apiUrl.addArgument(PARAM_PAGE, page); - } - - URL url = apiUrl.buildUrl(); - String webpage = WebBrowser.request(url); - - try { - WrapperMovie wrapper = mapper.readValue(webpage, WrapperMovie.class); - TmdbResultsList results = new TmdbResultsList(wrapper.getMovies()); - results.copyWrapper(wrapper); - return results; - } catch (IOException ex) { - LOG.warn("Failed to get top rated movies: {}", ex.getMessage()); - throw new MovieDbException(MovieDbExceptionType.MAPPING_FAILED, webpage, ex); - } - } - - /** - * This method lets users rate a movie. - * - * A valid session id is required. - * - * @param sessionId - * @param rating - * @throws MovieDbException - */ - public boolean postMovieRating(String sessionId, String rating) throws MovieDbException { - ApiUrl apiUrl = new ApiUrl(apiKey, BASE_MOVIE, "/rating"); - - apiUrl.addArgument(PARAM_SESSION, sessionId); - apiUrl.addArgument(PARAM_VALUE, rating); - - throw new MovieDbException(MovieDbExceptionType.UNKNOWN_CAUSE, "Not implemented yet"); - } - - // - // - // - /** - * 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 collectionId - * @param language - * @throws MovieDbException - */ - public CollectionInfo getCollectionInfo(int collectionId, String language) throws MovieDbException { - ApiUrl apiUrl = new ApiUrl(apiKey, BASE_COLLECTION); - apiUrl.addArgument(PARAM_ID, collectionId); - - if (StringUtils.isNotBlank(language)) { - apiUrl.addArgument(PARAM_LANGUAGE, language); - } - - URL url = apiUrl.buildUrl(); - String webpage = WebBrowser.request(url); - - try { - return mapper.readValue(webpage, CollectionInfo.class); - } catch (IOException ex) { - LOG.warn("Failed to get collection information: {}", ex.getMessage()); - throw new MovieDbException(MovieDbExceptionType.MAPPING_FAILED, webpage, ex); - } - } - - /** - * Get all of the images for a particular collection by collection id. - * - * @param collectionId - * @param language - * @throws MovieDbException - */ - public TmdbResultsList getCollectionImages(int collectionId, String language) throws MovieDbException { - ApiUrl apiUrl = new ApiUrl(apiKey, BASE_COLLECTION, "/images"); - apiUrl.addArgument(PARAM_ID, collectionId); - - if (StringUtils.isNotBlank(language)) { - apiUrl.addArgument(PARAM_LANGUAGE, language); - } - - URL url = apiUrl.buildUrl(); - String webpage = WebBrowser.request(url); - - try { - WrapperImages wrapper = mapper.readValue(webpage, WrapperImages.class); - TmdbResultsList results = new TmdbResultsList(wrapper.getAll(ArtworkType.POSTER, ArtworkType.BACKDROP)); - results.copyWrapper(wrapper); - return results; - } catch (IOException ex) { - LOG.warn("Failed to get collection images: {}", ex.getMessage()); - throw new MovieDbException(MovieDbExceptionType.MAPPING_FAILED, webpage, ex); - } - } - - // - // - // - /** - * This method is used to retrieve all of the basic person information. - * - * It will return the single highest rated profile image. - * - * @param personId - * @throws MovieDbException - */ - public Person getPersonInfo(int personId, String... appendToResponse) throws MovieDbException { - ApiUrl apiUrl = new ApiUrl(apiKey, BASE_PERSON); - - apiUrl.addArgument(PARAM_ID, personId); - apiUrl.appendToResponse(appendToResponse); - - URL url = apiUrl.buildUrl(); - String webpage = WebBrowser.request(url); - - try { - return mapper.readValue(webpage, Person.class); - } catch (IOException ex) { - LOG.warn("Failed to get movie info: {}", ex.getMessage()); - throw new MovieDbException(MovieDbExceptionType.MAPPING_FAILED, webpage, ex); - } - } - - /** - * This method is used to retrieve all of the cast & crew information for the person. - * - * It will return the single highest rated poster for each movie record. - * - * @param personId - * @throws MovieDbException - */ - public TmdbResultsList getPersonCredits(int personId, String... appendToResponse) throws MovieDbException { - ApiUrl apiUrl = new ApiUrl(apiKey, BASE_PERSON, "/credits"); - - apiUrl.addArgument(PARAM_ID, personId); - apiUrl.appendToResponse(appendToResponse); - - URL url = apiUrl.buildUrl(); - String webpage = WebBrowser.request(url); - - try { - WrapperPersonCredits wrapper = mapper.readValue(webpage, WrapperPersonCredits.class); - TmdbResultsList results = new TmdbResultsList(wrapper.getAll()); - results.copyWrapper(wrapper); - return results; - } catch (IOException ex) { - LOG.warn("Failed to get person credits: {}", ex.getMessage()); - throw new MovieDbException(MovieDbExceptionType.MAPPING_FAILED, webpage, ex); - } - } - - /** - * This method is used to retrieve all of the profile images for a person. - * - * @param personId - * @throws MovieDbException - */ - public TmdbResultsList getPersonImages(int personId) throws MovieDbException { - ApiUrl apiUrl = new ApiUrl(apiKey, BASE_PERSON, "/images"); - - apiUrl.addArgument(PARAM_ID, personId); - - URL url = apiUrl.buildUrl(); - String webpage = WebBrowser.request(url); - - try { - WrapperImages wrapper = mapper.readValue(webpage, WrapperImages.class); - TmdbResultsList results = new TmdbResultsList(wrapper.getAll(ArtworkType.PROFILE)); - results.copyWrapper(wrapper); - return results; - } catch (IOException ex) { - LOG.warn("Failed to get person images: {}", ex.getMessage()); - throw new MovieDbException(MovieDbExceptionType.MAPPING_FAILED, webpage, ex); - } - } - - /** - * Get the changes for a specific person id. - * - * Changes are grouped by key, and ordered by date in descending order. - * - * By default, only the last 24 hours of changes are returned. - * - * The maximum number of days that can be returned in a single request is 14. - * - * The language is present on fields that are translatable. - * - * @param personId - * @param startDate - * @param endDate - * @throws MovieDbException - */ - public void getPersonChanges(int personId, String startDate, String endDate) throws MovieDbException { - throw new MovieDbException(MovieDbExceptionType.UNKNOWN_CAUSE, "Not implemented yet"); - } - - /** - * Get the list of popular people on The Movie Database. - * - * This list refreshes every day. - * - * @return - * @throws MovieDbException - */ - public TmdbResultsList getPersonPopular() throws MovieDbException { - return getPersonPopular(0); - } - - /** - * Get the list of popular people on The Movie Database. - * - * This list refreshes every day. - * - * @param page - * @return - * @throws MovieDbException - */ - public TmdbResultsList getPersonPopular(int page) throws MovieDbException { - ApiUrl apiUrl = new ApiUrl(apiKey, BASE_PERSON, "/popular"); - - if (page > 0) { - apiUrl.addArgument(PARAM_PAGE, page); - } - - URL url = apiUrl.buildUrl(); - String webpage = WebBrowser.request(url); - - try { - WrapperPersonList wrapper = mapper.readValue(webpage, WrapperPersonList.class); - TmdbResultsList results = new TmdbResultsList(wrapper.getPersonList()); - results.copyWrapper(wrapper); - return results; - } catch (IOException ex) { - LOG.warn("Failed to get person images: {}", ex.getMessage()); - throw new MovieDbException(MovieDbExceptionType.MAPPING_FAILED, webpage, ex); - } - } - - /** - * Get the latest person id. - * - * @throws MovieDbException - */ - public Person getPersonLatest() throws MovieDbException { - ApiUrl apiUrl = new ApiUrl(apiKey, BASE_PERSON, "/latest"); - URL url = apiUrl.buildUrl(); - String webpage = WebBrowser.request(url); - - try { - return mapper.readValue(webpage, Person.class); - } catch (IOException ex) { - LOG.warn("Failed to get latest person: {}", ex.getMessage()); - throw new MovieDbException(MovieDbExceptionType.MAPPING_FAILED, webpage, ex); - } - } - - // - // - // - /** - * This method is used to retrieve the basic information about a production company on TMDb. - * - * @param companyId - * @throws MovieDbException - */ - public Company getCompanyInfo(int companyId) throws MovieDbException { - ApiUrl apiUrl = new ApiUrl(apiKey, BASE_COMPANY); - - apiUrl.addArgument(PARAM_ID, companyId); - - URL url = apiUrl.buildUrl(); - String webpage = WebBrowser.request(url); - - try { - return mapper.readValue(webpage, Company.class); - } catch (IOException ex) { - LOG.warn("Failed to get company information: {}", ex.getMessage()); - throw new MovieDbException(MovieDbExceptionType.MAPPING_FAILED, webpage, ex); - } - } - - /** - * This method is used to retrieve the movies associated with a company. - * - * These movies are returned in order of most recently released to oldest. The default response will return 20 movies per page. - * - * TODO: Implement more than 20 movies - * - * @param companyId - * @param language - * @param page - * @throws MovieDbException - */ - public TmdbResultsList getCompanyMovies(int companyId, String language, int page) throws MovieDbException { - ApiUrl apiUrl = new ApiUrl(apiKey, BASE_COMPANY, "/movies"); - - apiUrl.addArgument(PARAM_ID, companyId); - - if (StringUtils.isNotBlank(language)) { - apiUrl.addArgument(PARAM_LANGUAGE, language); - } - - if (page > 0) { - apiUrl.addArgument(PARAM_PAGE, page); - } - - URL url = apiUrl.buildUrl(); - String webpage = WebBrowser.request(url); - - try { - WrapperCompanyMovies wrapper = mapper.readValue(webpage, WrapperCompanyMovies.class); - TmdbResultsList results = new TmdbResultsList(wrapper.getResults()); - results.copyWrapper(wrapper); - return results; - } catch (IOException ex) { - LOG.warn("Failed to get company movies: {}", ex.getMessage()); - throw new MovieDbException(MovieDbExceptionType.MAPPING_FAILED, webpage, ex); - } - } - - // - // - // - /** - * You can use this method to retrieve the list of genres used on TMDb. - * - * These IDs will correspond to those found in movie calls. - * - * @param language - */ - public TmdbResultsList getGenreList(String language) throws MovieDbException { - ApiUrl apiUrl = new ApiUrl(apiKey, BASE_GENRE, "/list"); - apiUrl.addArgument(PARAM_LANGUAGE, language); - - URL url = apiUrl.buildUrl(); - String webpage = WebBrowser.request(url); - - try { - WrapperGenres wrapper = mapper.readValue(webpage, WrapperGenres.class); - TmdbResultsList results = new TmdbResultsList(wrapper.getGenres()); - results.copyWrapper(wrapper); - return results; - } catch (IOException ex) { - LOG.warn("Failed to get genre list: {}", ex.getMessage()); - throw new MovieDbException(MovieDbExceptionType.MAPPING_FAILED, webpage, ex); - } - } - - /** - * 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. - * - * @param genreId - * @param language - * @param page - */ - public TmdbResultsList getGenreMovies(int genreId, String language, int page, boolean includeAllMovies) throws MovieDbException { - ApiUrl apiUrl = new ApiUrl(apiKey, BASE_GENRE, "/movies"); - apiUrl.addArgument(PARAM_ID, genreId); - - if (StringUtils.isNotBlank(language)) { - apiUrl.addArgument(PARAM_LANGUAGE, language); - } - - if (page > 0) { - apiUrl.addArgument(PARAM_PAGE, page); - } - - apiUrl.addArgument(PARAM_INCLUDE_ALL_MOVIES, includeAllMovies); - - URL url = apiUrl.buildUrl(); - String webpage = WebBrowser.request(url); - - try { - WrapperMovie wrapper = mapper.readValue(webpage, WrapperMovie.class); - TmdbResultsList results = new TmdbResultsList(wrapper.getMovies()); - results.copyWrapper(wrapper); - return results; - } catch (IOException ex) { - LOG.warn("Failed to get genre movie list: {}", ex.getMessage()); - throw new MovieDbException(MovieDbExceptionType.MAPPING_FAILED, webpage, ex); - } - } - // - // - // - - /** - * Search Movies This is a good starting point to start finding movies on TMDb. - * - * @param movieName - * @param searchYear Limit the search to the provided year. Zero (0) will get all years - * @param language The language to include. Can be blank/null. - * @param includeAdult true or false to include adult titles in the search - * @param page The page of results to return. 0 to get the default (first page) - * @throws MovieDbException - */ - public TmdbResultsList searchMovie(String movieName, int searchYear, String language, boolean includeAdult, int page) throws MovieDbException { - ApiUrl apiUrl = new ApiUrl(apiKey, BASE_SEARCH, "movie"); - if (StringUtils.isNotBlank(movieName)) { - apiUrl.addArgument(PARAM_QUERY, movieName); - } - - if (searchYear > 0) { - apiUrl.addArgument(PARAM_YEAR, Integer.toString(searchYear)); - } - - if (StringUtils.isNotBlank(language)) { - apiUrl.addArgument(PARAM_LANGUAGE, language); - } - - apiUrl.addArgument(PARAM_ADULT, Boolean.toString(includeAdult)); - - if (page > 0) { - apiUrl.addArgument(PARAM_PAGE, Integer.toString(page)); - } - - URL url = apiUrl.buildUrl(); - - String webpage = WebBrowser.request(url); - try { - WrapperMovie wrapper = mapper.readValue(webpage, WrapperMovie.class); - TmdbResultsList results = new TmdbResultsList(wrapper.getMovies()); - results.copyWrapper(wrapper); - return results; - } catch (IOException ex) { - LOG.warn("Failed to find movie: {}", ex.getMessage()); - throw new MovieDbException(MovieDbExceptionType.MAPPING_FAILED, webpage, ex); - } - - } - - /** - * Search for collections by name. - * - * @param query - * @param language - * @param page - * @throws MovieDbException - */ - public TmdbResultsList searchCollection(String query, String language, int page) throws MovieDbException { - ApiUrl apiUrl = new ApiUrl(apiKey, BASE_SEARCH, "collections"); - - if (StringUtils.isNotBlank(query)) { - apiUrl.addArgument(PARAM_QUERY, query); - } - - if (StringUtils.isNotBlank(language)) { - apiUrl.addArgument(PARAM_LANGUAGE, language); - } - - if (page > 0) { - apiUrl.addArgument(PARAM_PAGE, Integer.toString(page)); - } - - URL url = apiUrl.buildUrl(); - - String webpage = WebBrowser.request(url); - try { - WrapperCollection wrapper = mapper.readValue(webpage, WrapperCollection.class); - TmdbResultsList results = new TmdbResultsList(wrapper.getResults()); - results.copyWrapper(wrapper); - return results; - } catch (IOException ex) { - LOG.warn("Failed to find collection: {}", ex.getMessage()); - throw new MovieDbException(MovieDbExceptionType.MAPPING_FAILED, webpage, ex); - } - } - - /** - * This is a good starting point to start finding people on TMDb. - * - * The idea is to be a quick and light method so you can iterate through people quickly. - * - * @param personName - * @param includeAdult - * @param page - * @throws MovieDbException - */ - public TmdbResultsList searchPeople(String personName, boolean includeAdult, int page) throws MovieDbException { - ApiUrl apiUrl = new ApiUrl(apiKey, BASE_SEARCH, "person"); - apiUrl.addArgument(PARAM_QUERY, personName); - apiUrl.addArgument(PARAM_ADULT, includeAdult); - - if (page > 0) { - apiUrl.addArgument(PARAM_PAGE, page); - } - - URL url = apiUrl.buildUrl(); - String webpage = WebBrowser.request(url); - - try { - WrapperPerson wrapper = mapper.readValue(webpage, WrapperPerson.class); - TmdbResultsList results = new TmdbResultsList(wrapper.getResults()); - results.copyWrapper(wrapper); - return results; - } catch (IOException ex) { - LOG.warn("Failed to find person: {}", ex.getMessage()); - throw new MovieDbException(MovieDbExceptionType.MAPPING_FAILED, webpage, ex); - } - } - - /** - * Search for lists by name and description. - * - * @param query - * @param language - * @param page - * @throws MovieDbException - */ - public TmdbResultsList searchList(String query, String language, int page) throws MovieDbException { - ApiUrl apiUrl = new ApiUrl(apiKey, BASE_SEARCH, "list"); - - if (StringUtils.isNotBlank(query)) { - apiUrl.addArgument(PARAM_QUERY, query); - } - - if (StringUtils.isNotBlank(language)) { - apiUrl.addArgument(PARAM_LANGUAGE, language); - } - - if (page > 0) { - apiUrl.addArgument(PARAM_PAGE, Integer.toString(page)); - } - - URL url = apiUrl.buildUrl(); - - String webpage = WebBrowser.request(url); - try { - WrapperMovieList wrapper = mapper.readValue(webpage, WrapperMovieList.class); - TmdbResultsList results = new TmdbResultsList(wrapper.getMovieList()); - results.copyWrapper(wrapper); - return results; - } catch (IOException ex) { - LOG.warn("Failed to find list: {}", ex.getMessage()); - throw new MovieDbException(MovieDbExceptionType.MAPPING_FAILED, webpage, ex); - } - } - - /** - * Search Companies. - * - * You can use this method to search for production companies that are part of TMDb. The company IDs will map to those returned - * on movie calls. - * - * http://help.themoviedb.org/kb/api/search-companies - * - * @param companyName - * @param page - * @throws MovieDbException - */ - public TmdbResultsList searchCompanies(String companyName, int page) throws MovieDbException { - ApiUrl apiUrl = new ApiUrl(apiKey, BASE_SEARCH, "company"); - apiUrl.addArgument(PARAM_QUERY, companyName); - - if (page > 0) { - apiUrl.addArgument(PARAM_PAGE, page); - } - - URL url = apiUrl.buildUrl(); - String webpage = WebBrowser.request(url); - try { - WrapperCompany wrapper = mapper.readValue(webpage, WrapperCompany.class); - TmdbResultsList results = new TmdbResultsList(wrapper.getResults()); - results.copyWrapper(wrapper); - return results; - } catch (IOException ex) { - LOG.warn("Failed to find company: {}", ex.getMessage()); - throw new MovieDbException(MovieDbExceptionType.MAPPING_FAILED, webpage, ex); - } - } - - /** - * Search for keywords by name - * - * @param query - * @param page - * @throws MovieDbException - */ - public TmdbResultsList searchKeyword(String query, int page) throws MovieDbException { - ApiUrl apiUrl = new ApiUrl(apiKey, BASE_SEARCH, "keyword"); - - if (StringUtils.isNotBlank(query)) { - apiUrl.addArgument(PARAM_QUERY, query); - } - - if (page > 0) { - apiUrl.addArgument(PARAM_PAGE, Integer.toString(page)); - } - - URL url = apiUrl.buildUrl(); - - String webpage = WebBrowser.request(url); - try { - WrapperKeywords wrapper = mapper.readValue(webpage, WrapperKeywords.class); - TmdbResultsList results = new TmdbResultsList(wrapper.getResults()); - results.copyWrapper(wrapper); - return results; - } catch (IOException ex) { - LOG.warn("Failed to find keyword: {}", ex.getMessage()); - throw new MovieDbException(MovieDbExceptionType.MAPPING_FAILED, webpage, ex); - } - } - // - // - // - - /** - * Get a list by its ID - * - * @param listId - * @return The list and its items - * @throws MovieDbException - */ - public MovieDbList getList(String listId) throws MovieDbException { - ApiUrl apiUrl = new ApiUrl(apiKey, BASE_LIST); - apiUrl.addArgument(PARAM_ID, listId); - - URL url = apiUrl.buildUrl(); - String webpage = WebBrowser.request(url); - - try { - return mapper.readValue(webpage, MovieDbList.class); - } catch (IOException ex) { - LOG.warn("Failed to get list: {}", ex.getMessage()); - throw new MovieDbException(MovieDbExceptionType.MAPPING_FAILED, webpage, ex); - } - } - // - // - // - - /** - * Get the basic information for a specific keyword id. - * - * @param keywordId - * @return - * @throws MovieDbException - */ - public Keyword getKeyword(String keywordId) throws MovieDbException { - ApiUrl apiUrl = new ApiUrl(apiKey, BASE_KEYWORD); - apiUrl.addArgument(PARAM_ID, keywordId); - - URL url = apiUrl.buildUrl(); - String webpage = WebBrowser.request(url); - - try { - return mapper.readValue(webpage, Keyword.class); - } catch (IOException ex) { - LOG.warn("Failed to get keyword: {}", ex.getMessage()); - throw new MovieDbException(MovieDbExceptionType.MAPPING_FAILED, webpage, ex); - } - - } - - /** - * Get the list of movies for a particular keyword by id. - * - * @param keywordId - * @param language - * @param page - * @return List of movies with the keyword - * @throws MovieDbException - */ - public TmdbResultsList getKeywordMovies(String keywordId, String language, int page) throws MovieDbException { - ApiUrl apiUrl = new ApiUrl(apiKey, BASE_KEYWORD, "/movies"); - apiUrl.addArgument(PARAM_ID, keywordId); - - if (StringUtils.isNotBlank(language)) { - apiUrl.addArgument(PARAM_LANGUAGE, language); - } - - if (page > 0) { - apiUrl.addArgument(PARAM_PAGE, page); - } - - URL url = apiUrl.buildUrl(); - String webpage = WebBrowser.request(url); - - try { - WrapperKeywordMovies wrapper = mapper.readValue(webpage, WrapperKeywordMovies.class); - TmdbResultsList results = new TmdbResultsList(wrapper.getResults()); - results.copyWrapper(wrapper); - return results; - } catch (IOException ex) { - LOG.warn("Failed to get top rated movies: {}", ex.getMessage()); - throw new MovieDbException(MovieDbExceptionType.MAPPING_FAILED, webpage, ex); - } - - } - // - // - // - - public void getMovieChangesList(int page, String startDate, String endDate) throws MovieDbException { - throw new MovieDbException(MovieDbExceptionType.UNKNOWN_CAUSE, "Not implemented yet"); - } - - public void getPersonChangesList(int page, String startDate, String endDate) throws MovieDbException { - throw new MovieDbException(MovieDbExceptionType.UNKNOWN_CAUSE, "Not implemented yet"); - } - // - - // - public TmdbResultsList getJobs() throws MovieDbException { - ApiUrl apiUrl = new ApiUrl(apiKey, BASE_JOB, "/list"); - - URL url = apiUrl.buildUrl(); - String webpage = WebBrowser.request(url); - - try { - WrapperJobList wrapper = mapper.readValue(webpage, WrapperJobList.class); - TmdbResultsList results = new TmdbResultsList(wrapper.getJobs()); - results.copyWrapper(wrapper); - return results; - } catch (IOException ex) { - LOG.warn("Failed to get job list: {}", ex.getMessage()); - throw new MovieDbException(MovieDbExceptionType.MAPPING_FAILED, webpage, ex); - } - } - // - - // - /** - * Discover movies by different types of data like average rating, number of votes, genres and certifications. - * - * You can alternatively create a "discover" object and pass it to this method to cut out the requirement for all of these - * parameters - * - * @param page Minimum value is 1 - * @param language ISO 639-1 code. - * @param sortBy Available options are vote_average.desc, vote_average.asc, release_date.desc, release_date.asc, - * popularity.desc, popularity.asc - * @param includeAdult Toggle the inclusion of adult titles - * @param year Filter the results release dates to matches that include this value - * @param primaryReleaseYear Filter the results so that only the primary release date year has this value - * @param voteCountGte Only include movies that are equal to, or have a vote count higher than this value - * @param voteAverageGte Only include movies that are equal to, or have a higher average rating than this value - * @param withGenres Only include movies with the specified genres. Expected value is an integer (the id of a genre). Multiple - * values can be specified. Comma separated indicates an 'AND' query, while a pipe (|) separated value indicates an 'OR'. - * @param releaseDateGte The minimum release to include. Expected format is YYYY-MM-DD - * @param releaseDateLte The maximum release to include. Expected format is YYYY-MM-DD - * @param certificationCountry Only include movies with certifications for a specific country. When this value is specified, - * 'certificationLte' is required. A ISO 3166-1 is expected. - * @param certificationLte Only include movies with this certification and lower. Expected value is a valid certification for - * the specified 'certificationCountry'. - * @param withCompanies Filter movies to include a specific company. Expected value is an integer (the id of a company). They - * can be comma separated to indicate an 'AND' query. - * @return - * @throws MovieDbException - */ - public TmdbResultsList getDiscover(int page, String language, String sortBy, boolean includeAdult, int year, - int primaryReleaseYear, int voteCountGte, float voteAverageGte, String withGenres, String releaseDateGte, - String releaseDateLte, String certificationCountry, String certificationLte, String withCompanies) throws MovieDbException { - - Discover discover = new Discover(); - discover.page(page) - .language(language) - .sortBy(sortBy) - .includeAdult(includeAdult) - .year(year) - .primaryReleaseYear(primaryReleaseYear) - .voteCountGte(voteCountGte) - .voteAverageGte(voteAverageGte) - .withGenres(withGenres) - .releaseDateGte(releaseDateGte) - .releaseDateLte(releaseDateLte) - .certificationCountry(certificationCountry) - .certificationLte(certificationLte) - .withCompanies(withCompanies); - - return getDiscover(discover); - } - - /** - * Discover movies by different types of data like average rating, number of votes, genres and certifications. - * - * @param discover A discover object containing the search criteria required - * @return - * @throws MovieDbException - */ - public TmdbResultsList getDiscover(Discover discover) throws MovieDbException { - ApiUrl apiUrl = new ApiUrl(apiKey, BASE_DISCOVER, "/movie"); - - apiUrl.setArguments(discover.getParams()); - - URL url = apiUrl.buildUrl(); - String webpage = WebBrowser.request(url); - - try { - WrapperMovie wrapper = mapper.readValue(webpage, WrapperMovie.class); - TmdbResultsList results = new TmdbResultsList(wrapper.getMovies()); - results.copyWrapper(wrapper); - return results; - } catch (IOException ex) { - LOG.warn("Failed to get discover list: {}", ex.getMessage()); - throw new MovieDbException(MovieDbExceptionType.MAPPING_FAILED, webpage, ex); - } - } - // -} +/* + * Copyright (c) 2004-2013 Stuart Boston + * + * This file is part of TheMovieDB API. + * + * TheMovieDB API is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * any later version. + * + * TheMovieDB API is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with TheMovieDB API. If not, see . + * + */ +package com.omertron.themoviedbapi; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.omertron.themoviedbapi.MovieDbException.MovieDbExceptionType; +import com.omertron.themoviedbapi.model.*; +import com.omertron.themoviedbapi.results.*; +import com.omertron.themoviedbapi.tools.ApiUrl; +import static com.omertron.themoviedbapi.tools.ApiUrl.*; +import com.omertron.themoviedbapi.tools.WebBrowser; +import com.omertron.themoviedbapi.wrapper.*; +import java.io.IOException; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import org.apache.commons.lang3.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * 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 TheMovieDbApi { + + private static final Logger LOG = LoggerFactory.getLogger(TheMovieDbApi.class); + private String apiKey; + private TmdbConfiguration tmdbConfig; + // API Methods + private static final String BASE_MOVIE = "movie/"; + private static final String BASE_PERSON = "person/"; + private static final String BASE_COMPANY = "company/"; + private static final String BASE_GENRE = "genre/"; + private static final String BASE_AUTH = "authentication/"; + private static final String BASE_COLLECTION = "collection/"; +// private static final String BASE_ACCOUNT = "account/"; + private static final String BASE_SEARCH = "search/"; + private static final String BASE_LIST = "list/"; + private static final String BASE_KEYWORD = "keyword/"; + private static final String BASE_JOB = "job/"; + private static final String BASE_DISCOVER = "discover/"; + // Jackson JSON configuration + private static ObjectMapper mapper = new ObjectMapper(); + + /** + * API for The Movie Db. + * + * @param apiKey + * @throws MovieDbException + */ + public TheMovieDbApi(String apiKey) throws MovieDbException { + this.apiKey = apiKey; + ApiUrl apiUrl = new ApiUrl(apiKey, "configuration"); + URL configUrl = apiUrl.buildUrl(); + String webpage = WebBrowser.request(configUrl); + + try { + WrapperConfig wc = mapper.readValue(webpage, WrapperConfig.class); + tmdbConfig = wc.getTmdbConfiguration(); + } catch (IOException ex) { + throw new MovieDbException(MovieDbExceptionType.MAPPING_FAILED, "Failed to read configuration", ex); + } + } + + /** + * Get the API key that is to be used + * + */ + public String getApiKey() { + return apiKey; + } + + /** + * Set the proxy information + * + * @param host + * @param port + * @param username + * @param password + */ + public void setProxy(String host, String port, String username, String password) { + WebBrowser.setProxyHost(host); + WebBrowser.setProxyPort(port); + WebBrowser.setProxyUsername(username); + WebBrowser.setProxyPassword(password); + } + + /** + * Set the connection and read time out values + * + * @param connect + * @param read + */ + public void setTimeout(int connect, int read) { + WebBrowser.setWebTimeoutConnect(connect); + 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 exact match + * @return True if there is a match, False otherwise. + */ + public static boolean compareMovies(MovieDb moviedb, String title, String year) { + return compareMovies(moviedb, title, year, 0); + } + + /** + * 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 + * @param maxDistance The Levenshtein Distance between the two titles. 0 = exact match + * @return True if there is a match, False otherwise. + */ + public static boolean compareMovies(MovieDb moviedb, String title, String year, int maxDistance) { + if ((moviedb == null) || (StringUtils.isBlank(title))) { + return Boolean.FALSE; + } + + if (isValidYear(year) && isValidYear(moviedb.getReleaseDate())) { + // Compare with year + String movieYear = moviedb.getReleaseDate().substring(0, 4); + if (movieYear.equals(year)) { + if (compareDistance(moviedb.getOriginalTitle(), title, maxDistance)) { + return Boolean.TRUE; + } + + if (compareDistance(moviedb.getTitle(), title, maxDistance)) { + return Boolean.TRUE; + } + } + } + + // Compare without year + if (compareDistance(moviedb.getOriginalTitle(), title, maxDistance)) { + return Boolean.TRUE; + } + + if (compareDistance(moviedb.getTitle(), title, maxDistance)) { + return Boolean.TRUE; + } + + return Boolean.FALSE; + } + + /** + * Compare the Levenshtein Distance between the two strings + * + * @param title1 + * @param title2 + * @param distance + */ + private static boolean compareDistance(String title1, String title2, int distance) { + return (StringUtils.getLevenshteinDistance(title1, title2) <= distance); + } + + /** + * Check the year is not blank or UNKNOWN + * + * @param year + */ + private static boolean isValidYear(String year) { + return (StringUtils.isNotBlank(year) && !year.equals("UNKNOWN")); + } + + // + /** + * Get the configuration information + */ + public TmdbConfiguration getConfiguration() { + return tmdbConfig; + } + + /** + * Generate the full image URL from the size and image path + * + * @param imagePath + * @param requiredSize + * @throws MovieDbException + */ + public URL createImageUrl(String imagePath, String requiredSize) throws MovieDbException { + if (!tmdbConfig.isValidSize(requiredSize)) { + throw new MovieDbException(MovieDbExceptionType.INVALID_IMAGE, requiredSize); + } + + StringBuilder sb = new StringBuilder(tmdbConfig.getBaseUrl()); + sb.append(requiredSize); + sb.append(imagePath); + try { + return (new URL(sb.toString())); + } catch (MalformedURLException ex) { + LOG.warn("Failed to create image URL: {}", ex.getMessage()); + throw new MovieDbException(MovieDbExceptionType.INVALID_URL, sb.toString(), ex); + } + } + + // + // + // + /** + * This method is used to generate a valid request token for user based authentication. + * + * A request token is required in order to request a session id. + * + * You can generate any number of request tokens but they will expire after 60 minutes. + * + * As soon as a valid session id has been created the token will be destroyed. + * + * @throws MovieDbException + */ + public TokenAuthorisation getAuthorisationToken() throws MovieDbException { + ApiUrl apiUrl = new ApiUrl(apiKey, BASE_AUTH, "token/new"); + + URL url = apiUrl.buildUrl(); + String webpage = WebBrowser.request(url); + + try { + return mapper.readValue(webpage, TokenAuthorisation.class); + } catch (IOException ex) { + LOG.warn("Failed to get Authorisation Token: {}", ex.getMessage()); + throw new MovieDbException(MovieDbExceptionType.AUTHORISATION_FAILURE, webpage, ex); + } + } + + /** + * This method is used to generate a session id for user based authentication. + * + * A session id is required in order to use any of the write methods. + * + * @param token + * @throws MovieDbException + */ + public TokenSession getSessionToken(TokenAuthorisation token) throws MovieDbException { + ApiUrl apiUrl = new ApiUrl(apiKey, BASE_AUTH, "session/new"); + + if (!token.getSuccess()) { + LOG.warn("Authorisation token was not successful!"); + throw new MovieDbException(MovieDbExceptionType.AUTHORISATION_FAILURE, "Authorisation token was not successful!"); + } + + apiUrl.addArgument(PARAM_TOKEN, token.getRequestToken()); + URL url = apiUrl.buildUrl(); + String webpage = WebBrowser.request(url); + + try { + return mapper.readValue(webpage, TokenSession.class); + } catch (IOException ex) { + LOG.warn("Failed to get Session Token: {}", ex.getMessage()); + throw new MovieDbException(MovieDbExceptionType.MAPPING_FAILED, webpage, ex); + } + } + + /** + * This method is used to generate a guest session id. + * + * A guest session can be used to rate movies without having a registered TMDb user account. + * + * You should only generate a single guest session per user (or device) as you will be able to attach the ratings to a TMDb user + * account in the future. + * + * There are also IP limits in place so you should always make sure it's the end user doing the guest session actions. + * + * If a guest session is not used for the first time within 24 hours, it will be automatically discarded. + * + * @throws MovieDbException + */ + public TokenSession getGuestSessionToken() throws MovieDbException { + ApiUrl apiUrl = new ApiUrl(apiKey, BASE_AUTH, "guest_session/new"); + + URL url = apiUrl.buildUrl(); + String webpage = WebBrowser.request(url); + + try { + return mapper.readValue(webpage, TokenSession.class); + } catch (IOException ex) { + LOG.warn("Failed to get Session Token: {}", ex.getMessage()); + throw new MovieDbException(MovieDbExceptionType.MAPPING_FAILED, webpage, ex); + } + } + + // + // + // + // + // + // + /** + * 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 + * @throws MovieDbException + */ + public MovieDb getMovieInfo(int movieId, String language, String... appendToResponse) throws MovieDbException { + ApiUrl apiUrl = new ApiUrl(apiKey, BASE_MOVIE); + + apiUrl.addArgument(PARAM_ID, movieId); + + if (StringUtils.isNotBlank(language)) { + apiUrl.addArgument(PARAM_LANGUAGE, language); + } + + apiUrl.appendToResponse(appendToResponse); + + URL url = apiUrl.buildUrl(); + String webpage = WebBrowser.request(url); + try { + return mapper.readValue(webpage, MovieDb.class); + } catch (IOException ex) { + LOG.warn("Failed to get movie info: {}", ex.getMessage()); + throw new MovieDbException(MovieDbExceptionType.MAPPING_FAILED, webpage, ex); + } + } + + /** + * This method is used to retrieve all of the basic movie information. + * + * It will return the single highest rated poster and backdrop. + * + * @param imdbId + * @param language + * @throws MovieDbException + */ + public MovieDb getMovieInfoImdb(String imdbId, String language, String... appendToResponse) throws MovieDbException { + ApiUrl apiUrl = new ApiUrl(apiKey, BASE_MOVIE); + + apiUrl.addArgument(PARAM_ID, imdbId); + + if (StringUtils.isNotBlank(language)) { + apiUrl.addArgument(PARAM_LANGUAGE, language); + } + + apiUrl.appendToResponse(appendToResponse); + + URL url = apiUrl.buildUrl(); + String webpage = WebBrowser.request(url); + try { + return mapper.readValue(webpage, MovieDb.class); + } catch (IOException ex) { + LOG.warn("Failed to get movie info: {}", ex.getMessage()); + throw new MovieDbException(MovieDbExceptionType.MAPPING_FAILED, webpage, ex); + } + } + + /** + * This method is used to retrieve all of the alternative titles we have for a particular movie. + * + * @param movieId + * @param country + * @throws MovieDbException + */ + public TmdbResultsList getMovieAlternativeTitles(int movieId, String country, String... appendToResponse) throws MovieDbException { + ApiUrl apiUrl = new ApiUrl(apiKey, BASE_MOVIE, "/alternative_titles"); + apiUrl.addArgument(PARAM_ID, movieId); + + if (StringUtils.isNotBlank(country)) { + apiUrl.addArgument(PARAM_COUNTRY, country); + } + + apiUrl.appendToResponse(appendToResponse); + + URL url = apiUrl.buildUrl(); + String webpage = WebBrowser.request(url); + try { + WrapperAlternativeTitles wrapper = mapper.readValue(webpage, WrapperAlternativeTitles.class); + TmdbResultsList results = new TmdbResultsList(wrapper.getTitles()); + results.copyWrapper(wrapper); + return results; + } catch (IOException ex) { + LOG.warn("Failed to get movie alternative titles: {}", ex.getMessage()); + throw new MovieDbException(MovieDbExceptionType.MAPPING_FAILED, webpage, ex); + } + } + + /** + * Get the cast information for a specific movie id. + * + * TODO: Add a function to enrich the data with the people methods + * + * @param movieId + * @throws MovieDbException + */ + public TmdbResultsList getMovieCasts(int movieId, String... appendToResponse) throws MovieDbException { + ApiUrl apiUrl = new ApiUrl(apiKey, BASE_MOVIE, "/casts"); + apiUrl.addArgument(PARAM_ID, movieId); + + apiUrl.appendToResponse(appendToResponse); + + URL url = apiUrl.buildUrl(); + String webpage = WebBrowser.request(url); + + try { + WrapperMovieCasts wrapper = mapper.readValue(webpage, WrapperMovieCasts.class); + TmdbResultsList results = new TmdbResultsList(wrapper.getAll()); + results.copyWrapper(wrapper); + return results; + } catch (IOException ex) { + LOG.warn("Failed to get movie casts: {}", ex.getMessage()); + throw new MovieDbException(MovieDbExceptionType.MAPPING_FAILED, webpage, ex); + } + } + + /** + * This method should be used when you’re wanting to retrieve all of the images for a particular movie. + * + * @param movieId + * @param language + * @throws MovieDbException + */ + public TmdbResultsList getMovieImages(int movieId, String language, String... appendToResponse) throws MovieDbException { + ApiUrl apiUrl = new ApiUrl(apiKey, BASE_MOVIE, "/images"); + apiUrl.addArgument(PARAM_ID, movieId); + + if (StringUtils.isNotBlank(language)) { + apiUrl.addArgument(PARAM_LANGUAGE, language); + } + + apiUrl.appendToResponse(appendToResponse); + + URL url = apiUrl.buildUrl(); + String webpage = WebBrowser.request(url); + + try { + WrapperImages wrapper = mapper.readValue(webpage, WrapperImages.class); + TmdbResultsList results = new TmdbResultsList(wrapper.getAll()); + results.copyWrapper(wrapper); + return results; + } catch (IOException ex) { + LOG.warn("Failed to get movie images: {}", ex.getMessage()); + throw new MovieDbException(MovieDbExceptionType.MAPPING_FAILED, webpage, ex); + } + } + + /** + * 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 + * @throws MovieDbException + */ + public TmdbResultsList getMovieKeywords(int movieId, String... appendToResponse) throws MovieDbException { + ApiUrl apiUrl = new ApiUrl(apiKey, BASE_MOVIE, "/keywords"); + apiUrl.addArgument(PARAM_ID, movieId); + + apiUrl.appendToResponse(appendToResponse); + + URL url = apiUrl.buildUrl(); + String webpage = WebBrowser.request(url); + + try { + WrapperMovieKeywords wrapper = mapper.readValue(webpage, WrapperMovieKeywords.class); + TmdbResultsList results = new TmdbResultsList(wrapper.getKeywords()); + results.copyWrapper(wrapper); + return results; + } catch (IOException ex) { + LOG.warn("Failed to get movie keywords: {}", ex.getMessage()); + throw new MovieDbException(MovieDbExceptionType.MAPPING_FAILED, webpage, ex); + } + } + + /** + * This method is used to retrieve all of the release and certification data we have for a specific movie. + * + * @param movieId + * @param language + * @throws MovieDbException + */ + public TmdbResultsList getMovieReleaseInfo(int movieId, String language, String... appendToResponse) throws MovieDbException { + ApiUrl apiUrl = new ApiUrl(apiKey, BASE_MOVIE, "/releases"); + apiUrl.addArgument(PARAM_ID, movieId); + apiUrl.addArgument(PARAM_LANGUAGE, language); + + apiUrl.appendToResponse(appendToResponse); + + URL url = apiUrl.buildUrl(); + String webpage = WebBrowser.request(url); + + try { + WrapperReleaseInfo wrapper = mapper.readValue(webpage, WrapperReleaseInfo.class); + TmdbResultsList results = new TmdbResultsList(wrapper.getCountries()); + results.copyWrapper(wrapper); + return results; + } catch (IOException ex) { + LOG.warn("Failed to get movie release information: {}", ex.getMessage()); + throw new MovieDbException(MovieDbExceptionType.MAPPING_FAILED, webpage, ex); + } + } + + /** + * This method is used to retrieve all of the trailers for a particular movie. + * + * Supported sites are YouTube and QuickTime. + * + * @param movieId + * @param language + * @throws MovieDbException + */ + public TmdbResultsList getMovieTrailers(int movieId, String language, String... appendToResponse) throws MovieDbException { + ApiUrl apiUrl = new ApiUrl(apiKey, BASE_MOVIE, "/trailers"); + apiUrl.addArgument(PARAM_ID, movieId); + + if (StringUtils.isNotBlank(language)) { + apiUrl.addArgument(PARAM_LANGUAGE, language); + } + + apiUrl.appendToResponse(appendToResponse); + + URL url = apiUrl.buildUrl(); + String webpage = WebBrowser.request(url); + + try { + WrapperTrailers wrapper = mapper.readValue(webpage, WrapperTrailers.class); + TmdbResultsList results = new TmdbResultsList(wrapper.getAll()); + results.copyWrapper(wrapper); + return results; + } catch (IOException ex) { + LOG.warn("Failed to get movie trailers: {}", ex.getMessage()); + throw new MovieDbException(MovieDbExceptionType.MAPPING_FAILED, webpage, ex); + } + } + + /** + * This method is used to retrieve a list of the available translations for a specific movie. + * + * @param movieId + * @throws MovieDbException + */ + public TmdbResultsList getMovieTranslations(int movieId, String... appendToResponse) throws MovieDbException { + ApiUrl apiUrl = new ApiUrl(apiKey, BASE_MOVIE, "/translations"); + apiUrl.addArgument(PARAM_ID, movieId); + + apiUrl.appendToResponse(appendToResponse); + + URL url = apiUrl.buildUrl(); + String webpage = WebBrowser.request(url); + + try { + WrapperTranslations wrapper = mapper.readValue(webpage, WrapperTranslations.class); + TmdbResultsList results = new TmdbResultsList(wrapper.getTranslations()); + results.copyWrapper(wrapper); + return results; + } catch (IOException ex) { + LOG.warn("Failed to get movie tranlations: {}", ex.getMessage()); + throw new MovieDbException(MovieDbExceptionType.MAPPING_FAILED, webpage, ex); + } + } + + /** + * The similar movies method will let you retrieve the similar movies for a particular movie. + * + * This data is created dynamically but with the help of users votes on TMDb. + * + * The data is much better with movies that have more keywords + * + * @param movieId + * @param language + * @param page + * @throws MovieDbException + */ + public TmdbResultsList getSimilarMovies(int movieId, String language, int page, String... appendToResponse) throws MovieDbException { + ApiUrl apiUrl = new ApiUrl(apiKey, BASE_MOVIE, "/similar_movies"); + apiUrl.addArgument(PARAM_ID, movieId); + + if (StringUtils.isNotBlank(language)) { + apiUrl.addArgument(PARAM_LANGUAGE, language); + } + + if (page > 0) { + apiUrl.addArgument(PARAM_PAGE, page); + } + + apiUrl.appendToResponse(appendToResponse); + + URL url = apiUrl.buildUrl(); + String webpage = WebBrowser.request(url); + + try { + WrapperMovie wrapper = mapper.readValue(webpage, WrapperMovie.class); + TmdbResultsList results = new TmdbResultsList(wrapper.getMovies()); + results.copyWrapper(wrapper); + return results; + } catch (IOException ex) { + LOG.warn("Failed to get similar movies: {}", ex.getMessage()); + throw new MovieDbException(MovieDbExceptionType.MAPPING_FAILED, webpage, ex); + } + } + + public TmdbResultsList getReviews(int movieId, String language, int page, String... appendToResponse) throws MovieDbException { + ApiUrl apiUrl = new ApiUrl(apiKey, BASE_MOVIE, "/reviews"); + apiUrl.addArgument(PARAM_ID, movieId); + + if (StringUtils.isNotBlank(language)) { + apiUrl.addArgument(PARAM_LANGUAGE, language); + } + + if (page > 0) { + apiUrl.addArgument(PARAM_PAGE, page); + } + + apiUrl.appendToResponse(appendToResponse); + + URL url = apiUrl.buildUrl(); + String webpage = WebBrowser.request(url); + + try { + WrapperReviews wrapper = mapper.readValue(webpage, WrapperReviews.class); + TmdbResultsList results = new TmdbResultsList(wrapper.getReviews()); + results.copyWrapper(wrapper); + return results; + } catch (IOException ex) { + LOG.warn("Failed to get reviews: {}", ex.getMessage()); + throw new MovieDbException(MovieDbExceptionType.MAPPING_FAILED, webpage, ex); + } + } + + /** + * Get the lists that the movie belongs to + * + * @param movieId + * @param language + * @param page + * @throws MovieDbException + */ + public TmdbResultsList getMovieLists(int movieId, String language, int page, String... appendToResponse) throws MovieDbException { + ApiUrl apiUrl = new ApiUrl(apiKey, BASE_MOVIE, "/lists"); + apiUrl.addArgument(PARAM_ID, movieId); + + if (StringUtils.isNotBlank(language)) { + apiUrl.addArgument(PARAM_LANGUAGE, language); + } + + if (page > 0) { + apiUrl.addArgument(PARAM_PAGE, page); + } + + apiUrl.appendToResponse(appendToResponse); + + URL url = apiUrl.buildUrl(); + String webpage = WebBrowser.request(url); + + try { + WrapperMovieList wrapper = mapper.readValue(webpage, WrapperMovieList.class); + TmdbResultsList results = new TmdbResultsList(wrapper.getMovieList()); + results.copyWrapper(wrapper); + return results; + } catch (IOException ex) { + LOG.warn("Failed to get movie lists: {}", ex.getMessage()); + throw new MovieDbException(MovieDbExceptionType.MAPPING_FAILED, webpage, ex); + } + } + + /** + * Get the changes for a specific movie id. + * + * Changes are grouped by key, and ordered by date in descending order. + * + * By default, only the last 24 hours of changes are returned. + * + * The maximum number of days that can be returned in a single request is 14. + * + * The language is present on fields that are translatable. + * + * TODO: DOES NOT WORK AT THE MOMENT. This is due to the "value" item changing type in the ChangeItem + * + * @param movieId + * @param startDate the start date of the changes, optional + * @param endDate the end date of the changes, optional + * @throws MovieDbException + */ + public TmdbResultsMap> getMovieChanges(int movieId, String startDate, String endDate) throws MovieDbException { + ApiUrl apiUrl = new ApiUrl(apiKey, BASE_MOVIE, "/changes"); + apiUrl.addArgument(PARAM_ID, movieId); + + if (StringUtils.isNotBlank(startDate)) { + apiUrl.addArgument(PARAM_START_DATE, startDate); + } + + if (StringUtils.isNotBlank(endDate)) { + apiUrl.addArgument(PARAM_END_DATE, endDate); + } + + URL url = apiUrl.buildUrl(); + String webpage = WebBrowser.request(url); + try { + WrapperChanges wrapper = mapper.readValue(webpage, WrapperChanges.class); + + Map> results = new HashMap>(); + for (ChangeKeyItem changeItem : wrapper.getChangedItems()) { + results.put(changeItem.getKey(), changeItem.getChangedItems()); + } + + return new TmdbResultsMap>(results); + } catch (IOException ex) { + LOG.warn("Failed to get movie changes: {}", ex.getMessage()); + throw new MovieDbException(MovieDbExceptionType.MAPPING_FAILED, webpage, ex); + } + + } + + /** + * This method is used to retrieve the newest movie that was added to TMDb. + * + */ + public MovieDb getLatestMovie() throws MovieDbException { + ApiUrl apiUrl = new ApiUrl(apiKey, BASE_MOVIE, "/latest"); + URL url = apiUrl.buildUrl(); + String webpage = WebBrowser.request(url); + + try { + return mapper.readValue(webpage, MovieDb.class); + } catch (IOException ex) { + LOG.warn("Failed to get latest movie: {}", ex.getMessage()); + throw new MovieDbException(MovieDbExceptionType.MAPPING_FAILED, webpage, ex); + } + } + + /** + * Get the list of upcoming movies. + * + * This list refreshes every day. + * + * The maximum number of items this list will include is 100. + * + * @throws MovieDbException + */ + public TmdbResultsList getUpcoming(String language, int page) throws MovieDbException { + ApiUrl apiUrl = new ApiUrl(apiKey, BASE_MOVIE, "upcoming"); + + if (StringUtils.isNotBlank(language)) { + apiUrl.addArgument(PARAM_LANGUAGE, language); + } + + if (page > 0) { + apiUrl.addArgument(PARAM_PAGE, page); + } + + URL url = apiUrl.buildUrl(); + String webpage = WebBrowser.request(url); + + try { + WrapperMovie wrapper = mapper.readValue(webpage, WrapperMovie.class); + TmdbResultsList results = new TmdbResultsList(wrapper.getMovies()); + results.copyWrapper(wrapper); + return results; + } catch (IOException ex) { + LOG.warn("Failed to get upcoming 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. + * + * TODO: Implement more than 20 movies + * + * @param language + * @param page + * @throws MovieDbException + */ + public TmdbResultsList getNowPlayingMovies(String language, int page) throws MovieDbException { + ApiUrl apiUrl = new ApiUrl(apiKey, BASE_MOVIE, "now-playing"); + + if (StringUtils.isNotBlank(language)) { + apiUrl.addArgument(PARAM_LANGUAGE, language); + } + + if (page > 0) { + apiUrl.addArgument(PARAM_PAGE, page); + } + + URL url = apiUrl.buildUrl(); + String webpage = WebBrowser.request(url); + + try { + WrapperMovie wrapper = mapper.readValue(webpage, WrapperMovie.class); + TmdbResultsList results = new TmdbResultsList(wrapper.getMovies()); + results.copyWrapper(wrapper); + return results; + } catch (IOException ex) { + LOG.warn("Failed to get now playing movies: {}", ex.getMessage()); + throw new MovieDbException(MovieDbExceptionType.MAPPING_FAILED, webpage, ex); + } + } + + /** + * This method is used to retrieve the daily movie popularity list. + * + * This list is updated daily. The default response will return 20 movies. + * + * TODO: Implement more than 20 movies + * + * @param language + * @param page + * @throws MovieDbException + */ + public TmdbResultsList getPopularMovieList(String language, int page) throws MovieDbException { + ApiUrl apiUrl = new ApiUrl(apiKey, BASE_MOVIE, "popular"); + + if (StringUtils.isNotBlank(language)) { + apiUrl.addArgument(PARAM_LANGUAGE, language); + } + + if (page > 0) { + apiUrl.addArgument(PARAM_PAGE, page); + } + + URL url = apiUrl.buildUrl(); + String webpage = WebBrowser.request(url); + + try { + WrapperMovie wrapper = mapper.readValue(webpage, WrapperMovie.class); + TmdbResultsList results = new TmdbResultsList(wrapper.getMovies()); + results.copyWrapper(wrapper); + return results; + } catch (IOException ex) { + LOG.warn("Failed to get popular movie list: {}", ex.getMessage()); + throw new MovieDbException(MovieDbExceptionType.MAPPING_FAILED, webpage, ex); + } + } + + /** + * This method is used to retrieve the top rated movies that have over 10 votes on TMDb. + * + * The default response will return 20 movies. + * + * TODO: Implement more than 20 movies + * + * @param language + * @param page + * @throws MovieDbException + */ + public TmdbResultsList getTopRatedMovies(String language, int page) throws MovieDbException { + ApiUrl apiUrl = new ApiUrl(apiKey, BASE_MOVIE, "top-rated"); + + if (StringUtils.isNotBlank(language)) { + apiUrl.addArgument(PARAM_LANGUAGE, language); + } + + if (page > 0) { + apiUrl.addArgument(PARAM_PAGE, page); + } + + URL url = apiUrl.buildUrl(); + String webpage = WebBrowser.request(url); + + try { + WrapperMovie wrapper = mapper.readValue(webpage, WrapperMovie.class); + TmdbResultsList results = new TmdbResultsList(wrapper.getMovies()); + results.copyWrapper(wrapper); + return results; + } catch (IOException ex) { + LOG.warn("Failed to get top rated movies: {}", ex.getMessage()); + throw new MovieDbException(MovieDbExceptionType.MAPPING_FAILED, webpage, ex); + } + } + + /** + * This method lets users rate a movie. + * + * A valid session id is required. + * + * @param sessionId + * @param rating + * @throws MovieDbException + */ + public boolean postMovieRating(String sessionId, String rating) throws MovieDbException { + ApiUrl apiUrl = new ApiUrl(apiKey, BASE_MOVIE, "/rating"); + + apiUrl.addArgument(PARAM_SESSION, sessionId); + apiUrl.addArgument(PARAM_VALUE, rating); + + throw new MovieDbException(MovieDbExceptionType.UNKNOWN_CAUSE, "Not implemented yet"); + } + + // + // + // + /** + * 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 collectionId + * @param language + * @throws MovieDbException + */ + public CollectionInfo getCollectionInfo(int collectionId, String language) throws MovieDbException { + ApiUrl apiUrl = new ApiUrl(apiKey, BASE_COLLECTION); + apiUrl.addArgument(PARAM_ID, collectionId); + + if (StringUtils.isNotBlank(language)) { + apiUrl.addArgument(PARAM_LANGUAGE, language); + } + + URL url = apiUrl.buildUrl(); + String webpage = WebBrowser.request(url); + + try { + return mapper.readValue(webpage, CollectionInfo.class); + } catch (IOException ex) { + LOG.warn("Failed to get collection information: {}", ex.getMessage()); + throw new MovieDbException(MovieDbExceptionType.MAPPING_FAILED, webpage, ex); + } + } + + /** + * Get all of the images for a particular collection by collection id. + * + * @param collectionId + * @param language + * @throws MovieDbException + */ + public TmdbResultsList getCollectionImages(int collectionId, String language) throws MovieDbException { + ApiUrl apiUrl = new ApiUrl(apiKey, BASE_COLLECTION, "/images"); + apiUrl.addArgument(PARAM_ID, collectionId); + + if (StringUtils.isNotBlank(language)) { + apiUrl.addArgument(PARAM_LANGUAGE, language); + } + + URL url = apiUrl.buildUrl(); + String webpage = WebBrowser.request(url); + + try { + WrapperImages wrapper = mapper.readValue(webpage, WrapperImages.class); + TmdbResultsList results = new TmdbResultsList(wrapper.getAll(ArtworkType.POSTER, ArtworkType.BACKDROP)); + results.copyWrapper(wrapper); + return results; + } catch (IOException ex) { + LOG.warn("Failed to get collection images: {}", ex.getMessage()); + throw new MovieDbException(MovieDbExceptionType.MAPPING_FAILED, webpage, ex); + } + } + + // + // + // + /** + * This method is used to retrieve all of the basic person information. + * + * It will return the single highest rated profile image. + * + * @param personId + * @throws MovieDbException + */ + public Person getPersonInfo(int personId, String... appendToResponse) throws MovieDbException { + ApiUrl apiUrl = new ApiUrl(apiKey, BASE_PERSON); + + apiUrl.addArgument(PARAM_ID, personId); + apiUrl.appendToResponse(appendToResponse); + + URL url = apiUrl.buildUrl(); + String webpage = WebBrowser.request(url); + + try { + return mapper.readValue(webpage, Person.class); + } catch (IOException ex) { + LOG.warn("Failed to get movie info: {}", ex.getMessage()); + throw new MovieDbException(MovieDbExceptionType.MAPPING_FAILED, webpage, ex); + } + } + + /** + * This method is used to retrieve all of the cast & crew information for the person. + * + * It will return the single highest rated poster for each movie record. + * + * @param personId + * @throws MovieDbException + */ + public TmdbResultsList getPersonCredits(int personId, String... appendToResponse) throws MovieDbException { + ApiUrl apiUrl = new ApiUrl(apiKey, BASE_PERSON, "/credits"); + + apiUrl.addArgument(PARAM_ID, personId); + apiUrl.appendToResponse(appendToResponse); + + URL url = apiUrl.buildUrl(); + String webpage = WebBrowser.request(url); + + try { + WrapperPersonCredits wrapper = mapper.readValue(webpage, WrapperPersonCredits.class); + TmdbResultsList results = new TmdbResultsList(wrapper.getAll()); + results.copyWrapper(wrapper); + return results; + } catch (IOException ex) { + LOG.warn("Failed to get person credits: {}", ex.getMessage()); + throw new MovieDbException(MovieDbExceptionType.MAPPING_FAILED, webpage, ex); + } + } + + /** + * This method is used to retrieve all of the profile images for a person. + * + * @param personId + * @throws MovieDbException + */ + public TmdbResultsList getPersonImages(int personId) throws MovieDbException { + ApiUrl apiUrl = new ApiUrl(apiKey, BASE_PERSON, "/images"); + + apiUrl.addArgument(PARAM_ID, personId); + + URL url = apiUrl.buildUrl(); + String webpage = WebBrowser.request(url); + + try { + WrapperImages wrapper = mapper.readValue(webpage, WrapperImages.class); + TmdbResultsList results = new TmdbResultsList(wrapper.getAll(ArtworkType.PROFILE)); + results.copyWrapper(wrapper); + return results; + } catch (IOException ex) { + LOG.warn("Failed to get person images: {}", ex.getMessage()); + throw new MovieDbException(MovieDbExceptionType.MAPPING_FAILED, webpage, ex); + } + } + + /** + * Get the changes for a specific person id. + * + * Changes are grouped by key, and ordered by date in descending order. + * + * By default, only the last 24 hours of changes are returned. + * + * The maximum number of days that can be returned in a single request is 14. + * + * The language is present on fields that are translatable. + * + * @param personId + * @param startDate + * @param endDate + * @throws MovieDbException + */ + public void getPersonChanges(int personId, String startDate, String endDate) throws MovieDbException { + throw new MovieDbException(MovieDbExceptionType.UNKNOWN_CAUSE, "Not implemented yet"); + } + + /** + * Get the list of popular people on The Movie Database. + * + * This list refreshes every day. + * + * @return + * @throws MovieDbException + */ + public TmdbResultsList getPersonPopular() throws MovieDbException { + return getPersonPopular(0); + } + + /** + * Get the list of popular people on The Movie Database. + * + * This list refreshes every day. + * + * @param page + * @return + * @throws MovieDbException + */ + public TmdbResultsList getPersonPopular(int page) throws MovieDbException { + ApiUrl apiUrl = new ApiUrl(apiKey, BASE_PERSON, "/popular"); + + if (page > 0) { + apiUrl.addArgument(PARAM_PAGE, page); + } + + URL url = apiUrl.buildUrl(); + String webpage = WebBrowser.request(url); + + try { + WrapperPersonList wrapper = mapper.readValue(webpage, WrapperPersonList.class); + TmdbResultsList results = new TmdbResultsList(wrapper.getPersonList()); + results.copyWrapper(wrapper); + return results; + } catch (IOException ex) { + LOG.warn("Failed to get person images: {}", ex.getMessage()); + throw new MovieDbException(MovieDbExceptionType.MAPPING_FAILED, webpage, ex); + } + } + + /** + * Get the latest person id. + * + * @throws MovieDbException + */ + public Person getPersonLatest() throws MovieDbException { + ApiUrl apiUrl = new ApiUrl(apiKey, BASE_PERSON, "/latest"); + URL url = apiUrl.buildUrl(); + String webpage = WebBrowser.request(url); + + try { + return mapper.readValue(webpage, Person.class); + } catch (IOException ex) { + LOG.warn("Failed to get latest person: {}", ex.getMessage()); + throw new MovieDbException(MovieDbExceptionType.MAPPING_FAILED, webpage, ex); + } + } + + // + // + // + /** + * This method is used to retrieve the basic information about a production company on TMDb. + * + * @param companyId + * @throws MovieDbException + */ + public Company getCompanyInfo(int companyId) throws MovieDbException { + ApiUrl apiUrl = new ApiUrl(apiKey, BASE_COMPANY); + + apiUrl.addArgument(PARAM_ID, companyId); + + URL url = apiUrl.buildUrl(); + String webpage = WebBrowser.request(url); + + try { + return mapper.readValue(webpage, Company.class); + } catch (IOException ex) { + LOG.warn("Failed to get company information: {}", ex.getMessage()); + throw new MovieDbException(MovieDbExceptionType.MAPPING_FAILED, webpage, ex); + } + } + + /** + * This method is used to retrieve the movies associated with a company. + * + * These movies are returned in order of most recently released to oldest. The default response will return 20 movies per page. + * + * TODO: Implement more than 20 movies + * + * @param companyId + * @param language + * @param page + * @throws MovieDbException + */ + public TmdbResultsList getCompanyMovies(int companyId, String language, int page) throws MovieDbException { + ApiUrl apiUrl = new ApiUrl(apiKey, BASE_COMPANY, "/movies"); + + apiUrl.addArgument(PARAM_ID, companyId); + + if (StringUtils.isNotBlank(language)) { + apiUrl.addArgument(PARAM_LANGUAGE, language); + } + + if (page > 0) { + apiUrl.addArgument(PARAM_PAGE, page); + } + + URL url = apiUrl.buildUrl(); + String webpage = WebBrowser.request(url); + + try { + WrapperCompanyMovies wrapper = mapper.readValue(webpage, WrapperCompanyMovies.class); + TmdbResultsList results = new TmdbResultsList(wrapper.getResults()); + results.copyWrapper(wrapper); + return results; + } catch (IOException ex) { + LOG.warn("Failed to get company movies: {}", ex.getMessage()); + throw new MovieDbException(MovieDbExceptionType.MAPPING_FAILED, webpage, ex); + } + } + + // + // + // + /** + * You can use this method to retrieve the list of genres used on TMDb. + * + * These IDs will correspond to those found in movie calls. + * + * @param language + */ + public TmdbResultsList getGenreList(String language) throws MovieDbException { + ApiUrl apiUrl = new ApiUrl(apiKey, BASE_GENRE, "/list"); + apiUrl.addArgument(PARAM_LANGUAGE, language); + + URL url = apiUrl.buildUrl(); + String webpage = WebBrowser.request(url); + + try { + WrapperGenres wrapper = mapper.readValue(webpage, WrapperGenres.class); + TmdbResultsList results = new TmdbResultsList(wrapper.getGenres()); + results.copyWrapper(wrapper); + return results; + } catch (IOException ex) { + LOG.warn("Failed to get genre list: {}", ex.getMessage()); + throw new MovieDbException(MovieDbExceptionType.MAPPING_FAILED, webpage, ex); + } + } + + /** + * 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. + * + * @param genreId + * @param language + * @param page + */ + public TmdbResultsList getGenreMovies(int genreId, String language, int page, boolean includeAllMovies) throws MovieDbException { + ApiUrl apiUrl = new ApiUrl(apiKey, BASE_GENRE, "/movies"); + apiUrl.addArgument(PARAM_ID, genreId); + + if (StringUtils.isNotBlank(language)) { + apiUrl.addArgument(PARAM_LANGUAGE, language); + } + + if (page > 0) { + apiUrl.addArgument(PARAM_PAGE, page); + } + + apiUrl.addArgument(PARAM_INCLUDE_ALL_MOVIES, includeAllMovies); + + URL url = apiUrl.buildUrl(); + String webpage = WebBrowser.request(url); + + try { + WrapperMovie wrapper = mapper.readValue(webpage, WrapperMovie.class); + TmdbResultsList results = new TmdbResultsList(wrapper.getMovies()); + results.copyWrapper(wrapper); + return results; + } catch (IOException ex) { + LOG.warn("Failed to get genre movie list: {}", ex.getMessage()); + throw new MovieDbException(MovieDbExceptionType.MAPPING_FAILED, webpage, ex); + } + } + // + // + // + + /** + * Search Movies This is a good starting point to start finding movies on TMDb. + * + * @param movieName + * @param searchYear Limit the search to the provided year. Zero (0) will get all years + * @param language The language to include. Can be blank/null. + * @param includeAdult true or false to include adult titles in the search + * @param page The page of results to return. 0 to get the default (first page) + * @throws MovieDbException + */ + public TmdbResultsList searchMovie(String movieName, int searchYear, String language, boolean includeAdult, int page) throws MovieDbException { + ApiUrl apiUrl = new ApiUrl(apiKey, BASE_SEARCH, "movie"); + if (StringUtils.isNotBlank(movieName)) { + apiUrl.addArgument(PARAM_QUERY, movieName); + } + + if (searchYear > 0) { + apiUrl.addArgument(PARAM_YEAR, Integer.toString(searchYear)); + } + + if (StringUtils.isNotBlank(language)) { + apiUrl.addArgument(PARAM_LANGUAGE, language); + } + + apiUrl.addArgument(PARAM_ADULT, Boolean.toString(includeAdult)); + + if (page > 0) { + apiUrl.addArgument(PARAM_PAGE, Integer.toString(page)); + } + + URL url = apiUrl.buildUrl(); + + String webpage = WebBrowser.request(url); + try { + WrapperMovie wrapper = mapper.readValue(webpage, WrapperMovie.class); + TmdbResultsList results = new TmdbResultsList(wrapper.getMovies()); + results.copyWrapper(wrapper); + return results; + } catch (IOException ex) { + LOG.warn("Failed to find movie: {}", ex.getMessage()); + throw new MovieDbException(MovieDbExceptionType.MAPPING_FAILED, webpage, ex); + } + + } + + /** + * Search for collections by name. + * + * @param query + * @param language + * @param page + * @throws MovieDbException + */ + public TmdbResultsList searchCollection(String query, String language, int page) throws MovieDbException { + ApiUrl apiUrl = new ApiUrl(apiKey, BASE_SEARCH, "collections"); + + if (StringUtils.isNotBlank(query)) { + apiUrl.addArgument(PARAM_QUERY, query); + } + + if (StringUtils.isNotBlank(language)) { + apiUrl.addArgument(PARAM_LANGUAGE, language); + } + + if (page > 0) { + apiUrl.addArgument(PARAM_PAGE, Integer.toString(page)); + } + + URL url = apiUrl.buildUrl(); + + String webpage = WebBrowser.request(url); + try { + WrapperCollection wrapper = mapper.readValue(webpage, WrapperCollection.class); + TmdbResultsList results = new TmdbResultsList(wrapper.getResults()); + results.copyWrapper(wrapper); + return results; + } catch (IOException ex) { + LOG.warn("Failed to find collection: {}", ex.getMessage()); + throw new MovieDbException(MovieDbExceptionType.MAPPING_FAILED, webpage, ex); + } + } + + /** + * This is a good starting point to start finding people on TMDb. + * + * The idea is to be a quick and light method so you can iterate through people quickly. + * + * @param personName + * @param includeAdult + * @param page + * @throws MovieDbException + */ + public TmdbResultsList searchPeople(String personName, boolean includeAdult, int page) throws MovieDbException { + ApiUrl apiUrl = new ApiUrl(apiKey, BASE_SEARCH, "person"); + apiUrl.addArgument(PARAM_QUERY, personName); + apiUrl.addArgument(PARAM_ADULT, includeAdult); + + if (page > 0) { + apiUrl.addArgument(PARAM_PAGE, page); + } + + URL url = apiUrl.buildUrl(); + String webpage = WebBrowser.request(url); + + try { + WrapperPerson wrapper = mapper.readValue(webpage, WrapperPerson.class); + TmdbResultsList results = new TmdbResultsList(wrapper.getResults()); + results.copyWrapper(wrapper); + return results; + } catch (IOException ex) { + LOG.warn("Failed to find person: {}", ex.getMessage()); + throw new MovieDbException(MovieDbExceptionType.MAPPING_FAILED, webpage, ex); + } + } + + /** + * Search for lists by name and description. + * + * @param query + * @param language + * @param page + * @throws MovieDbException + */ + public TmdbResultsList searchList(String query, String language, int page) throws MovieDbException { + ApiUrl apiUrl = new ApiUrl(apiKey, BASE_SEARCH, "list"); + + if (StringUtils.isNotBlank(query)) { + apiUrl.addArgument(PARAM_QUERY, query); + } + + if (StringUtils.isNotBlank(language)) { + apiUrl.addArgument(PARAM_LANGUAGE, language); + } + + if (page > 0) { + apiUrl.addArgument(PARAM_PAGE, Integer.toString(page)); + } + + URL url = apiUrl.buildUrl(); + + String webpage = WebBrowser.request(url); + try { + WrapperMovieList wrapper = mapper.readValue(webpage, WrapperMovieList.class); + TmdbResultsList results = new TmdbResultsList(wrapper.getMovieList()); + results.copyWrapper(wrapper); + return results; + } catch (IOException ex) { + LOG.warn("Failed to find list: {}", ex.getMessage()); + throw new MovieDbException(MovieDbExceptionType.MAPPING_FAILED, webpage, ex); + } + } + + /** + * Search Companies. + * + * You can use this method to search for production companies that are part of TMDb. The company IDs will map to those returned + * on movie calls. + * + * http://help.themoviedb.org/kb/api/search-companies + * + * @param companyName + * @param page + * @throws MovieDbException + */ + public TmdbResultsList searchCompanies(String companyName, int page) throws MovieDbException { + ApiUrl apiUrl = new ApiUrl(apiKey, BASE_SEARCH, "company"); + apiUrl.addArgument(PARAM_QUERY, companyName); + + if (page > 0) { + apiUrl.addArgument(PARAM_PAGE, page); + } + + URL url = apiUrl.buildUrl(); + String webpage = WebBrowser.request(url); + try { + WrapperCompany wrapper = mapper.readValue(webpage, WrapperCompany.class); + TmdbResultsList results = new TmdbResultsList(wrapper.getResults()); + results.copyWrapper(wrapper); + return results; + } catch (IOException ex) { + LOG.warn("Failed to find company: {}", ex.getMessage()); + throw new MovieDbException(MovieDbExceptionType.MAPPING_FAILED, webpage, ex); + } + } + + /** + * Search for keywords by name + * + * @param query + * @param page + * @throws MovieDbException + */ + public TmdbResultsList searchKeyword(String query, int page) throws MovieDbException { + ApiUrl apiUrl = new ApiUrl(apiKey, BASE_SEARCH, "keyword"); + + if (StringUtils.isNotBlank(query)) { + apiUrl.addArgument(PARAM_QUERY, query); + } + + if (page > 0) { + apiUrl.addArgument(PARAM_PAGE, Integer.toString(page)); + } + + URL url = apiUrl.buildUrl(); + + String webpage = WebBrowser.request(url); + try { + WrapperKeywords wrapper = mapper.readValue(webpage, WrapperKeywords.class); + TmdbResultsList results = new TmdbResultsList(wrapper.getResults()); + results.copyWrapper(wrapper); + return results; + } catch (IOException ex) { + LOG.warn("Failed to find keyword: {}", ex.getMessage()); + throw new MovieDbException(MovieDbExceptionType.MAPPING_FAILED, webpage, ex); + } + } + // + // + // + + /** + * Get a list by its ID + * + * @param listId + * @return The list and its items + * @throws MovieDbException + */ + public MovieDbList getList(String listId) throws MovieDbException { + ApiUrl apiUrl = new ApiUrl(apiKey, BASE_LIST); + apiUrl.addArgument(PARAM_ID, listId); + + URL url = apiUrl.buildUrl(); + String webpage = WebBrowser.request(url); + + try { + return mapper.readValue(webpage, MovieDbList.class); + } catch (IOException ex) { + LOG.warn("Failed to get list: {}", ex.getMessage()); + throw new MovieDbException(MovieDbExceptionType.MAPPING_FAILED, webpage, ex); + } + } + // + // + // + + /** + * Get the basic information for a specific keyword id. + * + * @param keywordId + * @return + * @throws MovieDbException + */ + public Keyword getKeyword(String keywordId) throws MovieDbException { + ApiUrl apiUrl = new ApiUrl(apiKey, BASE_KEYWORD); + apiUrl.addArgument(PARAM_ID, keywordId); + + URL url = apiUrl.buildUrl(); + String webpage = WebBrowser.request(url); + + try { + return mapper.readValue(webpage, Keyword.class); + } catch (IOException ex) { + LOG.warn("Failed to get keyword: {}", ex.getMessage()); + throw new MovieDbException(MovieDbExceptionType.MAPPING_FAILED, webpage, ex); + } + + } + + /** + * Get the list of movies for a particular keyword by id. + * + * @param keywordId + * @param language + * @param page + * @return List of movies with the keyword + * @throws MovieDbException + */ + public TmdbResultsList getKeywordMovies(String keywordId, String language, int page) throws MovieDbException { + ApiUrl apiUrl = new ApiUrl(apiKey, BASE_KEYWORD, "/movies"); + apiUrl.addArgument(PARAM_ID, keywordId); + + if (StringUtils.isNotBlank(language)) { + apiUrl.addArgument(PARAM_LANGUAGE, language); + } + + if (page > 0) { + apiUrl.addArgument(PARAM_PAGE, page); + } + + URL url = apiUrl.buildUrl(); + String webpage = WebBrowser.request(url); + + try { + WrapperKeywordMovies wrapper = mapper.readValue(webpage, WrapperKeywordMovies.class); + TmdbResultsList results = new TmdbResultsList(wrapper.getResults()); + results.copyWrapper(wrapper); + return results; + } catch (IOException ex) { + LOG.warn("Failed to get top rated movies: {}", ex.getMessage()); + throw new MovieDbException(MovieDbExceptionType.MAPPING_FAILED, webpage, ex); + } + + } + // + // + // + + public void getMovieChangesList(int page, String startDate, String endDate) throws MovieDbException { + throw new MovieDbException(MovieDbExceptionType.UNKNOWN_CAUSE, "Not implemented yet"); + } + + public void getPersonChangesList(int page, String startDate, String endDate) throws MovieDbException { + throw new MovieDbException(MovieDbExceptionType.UNKNOWN_CAUSE, "Not implemented yet"); + } + // + + // + public TmdbResultsList getJobs() throws MovieDbException { + ApiUrl apiUrl = new ApiUrl(apiKey, BASE_JOB, "/list"); + + URL url = apiUrl.buildUrl(); + String webpage = WebBrowser.request(url); + + try { + WrapperJobList wrapper = mapper.readValue(webpage, WrapperJobList.class); + TmdbResultsList results = new TmdbResultsList(wrapper.getJobs()); + results.copyWrapper(wrapper); + return results; + } catch (IOException ex) { + LOG.warn("Failed to get job list: {}", ex.getMessage()); + throw new MovieDbException(MovieDbExceptionType.MAPPING_FAILED, webpage, ex); + } + } + // + + // + /** + * Discover movies by different types of data like average rating, number of votes, genres and certifications. + * + * You can alternatively create a "discover" object and pass it to this method to cut out the requirement for all of these + * parameters + * + * @param page Minimum value is 1 + * @param language ISO 639-1 code. + * @param sortBy Available options are vote_average.desc, vote_average.asc, release_date.desc, release_date.asc, + * popularity.desc, popularity.asc + * @param includeAdult Toggle the inclusion of adult titles + * @param year Filter the results release dates to matches that include this value + * @param primaryReleaseYear Filter the results so that only the primary release date year has this value + * @param voteCountGte Only include movies that are equal to, or have a vote count higher than this value + * @param voteAverageGte Only include movies that are equal to, or have a higher average rating than this value + * @param withGenres Only include movies with the specified genres. Expected value is an integer (the id of a genre). Multiple + * values can be specified. Comma separated indicates an 'AND' query, while a pipe (|) separated value indicates an 'OR'. + * @param releaseDateGte The minimum release to include. Expected format is YYYY-MM-DD + * @param releaseDateLte The maximum release to include. Expected format is YYYY-MM-DD + * @param certificationCountry Only include movies with certifications for a specific country. When this value is specified, + * 'certificationLte' is required. A ISO 3166-1 is expected. + * @param certificationLte Only include movies with this certification and lower. Expected value is a valid certification for + * the specified 'certificationCountry'. + * @param withCompanies Filter movies to include a specific company. Expected value is an integer (the id of a company). They + * can be comma separated to indicate an 'AND' query. + * @return + * @throws MovieDbException + */ + public TmdbResultsList getDiscover(int page, String language, String sortBy, boolean includeAdult, int year, + int primaryReleaseYear, int voteCountGte, float voteAverageGte, String withGenres, String releaseDateGte, + String releaseDateLte, String certificationCountry, String certificationLte, String withCompanies) throws MovieDbException { + + Discover discover = new Discover(); + discover.page(page) + .language(language) + .sortBy(sortBy) + .includeAdult(includeAdult) + .year(year) + .primaryReleaseYear(primaryReleaseYear) + .voteCountGte(voteCountGte) + .voteAverageGte(voteAverageGte) + .withGenres(withGenres) + .releaseDateGte(releaseDateGte) + .releaseDateLte(releaseDateLte) + .certificationCountry(certificationCountry) + .certificationLte(certificationLte) + .withCompanies(withCompanies); + + return getDiscover(discover); + } + + /** + * Discover movies by different types of data like average rating, number of votes, genres and certifications. + * + * @param discover A discover object containing the search criteria required + * @return + * @throws MovieDbException + */ + public TmdbResultsList getDiscover(Discover discover) throws MovieDbException { + ApiUrl apiUrl = new ApiUrl(apiKey, BASE_DISCOVER, "/movie"); + + apiUrl.setArguments(discover.getParams()); + + URL url = apiUrl.buildUrl(); + String webpage = WebBrowser.request(url); + + try { + WrapperMovie wrapper = mapper.readValue(webpage, WrapperMovie.class); + TmdbResultsList results = new TmdbResultsList(wrapper.getMovies()); + results.copyWrapper(wrapper); + return results; + } catch (IOException ex) { + LOG.warn("Failed to get discover list: {}", ex.getMessage()); + throw new MovieDbException(MovieDbExceptionType.MAPPING_FAILED, webpage, ex); + } + } + // +} diff --git a/src/main/java/com/omertron/themoviedbapi/model/ChangeKeyItem.java b/src/main/java/com/omertron/themoviedbapi/model/ChangeKeyItem.java new file mode 100644 index 000000000..f6be48132 --- /dev/null +++ b/src/main/java/com/omertron/themoviedbapi/model/ChangeKeyItem.java @@ -0,0 +1,51 @@ +package com.omertron.themoviedbapi.model; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import com.fasterxml.jackson.annotation.JsonAnyGetter; +import com.fasterxml.jackson.annotation.JsonAnySetter; +import com.fasterxml.jackson.annotation.JsonProperty; +import org.apache.commons.lang3.builder.ToStringBuilder; + +public class ChangeKeyItem { + + private static final long serialVersionUID = 1L; + @JsonProperty("key") + private String key; + @JsonProperty("items") + private List changedItems = new ArrayList(); + private Map newItems = new HashMap(); + + public String getKey() { + return key; + } + + public void setKey(String key) { + this.key = key; + } + + public List getChangedItems() { + return changedItems; + } + + public void setChangedItems(List changes) { + this.changedItems = changes; + } + + @JsonAnyGetter + public Map getNewItems() { + return this.newItems; + } + + @JsonAnySetter + public void setNewItems(String name, Object value) { + this.newItems.put(name, value); + } + + @Override + public String toString() { + return ToStringBuilder.reflectionToString(this); + } +} diff --git a/src/main/java/com/omertron/themoviedbapi/model/ChangedItem.java b/src/main/java/com/omertron/themoviedbapi/model/ChangedItem.java new file mode 100644 index 000000000..536ef3e9a --- /dev/null +++ b/src/main/java/com/omertron/themoviedbapi/model/ChangedItem.java @@ -0,0 +1,79 @@ +package com.omertron.themoviedbapi.model; + +import java.util.HashMap; +import java.util.Map; +import com.fasterxml.jackson.annotation.JsonAnyGetter; +import com.fasterxml.jackson.annotation.JsonAnySetter; +import com.fasterxml.jackson.annotation.JsonProperty; +import org.apache.commons.lang3.builder.ToStringBuilder; + +public class ChangedItem { + + private static final long serialVersionUID = 1L; + @JsonProperty("id") + private String id; + @JsonProperty("action") + private String action; + @JsonProperty("time") + private String time; + @JsonProperty("iso_639_1") + private String language; + @JsonProperty("value") + private Object value; + private Map newItems = new HashMap(); + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getAction() { + return action; + } + + public void setAction(String action) { + this.action = action; + } + + public String getTime() { + return time; + } + + public void setTime(String time) { + this.time = time; + } + + public String getLanguage() { + return language; + } + + public void setLanguage(String language) { + this.language = language; + } + + public Object getValue() { + return value; + } + + public void setValue(Object value) { + this.value = value; + } + + @JsonAnyGetter + public Map getNewItems() { + return this.newItems; + } + + @JsonAnySetter + public void setNewItems(String name, Object value) { + this.newItems.put(name, value); + } + + @Override + public String toString() { + return ToStringBuilder.reflectionToString(this); + } +} diff --git a/src/main/java/com/omertron/themoviedbapi/wrapper/AbstractWrapperId.java b/src/main/java/com/omertron/themoviedbapi/wrapper/AbstractWrapperId.java index 5e07ae62c..e11cfd9be 100644 --- a/src/main/java/com/omertron/themoviedbapi/wrapper/AbstractWrapperId.java +++ b/src/main/java/com/omertron/themoviedbapi/wrapper/AbstractWrapperId.java @@ -1,50 +1,50 @@ -/* - * Copyright (c) 2004-2013 Stuart Boston - * - * This file is part of TheMovieDB API. - * - * TheMovieDB API is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * any later version. - * - * TheMovieDB API is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with TheMovieDB API. If not, see . - * - */ -package com.omertron.themoviedbapi.wrapper; - -import com.fasterxml.jackson.annotation.JsonProperty; - -/** - * Base class for the wrappers - * - * @author Stuart - */ -public class AbstractWrapperId extends AbstractWrapper implements IWrapperId { - /* - * Properties - */ - @JsonProperty("id") - private int id; - - public AbstractWrapperId(Class classToLog) { - super(classToLog); - } - - @Override - public int getId() { - return id; - } - - @Override - public void setId(int id) { - this.id = id; - } - -} +/* + * Copyright (c) 2004-2013 Stuart Boston + * + * This file is part of TheMovieDB API. + * + * TheMovieDB API is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * any later version. + * + * TheMovieDB API is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with TheMovieDB API. If not, see . + * + */ +package com.omertron.themoviedbapi.wrapper; + +import com.fasterxml.jackson.annotation.JsonProperty; + +/** + * Base class for the wrappers + * + * @author Stuart + */ +public class AbstractWrapperId extends AbstractWrapper implements IWrapperId { + /* + * Properties + */ + + @JsonProperty("id") + private int id; + + public AbstractWrapperId(Class classToLog) { + super(classToLog); + } + + @Override + public int getId() { + return id; + } + + @Override + public void setId(int id) { + this.id = id; + } +} diff --git a/src/main/java/com/omertron/themoviedbapi/wrapper/WrapperChanges.java b/src/main/java/com/omertron/themoviedbapi/wrapper/WrapperChanges.java new file mode 100644 index 000000000..c7cb990b4 --- /dev/null +++ b/src/main/java/com/omertron/themoviedbapi/wrapper/WrapperChanges.java @@ -0,0 +1,41 @@ +package com.omertron.themoviedbapi.wrapper; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import com.fasterxml.jackson.annotation.JsonAnyGetter; +import com.fasterxml.jackson.annotation.JsonAnySetter; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.omertron.themoviedbapi.model.ChangeKeyItem; +import org.apache.commons.lang3.builder.ToStringBuilder; + +public class WrapperChanges { + + @JsonProperty("changes") + private List changedItems = new ArrayList(); + private Map newItems = new HashMap(); + + public List getChangedItems() { + return changedItems; + } + + public void setChangedItems(List changes) { + this.changedItems = changes; + } + + @JsonAnyGetter + public Map getNewItems() { + return this.newItems; + } + + @JsonAnySetter + public void setNewItems(String name, Object value) { + this.newItems.put(name, value); + } + + @Override + public String toString() { + return ToStringBuilder.reflectionToString(this); + } +} diff --git a/src/test/java/com/omertron/themoviedbapi/TempTest.java b/src/test/java/com/omertron/themoviedbapi/TempTest.java deleted file mode 100644 index 3ae578278..000000000 --- a/src/test/java/com/omertron/themoviedbapi/TempTest.java +++ /dev/null @@ -1,784 +0,0 @@ -/* - * Copyright (c) 2004-2013 Stuart Boston - * - * This file is part of TheMovieDB API. - * - * TheMovieDB API is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * any later version. - * - * TheMovieDB API is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with TheMovieDB API. If not, see . - * - */ -package com.omertron.themoviedbapi; - -import com.omertron.themoviedbapi.model.AlternativeTitle; -import com.omertron.themoviedbapi.model.Artwork; -import com.omertron.themoviedbapi.model.Collection; -import com.omertron.themoviedbapi.model.CollectionInfo; -import com.omertron.themoviedbapi.model.Company; -import com.omertron.themoviedbapi.model.Discover; -import com.omertron.themoviedbapi.model.Genre; -import com.omertron.themoviedbapi.model.JobDepartment; -import com.omertron.themoviedbapi.model.Keyword; -import com.omertron.themoviedbapi.model.KeywordMovie; -import com.omertron.themoviedbapi.model.MovieChanges; -import com.omertron.themoviedbapi.model.MovieDb; -import com.omertron.themoviedbapi.model.MovieDbList; -import com.omertron.themoviedbapi.model.MovieList; -import com.omertron.themoviedbapi.model.Person; -import com.omertron.themoviedbapi.model.PersonCredit; -import com.omertron.themoviedbapi.model.ReleaseInfo; -import com.omertron.themoviedbapi.model.Reviews; -import com.omertron.themoviedbapi.model.TmdbConfiguration; -import com.omertron.themoviedbapi.model.TokenAuthorisation; -import com.omertron.themoviedbapi.model.TokenSession; -import com.omertron.themoviedbapi.model.Trailer; -import com.omertron.themoviedbapi.model.Translation; -import com.omertron.themoviedbapi.results.TmdbResultsList; -import java.io.IOException; -import org.apache.commons.lang3.StringUtils; -import org.junit.*; -import static org.junit.Assert.*; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * Test cases for TheMovieDbApi API - * - * @author stuart.boston - */ -public class TheMovieDbApiTest { - - // Logger - private static final Logger LOG = LoggerFactory.getLogger(TheMovieDbApiTest.class); - // API Key - private static final String API_KEY = "5a1a77e2eba8984804586122754f969f"; - private static TheMovieDbApi tmdb; - // Test data - private static final int ID_MOVIE_BLADE_RUNNER = 78; - private static final int ID_MOVIE_THE_AVENGERS = 24428; - private static final int ID_COLLECTION_STAR_WARS = 10; - private static final int ID_PERSON_BRUCE_WILLIS = 62; - private static final int ID_COMPANY_LUCASFILM = 1; - private static final String COMPANY_NAME = "Marvel Studios"; - private static final int ID_GENRE_ACTION = 28; - private static final String ID_KEYWORD = "1721"; - // Languages - private static final String LANGUAGE_DEFAULT = ""; - private static final String LANGUAGE_ENGLISH = "en"; - private static final String LANGUAGE_RUSSIAN = "ru"; - - public TheMovieDbApiTest() throws MovieDbException { - } - - @BeforeClass - public static void setUpClass() throws Exception { - tmdb = new TheMovieDbApi(API_KEY); - TestLogger.Configure(); - } - - @AfterClass - public static void tearDownClass() throws Exception { - } - - @Before - public void setUp() { - } - - @After - public void tearDown() { - } - - /** - * Test of getConfiguration method, of class TheMovieDbApi. - */ - @Test - public void testConfiguration() throws IOException { - LOG.info("Test Configuration"); - - TmdbConfiguration tmdbConfig = tmdb.getConfiguration(); - assertNotNull("Configuration failed", tmdbConfig); - assertTrue("No base URL", StringUtils.isNotBlank(tmdbConfig.getBaseUrl())); - assertTrue("No backdrop sizes", tmdbConfig.getBackdropSizes().size() > 0); - assertTrue("No poster sizes", tmdbConfig.getPosterSizes().size() > 0); - assertTrue("No profile sizes", tmdbConfig.getProfileSizes().size() > 0); - LOG.info(tmdbConfig.toString()); - } - - /** - * Test of searchMovie method, of class TheMovieDbApi. - */ - @Test - public void testSearchMovie() throws MovieDbException { - LOG.info("searchMovie"); - - // Try a movie with less than 1 page of results - TmdbResultsList movieList = tmdb.searchMovie("Blade Runner", 0, "", true, 0); -// List movieList = tmdb.searchMovie("Blade Runner", "", true); - assertTrue("No movies found, should be at least 1", movieList.getResults().size() > 0); - - // Try a russian langugage movie - movieList = tmdb.searchMovie("О чём говорят мужчины", 0, LANGUAGE_RUSSIAN, true, 0); - assertTrue("No 'RU' movies found, should be at least 1", movieList.getResults().size() > 0); - - // Try a movie with more than 20 results - movieList = tmdb.searchMovie("Star Wars", 0, LANGUAGE_ENGLISH, false, 0); - assertTrue("Not enough movies found, should be over 15, found " + movieList.getResults().size(), movieList.getResults().size() >= 15); - } - - /** - * Test of getMovieInfo method, of class TheMovieDbApi. - */ - @Test - public void testGetMovieInfo() throws MovieDbException { - LOG.info("getMovieInfo"); - MovieDb result = tmdb.getMovieInfo(ID_MOVIE_BLADE_RUNNER, LANGUAGE_ENGLISH, "alternative_titles,casts,images,keywords,releases,trailers,translations,similar_movies,reviews,lists"); - assertEquals("Incorrect movie information", "Blade Runner", result.getOriginalTitle()); - } - - /** - * Test of getMovieAlternativeTitles method, of class TheMovieDbApi. - */ - @Test - public void testGetMovieAlternativeTitles() throws MovieDbException { - LOG.info("getMovieAlternativeTitles"); - String country = ""; - TmdbResultsList result = tmdb.getMovieAlternativeTitles(ID_MOVIE_BLADE_RUNNER, country, "casts,images,keywords,releases,trailers,translations,similar_movies,reviews,lists"); - assertTrue("No alternative titles found", result.getResults().size() > 0); - - country = "US"; - result = tmdb.getMovieAlternativeTitles(ID_MOVIE_BLADE_RUNNER, country); - assertTrue("No alternative titles found", result.getResults().size() > 0); - - } - - /** - * Test of getMovieCasts method, of class TheMovieDbApi. - */ - @Test - public void testGetMovieCasts() throws MovieDbException { - LOG.info("getMovieCasts"); - TmdbResultsList people = tmdb.getMovieCasts(ID_MOVIE_BLADE_RUNNER, "alternative_titles,casts,images,keywords,releases,trailers,translations,similar_movies,reviews,lists"); - assertTrue("No cast information", people.getResults().size() > 0); - - String name1 = "Harrison Ford"; - String name2 = "Charles Knode"; - boolean foundName1 = Boolean.FALSE; - boolean foundName2 = Boolean.FALSE; - - for (Person person : people.getResults()) { - if (!foundName1 && person.getName().equalsIgnoreCase(name1)) { - foundName1 = Boolean.TRUE; - } - - if (!foundName2 && person.getName().equalsIgnoreCase(name2)) { - foundName2 = Boolean.TRUE; - } - } - assertTrue("Couldn't find " + name1, foundName1); - assertTrue("Couldn't find " + name2, foundName2); - } - - /** - * Test of getMovieImages method, of class TheMovieDbApi. - */ - @Test - public void testGetMovieImages() throws MovieDbException { - LOG.info("getMovieImages"); - String language = ""; - TmdbResultsList result = tmdb.getMovieImages(ID_MOVIE_BLADE_RUNNER, language); - assertFalse("No artwork found", result.getResults().isEmpty()); - } - - /** - * Test of getMovieKeywords method, of class TheMovieDbApi. - */ - @Test - public void testGetMovieKeywords() throws MovieDbException { - LOG.info("getMovieKeywords"); - TmdbResultsList result = tmdb.getMovieKeywords(ID_MOVIE_BLADE_RUNNER); - assertFalse("No keywords found", result.getResults().isEmpty()); - } - - /** - * Test of getMovieReleaseInfo method, of class TheMovieDbApi. - */ - @Test - public void testGetMovieReleaseInfo() throws MovieDbException { - LOG.info("getMovieReleaseInfo"); - TmdbResultsList result = tmdb.getMovieReleaseInfo(ID_MOVIE_BLADE_RUNNER, ""); - assertFalse("Release information missing", result.getResults().isEmpty()); - } - - /** - * Test of getMovieTrailers method, of class TheMovieDbApi. - */ - @Test - public void testGetMovieTrailers() throws MovieDbException { - LOG.info("getMovieTrailers"); - TmdbResultsList result = tmdb.getMovieTrailers(ID_MOVIE_BLADE_RUNNER, ""); - assertFalse("Movie trailers missing", result.getResults().isEmpty()); - } - - /** - * Test of getMovieTranslations method, of class TheMovieDbApi. - */ - @Test - public void testGetMovieTranslations() throws MovieDbException { - LOG.info("getMovieTranslations"); - TmdbResultsList result = tmdb.getMovieTranslations(ID_MOVIE_BLADE_RUNNER); - assertFalse("No translations found", result.getResults().isEmpty()); - } - - /** - * Test of getCollectionInfo method, of class TheMovieDbApi. - */ - @Test - public void testGetCollectionInfo() throws MovieDbException { - LOG.info("getCollectionInfo"); - String language = ""; - CollectionInfo result = tmdb.getCollectionInfo(ID_COLLECTION_STAR_WARS, language); - assertFalse("No collection information", result.getParts().isEmpty()); - } - - /** - * Test of createImageUrl method, of class TheMovieDbApi. - * - * @throws MovieDbException - */ - @Test - public void testCreateImageUrl() throws MovieDbException { - LOG.info("createImageUrl"); - MovieDb movie = tmdb.getMovieInfo(ID_MOVIE_BLADE_RUNNER, ""); - String result = tmdb.createImageUrl(movie.getPosterPath(), "original").toString(); - assertTrue("Error compiling image URL", !result.isEmpty()); - } - - /** - * Test of getMovieInfoImdb method, of class TheMovieDbApi. - */ - @Test - public void testGetMovieInfoImdb() throws MovieDbException { - LOG.info("getMovieInfoImdb"); - MovieDb result = tmdb.getMovieInfoImdb("tt0076759", "en-US"); - assertTrue("Error getting the movie from IMDB ID", result.getId() == 11); - } - - /** - * Test of getApiKey method, of class TheMovieDbApi. - */ - @Test - public void testGetApiKey() { - // Not required - } - - /** - * Test of getApiBase method, of class TheMovieDbApi. - */ - @Test - public void testGetApiBase() { - // Not required - } - - /** - * Test of getConfiguration method, of class TheMovieDbApi. - */ - @Test - public void testGetConfiguration() { - // Not required - } - - /** - * Test of searchPeople method, of class TheMovieDbApi. - */ - @Test - public void testSearchPeople() throws MovieDbException { - LOG.info("searchPeople"); - String personName = "Bruce Willis"; - boolean includeAdult = false; - TmdbResultsList result = tmdb.searchPeople(personName, includeAdult, 0); - assertTrue("Couldn't find the person", result.getResults().size() > 0); - } - - /** - * Test of getPersonInfo method, of class TheMovieDbApi. - */ - @Test - public void testGetPersonInfo() throws MovieDbException { - LOG.info("getPersonInfo"); - Person result = tmdb.getPersonInfo(ID_PERSON_BRUCE_WILLIS); - assertTrue("Wrong actor returned", result.getId() == ID_PERSON_BRUCE_WILLIS); - } - - /** - * Test of getPersonCredits method, of class TheMovieDbApi. - */ - @Test - public void testGetPersonCredits() throws MovieDbException { - LOG.info("getPersonCredits"); - - TmdbResultsList result = tmdb.getPersonCredits(ID_PERSON_BRUCE_WILLIS); - assertTrue("No cast information", result.getResults().size() > 0); - } - - /** - * Test of getPersonImages method, of class TheMovieDbApi. - */ - @Test - public void testGetPersonImages() throws MovieDbException { - LOG.info("getPersonImages"); - - TmdbResultsList result = tmdb.getPersonImages(ID_PERSON_BRUCE_WILLIS); - assertTrue("No cast information", result.getResults().size() > 0); - } - - /** - * Test of getLatestMovie method, of class TheMovieDbApi. - */ - @Test - public void testGetLatestMovie() throws MovieDbException { - LOG.info("getLatestMovie"); - MovieDb result = tmdb.getLatestMovie(); - assertTrue("No latest movie found", result != null); - assertTrue("No latest movie found", result.getId() > 0); - } - - /** - * Test of compareMovies method, of class TheMovieDbApi. - */ - @Test - public void testCompareMovies() { - // Not required - } - - /** - * Test of setProxy method, of class TheMovieDbApi. - */ - @Test - public void testSetProxy() { - // Not required - } - - /** - * Test of setTimeout method, of class TheMovieDbApi. - */ - @Test - public void testSetTimeout() { - // Not required - } - - /** - * Test of getNowPlayingMovies method, of class TheMovieDbApi. - */ - @Test - public void testGetNowPlayingMovies() throws MovieDbException { - LOG.info("getNowPlayingMovies"); - TmdbResultsList result = tmdb.getNowPlayingMovies(LANGUAGE_DEFAULT, 0); - assertTrue("No now playing movies found", !result.getResults().isEmpty()); - } - - /** - * Test of getPopularMovieList method, of class TheMovieDbApi. - */ - @Test - public void testGetPopularMovieList() throws MovieDbException { - LOG.info("getPopularMovieList"); - TmdbResultsList result = tmdb.getPopularMovieList(LANGUAGE_DEFAULT, 0); - assertTrue("No popular movies found", !result.getResults().isEmpty()); - } - - /** - * Test of getTopRatedMovies method, of class TheMovieDbApi. - */ - @Test - public void testGetTopRatedMovies() throws MovieDbException { - LOG.info("getTopRatedMovies"); - TmdbResultsList result = tmdb.getTopRatedMovies(LANGUAGE_DEFAULT, 0); - assertTrue("No top rated movies found", !result.getResults().isEmpty()); - } - - /** - * Test of getCompanyInfo method, of class TheMovieDbApi. - */ - @Test - public void testGetCompanyInfo() throws MovieDbException { - LOG.info("getCompanyInfo"); - Company company = tmdb.getCompanyInfo(ID_COMPANY_LUCASFILM); - assertTrue("No company information found", company.getCompanyId() > 0); - } - - /** - * Test of getCompanyMovies method, of class TheMovieDbApi. - */ - @Test - public void testGetCompanyMovies() throws MovieDbException { - LOG.info("getCompanyMovies"); - TmdbResultsList result = tmdb.getCompanyMovies(ID_COMPANY_LUCASFILM, LANGUAGE_DEFAULT, 0); - assertTrue("No company movies found", !result.getResults().isEmpty()); - } - - /** - * Test of searchCompanies method, of class TheMovieDbApi. - */ - @Test - public void testSearchCompanies() throws MovieDbException { - LOG.info("searchCompanies"); - TmdbResultsList result = tmdb.searchCompanies(COMPANY_NAME, 0); - assertTrue("No company information found", !result.getResults().isEmpty()); - } - - /** - * Test of getSimilarMovies method, of class TheMovieDbApi. - */ - @Test - public void testGetSimilarMovies() throws MovieDbException { - LOG.info("getSimilarMovies"); - TmdbResultsList result = tmdb.getSimilarMovies(ID_MOVIE_BLADE_RUNNER, LANGUAGE_DEFAULT, 0); - assertTrue("No similar movies found", !result.getResults().isEmpty()); - } - - /** - * Test of getGenreList method, of class TheMovieDbApi. - */ - @Test - public void testGetGenreList() throws MovieDbException { - LOG.info("getGenreList"); - TmdbResultsList result = tmdb.getGenreList(LANGUAGE_DEFAULT); - assertTrue("No genres found", !result.getResults().isEmpty()); - } - - /** - * Test of getGenreMovies method, of class TheMovieDbApi. - */ - @Test - public void testGetGenreMovies() throws MovieDbException { - LOG.info("getGenreMovies"); - TmdbResultsList result = tmdb.getGenreMovies(ID_GENRE_ACTION, LANGUAGE_DEFAULT, 0, Boolean.TRUE); - assertTrue("No genre movies found", !result.getResults().isEmpty()); - } - - /** - * Test of getUpcoming method, of class TheMovieDbApi. - */ - @Test - public void testGetUpcoming() throws Exception { - LOG.info("getUpcoming"); - TmdbResultsList result = tmdb.getUpcoming(LANGUAGE_DEFAULT, 0); - assertTrue("No upcoming movies found", !result.getResults().isEmpty()); - } - - /** - * Test of getCollectionImages method, of class TheMovieDbApi. - */ - @Test - public void testGetCollectionImages() throws Exception { - LOG.info("getCollectionImages"); - TmdbResultsList result = tmdb.getCollectionImages(ID_COLLECTION_STAR_WARS, LANGUAGE_DEFAULT); - assertFalse("No artwork found", result.getResults().isEmpty()); - } - - /** - * Test of getAuthorisationToken method, of class TheMovieDbApi. - */ - @Test - public void testGetAuthorisationToken() throws Exception { - LOG.info("getAuthorisationToken"); - TokenAuthorisation result = tmdb.getAuthorisationToken(); - assertFalse("Token is null", result == null); - assertTrue("Token is not valid", result.getSuccess()); - LOG.info(result.toString()); - } - - /** - * Test of getSessionToken method, of class TheMovieDbApi. - * - * TODO: Cannot be tested without a HTTP authorisation: http://help.themoviedb.org/kb/api/user-authentication - */ - public void testGetSessionToken() throws Exception { - LOG.info("getSessionToken"); - TokenAuthorisation token = tmdb.getAuthorisationToken(); - assertFalse("Token is null", token == null); - assertTrue("Token is not valid", token.getSuccess()); - LOG.info(token.toString()); - - TokenSession result = tmdb.getSessionToken(token); - assertFalse("Session token is null", result == null); - assertTrue("Session token is not valid", result.getSuccess()); - LOG.info(result.toString()); - } - - /** - * Test of getGuestSessionToken method, of class TheMovieDbApi. - */ - @Ignore("Not ready yet") - public void testGetGuestSessionToken() throws Exception { - LOG.info("getGuestSessionToken"); - TokenSession result = tmdb.getGuestSessionToken(); - - assertTrue("Failed to get guest session", result.getSuccess()); - } - - @Test - public void testGetMovieLists() throws Exception { - LOG.info("getMovieLists"); - TmdbResultsList result = tmdb.getMovieLists(ID_MOVIE_BLADE_RUNNER, LANGUAGE_ENGLISH, 0); - assertNotNull("No results found", result); - assertTrue("No results found", result.getResults().size() > 0); - } - - /** - * Test of getMovieChanges method,of class TheMovieDbApi - * - * TODO: Do not test this until it is fixed - */ - public void testGetMovieChanges() throws Exception { - LOG.info("getMovieChanges"); - - String startDate = ""; - String endDate = null; - - // Get some popular movies - TmdbResultsList movieList = tmdb.getPopularMovieList(LANGUAGE_DEFAULT, 0); - for (MovieDb movie : movieList.getResults()) { - TmdbResultsList result = tmdb.getMovieChanges(movie.getId(), startDate, endDate); - LOG.info("{} has {} changes.", new Object[]{movie.getTitle(), result.getResults().size()}); - } - - assertNotNull("No results found", movieList.getResults()); - assertTrue("No results found", movieList.getResults().size() > 0); - } - - @Test - public void testGetPersonLatest() throws Exception { - LOG.info("getPersonLatest"); - - Person result = tmdb.getPersonLatest(); - - assertNotNull("No results found", result); - assertTrue("No results found", StringUtils.isNotBlank(result.getName())); - } - - /** - * Test of searchCollection method, of class TheMovieDbApi. - */ - @Test - public void testSearchCollection() throws Exception { - LOG.info("searchCollection"); - String query = "batman"; - int page = 0; - TmdbResultsList result = tmdb.searchCollection(query, LANGUAGE_DEFAULT, page); - assertFalse("No collections found", result == null); - assertTrue("No collections found", result.getResults().size() > 0); - } - - /** - * Test of searchList method, of class TheMovieDbApi. - */ - @Test - public void testSearchList() throws Exception { - LOG.info("searchList"); - String query = "watch"; - int page = 0; - TmdbResultsList result = tmdb.searchList(query, LANGUAGE_DEFAULT, page); - assertFalse("No lists found", result.getResults() == null); - assertTrue("No lists found", result.getResults().size() > 0); - } - - /** - * Test of searchKeyword method, of class TheMovieDbApi. - */ - @Test - public void testSearchKeyword() throws Exception { - LOG.info("searchKeyword"); - String query = "action"; - int page = 0; - TmdbResultsList result = tmdb.searchKeyword(query, page); - assertFalse("No keywords found", result.getResults() == null); - assertTrue("No keywords found", result.getResults().size() > 0); - } - - /** - * Test of postMovieRating method, of class TheMovieDbApi. - * - * TODO: Cannot be tested without a HTTP authorisation: http://help.themoviedb.org/kb/api/user-authentication - */ - @Ignore("Not ready yet") - public void testPostMovieRating() throws Exception { - LOG.info("postMovieRating"); - String sessionId = ""; - String rating = ""; - boolean expResult = false; - boolean result = tmdb.postMovieRating(sessionId, rating); - assertEquals(expResult, result); - // TODO review the generated test code and remove the default call to fail. - fail("The test case is a prototype."); - } - - /** - * Test of getPersonChanges method, of class TheMovieDbApi. - * - */ - @Ignore("Not ready yet") - public void testGetPersonChanges() throws Exception { - LOG.info("getPersonChanges"); - String startDate = ""; - String endDate = ""; - tmdb.getPersonChanges(ID_PERSON_BRUCE_WILLIS, startDate, endDate); - } - - /** - * Test of getList method, of class TheMovieDbApi. - */ - @Test - public void testGetList() throws Exception { - LOG.info("getList"); - String listId = "509ec17b19c2950a0600050d"; - MovieDbList result = tmdb.getList(listId); - assertFalse("List not found", result.getItems().isEmpty()); - } - - /** - * Test of getKeyword method, of class TheMovieDbApi. - */ - @Test - public void testGetKeyword() throws Exception { - LOG.info("getKeyword"); - Keyword result = tmdb.getKeyword(ID_KEYWORD); - assertEquals("fight", result.getName()); - } - - /** - * Test of getKeywordMovies method, of class TheMovieDbApi. - */ - @Test - public void testGetKeywordMovies() throws Exception { - LOG.info("getKeywordMovies"); - int page = 0; - TmdbResultsList result = tmdb.getKeywordMovies(ID_KEYWORD, LANGUAGE_DEFAULT, page); - assertFalse("No keyword movies found", result.getResults().isEmpty()); - } - - /** - * Test of getReviews method, of class TheMovieDbApi. - */ - @Test - public void testGetReviews() throws Exception { - LOG.info("getReviews"); - int page = 0; - TmdbResultsList result = tmdb.getReviews(ID_MOVIE_THE_AVENGERS, LANGUAGE_DEFAULT, page); - - assertFalse("No reviews found", result.getResults().isEmpty()); - } - - /** - * Test of compareMovies method, of class TheMovieDbApi. - */ - @Ignore("Not required") - public void testCompareMovies_3args() { - } - - /** - * Test of compareMovies method, of class TheMovieDbApi. - */ - @Ignore("Not required") - public void testCompareMovies_4args() { - } - - /** - * Test of getPersonPopular method, of class TheMovieDbApi. - */ - @Ignore("Not required") - public void testGetPersonPopular_0args() throws Exception { - } - - /** - * Test of getPersonPopular method, of class TheMovieDbApi. - */ - @Test - public void testGetPersonPopular_int() throws Exception { - LOG.info("getPersonPopular"); - int page = 0; - TmdbResultsList result = tmdb.getPersonPopular(page); - assertFalse("No popular people", result.getResults().isEmpty()); - } - - /** - * Test of getGenreMovies method, of class TheMovieDbApi. - */ - @Ignore("Not required") - public void testGetGenreMovies_3args() throws Exception { - } - - /** - * Test of getGenreMovies method, of class TheMovieDbApi. - */ - @Ignore("Not required") - public void testGetGenreMovies_4args() throws Exception { - } - - /** - * Test of getMovieChangesList method, of class TheMovieDbApi. - */ - @Ignore("Not ready yet") - public void testGetMovieChangesList() throws Exception { - LOG.info("getMovieChangesList"); - int page = 0; - String startDate = ""; - String endDate = ""; - tmdb.getMovieChangesList(page, startDate, endDate); - // TODO review the generated test code and remove the default call to fail. - fail("The test case is a prototype."); - } - - /** - * Test of getPersonChangesList method, of class TheMovieDbApi. - */ - @Ignore("Not ready yet") - public void testGetPersonChangesList() throws Exception { - LOG.info("getPersonChangesList"); - int page = 0; - String startDate = ""; - String endDate = ""; - tmdb.getPersonChangesList(page, startDate, endDate); - // TODO review the generated test code and remove the default call to fail. - fail("The test case is a prototype."); - } - - /** - * Test of getJobs method, of class TheMovieDbApi. - */ - @Test - public void testGetJobs() throws Exception { - LOG.info("getJobs"); - TmdbResultsList result = tmdb.getJobs(); - assertFalse("No jobs found", result.getResults().isEmpty()); - } - - /** - * Test of getDiscover method, of class TheMovieDbApi. - */ - @Ignore("Not required") - public void testGetDiscover_14args() throws Exception { - } - - /** - * Test of getDiscover method, of class TheMovieDbApi. - */ - @Test - public void testGetDiscover_Discover() throws Exception { - LOG.info("getDiscover"); - Discover discover = new Discover(); - discover.year(2013).language(LANGUAGE_ENGLISH); - - TmdbResultsList result = tmdb.getDiscover(discover); - assertFalse("No movies discovered", result.getResults().isEmpty()); - } -} diff --git a/src/test/java/com/omertron/themoviedbapi/TheMovieDbApiTest.java b/src/test/java/com/omertron/themoviedbapi/TheMovieDbApiTest.java index 3ae578278..2765d331c 100644 --- a/src/test/java/com/omertron/themoviedbapi/TheMovieDbApiTest.java +++ b/src/test/java/com/omertron/themoviedbapi/TheMovieDbApiTest.java @@ -1,784 +1,787 @@ -/* - * Copyright (c) 2004-2013 Stuart Boston - * - * This file is part of TheMovieDB API. - * - * TheMovieDB API is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * any later version. - * - * TheMovieDB API is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with TheMovieDB API. If not, see . - * - */ -package com.omertron.themoviedbapi; - -import com.omertron.themoviedbapi.model.AlternativeTitle; -import com.omertron.themoviedbapi.model.Artwork; -import com.omertron.themoviedbapi.model.Collection; -import com.omertron.themoviedbapi.model.CollectionInfo; -import com.omertron.themoviedbapi.model.Company; -import com.omertron.themoviedbapi.model.Discover; -import com.omertron.themoviedbapi.model.Genre; -import com.omertron.themoviedbapi.model.JobDepartment; -import com.omertron.themoviedbapi.model.Keyword; -import com.omertron.themoviedbapi.model.KeywordMovie; -import com.omertron.themoviedbapi.model.MovieChanges; -import com.omertron.themoviedbapi.model.MovieDb; -import com.omertron.themoviedbapi.model.MovieDbList; -import com.omertron.themoviedbapi.model.MovieList; -import com.omertron.themoviedbapi.model.Person; -import com.omertron.themoviedbapi.model.PersonCredit; -import com.omertron.themoviedbapi.model.ReleaseInfo; -import com.omertron.themoviedbapi.model.Reviews; -import com.omertron.themoviedbapi.model.TmdbConfiguration; -import com.omertron.themoviedbapi.model.TokenAuthorisation; -import com.omertron.themoviedbapi.model.TokenSession; -import com.omertron.themoviedbapi.model.Trailer; -import com.omertron.themoviedbapi.model.Translation; -import com.omertron.themoviedbapi.results.TmdbResultsList; -import java.io.IOException; -import org.apache.commons.lang3.StringUtils; -import org.junit.*; -import static org.junit.Assert.*; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * Test cases for TheMovieDbApi API - * - * @author stuart.boston - */ -public class TheMovieDbApiTest { - - // Logger - private static final Logger LOG = LoggerFactory.getLogger(TheMovieDbApiTest.class); - // API Key - private static final String API_KEY = "5a1a77e2eba8984804586122754f969f"; - private static TheMovieDbApi tmdb; - // Test data - private static final int ID_MOVIE_BLADE_RUNNER = 78; - private static final int ID_MOVIE_THE_AVENGERS = 24428; - private static final int ID_COLLECTION_STAR_WARS = 10; - private static final int ID_PERSON_BRUCE_WILLIS = 62; - private static final int ID_COMPANY_LUCASFILM = 1; - private static final String COMPANY_NAME = "Marvel Studios"; - private static final int ID_GENRE_ACTION = 28; - private static final String ID_KEYWORD = "1721"; - // Languages - private static final String LANGUAGE_DEFAULT = ""; - private static final String LANGUAGE_ENGLISH = "en"; - private static final String LANGUAGE_RUSSIAN = "ru"; - - public TheMovieDbApiTest() throws MovieDbException { - } - - @BeforeClass - public static void setUpClass() throws Exception { - tmdb = new TheMovieDbApi(API_KEY); - TestLogger.Configure(); - } - - @AfterClass - public static void tearDownClass() throws Exception { - } - - @Before - public void setUp() { - } - - @After - public void tearDown() { - } - - /** - * Test of getConfiguration method, of class TheMovieDbApi. - */ - @Test - public void testConfiguration() throws IOException { - LOG.info("Test Configuration"); - - TmdbConfiguration tmdbConfig = tmdb.getConfiguration(); - assertNotNull("Configuration failed", tmdbConfig); - assertTrue("No base URL", StringUtils.isNotBlank(tmdbConfig.getBaseUrl())); - assertTrue("No backdrop sizes", tmdbConfig.getBackdropSizes().size() > 0); - assertTrue("No poster sizes", tmdbConfig.getPosterSizes().size() > 0); - assertTrue("No profile sizes", tmdbConfig.getProfileSizes().size() > 0); - LOG.info(tmdbConfig.toString()); - } - - /** - * Test of searchMovie method, of class TheMovieDbApi. - */ - @Test - public void testSearchMovie() throws MovieDbException { - LOG.info("searchMovie"); - - // Try a movie with less than 1 page of results - TmdbResultsList movieList = tmdb.searchMovie("Blade Runner", 0, "", true, 0); -// List movieList = tmdb.searchMovie("Blade Runner", "", true); - assertTrue("No movies found, should be at least 1", movieList.getResults().size() > 0); - - // Try a russian langugage movie - movieList = tmdb.searchMovie("О чём говорят мужчины", 0, LANGUAGE_RUSSIAN, true, 0); - assertTrue("No 'RU' movies found, should be at least 1", movieList.getResults().size() > 0); - - // Try a movie with more than 20 results - movieList = tmdb.searchMovie("Star Wars", 0, LANGUAGE_ENGLISH, false, 0); - assertTrue("Not enough movies found, should be over 15, found " + movieList.getResults().size(), movieList.getResults().size() >= 15); - } - - /** - * Test of getMovieInfo method, of class TheMovieDbApi. - */ - @Test - public void testGetMovieInfo() throws MovieDbException { - LOG.info("getMovieInfo"); - MovieDb result = tmdb.getMovieInfo(ID_MOVIE_BLADE_RUNNER, LANGUAGE_ENGLISH, "alternative_titles,casts,images,keywords,releases,trailers,translations,similar_movies,reviews,lists"); - assertEquals("Incorrect movie information", "Blade Runner", result.getOriginalTitle()); - } - - /** - * Test of getMovieAlternativeTitles method, of class TheMovieDbApi. - */ - @Test - public void testGetMovieAlternativeTitles() throws MovieDbException { - LOG.info("getMovieAlternativeTitles"); - String country = ""; - TmdbResultsList result = tmdb.getMovieAlternativeTitles(ID_MOVIE_BLADE_RUNNER, country, "casts,images,keywords,releases,trailers,translations,similar_movies,reviews,lists"); - assertTrue("No alternative titles found", result.getResults().size() > 0); - - country = "US"; - result = tmdb.getMovieAlternativeTitles(ID_MOVIE_BLADE_RUNNER, country); - assertTrue("No alternative titles found", result.getResults().size() > 0); - - } - - /** - * Test of getMovieCasts method, of class TheMovieDbApi. - */ - @Test - public void testGetMovieCasts() throws MovieDbException { - LOG.info("getMovieCasts"); - TmdbResultsList people = tmdb.getMovieCasts(ID_MOVIE_BLADE_RUNNER, "alternative_titles,casts,images,keywords,releases,trailers,translations,similar_movies,reviews,lists"); - assertTrue("No cast information", people.getResults().size() > 0); - - String name1 = "Harrison Ford"; - String name2 = "Charles Knode"; - boolean foundName1 = Boolean.FALSE; - boolean foundName2 = Boolean.FALSE; - - for (Person person : people.getResults()) { - if (!foundName1 && person.getName().equalsIgnoreCase(name1)) { - foundName1 = Boolean.TRUE; - } - - if (!foundName2 && person.getName().equalsIgnoreCase(name2)) { - foundName2 = Boolean.TRUE; - } - } - assertTrue("Couldn't find " + name1, foundName1); - assertTrue("Couldn't find " + name2, foundName2); - } - - /** - * Test of getMovieImages method, of class TheMovieDbApi. - */ - @Test - public void testGetMovieImages() throws MovieDbException { - LOG.info("getMovieImages"); - String language = ""; - TmdbResultsList result = tmdb.getMovieImages(ID_MOVIE_BLADE_RUNNER, language); - assertFalse("No artwork found", result.getResults().isEmpty()); - } - - /** - * Test of getMovieKeywords method, of class TheMovieDbApi. - */ - @Test - public void testGetMovieKeywords() throws MovieDbException { - LOG.info("getMovieKeywords"); - TmdbResultsList result = tmdb.getMovieKeywords(ID_MOVIE_BLADE_RUNNER); - assertFalse("No keywords found", result.getResults().isEmpty()); - } - - /** - * Test of getMovieReleaseInfo method, of class TheMovieDbApi. - */ - @Test - public void testGetMovieReleaseInfo() throws MovieDbException { - LOG.info("getMovieReleaseInfo"); - TmdbResultsList result = tmdb.getMovieReleaseInfo(ID_MOVIE_BLADE_RUNNER, ""); - assertFalse("Release information missing", result.getResults().isEmpty()); - } - - /** - * Test of getMovieTrailers method, of class TheMovieDbApi. - */ - @Test - public void testGetMovieTrailers() throws MovieDbException { - LOG.info("getMovieTrailers"); - TmdbResultsList result = tmdb.getMovieTrailers(ID_MOVIE_BLADE_RUNNER, ""); - assertFalse("Movie trailers missing", result.getResults().isEmpty()); - } - - /** - * Test of getMovieTranslations method, of class TheMovieDbApi. - */ - @Test - public void testGetMovieTranslations() throws MovieDbException { - LOG.info("getMovieTranslations"); - TmdbResultsList result = tmdb.getMovieTranslations(ID_MOVIE_BLADE_RUNNER); - assertFalse("No translations found", result.getResults().isEmpty()); - } - - /** - * Test of getCollectionInfo method, of class TheMovieDbApi. - */ - @Test - public void testGetCollectionInfo() throws MovieDbException { - LOG.info("getCollectionInfo"); - String language = ""; - CollectionInfo result = tmdb.getCollectionInfo(ID_COLLECTION_STAR_WARS, language); - assertFalse("No collection information", result.getParts().isEmpty()); - } - - /** - * Test of createImageUrl method, of class TheMovieDbApi. - * - * @throws MovieDbException - */ - @Test - public void testCreateImageUrl() throws MovieDbException { - LOG.info("createImageUrl"); - MovieDb movie = tmdb.getMovieInfo(ID_MOVIE_BLADE_RUNNER, ""); - String result = tmdb.createImageUrl(movie.getPosterPath(), "original").toString(); - assertTrue("Error compiling image URL", !result.isEmpty()); - } - - /** - * Test of getMovieInfoImdb method, of class TheMovieDbApi. - */ - @Test - public void testGetMovieInfoImdb() throws MovieDbException { - LOG.info("getMovieInfoImdb"); - MovieDb result = tmdb.getMovieInfoImdb("tt0076759", "en-US"); - assertTrue("Error getting the movie from IMDB ID", result.getId() == 11); - } - - /** - * Test of getApiKey method, of class TheMovieDbApi. - */ - @Test - public void testGetApiKey() { - // Not required - } - - /** - * Test of getApiBase method, of class TheMovieDbApi. - */ - @Test - public void testGetApiBase() { - // Not required - } - - /** - * Test of getConfiguration method, of class TheMovieDbApi. - */ - @Test - public void testGetConfiguration() { - // Not required - } - - /** - * Test of searchPeople method, of class TheMovieDbApi. - */ - @Test - public void testSearchPeople() throws MovieDbException { - LOG.info("searchPeople"); - String personName = "Bruce Willis"; - boolean includeAdult = false; - TmdbResultsList result = tmdb.searchPeople(personName, includeAdult, 0); - assertTrue("Couldn't find the person", result.getResults().size() > 0); - } - - /** - * Test of getPersonInfo method, of class TheMovieDbApi. - */ - @Test - public void testGetPersonInfo() throws MovieDbException { - LOG.info("getPersonInfo"); - Person result = tmdb.getPersonInfo(ID_PERSON_BRUCE_WILLIS); - assertTrue("Wrong actor returned", result.getId() == ID_PERSON_BRUCE_WILLIS); - } - - /** - * Test of getPersonCredits method, of class TheMovieDbApi. - */ - @Test - public void testGetPersonCredits() throws MovieDbException { - LOG.info("getPersonCredits"); - - TmdbResultsList result = tmdb.getPersonCredits(ID_PERSON_BRUCE_WILLIS); - assertTrue("No cast information", result.getResults().size() > 0); - } - - /** - * Test of getPersonImages method, of class TheMovieDbApi. - */ - @Test - public void testGetPersonImages() throws MovieDbException { - LOG.info("getPersonImages"); - - TmdbResultsList result = tmdb.getPersonImages(ID_PERSON_BRUCE_WILLIS); - assertTrue("No cast information", result.getResults().size() > 0); - } - - /** - * Test of getLatestMovie method, of class TheMovieDbApi. - */ - @Test - public void testGetLatestMovie() throws MovieDbException { - LOG.info("getLatestMovie"); - MovieDb result = tmdb.getLatestMovie(); - assertTrue("No latest movie found", result != null); - assertTrue("No latest movie found", result.getId() > 0); - } - - /** - * Test of compareMovies method, of class TheMovieDbApi. - */ - @Test - public void testCompareMovies() { - // Not required - } - - /** - * Test of setProxy method, of class TheMovieDbApi. - */ - @Test - public void testSetProxy() { - // Not required - } - - /** - * Test of setTimeout method, of class TheMovieDbApi. - */ - @Test - public void testSetTimeout() { - // Not required - } - - /** - * Test of getNowPlayingMovies method, of class TheMovieDbApi. - */ - @Test - public void testGetNowPlayingMovies() throws MovieDbException { - LOG.info("getNowPlayingMovies"); - TmdbResultsList result = tmdb.getNowPlayingMovies(LANGUAGE_DEFAULT, 0); - assertTrue("No now playing movies found", !result.getResults().isEmpty()); - } - - /** - * Test of getPopularMovieList method, of class TheMovieDbApi. - */ - @Test - public void testGetPopularMovieList() throws MovieDbException { - LOG.info("getPopularMovieList"); - TmdbResultsList result = tmdb.getPopularMovieList(LANGUAGE_DEFAULT, 0); - assertTrue("No popular movies found", !result.getResults().isEmpty()); - } - - /** - * Test of getTopRatedMovies method, of class TheMovieDbApi. - */ - @Test - public void testGetTopRatedMovies() throws MovieDbException { - LOG.info("getTopRatedMovies"); - TmdbResultsList result = tmdb.getTopRatedMovies(LANGUAGE_DEFAULT, 0); - assertTrue("No top rated movies found", !result.getResults().isEmpty()); - } - - /** - * Test of getCompanyInfo method, of class TheMovieDbApi. - */ - @Test - public void testGetCompanyInfo() throws MovieDbException { - LOG.info("getCompanyInfo"); - Company company = tmdb.getCompanyInfo(ID_COMPANY_LUCASFILM); - assertTrue("No company information found", company.getCompanyId() > 0); - } - - /** - * Test of getCompanyMovies method, of class TheMovieDbApi. - */ - @Test - public void testGetCompanyMovies() throws MovieDbException { - LOG.info("getCompanyMovies"); - TmdbResultsList result = tmdb.getCompanyMovies(ID_COMPANY_LUCASFILM, LANGUAGE_DEFAULT, 0); - assertTrue("No company movies found", !result.getResults().isEmpty()); - } - - /** - * Test of searchCompanies method, of class TheMovieDbApi. - */ - @Test - public void testSearchCompanies() throws MovieDbException { - LOG.info("searchCompanies"); - TmdbResultsList result = tmdb.searchCompanies(COMPANY_NAME, 0); - assertTrue("No company information found", !result.getResults().isEmpty()); - } - - /** - * Test of getSimilarMovies method, of class TheMovieDbApi. - */ - @Test - public void testGetSimilarMovies() throws MovieDbException { - LOG.info("getSimilarMovies"); - TmdbResultsList result = tmdb.getSimilarMovies(ID_MOVIE_BLADE_RUNNER, LANGUAGE_DEFAULT, 0); - assertTrue("No similar movies found", !result.getResults().isEmpty()); - } - - /** - * Test of getGenreList method, of class TheMovieDbApi. - */ - @Test - public void testGetGenreList() throws MovieDbException { - LOG.info("getGenreList"); - TmdbResultsList result = tmdb.getGenreList(LANGUAGE_DEFAULT); - assertTrue("No genres found", !result.getResults().isEmpty()); - } - - /** - * Test of getGenreMovies method, of class TheMovieDbApi. - */ - @Test - public void testGetGenreMovies() throws MovieDbException { - LOG.info("getGenreMovies"); - TmdbResultsList result = tmdb.getGenreMovies(ID_GENRE_ACTION, LANGUAGE_DEFAULT, 0, Boolean.TRUE); - assertTrue("No genre movies found", !result.getResults().isEmpty()); - } - - /** - * Test of getUpcoming method, of class TheMovieDbApi. - */ - @Test - public void testGetUpcoming() throws Exception { - LOG.info("getUpcoming"); - TmdbResultsList result = tmdb.getUpcoming(LANGUAGE_DEFAULT, 0); - assertTrue("No upcoming movies found", !result.getResults().isEmpty()); - } - - /** - * Test of getCollectionImages method, of class TheMovieDbApi. - */ - @Test - public void testGetCollectionImages() throws Exception { - LOG.info("getCollectionImages"); - TmdbResultsList result = tmdb.getCollectionImages(ID_COLLECTION_STAR_WARS, LANGUAGE_DEFAULT); - assertFalse("No artwork found", result.getResults().isEmpty()); - } - - /** - * Test of getAuthorisationToken method, of class TheMovieDbApi. - */ - @Test - public void testGetAuthorisationToken() throws Exception { - LOG.info("getAuthorisationToken"); - TokenAuthorisation result = tmdb.getAuthorisationToken(); - assertFalse("Token is null", result == null); - assertTrue("Token is not valid", result.getSuccess()); - LOG.info(result.toString()); - } - - /** - * Test of getSessionToken method, of class TheMovieDbApi. - * - * TODO: Cannot be tested without a HTTP authorisation: http://help.themoviedb.org/kb/api/user-authentication - */ - public void testGetSessionToken() throws Exception { - LOG.info("getSessionToken"); - TokenAuthorisation token = tmdb.getAuthorisationToken(); - assertFalse("Token is null", token == null); - assertTrue("Token is not valid", token.getSuccess()); - LOG.info(token.toString()); - - TokenSession result = tmdb.getSessionToken(token); - assertFalse("Session token is null", result == null); - assertTrue("Session token is not valid", result.getSuccess()); - LOG.info(result.toString()); - } - - /** - * Test of getGuestSessionToken method, of class TheMovieDbApi. - */ - @Ignore("Not ready yet") - public void testGetGuestSessionToken() throws Exception { - LOG.info("getGuestSessionToken"); - TokenSession result = tmdb.getGuestSessionToken(); - - assertTrue("Failed to get guest session", result.getSuccess()); - } - - @Test - public void testGetMovieLists() throws Exception { - LOG.info("getMovieLists"); - TmdbResultsList result = tmdb.getMovieLists(ID_MOVIE_BLADE_RUNNER, LANGUAGE_ENGLISH, 0); - assertNotNull("No results found", result); - assertTrue("No results found", result.getResults().size() > 0); - } - - /** - * Test of getMovieChanges method,of class TheMovieDbApi - * - * TODO: Do not test this until it is fixed - */ - public void testGetMovieChanges() throws Exception { - LOG.info("getMovieChanges"); - - String startDate = ""; - String endDate = null; - - // Get some popular movies - TmdbResultsList movieList = tmdb.getPopularMovieList(LANGUAGE_DEFAULT, 0); - for (MovieDb movie : movieList.getResults()) { - TmdbResultsList result = tmdb.getMovieChanges(movie.getId(), startDate, endDate); - LOG.info("{} has {} changes.", new Object[]{movie.getTitle(), result.getResults().size()}); - } - - assertNotNull("No results found", movieList.getResults()); - assertTrue("No results found", movieList.getResults().size() > 0); - } - - @Test - public void testGetPersonLatest() throws Exception { - LOG.info("getPersonLatest"); - - Person result = tmdb.getPersonLatest(); - - assertNotNull("No results found", result); - assertTrue("No results found", StringUtils.isNotBlank(result.getName())); - } - - /** - * Test of searchCollection method, of class TheMovieDbApi. - */ - @Test - public void testSearchCollection() throws Exception { - LOG.info("searchCollection"); - String query = "batman"; - int page = 0; - TmdbResultsList result = tmdb.searchCollection(query, LANGUAGE_DEFAULT, page); - assertFalse("No collections found", result == null); - assertTrue("No collections found", result.getResults().size() > 0); - } - - /** - * Test of searchList method, of class TheMovieDbApi. - */ - @Test - public void testSearchList() throws Exception { - LOG.info("searchList"); - String query = "watch"; - int page = 0; - TmdbResultsList result = tmdb.searchList(query, LANGUAGE_DEFAULT, page); - assertFalse("No lists found", result.getResults() == null); - assertTrue("No lists found", result.getResults().size() > 0); - } - - /** - * Test of searchKeyword method, of class TheMovieDbApi. - */ - @Test - public void testSearchKeyword() throws Exception { - LOG.info("searchKeyword"); - String query = "action"; - int page = 0; - TmdbResultsList result = tmdb.searchKeyword(query, page); - assertFalse("No keywords found", result.getResults() == null); - assertTrue("No keywords found", result.getResults().size() > 0); - } - - /** - * Test of postMovieRating method, of class TheMovieDbApi. - * - * TODO: Cannot be tested without a HTTP authorisation: http://help.themoviedb.org/kb/api/user-authentication - */ - @Ignore("Not ready yet") - public void testPostMovieRating() throws Exception { - LOG.info("postMovieRating"); - String sessionId = ""; - String rating = ""; - boolean expResult = false; - boolean result = tmdb.postMovieRating(sessionId, rating); - assertEquals(expResult, result); - // TODO review the generated test code and remove the default call to fail. - fail("The test case is a prototype."); - } - - /** - * Test of getPersonChanges method, of class TheMovieDbApi. - * - */ - @Ignore("Not ready yet") - public void testGetPersonChanges() throws Exception { - LOG.info("getPersonChanges"); - String startDate = ""; - String endDate = ""; - tmdb.getPersonChanges(ID_PERSON_BRUCE_WILLIS, startDate, endDate); - } - - /** - * Test of getList method, of class TheMovieDbApi. - */ - @Test - public void testGetList() throws Exception { - LOG.info("getList"); - String listId = "509ec17b19c2950a0600050d"; - MovieDbList result = tmdb.getList(listId); - assertFalse("List not found", result.getItems().isEmpty()); - } - - /** - * Test of getKeyword method, of class TheMovieDbApi. - */ - @Test - public void testGetKeyword() throws Exception { - LOG.info("getKeyword"); - Keyword result = tmdb.getKeyword(ID_KEYWORD); - assertEquals("fight", result.getName()); - } - - /** - * Test of getKeywordMovies method, of class TheMovieDbApi. - */ - @Test - public void testGetKeywordMovies() throws Exception { - LOG.info("getKeywordMovies"); - int page = 0; - TmdbResultsList result = tmdb.getKeywordMovies(ID_KEYWORD, LANGUAGE_DEFAULT, page); - assertFalse("No keyword movies found", result.getResults().isEmpty()); - } - - /** - * Test of getReviews method, of class TheMovieDbApi. - */ - @Test - public void testGetReviews() throws Exception { - LOG.info("getReviews"); - int page = 0; - TmdbResultsList result = tmdb.getReviews(ID_MOVIE_THE_AVENGERS, LANGUAGE_DEFAULT, page); - - assertFalse("No reviews found", result.getResults().isEmpty()); - } - - /** - * Test of compareMovies method, of class TheMovieDbApi. - */ - @Ignore("Not required") - public void testCompareMovies_3args() { - } - - /** - * Test of compareMovies method, of class TheMovieDbApi. - */ - @Ignore("Not required") - public void testCompareMovies_4args() { - } - - /** - * Test of getPersonPopular method, of class TheMovieDbApi. - */ - @Ignore("Not required") - public void testGetPersonPopular_0args() throws Exception { - } - - /** - * Test of getPersonPopular method, of class TheMovieDbApi. - */ - @Test - public void testGetPersonPopular_int() throws Exception { - LOG.info("getPersonPopular"); - int page = 0; - TmdbResultsList result = tmdb.getPersonPopular(page); - assertFalse("No popular people", result.getResults().isEmpty()); - } - - /** - * Test of getGenreMovies method, of class TheMovieDbApi. - */ - @Ignore("Not required") - public void testGetGenreMovies_3args() throws Exception { - } - - /** - * Test of getGenreMovies method, of class TheMovieDbApi. - */ - @Ignore("Not required") - public void testGetGenreMovies_4args() throws Exception { - } - - /** - * Test of getMovieChangesList method, of class TheMovieDbApi. - */ - @Ignore("Not ready yet") - public void testGetMovieChangesList() throws Exception { - LOG.info("getMovieChangesList"); - int page = 0; - String startDate = ""; - String endDate = ""; - tmdb.getMovieChangesList(page, startDate, endDate); - // TODO review the generated test code and remove the default call to fail. - fail("The test case is a prototype."); - } - - /** - * Test of getPersonChangesList method, of class TheMovieDbApi. - */ - @Ignore("Not ready yet") - public void testGetPersonChangesList() throws Exception { - LOG.info("getPersonChangesList"); - int page = 0; - String startDate = ""; - String endDate = ""; - tmdb.getPersonChangesList(page, startDate, endDate); - // TODO review the generated test code and remove the default call to fail. - fail("The test case is a prototype."); - } - - /** - * Test of getJobs method, of class TheMovieDbApi. - */ - @Test - public void testGetJobs() throws Exception { - LOG.info("getJobs"); - TmdbResultsList result = tmdb.getJobs(); - assertFalse("No jobs found", result.getResults().isEmpty()); - } - - /** - * Test of getDiscover method, of class TheMovieDbApi. - */ - @Ignore("Not required") - public void testGetDiscover_14args() throws Exception { - } - - /** - * Test of getDiscover method, of class TheMovieDbApi. - */ - @Test - public void testGetDiscover_Discover() throws Exception { - LOG.info("getDiscover"); - Discover discover = new Discover(); - discover.year(2013).language(LANGUAGE_ENGLISH); - - TmdbResultsList result = tmdb.getDiscover(discover); - assertFalse("No movies discovered", result.getResults().isEmpty()); - } -} +/* + * Copyright (c) 2004-2013 Stuart Boston + * + * This file is part of TheMovieDB API. + * + * TheMovieDB API is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * any later version. + * + * TheMovieDB API is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with TheMovieDB API. If not, see . + * + */ +package com.omertron.themoviedbapi; + +import com.omertron.themoviedbapi.model.AlternativeTitle; +import com.omertron.themoviedbapi.model.Artwork; +import com.omertron.themoviedbapi.model.ChangeKeyItem; +import com.omertron.themoviedbapi.model.ChangedItem; +import com.omertron.themoviedbapi.model.Collection; +import com.omertron.themoviedbapi.model.CollectionInfo; +import com.omertron.themoviedbapi.model.Company; +import com.omertron.themoviedbapi.model.Discover; +import com.omertron.themoviedbapi.model.Genre; +import com.omertron.themoviedbapi.model.JobDepartment; +import com.omertron.themoviedbapi.model.Keyword; +import com.omertron.themoviedbapi.model.KeywordMovie; +import com.omertron.themoviedbapi.model.MovieDb; +import com.omertron.themoviedbapi.model.MovieDbList; +import com.omertron.themoviedbapi.model.MovieList; +import com.omertron.themoviedbapi.model.Person; +import com.omertron.themoviedbapi.model.PersonCredit; +import com.omertron.themoviedbapi.model.ReleaseInfo; +import com.omertron.themoviedbapi.model.Reviews; +import com.omertron.themoviedbapi.model.TmdbConfiguration; +import com.omertron.themoviedbapi.model.TokenAuthorisation; +import com.omertron.themoviedbapi.model.TokenSession; +import com.omertron.themoviedbapi.model.Trailer; +import com.omertron.themoviedbapi.model.Translation; +import com.omertron.themoviedbapi.results.TmdbResultsList; +import com.omertron.themoviedbapi.results.TmdbResultsMap; +import java.io.IOException; +import java.util.List; +import org.apache.commons.lang3.StringUtils; +import org.junit.*; +import static org.junit.Assert.*; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Test cases for TheMovieDbApi API + * + * @author stuart.boston + */ +public class TheMovieDbApiTest { + + // Logger + private static final Logger LOG = LoggerFactory.getLogger(TheMovieDbApiTest.class); + // API Key + private static final String API_KEY = "5a1a77e2eba8984804586122754f969f"; + private static TheMovieDbApi tmdb; + // Test data + private static final int ID_MOVIE_BLADE_RUNNER = 78; + private static final int ID_MOVIE_THE_AVENGERS = 24428; + private static final int ID_COLLECTION_STAR_WARS = 10; + private static final int ID_PERSON_BRUCE_WILLIS = 62; + private static final int ID_COMPANY_LUCASFILM = 1; + private static final String COMPANY_NAME = "Marvel Studios"; + private static final int ID_GENRE_ACTION = 28; + private static final String ID_KEYWORD = "1721"; + // Languages + private static final String LANGUAGE_DEFAULT = ""; + private static final String LANGUAGE_ENGLISH = "en"; + private static final String LANGUAGE_RUSSIAN = "ru"; + + public TheMovieDbApiTest() throws MovieDbException { + } + + @BeforeClass + public static void setUpClass() throws Exception { + tmdb = new TheMovieDbApi(API_KEY); + TestLogger.Configure(); + } + + @AfterClass + public static void tearDownClass() throws Exception { + } + + @Before + public void setUp() { + } + + @After + public void tearDown() { + } + + /** + * Test of getConfiguration method, of class TheMovieDbApi. + */ + @Test + public void testConfiguration() throws IOException { + LOG.info("Test Configuration"); + + TmdbConfiguration tmdbConfig = tmdb.getConfiguration(); + assertNotNull("Configuration failed", tmdbConfig); + assertTrue("No base URL", StringUtils.isNotBlank(tmdbConfig.getBaseUrl())); + assertTrue("No backdrop sizes", tmdbConfig.getBackdropSizes().size() > 0); + assertTrue("No poster sizes", tmdbConfig.getPosterSizes().size() > 0); + assertTrue("No profile sizes", tmdbConfig.getProfileSizes().size() > 0); + LOG.info(tmdbConfig.toString()); + } + + /** + * Test of searchMovie method, of class TheMovieDbApi. + */ + @Test + public void testSearchMovie() throws MovieDbException { + LOG.info("searchMovie"); + + // Try a movie with less than 1 page of results + TmdbResultsList movieList = tmdb.searchMovie("Blade Runner", 0, "", true, 0); +// List movieList = tmdb.searchMovie("Blade Runner", "", true); + assertTrue("No movies found, should be at least 1", movieList.getResults().size() > 0); + + // Try a russian langugage movie + movieList = tmdb.searchMovie("О чём говорят мужчины", 0, LANGUAGE_RUSSIAN, true, 0); + assertTrue("No 'RU' movies found, should be at least 1", movieList.getResults().size() > 0); + + // Try a movie with more than 20 results + movieList = tmdb.searchMovie("Star Wars", 0, LANGUAGE_ENGLISH, false, 0); + assertTrue("Not enough movies found, should be over 15, found " + movieList.getResults().size(), movieList.getResults().size() >= 15); + } + + /** + * Test of getMovieInfo method, of class TheMovieDbApi. + */ + @Test + public void testGetMovieInfo() throws MovieDbException { + LOG.info("getMovieInfo"); + MovieDb result = tmdb.getMovieInfo(ID_MOVIE_BLADE_RUNNER, LANGUAGE_ENGLISH, "alternative_titles,casts,images,keywords,releases,trailers,translations,similar_movies,reviews,lists"); + assertEquals("Incorrect movie information", "Blade Runner", result.getOriginalTitle()); + } + + /** + * Test of getMovieAlternativeTitles method, of class TheMovieDbApi. + */ + @Test + public void testGetMovieAlternativeTitles() throws MovieDbException { + LOG.info("getMovieAlternativeTitles"); + String country = ""; + TmdbResultsList result = tmdb.getMovieAlternativeTitles(ID_MOVIE_BLADE_RUNNER, country, "casts,images,keywords,releases,trailers,translations,similar_movies,reviews,lists"); + assertTrue("No alternative titles found", result.getResults().size() > 0); + + country = "US"; + result = tmdb.getMovieAlternativeTitles(ID_MOVIE_BLADE_RUNNER, country); + assertTrue("No alternative titles found", result.getResults().size() > 0); + + } + + /** + * Test of getMovieCasts method, of class TheMovieDbApi. + */ + @Test + public void testGetMovieCasts() throws MovieDbException { + LOG.info("getMovieCasts"); + TmdbResultsList people = tmdb.getMovieCasts(ID_MOVIE_BLADE_RUNNER, "alternative_titles,casts,images,keywords,releases,trailers,translations,similar_movies,reviews,lists"); + assertTrue("No cast information", people.getResults().size() > 0); + + String name1 = "Harrison Ford"; + String name2 = "Charles Knode"; + boolean foundName1 = Boolean.FALSE; + boolean foundName2 = Boolean.FALSE; + + for (Person person : people.getResults()) { + if (!foundName1 && person.getName().equalsIgnoreCase(name1)) { + foundName1 = Boolean.TRUE; + } + + if (!foundName2 && person.getName().equalsIgnoreCase(name2)) { + foundName2 = Boolean.TRUE; + } + } + assertTrue("Couldn't find " + name1, foundName1); + assertTrue("Couldn't find " + name2, foundName2); + } + + /** + * Test of getMovieImages method, of class TheMovieDbApi. + */ + @Test + public void testGetMovieImages() throws MovieDbException { + LOG.info("getMovieImages"); + String language = ""; + TmdbResultsList result = tmdb.getMovieImages(ID_MOVIE_BLADE_RUNNER, language); + assertFalse("No artwork found", result.getResults().isEmpty()); + } + + /** + * Test of getMovieKeywords method, of class TheMovieDbApi. + */ + @Test + public void testGetMovieKeywords() throws MovieDbException { + LOG.info("getMovieKeywords"); + TmdbResultsList result = tmdb.getMovieKeywords(ID_MOVIE_BLADE_RUNNER); + assertFalse("No keywords found", result.getResults().isEmpty()); + } + + /** + * Test of getMovieReleaseInfo method, of class TheMovieDbApi. + */ + @Test + public void testGetMovieReleaseInfo() throws MovieDbException { + LOG.info("getMovieReleaseInfo"); + TmdbResultsList result = tmdb.getMovieReleaseInfo(ID_MOVIE_BLADE_RUNNER, ""); + assertFalse("Release information missing", result.getResults().isEmpty()); + } + + /** + * Test of getMovieTrailers method, of class TheMovieDbApi. + */ + @Test + public void testGetMovieTrailers() throws MovieDbException { + LOG.info("getMovieTrailers"); + TmdbResultsList result = tmdb.getMovieTrailers(ID_MOVIE_BLADE_RUNNER, ""); + assertFalse("Movie trailers missing", result.getResults().isEmpty()); + } + + /** + * Test of getMovieTranslations method, of class TheMovieDbApi. + */ + @Test + public void testGetMovieTranslations() throws MovieDbException { + LOG.info("getMovieTranslations"); + TmdbResultsList result = tmdb.getMovieTranslations(ID_MOVIE_BLADE_RUNNER); + assertFalse("No translations found", result.getResults().isEmpty()); + } + + /** + * Test of getCollectionInfo method, of class TheMovieDbApi. + */ + @Test + public void testGetCollectionInfo() throws MovieDbException { + LOG.info("getCollectionInfo"); + String language = ""; + CollectionInfo result = tmdb.getCollectionInfo(ID_COLLECTION_STAR_WARS, language); + assertFalse("No collection information", result.getParts().isEmpty()); + } + + /** + * Test of createImageUrl method, of class TheMovieDbApi. + * + * @throws MovieDbException + */ + @Test + public void testCreateImageUrl() throws MovieDbException { + LOG.info("createImageUrl"); + MovieDb movie = tmdb.getMovieInfo(ID_MOVIE_BLADE_RUNNER, ""); + String result = tmdb.createImageUrl(movie.getPosterPath(), "original").toString(); + assertTrue("Error compiling image URL", !result.isEmpty()); + } + + /** + * Test of getMovieInfoImdb method, of class TheMovieDbApi. + */ + @Test + public void testGetMovieInfoImdb() throws MovieDbException { + LOG.info("getMovieInfoImdb"); + MovieDb result = tmdb.getMovieInfoImdb("tt0076759", "en-US"); + assertTrue("Error getting the movie from IMDB ID", result.getId() == 11); + } + + /** + * Test of getApiKey method, of class TheMovieDbApi. + */ + @Test + public void testGetApiKey() { + // Not required + } + + /** + * Test of getApiBase method, of class TheMovieDbApi. + */ + @Test + public void testGetApiBase() { + // Not required + } + + /** + * Test of getConfiguration method, of class TheMovieDbApi. + */ + @Test + public void testGetConfiguration() { + // Not required + } + + /** + * Test of searchPeople method, of class TheMovieDbApi. + */ + @Test + public void testSearchPeople() throws MovieDbException { + LOG.info("searchPeople"); + String personName = "Bruce Willis"; + boolean includeAdult = false; + TmdbResultsList result = tmdb.searchPeople(personName, includeAdult, 0); + assertTrue("Couldn't find the person", result.getResults().size() > 0); + } + + /** + * Test of getPersonInfo method, of class TheMovieDbApi. + */ + @Test + public void testGetPersonInfo() throws MovieDbException { + LOG.info("getPersonInfo"); + Person result = tmdb.getPersonInfo(ID_PERSON_BRUCE_WILLIS); + assertTrue("Wrong actor returned", result.getId() == ID_PERSON_BRUCE_WILLIS); + } + + /** + * Test of getPersonCredits method, of class TheMovieDbApi. + */ + @Test + public void testGetPersonCredits() throws MovieDbException { + LOG.info("getPersonCredits"); + + TmdbResultsList result = tmdb.getPersonCredits(ID_PERSON_BRUCE_WILLIS); + assertTrue("No cast information", result.getResults().size() > 0); + } + + /** + * Test of getPersonImages method, of class TheMovieDbApi. + */ + @Test + public void testGetPersonImages() throws MovieDbException { + LOG.info("getPersonImages"); + + TmdbResultsList result = tmdb.getPersonImages(ID_PERSON_BRUCE_WILLIS); + assertTrue("No cast information", result.getResults().size() > 0); + } + + /** + * Test of getLatestMovie method, of class TheMovieDbApi. + */ + @Test + public void testGetLatestMovie() throws MovieDbException { + LOG.info("getLatestMovie"); + MovieDb result = tmdb.getLatestMovie(); + assertTrue("No latest movie found", result != null); + assertTrue("No latest movie found", result.getId() > 0); + } + + /** + * Test of compareMovies method, of class TheMovieDbApi. + */ + @Test + public void testCompareMovies() { + // Not required + } + + /** + * Test of setProxy method, of class TheMovieDbApi. + */ + @Test + public void testSetProxy() { + // Not required + } + + /** + * Test of setTimeout method, of class TheMovieDbApi. + */ + @Test + public void testSetTimeout() { + // Not required + } + + /** + * Test of getNowPlayingMovies method, of class TheMovieDbApi. + */ + @Test + public void testGetNowPlayingMovies() throws MovieDbException { + LOG.info("getNowPlayingMovies"); + TmdbResultsList result = tmdb.getNowPlayingMovies(LANGUAGE_DEFAULT, 0); + assertTrue("No now playing movies found", !result.getResults().isEmpty()); + } + + /** + * Test of getPopularMovieList method, of class TheMovieDbApi. + */ + @Test + public void testGetPopularMovieList() throws MovieDbException { + LOG.info("getPopularMovieList"); + TmdbResultsList result = tmdb.getPopularMovieList(LANGUAGE_DEFAULT, 0); + assertTrue("No popular movies found", !result.getResults().isEmpty()); + } + + /** + * Test of getTopRatedMovies method, of class TheMovieDbApi. + */ + @Test + public void testGetTopRatedMovies() throws MovieDbException { + LOG.info("getTopRatedMovies"); + TmdbResultsList result = tmdb.getTopRatedMovies(LANGUAGE_DEFAULT, 0); + assertTrue("No top rated movies found", !result.getResults().isEmpty()); + } + + /** + * Test of getCompanyInfo method, of class TheMovieDbApi. + */ + @Test + public void testGetCompanyInfo() throws MovieDbException { + LOG.info("getCompanyInfo"); + Company company = tmdb.getCompanyInfo(ID_COMPANY_LUCASFILM); + assertTrue("No company information found", company.getCompanyId() > 0); + } + + /** + * Test of getCompanyMovies method, of class TheMovieDbApi. + */ + @Test + public void testGetCompanyMovies() throws MovieDbException { + LOG.info("getCompanyMovies"); + TmdbResultsList result = tmdb.getCompanyMovies(ID_COMPANY_LUCASFILM, LANGUAGE_DEFAULT, 0); + assertTrue("No company movies found", !result.getResults().isEmpty()); + } + + /** + * Test of searchCompanies method, of class TheMovieDbApi. + */ + @Test + public void testSearchCompanies() throws MovieDbException { + LOG.info("searchCompanies"); + TmdbResultsList result = tmdb.searchCompanies(COMPANY_NAME, 0); + assertTrue("No company information found", !result.getResults().isEmpty()); + } + + /** + * Test of getSimilarMovies method, of class TheMovieDbApi. + */ + @Test + public void testGetSimilarMovies() throws MovieDbException { + LOG.info("getSimilarMovies"); + TmdbResultsList result = tmdb.getSimilarMovies(ID_MOVIE_BLADE_RUNNER, LANGUAGE_DEFAULT, 0); + assertTrue("No similar movies found", !result.getResults().isEmpty()); + } + + /** + * Test of getGenreList method, of class TheMovieDbApi. + */ + @Test + public void testGetGenreList() throws MovieDbException { + LOG.info("getGenreList"); + TmdbResultsList result = tmdb.getGenreList(LANGUAGE_DEFAULT); + assertTrue("No genres found", !result.getResults().isEmpty()); + } + + /** + * Test of getGenreMovies method, of class TheMovieDbApi. + */ + @Test + public void testGetGenreMovies() throws MovieDbException { + LOG.info("getGenreMovies"); + TmdbResultsList result = tmdb.getGenreMovies(ID_GENRE_ACTION, LANGUAGE_DEFAULT, 0, Boolean.TRUE); + assertTrue("No genre movies found", !result.getResults().isEmpty()); + } + + /** + * Test of getUpcoming method, of class TheMovieDbApi. + */ + @Test + public void testGetUpcoming() throws Exception { + LOG.info("getUpcoming"); + TmdbResultsList result = tmdb.getUpcoming(LANGUAGE_DEFAULT, 0); + assertTrue("No upcoming movies found", !result.getResults().isEmpty()); + } + + /** + * Test of getCollectionImages method, of class TheMovieDbApi. + */ + @Test + public void testGetCollectionImages() throws Exception { + LOG.info("getCollectionImages"); + TmdbResultsList result = tmdb.getCollectionImages(ID_COLLECTION_STAR_WARS, LANGUAGE_DEFAULT); + assertFalse("No artwork found", result.getResults().isEmpty()); + } + + /** + * Test of getAuthorisationToken method, of class TheMovieDbApi. + */ + @Test + public void testGetAuthorisationToken() throws Exception { + LOG.info("getAuthorisationToken"); + TokenAuthorisation result = tmdb.getAuthorisationToken(); + assertFalse("Token is null", result == null); + assertTrue("Token is not valid", result.getSuccess()); + LOG.info(result.toString()); + } + + /** + * Test of getSessionToken method, of class TheMovieDbApi. + * + * TODO: Cannot be tested without a HTTP authorisation: http://help.themoviedb.org/kb/api/user-authentication + */ + public void testGetSessionToken() throws Exception { + LOG.info("getSessionToken"); + TokenAuthorisation token = tmdb.getAuthorisationToken(); + assertFalse("Token is null", token == null); + assertTrue("Token is not valid", token.getSuccess()); + LOG.info(token.toString()); + + TokenSession result = tmdb.getSessionToken(token); + assertFalse("Session token is null", result == null); + assertTrue("Session token is not valid", result.getSuccess()); + LOG.info(result.toString()); + } + + /** + * Test of getGuestSessionToken method, of class TheMovieDbApi. + */ + @Ignore("Not ready yet") + public void testGetGuestSessionToken() throws Exception { + LOG.info("getGuestSessionToken"); + TokenSession result = tmdb.getGuestSessionToken(); + + assertTrue("Failed to get guest session", result.getSuccess()); + } + + @Test + public void testGetMovieLists() throws Exception { + LOG.info("getMovieLists"); + TmdbResultsList result = tmdb.getMovieLists(ID_MOVIE_BLADE_RUNNER, LANGUAGE_ENGLISH, 0); + assertNotNull("No results found", result); + assertTrue("No results found", result.getResults().size() > 0); + } + + /** + * Test of getMovieChanges method,of class TheMovieDbApi + * + * TODO: Do not test this until it is fixed + */ + @Test + public void testGetMovieChanges() throws Exception { + LOG.info("getMovieChanges"); + + String startDate = ""; + String endDate = null; + + // Get some popular movies + TmdbResultsList movieList = tmdb.getPopularMovieList(LANGUAGE_DEFAULT, 0); + for (MovieDb movie : movieList.getResults()) { + TmdbResultsMap> result = tmdb.getMovieChanges(movie.getId(), startDate, endDate); + LOG.info("{} has {} changes.", movie.getTitle(), result.getResults().size()); + assertTrue("No changes found", result.getResults().size() > 0); + break; + } + } + + @Test + public void testGetPersonLatest() throws Exception { + LOG.info("getPersonLatest"); + + Person result = tmdb.getPersonLatest(); + + assertNotNull("No results found", result); + assertTrue("No results found", StringUtils.isNotBlank(result.getName())); + } + + /** + * Test of searchCollection method, of class TheMovieDbApi. + */ + @Test + public void testSearchCollection() throws Exception { + LOG.info("searchCollection"); + String query = "batman"; + int page = 0; + TmdbResultsList result = tmdb.searchCollection(query, LANGUAGE_DEFAULT, page); + assertFalse("No collections found", result == null); + assertTrue("No collections found", result.getResults().size() > 0); + } + + /** + * Test of searchList method, of class TheMovieDbApi. + */ + @Test + public void testSearchList() throws Exception { + LOG.info("searchList"); + String query = "watch"; + int page = 0; + TmdbResultsList result = tmdb.searchList(query, LANGUAGE_DEFAULT, page); + assertFalse("No lists found", result.getResults() == null); + assertTrue("No lists found", result.getResults().size() > 0); + } + + /** + * Test of searchKeyword method, of class TheMovieDbApi. + */ + @Test + public void testSearchKeyword() throws Exception { + LOG.info("searchKeyword"); + String query = "action"; + int page = 0; + TmdbResultsList result = tmdb.searchKeyword(query, page); + assertFalse("No keywords found", result.getResults() == null); + assertTrue("No keywords found", result.getResults().size() > 0); + } + + /** + * Test of postMovieRating method, of class TheMovieDbApi. + * + * TODO: Cannot be tested without a HTTP authorisation: http://help.themoviedb.org/kb/api/user-authentication + */ + @Ignore("Not ready yet") + public void testPostMovieRating() throws Exception { + LOG.info("postMovieRating"); + String sessionId = ""; + String rating = ""; + boolean expResult = false; + boolean result = tmdb.postMovieRating(sessionId, rating); + assertEquals(expResult, result); + // TODO review the generated test code and remove the default call to fail. + fail("The test case is a prototype."); + } + + /** + * Test of getPersonChanges method, of class TheMovieDbApi. + * + */ + @Ignore("Not ready yet") + public void testGetPersonChanges() throws Exception { + LOG.info("getPersonChanges"); + String startDate = ""; + String endDate = ""; + tmdb.getPersonChanges(ID_PERSON_BRUCE_WILLIS, startDate, endDate); + } + + /** + * Test of getList method, of class TheMovieDbApi. + */ + @Test + public void testGetList() throws Exception { + LOG.info("getList"); + String listId = "509ec17b19c2950a0600050d"; + MovieDbList result = tmdb.getList(listId); + assertFalse("List not found", result.getItems().isEmpty()); + } + + /** + * Test of getKeyword method, of class TheMovieDbApi. + */ + @Test + public void testGetKeyword() throws Exception { + LOG.info("getKeyword"); + Keyword result = tmdb.getKeyword(ID_KEYWORD); + assertEquals("fight", result.getName()); + } + + /** + * Test of getKeywordMovies method, of class TheMovieDbApi. + */ + @Test + public void testGetKeywordMovies() throws Exception { + LOG.info("getKeywordMovies"); + int page = 0; + TmdbResultsList result = tmdb.getKeywordMovies(ID_KEYWORD, LANGUAGE_DEFAULT, page); + assertFalse("No keyword movies found", result.getResults().isEmpty()); + } + + /** + * Test of getReviews method, of class TheMovieDbApi. + */ + @Test + public void testGetReviews() throws Exception { + LOG.info("getReviews"); + int page = 0; + TmdbResultsList result = tmdb.getReviews(ID_MOVIE_THE_AVENGERS, LANGUAGE_DEFAULT, page); + + assertFalse("No reviews found", result.getResults().isEmpty()); + } + + /** + * Test of compareMovies method, of class TheMovieDbApi. + */ + @Ignore("Not required") + public void testCompareMovies_3args() { + } + + /** + * Test of compareMovies method, of class TheMovieDbApi. + */ + @Ignore("Not required") + public void testCompareMovies_4args() { + } + + /** + * Test of getPersonPopular method, of class TheMovieDbApi. + */ + @Ignore("Not required") + public void testGetPersonPopular_0args() throws Exception { + } + + /** + * Test of getPersonPopular method, of class TheMovieDbApi. + */ + @Test + public void testGetPersonPopular_int() throws Exception { + LOG.info("getPersonPopular"); + int page = 0; + TmdbResultsList result = tmdb.getPersonPopular(page); + assertFalse("No popular people", result.getResults().isEmpty()); + } + + /** + * Test of getGenreMovies method, of class TheMovieDbApi. + */ + @Ignore("Not required") + public void testGetGenreMovies_3args() throws Exception { + } + + /** + * Test of getGenreMovies method, of class TheMovieDbApi. + */ + @Ignore("Not required") + public void testGetGenreMovies_4args() throws Exception { + } + + /** + * Test of getMovieChangesList method, of class TheMovieDbApi. + */ + @Ignore("Not ready yet") + public void testGetMovieChangesList() throws Exception { + LOG.info("getMovieChangesList"); + int page = 0; + String startDate = ""; + String endDate = ""; + tmdb.getMovieChangesList(page, startDate, endDate); + // TODO review the generated test code and remove the default call to fail. + fail("The test case is a prototype."); + } + + /** + * Test of getPersonChangesList method, of class TheMovieDbApi. + */ + @Ignore("Not ready yet") + public void testGetPersonChangesList() throws Exception { + LOG.info("getPersonChangesList"); + int page = 0; + String startDate = ""; + String endDate = ""; + tmdb.getPersonChangesList(page, startDate, endDate); + // TODO review the generated test code and remove the default call to fail. + fail("The test case is a prototype."); + } + + /** + * Test of getJobs method, of class TheMovieDbApi. + */ + @Test + public void testGetJobs() throws Exception { + LOG.info("getJobs"); + TmdbResultsList result = tmdb.getJobs(); + assertFalse("No jobs found", result.getResults().isEmpty()); + } + + /** + * Test of getDiscover method, of class TheMovieDbApi. + */ + @Ignore("Not required") + public void testGetDiscover_14args() throws Exception { + } + + /** + * Test of getDiscover method, of class TheMovieDbApi. + */ + @Test + public void testGetDiscover_Discover() throws Exception { + LOG.info("getDiscover"); + Discover discover = new Discover(); + discover.year(2013).language(LANGUAGE_ENGLISH); + + TmdbResultsList result = tmdb.getDiscover(discover); + assertFalse("No movies discovered", result.getResults().isEmpty()); + } +}