diff --git a/themoviedbapi/src/main/java/com/moviejukebox/themoviedb/MovieDbException.java b/themoviedbapi/src/main/java/com/moviejukebox/themoviedb/MovieDbException.java index f57b7c169..e48762399 100644 --- a/themoviedbapi/src/main/java/com/moviejukebox/themoviedb/MovieDbException.java +++ b/themoviedbapi/src/main/java/com/moviejukebox/themoviedb/MovieDbException.java @@ -5,9 +5,9 @@ public class MovieDbException extends Exception { private static final long serialVersionUID = -8952129102483143278L; public enum MovieDbExceptionType { - UNKNOWN_CAUSE, INVALID_URL, HTTP_404_ERROR, MOVIE_ID_NOT_FOUND, MAPPING_FAILED, CONNECTION_ERROR, INVALID_IMAGE; + UNKNOWN_CAUSE, INVALID_URL, HTTP_404_ERROR, MOVIE_ID_NOT_FOUND, MAPPING_FAILED, CONNECTION_ERROR, INVALID_IMAGE, AUTHORISATION_FAILURE; } - + private final MovieDbExceptionType exceptionType; private final String response; @@ -22,7 +22,7 @@ public class MovieDbException extends Exception { this.exceptionType = exceptionType; this.response = response; } - + public MovieDbExceptionType getExceptionType() { return exceptionType; } diff --git a/themoviedbapi/src/main/java/com/moviejukebox/themoviedb/TheMovieDb.java b/themoviedbapi/src/main/java/com/moviejukebox/themoviedb/TheMovieDb.java index fe6a21e86..2cab9faa7 100644 --- a/themoviedbapi/src/main/java/com/moviejukebox/themoviedb/TheMovieDb.java +++ b/themoviedbapi/src/main/java/com/moviejukebox/themoviedb/TheMovieDb.java @@ -30,8 +30,7 @@ import org.codehaus.jackson.map.ObjectMapper; /** * The MovieDb API * - * This is for version 3 of the API as specified here: - * http://help.themoviedb.org/kb/api/about-3 + * This is for version 3 of the API as specified here: http://help.themoviedb.org/kb/api/about-3 * * @author stuart.boston */ @@ -55,11 +54,12 @@ public class TheMovieDb { 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/"; // Configuration private final ApiUrl tmdbConfigUrl = new ApiUrl(this, "configuration"); // Authentication - private final ApiUrl tmdbAuthToken = new ApiUrl(this, BASE_AUTH, "token/new"); - private final ApiUrl tmdbAuthSession = new ApiUrl(this, BASE_AUTH, "session/new"); + private final ApiUrl tmdbAuthorisationToken = new ApiUrl(this, BASE_AUTH, "token/new"); + private final ApiUrl tmdbAuthorisationSession = new ApiUrl(this, BASE_AUTH, "session/new"); // Account private final ApiUrl tmdbAccount = new ApiUrl(this, BASE_ACCOUNT); private final ApiUrl tmdbFavouriteMovies = new ApiUrl(this, BASE_ACCOUNT, "/favorite_movies"); @@ -78,10 +78,10 @@ public class TheMovieDb { private final ApiUrl tmdbMovieTranslations = new ApiUrl(this, BASE_MOVIE, "/translations"); private final ApiUrl tmdbMovieSimilarMovies = new ApiUrl(this, BASE_MOVIE, "/similar_movies"); private final ApiUrl tmdbLatestMovie = new ApiUrl(this, BASE_MOVIE, "/latest"); - private final ApiUrl tmdbUpcoming = new ApiUrl(this, BASE_MOVIE, "/upcoming"); - private final ApiUrl tmdbNowPlaying = new ApiUrl(this, BASE_MOVIE, "/now-playing"); - private final ApiUrl tmdbPopularMovieList = new ApiUrl(this, BASE_MOVIE, "/popular"); - private final ApiUrl tmdbTopRatedMovies = new ApiUrl(this, BASE_MOVIE, "/top-rated"); + private final ApiUrl tmdbUpcoming = new ApiUrl(this, BASE_MOVIE, "upcoming"); + private final ApiUrl tmdbNowPlaying = new ApiUrl(this, BASE_MOVIE, "now-playing"); + private final ApiUrl tmdbPopularMovieList = new ApiUrl(this, BASE_MOVIE, "popular"); + private final ApiUrl tmdbTopRatedMovies = new ApiUrl(this, BASE_MOVIE, "top-rated"); private final ApiUrl tmdbPostRating = new ApiUrl(this, BASE_MOVIE, "/rating"); // Collections private final ApiUrl tmdbCollectionInfo = new ApiUrl(this, BASE_COLLECTION); @@ -97,9 +97,9 @@ public class TheMovieDb { private final ApiUrl tmdbGenreList = new ApiUrl(this, BASE_GENRE, "/list"); private final ApiUrl tmdbGenreMovies = new ApiUrl(this, BASE_GENRE, "/movies"); // Search - private final ApiUrl tmdbSearchMovie = new ApiUrl(this, "search/movie"); - private final ApiUrl tmdbSearchPeople = new ApiUrl(this, "search/person"); - private final ApiUrl tmdbSearchCompanies = new ApiUrl(this, "search/company"); + private final ApiUrl tmdbSearchMovie = new ApiUrl(this, BASE_SEARCH, "movie"); + private final ApiUrl tmdbSearchPeople = new ApiUrl(this, BASE_SEARCH, "person"); + private final ApiUrl tmdbSearchCompanies = new ApiUrl(this, BASE_SEARCH, "company"); /* * Jackson JSON configuration @@ -114,9 +114,9 @@ public class TheMovieDb { */ public TheMovieDb(String apiKey) throws MovieDbException { this.apiKey = apiKey; - URL configUrl = tmdbConfigUrl.getQueryUrl(""); + URL configUrl = tmdbConfigUrl.buildUrl(); String webpage = WebBrowser.request(configUrl); - FilteringLayout.addApiKey(apiKey); + FilteringLayout.addReplacementString(apiKey); try { WrapperConfig wc = mapper.readValue(webpage, WrapperConfig.class); @@ -219,36 +219,100 @@ public class TheMovieDb { return false; } + // /** - * Search Movies This is a good starting point to start finding movies on - * TMDb. + * Get the configuration information + * + * @return + */ + public TmdbConfiguration getConfiguration() { + return tmdbConfig; + } + + /** + * Generate the full image URL from the size and image path + * + * @param imagePath + * @param requiredSize + * @return + * @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) { + LOGGER.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. * - * The idea is to be a quick and light method so you can iterate through - * movies quickly. + * A request token is required in order to request a session id. * - * http://help.themoviedb.org/kb/api/search-movies + * You can generate any number of request tokens but they will expire after 60 minutes. * - * TODO: Make the allResults work + * As soon as a valid session id has been created the token will be destroyed. * - * @param movieName - * @param language - * @param allResults * @return * @throws MovieDbException */ - public List searchMovie(String movieName, String language, boolean allResults) throws MovieDbException { + public TokenAuthorisation getAuthorisationToken() throws MovieDbException { + URL url = tmdbAuthorisationToken.buildUrl(); + String webpage = WebBrowser.request(url); + + try { + return mapper.readValue(webpage, TokenAuthorisation.class); + } catch (IOException ex) { + LOGGER.warn("Failed to get Authorisation Token: " + ex.getMessage()); + throw new MovieDbException(MovieDbExceptionType.AUTHORISATION_FAILURE, webpage, ex); + } + } - URL url = tmdbSearchMovie.getQueryUrl(movieName, language, 1); + /** + * 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 + * @return + * @throws MovieDbException + */ + public TokenSession getSessionToken(TokenAuthorisation token) throws MovieDbException { + if (!token.getSuccess()) { + LOGGER.warn("Authorisation token was not successful!"); + throw new MovieDbException(MovieDbExceptionType.AUTHORISATION_FAILURE, "Authorisation token was not successful!"); + } + + tmdbAuthorisationSession.addArgument(ApiUrl.PARAM_TOKEN, token.getRequestToken()); + URL url = tmdbAuthorisationSession.buildUrl(); String webpage = WebBrowser.request(url); + try { - WrapperMovie wrapper = mapper.readValue(webpage, WrapperMovie.class); - return wrapper.getMovies(); + return mapper.readValue(webpage, TokenSession.class); } catch (IOException ex) { - LOGGER.warn("Failed to find movie: " + ex.getMessage()); + LOGGER.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. * @@ -261,7 +325,13 @@ public class TheMovieDb { */ public MovieDb getMovieInfo(int movieId, String language) throws MovieDbException { - URL url = tmdbMovieInfo.getIdUrl(movieId, language); + tmdbMovieInfo.addArgument(ApiUrl.PARAM_ID, movieId); + + if (StringUtils.isNotBlank(language)) { + tmdbMovieInfo.addArgument(ApiUrl.PARAM_LANGUAGE, language); + } + + URL url = tmdbMovieInfo.buildUrl(); String webpage = WebBrowser.request(url); try { return mapper.readValue(webpage, MovieDb.class); @@ -283,7 +353,13 @@ public class TheMovieDb { */ public MovieDb getMovieInfoImdb(String imdbId, String language) throws MovieDbException { - URL url = tmdbMovieInfo.getIdUrl(imdbId, language); + tmdbMovieInfo.addArgument(ApiUrl.PARAM_ID, imdbId); + + if (StringUtils.isNotBlank(language)) { + tmdbMovieInfo.addArgument(ApiUrl.PARAM_LANGUAGE, language); + } + + URL url = tmdbMovieInfo.buildUrl(); String webpage = WebBrowser.request(url); try { return mapper.readValue(webpage, MovieDb.class); @@ -294,8 +370,7 @@ public class TheMovieDb { } /** - * This method is used to retrieve all of the alternative titles we have for - * a particular movie. + * This method is used to retrieve all of the alternative titles we have for a particular movie. * * @param movieId * @param country @@ -303,8 +378,13 @@ public class TheMovieDb { * @throws MovieDbException */ public List getMovieAlternativeTitles(int movieId, String country) throws MovieDbException { + tmdbMovieAltTitles.addArgument(ApiUrl.PARAM_ID, movieId); - URL url = tmdbMovieAltTitles.getIdUrl(movieId, ApiUrl.DEFAULT_STRING, country); + if (StringUtils.isNotBlank(country)) { + tmdbMovieAltTitles.addArgument(ApiUrl.PARAM_COUNTRY, country); + } + + URL url = tmdbMovieAltTitles.buildUrl(); String webpage = WebBrowser.request(url); try { WrapperAlternativeTitles wrapper = mapper.readValue(webpage, WrapperAlternativeTitles.class); @@ -316,7 +396,7 @@ public class TheMovieDb { } /** - * This method is used to retrieve all of the movie cast information. + * Get the cast information for a specific movie id. * * TODO: Add a function to enrich the data with the people methods * @@ -325,11 +405,12 @@ public class TheMovieDb { * @throws MovieDbException */ public List getMovieCasts(int movieId) throws MovieDbException { - List people = new ArrayList(); - URL url = tmdbMovieCasts.getIdUrl(movieId); + tmdbMovieCasts.addArgument(ApiUrl.PARAM_ID, movieId); + URL url = tmdbMovieCasts.buildUrl(); String webpage = WebBrowser.request(url); + try { WrapperMovieCasts wrapper = mapper.readValue(webpage, WrapperMovieCasts.class); @@ -355,8 +436,7 @@ public class TheMovieDb { } /** - * This method should be used when you’re wanting to retrieve all of the - * images for a particular movie. + * This method should be used when you’re wanting to retrieve all of the images for a particular movie. * * @param movieId * @param language @@ -365,8 +445,14 @@ public class TheMovieDb { */ public List getMovieImages(int movieId, String language) throws MovieDbException { + tmdbMovieImages.addArgument(ApiUrl.PARAM_ID, movieId); + + if (StringUtils.isNotBlank(language)) { + tmdbMovieImages.addArgument(ApiUrl.PARAM_LANGUAGE, language); + } + List artwork = new ArrayList(); - URL url = tmdbMovieImages.getIdUrl(movieId, language); + URL url = tmdbMovieImages.buildUrl(); String webpage = WebBrowser.request(url); try { WrapperImages wrapper = mapper.readValue(webpage, WrapperImages.class); @@ -391,8 +477,7 @@ public class TheMovieDb { } /** - * This method is used to retrieve all of the keywords that have been added - * to a particular movie. + * This method is used to retrieve all of the keywords that have been added to a particular movie. * * Currently, only English keywords exist. * @@ -402,7 +487,9 @@ public class TheMovieDb { */ public List getMovieKeywords(int movieId) throws MovieDbException { - URL url = tmdbMovieKeywords.getIdUrl(movieId); + tmdbMovieKeywords.addArgument(ApiUrl.PARAM_ID, movieId); + + URL url = tmdbMovieKeywords.buildUrl(); String webpage = WebBrowser.request(url); try { @@ -415,8 +502,7 @@ public class TheMovieDb { } /** - * This method is used to retrieve all of the release and certification data - * we have for a specific movie. + * This method is used to retrieve all of the release and certification data we have for a specific movie. * * @param movieId * @param language @@ -425,7 +511,10 @@ public class TheMovieDb { */ public List getMovieReleaseInfo(int movieId, String language) throws MovieDbException { - URL url = tmdbMovieReleaseInfo.getIdUrl(movieId); + tmdbMovieReleaseInfo.addArgument(ApiUrl.PARAM_ID, movieId); + tmdbMovieReleaseInfo.addArgument(ApiUrl.PARAM_LANGUAGE, language); + + URL url = tmdbMovieReleaseInfo.buildUrl(); String webpage = WebBrowser.request(url); try { @@ -438,8 +527,7 @@ public class TheMovieDb { } /** - * This method is used to retrieve all of the trailers for a particular - * movie. + * This method is used to retrieve all of the trailers for a particular movie. * * Supported sites are YouTube and QuickTime. * @@ -451,7 +539,14 @@ public class TheMovieDb { public List getMovieTrailers(int movieId, String language) throws MovieDbException { List trailers = new ArrayList(); - URL url = tmdbMovieTrailers.getIdUrl(movieId, language); + + tmdbMovieTrailers.addArgument(ApiUrl.PARAM_ID, movieId); + + if (StringUtils.isNotBlank(language)) { + tmdbMovieTrailers.addArgument(ApiUrl.PARAM_LANGUAGE, language); + } + + URL url = tmdbMovieTrailers.buildUrl(); String webpage = WebBrowser.request(url); try { @@ -475,8 +570,7 @@ public class TheMovieDb { } /** - * This method is used to retrieve a list of the available translations for - * a specific movie. + * This method is used to retrieve a list of the available translations for a specific movie. * * @param movieId * @return @@ -484,7 +578,8 @@ public class TheMovieDb { */ public List getMovieTranslations(int movieId) throws MovieDbException { - URL url = tmdbMovieTranslations.getIdUrl(movieId); + tmdbMovieTranslations.addArgument(ApiUrl.PARAM_ID, movieId); + URL url = tmdbMovieTranslations.buildUrl(); String webpage = WebBrowser.request(url); try { @@ -497,90 +592,287 @@ public class TheMovieDb { } /** - * This method is used to retrieve all of the basic information about a - * movie collection. + * The similar movies method will let you retrieve the similar movies for a particular movie. * - * You can get the ID needed for this method by making a getMovieInfo - * request for the belongs_to_collection. + * 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 allResults * @return * @throws MovieDbException */ - public CollectionInfo getCollectionInfo(int movieId, String language) throws MovieDbException { + public List getSimilarMovies(int movieId, String language, int page) throws MovieDbException { + tmdbMovieSimilarMovies.addArgument(ApiUrl.PARAM_ID, movieId); - URL url = tmdbCollectionInfo.getIdUrl(movieId); + if (StringUtils.isNotBlank(language)) { + tmdbMovieSimilarMovies.addArgument(ApiUrl.PARAM_LANGUAGE, language); + } + + if (page > 0) { + tmdbMovieSimilarMovies.addArgument(ApiUrl.PARAM_PAGE, page); + } + + URL url = tmdbMovieSimilarMovies.buildUrl(); String webpage = WebBrowser.request(url); try { - return mapper.readValue(webpage, CollectionInfo.class); + WrapperMovie wrapper = mapper.readValue(webpage, WrapperMovie.class); + return wrapper.getMovies(); } catch (IOException ex) { - LOGGER.warn("Failed to get movie tranlations: " + ex.getMessage()); + LOGGER.warn("Failed to get similar movies: " + ex.getMessage()); throw new MovieDbException(MovieDbExceptionType.MAPPING_FAILED, webpage, ex); } } /** - * Get the configuration information + * This method is used to retrieve the newest movie that was added to TMDb. * * @return */ - public TmdbConfiguration getConfiguration() { - return tmdbConfig; + public MovieDb getLatestMovie() throws MovieDbException { + + URL url = tmdbLatestMovie.buildUrl(); + String webpage = WebBrowser.request(url); + + try { + return mapper.readValue(webpage, MovieDb.class); + } catch (IOException ex) { + LOGGER.warn("Failed to get latest movie: " + ex.getMessage()); + throw new MovieDbException(MovieDbExceptionType.MAPPING_FAILED, webpage, ex); + } } /** - * Generate the full image URL from the size and image path + * Get the list of upcoming movies. + * + * This list refreshes every day. + * + * The maximum number of items this list will include is 100. * - * @param imagePath - * @param requiredSize * @return * @throws MovieDbException */ - public URL createImageUrl(String imagePath, String requiredSize) throws MovieDbException { - if (!tmdbConfig.isValidSize(requiredSize)) { - throw new MovieDbException(MovieDbExceptionType.INVALID_IMAGE, requiredSize); + public List getUpcoming(String language, int page) throws MovieDbException { + if (StringUtils.isNotBlank(language)) { + tmdbUpcoming.addArgument(ApiUrl.PARAM_LANGUAGE, language); } - StringBuilder sb = new StringBuilder(tmdbConfig.getBaseUrl()); - sb.append(requiredSize); - sb.append(imagePath); + if (page > 0) { + tmdbUpcoming.addArgument(ApiUrl.PARAM_PAGE, page); + } + + URL url = tmdbUpcoming.buildUrl(); + String webpage = WebBrowser.request(url); + try { - return (new URL(sb.toString())); - } catch (MalformedURLException ex) { - LOGGER.warn("Failed to create image URL: " + ex.getMessage()); - throw new MovieDbException(MovieDbExceptionType.INVALID_URL, sb.toString(), ex); + WrapperMovie wrapper = mapper.readValue(webpage, WrapperMovie.class); + return wrapper.getMovies(); + } catch (IOException ex) { + LOGGER.warn("Failed to get upcoming movies: " + ex.getMessage()); + throw new MovieDbException(MovieDbExceptionType.MAPPING_FAILED, webpage, ex); } + } /** - * This is a good starting point to start finding people on TMDb. + * This method is used to retrieve the movies currently in theatres. * - * The idea is to be a quick and light method so you can iterate through - * people quickly. + * This is a curated list that will normally contain 100 movies. The default response will return 20 movies. * - * TODO: Fix allResults + * TODO: Implement more than 20 movies * - * @param personName + * @param language * @param allResults * @return * @throws MovieDbException */ - public List searchPeople(String personName, boolean allResults) throws MovieDbException { + public List getNowPlayingMovies(String language, int page) throws MovieDbException { + + if (StringUtils.isNotBlank(language)) { + tmdbNowPlaying.addArgument(ApiUrl.PARAM_LANGUAGE, language); + } + + if (page > 0) { + tmdbNowPlaying.addArgument(ApiUrl.PARAM_PAGE, page); + } - URL url = tmdbSearchPeople.getQueryUrl(personName, "", 1); + URL url = tmdbNowPlaying.buildUrl(); String webpage = WebBrowser.request(url); try { - WrapperPerson wrapper = mapper.readValue(webpage, WrapperPerson.class); - return wrapper.getResults(); + WrapperMovie wrapper = mapper.readValue(webpage, WrapperMovie.class); + return wrapper.getMovies(); } catch (IOException ex) { - LOGGER.warn("Failed to find person: " + ex.getMessage()); + LOGGER.warn("Failed to get now playing movies: " + ex.getMessage()); + throw new MovieDbException(MovieDbExceptionType.MAPPING_FAILED, webpage, ex); + } + } + + /** + * This method is used to retrieve the 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 allResults + * @return + * @throws MovieDbException + */ + public List getPopularMovieList(String language, int page) throws MovieDbException { + if (StringUtils.isNotBlank(language)) { + tmdbPopularMovieList.addArgument(ApiUrl.PARAM_LANGUAGE, language); + } + + if (page > 0) { + tmdbPopularMovieList.addArgument(ApiUrl.PARAM_PAGE, page); + } + + URL url = tmdbPopularMovieList.buildUrl(); + String webpage = WebBrowser.request(url); + + try { + WrapperMovie wrapper = mapper.readValue(webpage, WrapperMovie.class); + return wrapper.getMovies(); + } catch (IOException ex) { + LOGGER.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 allResults + * @return + * @throws MovieDbException + */ + public List getTopRatedMovies(String language, int page) throws MovieDbException { + if (StringUtils.isNotBlank(language)) { + tmdbTopRatedMovies.addArgument(ApiUrl.PARAM_LANGUAGE, language); + } + + if (page > 0) { + tmdbTopRatedMovies.addArgument(ApiUrl.PARAM_PAGE, page); + } + + URL url = tmdbTopRatedMovies.buildUrl(); + String webpage = WebBrowser.request(url); + + try { + WrapperMovie wrapper = mapper.readValue(webpage, WrapperMovie.class); + return wrapper.getMovies(); + } catch (IOException ex) { + LOGGER.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 + * @return + * @throws MovieDbException + */ + public boolean postMovieRating(String sessionId, String rating) throws MovieDbException { + + tmdbPostRating.addArgument(ApiUrl.PARAM_SESSION, sessionId); + tmdbPostRating.addArgument(ApiUrl.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 + * @return + * @throws MovieDbException + */ + public CollectionInfo getCollectionInfo(int collectionId, String language) throws MovieDbException { + + tmdbCollectionInfo.addArgument(ApiUrl.PARAM_ID, collectionId); + + if (StringUtils.isNotBlank(language)) { + tmdbCollectionInfo.addArgument(ApiUrl.PARAM_LANGUAGE, language); + } + + URL url = tmdbCollectionInfo.buildUrl(); + String webpage = WebBrowser.request(url); + + try { + return mapper.readValue(webpage, CollectionInfo.class); + } catch (IOException ex) { + LOGGER.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 + * @return + * @throws MovieDbException + */ + public List getCollectionImages(int collectionId, String language) throws MovieDbException { + List artwork = new ArrayList(); + + tmdbCollectionImages.addArgument(ApiUrl.PARAM_ID, collectionId); + + if (StringUtils.isNotBlank(language)) { + tmdbCollectionImages.addArgument(ApiUrl.PARAM_LANGUAGE, language); + } + + URL url = tmdbCollectionImages.buildUrl(); + String webpage = WebBrowser.request(url); + + try { + WrapperImages wrapper = mapper.readValue(webpage, WrapperImages.class); + + // Add all the posters to the list + for (Artwork poster : wrapper.getPosters()) { + poster.setArtworkType(ArtworkType.POSTER); + artwork.add(poster); + } + + // Add all the backdrops to the list + for (Artwork backdrop : wrapper.getBackdrops()) { + backdrop.setArtworkType(ArtworkType.BACKDROP); + artwork.add(backdrop); + } + + return artwork; + } catch (IOException ex) { + LOGGER.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. * @@ -592,7 +884,9 @@ public class TheMovieDb { */ public Person getPersonInfo(int personId) throws MovieDbException { - URL url = tmdbPersonInfo.getIdUrl(personId); + tmdbPersonInfo.addArgument(ApiUrl.PARAM_ID, personId); + + URL url = tmdbPersonInfo.buildUrl(); String webpage = WebBrowser.request(url); try { @@ -604,8 +898,7 @@ public class TheMovieDb { } /** - * This method is used to retrieve all of the cast & crew information for - * the person. + * 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. * @@ -617,7 +910,9 @@ public class TheMovieDb { List personCredits = new ArrayList(); - URL url = tmdbPersonCredits.getIdUrl(personId); + tmdbPersonCredits.addArgument(ApiUrl.PARAM_ID, personId); + + URL url = tmdbPersonCredits.buildUrl(); String webpage = WebBrowser.request(url); try { @@ -651,7 +946,9 @@ public class TheMovieDb { List personImages = new ArrayList(); - URL url = tmdbPersonImages.getIdUrl(personId); + tmdbPersonImages.addArgument(ApiUrl.PARAM_ID, personId); + + URL url = tmdbPersonImages.buildUrl(); String webpage = WebBrowser.request(url); try { @@ -669,275 +966,391 @@ public class TheMovieDb { } } + // + // + // /** - * This method is used to retrieve the newest movie that was added to TMDb. + * This method is used to retrieve the basic information about a production company on TMDb. * + * @param companyId * @return + * @throws MovieDbException */ - public MovieDb getLatestMovie() throws MovieDbException { + public Company getCompanyInfo(int companyId) throws MovieDbException { - URL url = tmdbLatestMovie.getIdUrl(""); + tmdbCompanyInfo.addArgument(ApiUrl.PARAM_ID, companyId); + + URL url = tmdbCompanyInfo.buildUrl(); String webpage = WebBrowser.request(url); try { - return mapper.readValue(webpage, MovieDb.class); + return mapper.readValue(webpage, Company.class); } catch (IOException ex) { - LOGGER.warn("Failed to get latest movie: " + ex.getMessage()); + LOGGER.warn("Failed to get company information: " + ex.getMessage()); throw new MovieDbException(MovieDbExceptionType.MAPPING_FAILED, webpage, ex); } } /** - * Get the list of upcoming movies. + * This method is used to retrieve the movies associated with a company. * - * This list refreshes every day. + * These movies are returned in order of most recently released to oldest. The default response will return 20 + * movies per page. * - * The maximum number of items this list will include is 100. + * TODO: Implement more than 20 movies * + * @param companyId + * @param language + * @param allResults * @return * @throws MovieDbException */ - public List getUpcoming(String language) throws MovieDbException { - URL url = tmdbUpcoming.getIdUrl("", language); + public List getCompanyMovies(int companyId, String language, int page) throws MovieDbException { + + tmdbCompanyMovies.addArgument(ApiUrl.PARAM_ID, companyId); + + if (StringUtils.isNotBlank(language)) { + tmdbCompanyMovies.addArgument(ApiUrl.PARAM_LANGUAGE, language); + } + + if (page > 0) { + tmdbCompanyMovies.addArgument(ApiUrl.PARAM_PAGE, page); + } + + URL url = tmdbCompanyMovies.buildUrl(); String webpage = WebBrowser.request(url); try { - WrapperMovie wrapper = mapper.readValue(webpage, WrapperMovie.class); - return wrapper.getMovies(); + WrapperCompanyMovies wrapper = mapper.readValue(webpage, WrapperCompanyMovies.class); + return wrapper.getResults(); } catch (IOException ex) { - LOGGER.warn("Failed to get upcoming movies: " + ex.getMessage()); + LOGGER.warn("Failed to get company 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. + * You can use this method to retrieve the list of genres used on TMDb. * - * TODO: Implement more than 20 movies + * These IDs will correspond to those found in movie calls. * * @param language - * @param allResults * @return - * @throws MovieDbException */ - public List getNowPlayingMovies(String language, boolean allResults) throws MovieDbException { - URL url = tmdbNowPlaying.getIdUrl("", language); + public List getGenreList(String language) throws MovieDbException { + tmdbGenreList.addArgument(ApiUrl.PARAM_LANGUAGE, language); + + URL url = tmdbGenreList.buildUrl(); String webpage = WebBrowser.request(url); try { - WrapperMovie wrapper = mapper.readValue(webpage, WrapperMovie.class); - return wrapper.getMovies(); + WrapperGenres wrapper = mapper.readValue(webpage, WrapperGenres.class); + return wrapper.getGenres(); } catch (IOException ex) { - LOGGER.warn("Failed to get now playing movies: " + ex.getMessage()); + LOGGER.warn("Failed to get genre list: " + ex.getMessage()); throw new MovieDbException(MovieDbExceptionType.MAPPING_FAILED, webpage, ex); } } /** - * This method is used to retrieve the daily movie popularity list. + * Get a list of movies per genre. * - * This list is updated daily. The default response will return 20 movies. + * It is important to understand that only movies with more than 10 votes get listed. * - * TODO: Implement more than 20 movies + * This prevents movies from 1 10/10 rating from being listed first and for the first 5 pages. * + * @param genreId * @param language * @param allResults * @return - * @throws MovieDbException */ - public List getPopularMovieList(String language, boolean allResults) throws MovieDbException { - URL url = tmdbPopularMovieList.getIdUrl("", language); + public List getGenreMovies(int genreId, String language, int page) throws MovieDbException { + + tmdbGenreMovies.addArgument(ApiUrl.PARAM_ID, genreId); + + if (StringUtils.isNotBlank(language)) { + tmdbGenreMovies.addArgument(ApiUrl.PARAM_LANGUAGE, language); + } + + if (page > 0) { + tmdbGenreMovies.addArgument(ApiUrl.PARAM_PAGE, page); + } + + URL url = tmdbGenreMovies.buildUrl(); String webpage = WebBrowser.request(url); try { WrapperMovie wrapper = mapper.readValue(webpage, WrapperMovie.class); return wrapper.getMovies(); } catch (IOException ex) { - LOGGER.warn("Failed to get popular movie list: " + ex.getMessage()); + LOGGER.warn("Failed to get genre 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. + * Search Movies This is a good starting point to start finding movies on TMDb. * - * TODO: Implement more than 20 movies - * - * @param language - * @param allResults + * @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) * @return * @throws MovieDbException */ - public List getTopRatedMovies(String language, boolean allResults) throws MovieDbException { - URL url = tmdbTopRatedMovies.getIdUrl("", language); - String webpage = WebBrowser.request(url); + public List searchMovie(String movieName, int searchYear, String language, boolean includeAdult, int page) throws MovieDbException { + if (StringUtils.isNotBlank(movieName)) { + tmdbSearchMovie.addArgument(ApiUrl.PARAM_QUERY, movieName); + } + + if (searchYear > 0) { + tmdbSearchMovie.addArgument(ApiUrl.PARAM_YEAR, Integer.toString(searchYear)); + } + + if (StringUtils.isNotBlank(language)) { + tmdbSearchMovie.addArgument(ApiUrl.PARAM_LANGUAGE, language); + } + + tmdbSearchMovie.addArgument(ApiUrl.PARAM_ADULT, Boolean.toString(includeAdult)); + + if (page > 0) { + tmdbSearchMovie.addArgument(ApiUrl.PARAM_PAGE, Integer.toString(page)); + } + URL url = tmdbSearchMovie.buildUrl(); + LOGGER.info(url.toString()); + + String webpage = WebBrowser.request(url); try { WrapperMovie wrapper = mapper.readValue(webpage, WrapperMovie.class); return wrapper.getMovies(); } catch (IOException ex) { - LOGGER.warn("Failed to get top rated movies: " + ex.getMessage()); + LOGGER.warn("Failed to find movie: " + 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. + * Search Companies. * - * @param companyId + * 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 * @return * @throws MovieDbException */ - public Company getCompanyInfo(int companyId) throws MovieDbException { - URL url = tmdbCompanyInfo.getIdUrl(companyId); - String webpage = WebBrowser.request(url); + public List searchCompanies(String companyName, int page) throws MovieDbException { + tmdbSearchCompanies.addArgument(ApiUrl.PARAM_QUERY, companyName); + + if (page > 0) { + tmdbSearchCompanies.addArgument(ApiUrl.PARAM_PAGE, page); + } + URL url = tmdbSearchCompanies.buildUrl(); + String webpage = WebBrowser.request(url); try { - return mapper.readValue(webpage, Company.class); + WrapperCompany wrapper = mapper.readValue(webpage, WrapperCompany.class); + return wrapper.getResults(); } catch (IOException ex) { - LOGGER.warn("Failed to get company information: " + ex.getMessage()); + LOGGER.warn("Failed to find company: " + ex.getMessage()); throw new MovieDbException(MovieDbExceptionType.MAPPING_FAILED, webpage, ex); } } /** - * This method is used to retrieve the movies associated with a company. + * This is a good starting point to start finding people on TMDb. * - * These movies are returned in order of most recently released to oldest. - * The default response will return 20 movies per page. + * The idea is to be a quick and light method so you can iterate through people quickly. * - * TODO: Implement more than 20 movies + * TODO: Fix allResults * - * @param companyId - * @param language + * @param personName * @param allResults * @return * @throws MovieDbException */ - public List getCompanyMovies(int companyId, String language, boolean allResults) throws MovieDbException { - URL url = tmdbCompanyMovies.getIdUrl(companyId, language); + public List searchPeople(String personName, boolean includeAdult, int page) throws MovieDbException { + tmdbSearchPeople.addArgument(ApiUrl.PARAM_QUERY, personName); + tmdbSearchPeople.addArgument(ApiUrl.PARAM_ADULT, includeAdult); + + if (page > 0) { + tmdbSearchPeople.addArgument(ApiUrl.PARAM_PAGE, page); + } + + URL url = tmdbSearchPeople.buildUrl(); String webpage = WebBrowser.request(url); try { - WrapperCompanyMovies wrapper = mapper.readValue(webpage, WrapperCompanyMovies.class); + WrapperPerson wrapper = mapper.readValue(webpage, WrapperPerson.class); return wrapper.getResults(); } catch (IOException ex) { - LOGGER.warn("Failed to get company movies: " + ex.getMessage()); + LOGGER.warn("Failed to find person: " + 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. + // + // + /* + * Deprecated Functions. * - * http://help.themoviedb.org/kb/api/search-companies + * Will be removed in next version: 3.3 + */ + // + /** + * This interface will be deprecated in the next version * - * TODO: Make the allResults work + * @param movieName + * @param language + * @param allResults + * @return + * @throws MovieDbException + * @deprecated + */ + @Deprecated + public List searchMovie(String movieName, String language, boolean allResults) throws MovieDbException { + return searchMovie(movieName, 0, language, allResults, 0); + } + + /** + * This interface will be deprecated in the next version * * @param companyName * @param language * @param allResults * @return * @throws MovieDbException + * @deprecated */ + @Deprecated public List searchCompanies(String companyName, String language, boolean allResults) throws MovieDbException { - - URL url = tmdbSearchCompanies.getQueryUrl(companyName, language, 1); - String webpage = WebBrowser.request(url); - try { - WrapperCompany wrapper = mapper.readValue(webpage, WrapperCompany.class); - return wrapper.getResults(); - } catch (IOException ex) { - LOGGER.warn("Failed to find company: " + ex.getMessage()); - throw new MovieDbException(MovieDbExceptionType.MAPPING_FAILED, webpage, ex); - } + return searchCompanies(companyName, 0); } /** - * 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. + * This interface will be deprecated in the next version * - * The data is much better with movies that have more keywords + * @param personName + * @param allResults + * @return + * @throws MovieDbException + * @deprecated + */ + @Deprecated + public List searchPeople(String personName, boolean allResults) throws MovieDbException { + return searchPeople(personName, allResults, 0); + } + + /** + * This interface will be deprecated in the next version * * @param movieId * @param language * @param allResults * @return * @throws MovieDbException + * @deprecated */ + @Deprecated public List getSimilarMovies(int movieId, String language, boolean allResults) throws MovieDbException { - - URL url = tmdbMovieSimilarMovies.getIdUrl(movieId, language); - String webpage = WebBrowser.request(url); - - try { - WrapperMovie wrapper = mapper.readValue(webpage, WrapperMovie.class); - return wrapper.getMovies(); - } catch (IOException ex) { - LOGGER.warn("Failed to get similar movies: " + ex.getMessage()); - throw new MovieDbException(MovieDbExceptionType.MAPPING_FAILED, webpage, ex); - } + return getSimilarMovies(movieId, language, 0); } /** - * You can use this method to retrieve the list of genres used on TMDb. + * This interface will be deprecated in the next version * - * These IDs will correspond to those found in movie calls. + * @param language + * @return + * @throws MovieDbException + * @deprecated + */ + @Deprecated + public List getUpcoming(String language) throws MovieDbException { + return getUpcoming(language, 0); + } + + /** + * This interface will be deprecated in the next version * * @param language + * @param allResults * @return + * @throws MovieDbException + * @deprecated */ - public List getGenreList(String language) throws MovieDbException { - URL url = tmdbGenreList.getQueryUrl("", language); - String webpage = WebBrowser.request(url); + @Deprecated + public List getNowPlayingMovies(String language, boolean allResults) throws MovieDbException { + return getNowPlayingMovies(language, 0); + } - try { - WrapperGenres wrapper = mapper.readValue(webpage, WrapperGenres.class); - return wrapper.getGenres(); - } catch (IOException ex) { - LOGGER.warn("Failed to get genre list: " + ex.getMessage()); - throw new MovieDbException(MovieDbExceptionType.MAPPING_FAILED, webpage, ex); - } + /** + * This interface will be deprecated in the next version + * + * @param language + * @param allResults + * @return + * @throws MovieDbException + * @deprecated + */ + @Deprecated + public List getPopularMovieList(String language, boolean allResults) throws MovieDbException { + return getPopularMovieList(language, 0); } /** - * Get a list of movies per genre. + * This interface will be deprecated in the next version * - * It is important to understand that only movies with more than 10 votes - * get listed. + * @param language + * @param allResults + * @return + * @throws MovieDbException + * @deprecated + */ + @Deprecated + public List getTopRatedMovies(String language, boolean allResults) throws MovieDbException { + return getTopRatedMovies(language, 0); + } + + /** + * This interface will be deprecated in the next version * - * This prevents movies from 1 10/10 rating from being listed first and for - * the first 5 pages. + * @param companyId + * @param language + * @param allResults + * @return + * @throws MovieDbException + * @deprecated + */ + @Deprecated + public List getCompanyMovies(int companyId, String language, boolean allResults) throws MovieDbException { + return getCompanyMovies(companyId, language, 0); + } + + /** + * This interface will be deprecated in the next version * * @param genreId * @param language * @param allResults * @return + * @throws MovieDbException + * @deprecated */ + @Deprecated public List getGenreMovies(int genreId, String language, boolean allResults) throws MovieDbException { - URL url = tmdbGenreMovies.getIdUrl(genreId, language); - String webpage = WebBrowser.request(url); - - try { - WrapperMovie wrapper = mapper.readValue(webpage, WrapperMovie.class); - return wrapper.getMovies(); - } catch (IOException ex) { - LOGGER.warn("Failed to get genre movie list: " + ex.getMessage()); - throw new MovieDbException(MovieDbExceptionType.MAPPING_FAILED, webpage, ex); - } + return getGenreMovies(genreId, language, 0); } + // } diff --git a/themoviedbapi/src/main/java/com/moviejukebox/themoviedb/tools/ApiUrl.java b/themoviedbapi/src/main/java/com/moviejukebox/themoviedb/tools/ApiUrl.java index a7f920cc5..d940b5885 100644 --- a/themoviedbapi/src/main/java/com/moviejukebox/themoviedb/tools/ApiUrl.java +++ b/themoviedbapi/src/main/java/com/moviejukebox/themoviedb/tools/ApiUrl.java @@ -17,7 +17,8 @@ import java.io.UnsupportedEncodingException; import java.net.MalformedURLException; import java.net.URL; import java.net.URLEncoder; -import org.apache.commons.lang3.StringUtils; +import java.util.HashMap; +import java.util.Map; import org.apache.log4j.Logger; /** @@ -35,28 +36,42 @@ public class ApiUrl { * TheMovieDb API Base URL */ private static final String TMDB_API_BASE = "http://api.themoviedb.org/3/"; +// private static final String TMDB_API_BASE = "http://private-3aa3-themoviedb.apiary.io/3/"; /* * Parameter configuration */ private static final String DELIMITER_FIRST = "?"; private static final String DELIMITER_SUBSEQUENT = "&"; - private static final String PARAMETER_API_KEY = "api_key="; // The API Key is always needed and always first - private static final String PARAMETER_QUERY = "query="; - private static final String PARAMETER_LANGUAGE = DELIMITER_SUBSEQUENT + "language="; - private static final String PARAMETER_COUNTRY = DELIMITER_SUBSEQUENT + "country="; - private static final String PARAMETER_PAGE = DELIMITER_SUBSEQUENT + "page="; - public static final String DEFAULT_STRING = ""; - public static final int DEFAULT_INT = -1; + private static final String DEFAULT_STRING = ""; /* * Properties */ + private TheMovieDb tmdb; private String method; private String submethod; - private TheMovieDb tmdb; + private Map arguments = new HashMap(); + /* + * API Parameters + */ + public static final String PARAM_ADULT = "include_adult="; + public static final String PARAM_API_KEY = "api_key="; + public static final String PARAM_COUNTRY = "country="; + public static final String PARAM_FAVORITE = "favorite="; + public static final String PARAM_ID = "id="; + public static final String PARAM_LANGUAGE = "language="; +// public static final String PARAM_MOVIE_ID = "movie_id="; + public static final String PARAM_MOVIE_WATCHLIST = "movie_watchlist="; + public static final String PARAM_PAGE = "page="; + public static final String PARAM_QUERY = "query="; + public static final String PARAM_SESSION = "session_id="; + public static final String PARAM_TOKEN = "request_token="; + public static final String PARAM_VALUE = "value="; + public static final String PARAM_YEAR = "year="; // /** * Constructor for the simple API URL method without a sub-method + * * @param method */ public ApiUrl(TheMovieDb tmdb, String method) { @@ -67,6 +82,7 @@ public class ApiUrl { /** * Constructor for the API URL with a sub-method + * * @param method * @param submethod */ @@ -78,69 +94,58 @@ public class ApiUrl { // /** - * Create the full URL with the API. + * Build the URL from the pre-created arguments. * - * @param query - * @param tmdbId - * @param language - * @param country - * @param page * @return */ - private URL getFullUrl(String query, String movieId, String language, String country, int page) { + public URL buildUrl() { StringBuilder urlString = new StringBuilder(TMDB_API_BASE); // Get the start of the URL urlString.append(method); - // Append the search term if required - if (StringUtils.isNotBlank(query)) { - urlString.append(DELIMITER_FIRST); - urlString.append(PARAMETER_QUERY); + // We have either a queury, or a direct request + if (arguments.containsKey(PARAM_QUERY)) { + // Append the suffix of the API URL + urlString.append(submethod); + + // Append the key information + urlString.append(DELIMITER_FIRST).append(PARAM_API_KEY); + urlString.append(tmdb.getApiKey()); + + // Append the search term + urlString.append(DELIMITER_SUBSEQUENT); + urlString.append(PARAM_QUERY); + + String query = arguments.get(PARAM_QUERY); try { urlString.append(URLEncoder.encode(query, "UTF-8")); } catch (UnsupportedEncodingException ex) { + LOGGER.trace("Unable to encode query: '" + query + "' trying raw."); // If we can't encode it, try it raw urlString.append(query); } - } - - // Append the ID if provided - if (StringUtils.isNotBlank(movieId)) { - urlString.append(movieId); - } - - // Append the suffix of the API URL - urlString.append(submethod); - // Append the key information - if (StringUtils.isBlank(query)) { - // This is the first parameter - urlString.append(DELIMITER_FIRST); + arguments.remove(PARAM_QUERY); } else { - // The first parameter was the query - urlString.append(DELIMITER_SUBSEQUENT); - } - urlString.append(PARAMETER_API_KEY); - urlString.append(tmdb.getApiKey()); + // Append the ID if provided + if (arguments.containsKey(PARAM_ID)) { + urlString.append(arguments.get(PARAM_ID)); + arguments.remove(PARAM_ID); + } - // Append the language to the URL - if (StringUtils.isNotBlank(language)) { - urlString.append(PARAMETER_LANGUAGE); - urlString.append(language); - } + // Append the suffix of the API URL + urlString.append(submethod); - // Append the country to the URL - if (StringUtils.isNotBlank(country)) { - urlString.append(PARAMETER_COUNTRY); - urlString.append(country); + // Append the key information + urlString.append(DELIMITER_FIRST).append(PARAM_API_KEY); + urlString.append(tmdb.getApiKey()); } - // Append the page to the URL - if (page > DEFAULT_INT) { - urlString.append(PARAMETER_PAGE); - urlString.append(page); + for (Map.Entry argEntry : arguments.entrySet()) { + urlString.append(DELIMITER_SUBSEQUENT).append(argEntry.getKey()); + urlString.append(argEntry.getValue()); } try { @@ -149,100 +154,54 @@ public class ApiUrl { } catch (MalformedURLException ex) { LOGGER.warn("Failed to create URL " + urlString.toString() + " - " + ex.toString()); return null; + } finally { + arguments.clear(); } } /** - * Create an URL using the query (string), language and page + * Add arguments individually * - * @param query - * @param language - * @param page - * @return - */ - public URL getQueryUrl(String query, String language, int page) { - return getFullUrl(query, DEFAULT_STRING, language, null, page); - } - - /** - * Create an URL using the query (string) - * @param query - * @return - */ - public URL getQueryUrl(String query) { - return getQueryUrl(query, DEFAULT_STRING, DEFAULT_INT); - } - - /** - * Create an URL using the query (string) and language - * @param query - * @param language - * @return + * @param key + * @param value */ - public URL getQueryUrl(String query, String language) { - return getQueryUrl(query, language, DEFAULT_INT); + public void addArgument(String key, String value) { + arguments.put(key, value); } /** - * Create an URL using the movie ID, language and country code + * Add arguments individually * - * @param movieId - * @param language - * @param country - * @return - */ - public URL getIdUrl(String movieId, String language, String country) { - return getFullUrl(DEFAULT_STRING, movieId, language, country, DEFAULT_INT); - } - - /** - * Create an URL using the movie ID and language - * @param movieId - * @param language - * @return - */ - public URL getIdUrl(String movieId, String language) { - return getIdUrl(movieId, language, DEFAULT_STRING); - } - - /** - * Create an URL using the movie ID - * @param movieId - * @return + * @param key + * @param value */ - public URL getIdUrl(String movieId) { - return getIdUrl(movieId, DEFAULT_STRING, DEFAULT_STRING); + public void addArgument(String key, int value) { + arguments.put(key, Integer.toString(value)); } /** - * Create an URL using the movie ID, language and country code + * Add arguments individually * - * @param movieId - * @param language - * @param country - * @return + * @param key + * @param value */ - public URL getIdUrl(int movieId, String language, String country) { - return getIdUrl(String.valueOf(movieId), language, country); + public void addArgument(String key, boolean value) { + arguments.put(key, Boolean.toString(value)); } /** - * Create an URL using the movie ID and language - * @param movieId - * @param language - * @return + * Clear the arguments */ - public URL getIdUrl(int movieId, String language) { - return getIdUrl(String.valueOf(movieId), language, DEFAULT_STRING); + public void clearArguments() { + arguments.clear(); } /** - * Create an URL using the movie ID - * @param movieId - * @return + * Set the arguments directly + * + * @param args */ - public URL getIdUrl(int movieId) { - return getIdUrl(String.valueOf(movieId), DEFAULT_STRING, DEFAULT_STRING); + public void setArguments(Map args) { + arguments.putAll(args); } - } diff --git a/themoviedbapi/src/main/java/com/moviejukebox/themoviedb/tools/FilteringLayout.java b/themoviedbapi/src/main/java/com/moviejukebox/themoviedb/tools/FilteringLayout.java index f63079ea1..bd59bcad9 100644 --- a/themoviedbapi/src/main/java/com/moviejukebox/themoviedb/tools/FilteringLayout.java +++ b/themoviedbapi/src/main/java/com/moviejukebox/themoviedb/tools/FilteringLayout.java @@ -20,18 +20,27 @@ import org.apache.log4j.spi.LoggingEvent; /** * Log4J Filtering routine to remove API keys from the output + * * @author Stuart.Boston * */ public class FilteringLayout extends PatternLayout { - private static Pattern apiKeys = Pattern.compile("DO_NOT_MATCH"); - public static void addApiKey(String apiKey) { - apiKeys = Pattern.compile(apiKey); + private static final String REPLACEMENT = "[APIKEY]"; + private static Pattern replacementPattern = Pattern.compile("DO_NOT_MATCH"); + + /** + * Add the string to replace in the log output + * + * @param replacementString + */ + public static void addReplacementString(String replacementString) { + replacementPattern = Pattern.compile(replacementString); } /** * Extend the format to remove the API_KEYS from the output + * * @param event * @return */ @@ -40,12 +49,12 @@ public class FilteringLayout extends PatternLayout { if (event.getMessage() instanceof String) { String message = event.getRenderedMessage(); - Matcher matcher = apiKeys.matcher(message); + Matcher matcher = replacementPattern.matcher(message); if (matcher.find()) { - String maskedMessage = matcher.replaceAll("[APIKEY]"); + String maskedMessage = matcher.replaceAll(REPLACEMENT); - Throwable throwable = event.getThrowableInformation() != null ? - event.getThrowableInformation().getThrowable() : null; + Throwable throwable = event.getThrowableInformation() != null + ? event.getThrowableInformation().getThrowable() : null; LoggingEvent maskedEvent = new LoggingEvent(event.fqnOfCategoryClass, Logger.getLogger(event.getLoggerName()), event.timeStamp, diff --git a/themoviedbapi/src/test/java/com/moviejukebox/themoviedb/TheMovieDbTest.java b/themoviedbapi/src/test/java/com/moviejukebox/themoviedb/TheMovieDbTest.java index 98e38a027..963fcee2e 100644 --- a/themoviedbapi/src/test/java/com/moviejukebox/themoviedb/TheMovieDbTest.java +++ b/themoviedbapi/src/test/java/com/moviejukebox/themoviedb/TheMovieDbTest.java @@ -13,9 +13,11 @@ package com.moviejukebox.themoviedb; import com.moviejukebox.themoviedb.model.*; +import com.moviejukebox.themoviedb.tools.FilteringLayout; import java.io.IOException; import java.util.List; import org.apache.commons.lang3.StringUtils; +import org.apache.log4j.Level; import org.apache.log4j.Logger; import org.junit.*; import static org.junit.Assert.*; @@ -46,6 +48,10 @@ public class TheMovieDbTest { @BeforeClass public static void setUpClass() throws Exception { + // Set the logger level to TRACE + Logger.getRootLogger().setLevel(Level.TRACE); + // Show the version of the API + TheMovieDb.showVersion(); } @AfterClass @@ -54,6 +60,8 @@ public class TheMovieDbTest { @Before public void setUp() { + // Make sure the filter isn't applied to the test output + FilteringLayout.addReplacementString("DO_NOT_MATCH"); } @After @@ -76,6 +84,14 @@ public class TheMovieDbTest { LOGGER.info(tmdbConfig.toString()); } + /** + * Test of showVersion method, of class TheMovieDb. + */ + @Test + public void testShowVersion() { + // Not required + } + /** * Test of searchMovie method, of class TheMovieDb. */ @@ -84,15 +100,16 @@ public class TheMovieDbTest { LOGGER.info("searchMovie"); // Try a movie with less than 1 page of results - List movieList = tmdb.searchMovie("Blade Runner", "", true); + List 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.size() > 0); // Try a russian langugage movie - movieList = tmdb.searchMovie("О чём говорят мужчины", "ru", true); + movieList = tmdb.searchMovie("О чём говорят мужчины", 0, "ru", true, 0); assertTrue("No movies found, should be at least 1", movieList.size() > 0); // Try a movie with more than 20 results - movieList = tmdb.searchMovie("Star Wars", "en", false); + movieList = tmdb.searchMovie("Star Wars", 0, "en", false, 0); assertTrue("Not enough movies found, should be over 15, found " + movieList.size(), movieList.size() >= 15); } @@ -212,6 +229,10 @@ public class TheMovieDbTest { assertFalse("No collection information", result.getParts().isEmpty()); } + /** + * Test of createImageUrl method, of class TheMovieDb. + * @throws MovieDbException + */ @Test public void testCreateImageUrl() throws MovieDbException { LOGGER.info("createImageUrl"); @@ -383,14 +404,6 @@ public class TheMovieDbTest { assertTrue("No company movies found", !results.isEmpty()); } - /** - * Test of showVersion method, of class TheMovieDb. - */ - @Test - public void testShowVersion() { - // Not required - } - /** * Test of searchCompanies method, of class TheMovieDb. */ @@ -430,4 +443,54 @@ public class TheMovieDbTest { List results = tmdb.getGenreMovies(ID_GENRE_ACTION, "", true); assertTrue("No genre movies found", !results.isEmpty()); } + + /** + * Test of getUpcoming method, of class TheMovieDb. + */ + @Test + public void testGetUpcoming() throws Exception { + LOGGER.info("getUpcoming"); + List results = tmdb.getUpcoming(""); + assertTrue("No upcoming movies found", !results.isEmpty()); + } + + /** + * Test of getCollectionImages method, of class TheMovieDb. + */ + @Test + public void testGetCollectionImages() throws Exception { + LOGGER.info("getCollectionImages"); + String language = ""; + List result = tmdb.getCollectionImages(ID_MOVIE_STAR_WARS_COLLECTION, language); + assertFalse("No artwork found", result.isEmpty()); + } + + /** + * Test of getAuthorisationToken method, of class TheMovieDb. + */ +// @Test + public void testGetAuthorisationToken() throws Exception { + LOGGER.info("getAuthorisationToken"); + TokenAuthorisation result = tmdb.getAuthorisationToken(); + assertFalse("Token is null", result == null); + assertTrue("Token is not valid", result.getSuccess()); + LOGGER.info(result.toString()); + } + + /** + * Test of getSessionToken method, of class TheMovieDb. + */ +// @Test + public void testGetSessionToken() throws Exception { + LOGGER.info("getSessionToken"); + TokenAuthorisation token = tmdb.getAuthorisationToken(); + assertFalse("Token is null", token == null); + assertTrue("Token is not valid", token.getSuccess()); + LOGGER.info(token.toString()); + + TokenSession result = tmdb.getSessionToken(token); + assertFalse("Session token is null", result == null); + assertTrue("Session token is not valid", result.getSuccess()); + LOGGER.info(result.toString()); + } }