diff --git a/.gitignore b/.gitignore index 2f6ca9dde..1e90a98cf 100644 --- a/.gitignore +++ b/.gitignore @@ -7,6 +7,7 @@ /target/ /nbactions.xml testing.properties +*.bin .settings .classpath .project diff --git a/pom.xml b/pom.xml index 5ad3bf0db..d2bf9e03e 100644 --- a/pom.xml +++ b/pom.xml @@ -13,7 +13,7 @@ com.omertron themoviedbapi - 3.12-SNAPSHOT + 4.0-SNAPSHOT jar API-The MovieDB @@ -117,6 +117,13 @@ api-common 1.4 + + + commons-io + commons-io + 2.4 + test + diff --git a/src/main/java/com/omertron/themoviedbapi/Compare.java b/src/main/java/com/omertron/themoviedbapi/Compare.java new file mode 100644 index 000000000..8719734dc --- /dev/null +++ b/src/main/java/com/omertron/themoviedbapi/Compare.java @@ -0,0 +1,141 @@ +/* + * Copyright (c) 2004-2015 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.movie.MovieDb; +import org.apache.commons.lang3.StringUtils; + +/** + * Compare various objects to see if the are logically the same. + * + * Allows for some variance of the details + * + * @author Stuart.Boston + */ +public class Compare { + + // Constants + private static final int YEAR_LENGTH = 4; + + private Compare() { + throw new UnsupportedOperationException("Utility class"); + } + + /** + * 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 movies(final MovieDb moviedb, final String title, final String year) { + return movies(moviedb, title, year, 0, true); + } + + /** + * 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 + * @param caseSensitive true if the comparison is to be case sensitive + * @return True if there is a match, False otherwise. + */ + public static boolean movies(final MovieDb moviedb, final String title, final String year, int maxDistance, boolean caseSensitive) { + if ((moviedb == null) || (StringUtils.isBlank(title))) { + return false; + } + + String primaryTitle, firstCompareTitle, secondCompareTitle; + if (caseSensitive) { + primaryTitle = title; + firstCompareTitle = moviedb.getOriginalTitle(); + secondCompareTitle = moviedb.getTitle(); + } else { + primaryTitle = title.toLowerCase(); + firstCompareTitle = moviedb.getTitle().toLowerCase(); + secondCompareTitle = moviedb.getOriginalTitle().toLowerCase(); + } + + if (isValidYear(year) && isValidYear(moviedb.getReleaseDate())) { + // Compare with year + String movieYear = moviedb.getReleaseDate().substring(0, YEAR_LENGTH); + return movieYear.equals(year) && compareTitles(primaryTitle, firstCompareTitle, secondCompareTitle, maxDistance); + } + + // Compare without year + return compareTitles(primaryTitle, firstCompareTitle, secondCompareTitle, maxDistance); + } + + /** + * Compare a title with two other titles. + * + * @param primaryTitle Primary title + * @param firstCompareTitle First title to compare with + * @param secondCompareTitle Second title to compare with + * @param maxDistance Maximum difference between the titles + * @return + */ + private static boolean compareTitles(String primaryTitle, String firstCompareTitle, String secondCompareTitle, int maxDistance) { + // Compare with the first title + if (compareDistance(primaryTitle, firstCompareTitle, maxDistance)) { + return true; + } + + // Compare with the other title + return compareDistance(primaryTitle, secondCompareTitle, maxDistance); + } + + /** + * Compare the MovieDB object with a title & year, case sensitive + * + * @param moviedb + * @param title + * @param year + * @param maxDistance + * @return + */ + public static boolean movies(final MovieDb moviedb, final String title, final String year, int maxDistance) { + return Compare.movies(moviedb, title, year, maxDistance, true); + } + + /** + * Compare the Levenshtein Distance between the two strings + * + * @param title1 + * @param title2 + * @param distance + */ + private static boolean compareDistance(final String title1, final String title2, int distance) { + return StringUtils.getLevenshteinDistance(title1, title2) <= distance; + } + + /** + * Check the year is not blank or UNKNOWN + * + * @param year + */ + private static boolean isValidYear(final String year) { + return StringUtils.isNotBlank(year) && !"UNKNOWN".equals(year); + } +} diff --git a/src/main/java/com/omertron/themoviedbapi/TheMovieDbApi.java b/src/main/java/com/omertron/themoviedbapi/TheMovieDbApi.java index 18c27adc2..b5a0d1a06 100644 --- a/src/main/java/com/omertron/themoviedbapi/TheMovieDbApi.java +++ b/src/main/java/com/omertron/themoviedbapi/TheMovieDbApi.java @@ -19,104 +19,109 @@ */ package com.omertron.themoviedbapi; -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.omertron.themoviedbapi.model.Account; -import com.omertron.themoviedbapi.model.AlternativeTitle; -import com.omertron.themoviedbapi.model.Artwork; -import com.omertron.themoviedbapi.model.ArtworkType; -import com.omertron.themoviedbapi.model.ChangeKeyItem; -import com.omertron.themoviedbapi.model.ChangedItem; -import com.omertron.themoviedbapi.model.ChangedMovie; -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.enumeration.ExternalSource; +import com.omertron.themoviedbapi.enumeration.MediaType; +import com.omertron.themoviedbapi.enumeration.SearchType; +import com.omertron.themoviedbapi.enumeration.SortBy; +import com.omertron.themoviedbapi.methods.TmdbAccount; +import com.omertron.themoviedbapi.methods.TmdbAuthentication; +import com.omertron.themoviedbapi.methods.TmdbCertifications; +import com.omertron.themoviedbapi.methods.TmdbChanges; +import com.omertron.themoviedbapi.methods.TmdbCollections; +import com.omertron.themoviedbapi.methods.TmdbCompanies; +import com.omertron.themoviedbapi.methods.TmdbConfiguration; +import com.omertron.themoviedbapi.methods.TmdbCredits; +import com.omertron.themoviedbapi.methods.TmdbDiscover; +import com.omertron.themoviedbapi.methods.TmdbFind; +import com.omertron.themoviedbapi.methods.TmdbGenres; +import com.omertron.themoviedbapi.methods.TmdbKeywords; +import com.omertron.themoviedbapi.methods.TmdbLists; +import com.omertron.themoviedbapi.methods.TmdbMovies; +import com.omertron.themoviedbapi.methods.TmdbNetworks; +import com.omertron.themoviedbapi.methods.TmdbPeople; +import com.omertron.themoviedbapi.methods.TmdbReviews; +import com.omertron.themoviedbapi.methods.TmdbSearch; +import com.omertron.themoviedbapi.methods.TmdbTV; +import com.omertron.themoviedbapi.model.Certification; +import com.omertron.themoviedbapi.model.FindResults; 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.ListItemStatus; -import com.omertron.themoviedbapi.model.MovieDb; -import com.omertron.themoviedbapi.model.MovieDbList; -import com.omertron.themoviedbapi.model.MovieDbListStatus; -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.StatusCode; -import com.omertron.themoviedbapi.model.TmdbConfiguration; -import com.omertron.themoviedbapi.model.TokenAuthorisation; -import com.omertron.themoviedbapi.model.TokenSession; -import com.omertron.themoviedbapi.model.Translation; -import com.omertron.themoviedbapi.model.Video; +import com.omertron.themoviedbapi.model.account.Account; +import com.omertron.themoviedbapi.model.artwork.Artwork; +import com.omertron.themoviedbapi.model.artwork.ArtworkMedia; +import com.omertron.themoviedbapi.model.authentication.TokenAuthorisation; +import com.omertron.themoviedbapi.model.authentication.TokenSession; +import com.omertron.themoviedbapi.model.change.ChangeListItem; +import com.omertron.themoviedbapi.model.collection.Collection; +import com.omertron.themoviedbapi.model.collection.CollectionInfo; +import com.omertron.themoviedbapi.model.company.Company; +import com.omertron.themoviedbapi.model.config.Configuration; +import com.omertron.themoviedbapi.model.config.JobDepartment; +import com.omertron.themoviedbapi.model.discover.Discover; +import com.omertron.themoviedbapi.model.keyword.Keyword; +import com.omertron.themoviedbapi.model.list.ListItem; +import com.omertron.themoviedbapi.model.list.UserList; +import com.omertron.themoviedbapi.model.media.MediaBasic; +import com.omertron.themoviedbapi.model.media.MediaCreditList; +import com.omertron.themoviedbapi.model.media.MediaState; +import com.omertron.themoviedbapi.model.media.AlternativeTitle; +import com.omertron.themoviedbapi.model.movie.MovieBasic; +import com.omertron.themoviedbapi.model.movie.MovieDb; +import com.omertron.themoviedbapi.model.movie.ReleaseInfo; +import com.omertron.themoviedbapi.model.media.Translation; +import com.omertron.themoviedbapi.model.media.Video; +import com.omertron.themoviedbapi.model.network.Network; +import com.omertron.themoviedbapi.model.person.ContentRating; +import com.omertron.themoviedbapi.model.person.CreditInfo; +import com.omertron.themoviedbapi.model.person.ExternalID; +import com.omertron.themoviedbapi.model.person.Person; +import com.omertron.themoviedbapi.model.person.PersonCredits; +import com.omertron.themoviedbapi.model.person.PersonFind; +import com.omertron.themoviedbapi.model.review.Review; +import com.omertron.themoviedbapi.model.tv.TVBasic; +import com.omertron.themoviedbapi.model.tv.TVInfo; import com.omertron.themoviedbapi.results.TmdbResultsList; import com.omertron.themoviedbapi.results.TmdbResultsMap; -import com.omertron.themoviedbapi.tools.ApiUrl; import com.omertron.themoviedbapi.tools.HttpTools; import com.omertron.themoviedbapi.tools.MethodBase; -import com.omertron.themoviedbapi.tools.MethodSub; -import com.omertron.themoviedbapi.tools.Param; -import com.omertron.themoviedbapi.tools.TmdbParameters; -import com.omertron.themoviedbapi.wrapper.WrapperAlternativeTitles; import com.omertron.themoviedbapi.wrapper.WrapperChanges; -import com.omertron.themoviedbapi.wrapper.WrapperCollection; -import com.omertron.themoviedbapi.wrapper.WrapperCompany; -import com.omertron.themoviedbapi.wrapper.WrapperCompanyMovies; -import com.omertron.themoviedbapi.wrapper.WrapperConfig; -import com.omertron.themoviedbapi.wrapper.WrapperGenres; -import com.omertron.themoviedbapi.wrapper.WrapperImages; -import com.omertron.themoviedbapi.wrapper.WrapperJobList; -import com.omertron.themoviedbapi.wrapper.WrapperKeywordMovies; -import com.omertron.themoviedbapi.wrapper.WrapperKeywords; -import com.omertron.themoviedbapi.wrapper.WrapperMovie; -import com.omertron.themoviedbapi.wrapper.WrapperMovieCasts; -import com.omertron.themoviedbapi.wrapper.WrapperMovieChanges; -import com.omertron.themoviedbapi.wrapper.WrapperMovieDbList; -import com.omertron.themoviedbapi.wrapper.WrapperMovieKeywords; -import com.omertron.themoviedbapi.wrapper.WrapperMovieList; -import com.omertron.themoviedbapi.wrapper.WrapperPerson; -import com.omertron.themoviedbapi.wrapper.WrapperPersonCredits; -import com.omertron.themoviedbapi.wrapper.WrapperPersonList; -import com.omertron.themoviedbapi.wrapper.WrapperReleaseInfo; -import com.omertron.themoviedbapi.wrapper.WrapperReviews; -import com.omertron.themoviedbapi.wrapper.WrapperTranslations; -import com.omertron.themoviedbapi.wrapper.WrapperVideos; -import java.io.IOException; -import java.net.MalformedURLException; import java.net.URL; -import java.util.Collections; -import java.util.HashMap; import java.util.List; import java.util.Map; -import org.apache.commons.lang3.StringUtils; import org.apache.http.client.HttpClient; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.yamj.api.common.exception.ApiExceptionType; import org.yamj.api.common.http.SimpleHttpClientBuilder; /** * 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 */ public class TheMovieDbApi { - private static final Logger LOG = LoggerFactory.getLogger(TheMovieDbApi.class); - private String apiKey; - private TmdbConfiguration tmdbConfig; private HttpTools httpTools; - // Jackson JSON configuration - private static ObjectMapper mapper = new ObjectMapper(); - // Constants - private static final String MOVIE_ID = "movie_id"; - private static final int YEAR_LENGTH = 4; - private static final int RATING_MAX = 10; - private static final int POST_SUCCESS_STATUS_CODE = 12; + // Sub-methods + private static TmdbAccount tmdbAccount; + private static TmdbAuthentication tmdbAuth; + private static TmdbCertifications tmdbCertifications; + private static TmdbChanges tmdbChanges; + private static TmdbCollections tmdbCollections; + private static TmdbCompanies tmdbCompany; + private static TmdbConfiguration tmdbConfiguration; + private static TmdbCredits tmdbCredits; + private static TmdbDiscover tmdbDiscover; + private static TmdbFind tmdbFind; + private static TmdbGenres tmdbGenre; + private static TmdbKeywords tmdbKeywords; + private static TmdbLists tmdbList; + private static TmdbMovies tmdbMovies; + private static TmdbNetworks tmdbNetworks; + private static TmdbPeople tmdbPeople; + private static TmdbReviews tmdbReviews; + private static TmdbSearch tmdbSearch; + private static TmdbTV tmdbTv; /** * API for The Movie Db. @@ -136,181 +141,228 @@ public class TheMovieDbApi { * @throws MovieDbException */ public TheMovieDbApi(String apiKey, HttpClient httpClient) throws MovieDbException { - this.apiKey = apiKey; this.httpTools = new HttpTools(httpClient); - - URL configUrl = new ApiUrl(apiKey, MethodBase.CONFIGURATION).buildUrl(); - String webpage = httpTools.getRequest(configUrl); - - try { - WrapperConfig wc = mapper.readValue(webpage, WrapperConfig.class); - tmdbConfig = wc.getTmdbConfiguration(); - } catch (IOException ex) { - throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, "Failed to read configuration", configUrl, ex); - } + initialise(apiKey, httpTools); } /** - * Compare the MovieDB object with a title & year + * Initialise the sub-classes once the API key and http client are known + * + * @param apiKey + * @param httpTools + */ + private void initialise(String apiKey, HttpTools httpTools) { + tmdbAccount = new TmdbAccount(apiKey, httpTools); + tmdbAuth = new TmdbAuthentication(apiKey, httpTools); + tmdbCertifications = new TmdbCertifications(apiKey, httpTools); + tmdbChanges = new TmdbChanges(apiKey, httpTools); + tmdbCollections = new TmdbCollections(apiKey, httpTools); + tmdbCompany = new TmdbCompanies(apiKey, httpTools); + tmdbConfiguration = new TmdbConfiguration(apiKey, httpTools); + tmdbCredits = new TmdbCredits(apiKey, httpTools); + tmdbDiscover = new TmdbDiscover(apiKey, httpTools); + tmdbFind = new TmdbFind(apiKey, httpTools); + tmdbGenre = new TmdbGenres(apiKey, httpTools); + tmdbKeywords = new TmdbKeywords(apiKey, httpTools); + tmdbList = new TmdbLists(apiKey, httpTools); + tmdbMovies = new TmdbMovies(apiKey, httpTools); + tmdbNetworks = new TmdbNetworks(apiKey, httpTools); + tmdbPeople = new TmdbPeople(apiKey, httpTools); + tmdbReviews = new TmdbReviews(apiKey, httpTools); + tmdbSearch = new TmdbSearch(apiKey, httpTools); + tmdbTv = new TmdbTV(apiKey, httpTools); + } + + // + /** + * Get the basic information for an account. You will need to have a valid + * session id. * - * @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. + * @param sessionId + * @return + * @throws MovieDbException */ - public static boolean compareMovies(MovieDb moviedb, String title, String year) { - return compareMovies(moviedb, title, year, 0); + public Account getAccount(String sessionId) throws MovieDbException { + return tmdbAccount.getAccount(sessionId); } /** - * Compare the MovieDB object with a title & year + * Get all lists of a given user * - * @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 - * @param caseSensitive true if the comparison is to be case sensitive - * @return True if there is a match, False otherwise. + * @param sessionId + * @param accountId + * @return The lists + * @throws MovieDbException */ - public static boolean compareMovies(MovieDb moviedb, String title, String year, int maxDistance, boolean caseSensitive) { - if ((moviedb == null) || (StringUtils.isBlank(title))) { - return Boolean.FALSE; - } - - String cmpTitle, cmpOtherTitle, cmpOriginalTitle; - if (caseSensitive) { - cmpTitle = title; - cmpOtherTitle = moviedb.getOriginalTitle(); - cmpOriginalTitle = moviedb.getTitle(); - } else { - cmpTitle = title.toLowerCase(); - cmpOtherTitle = moviedb.getTitle().toLowerCase(); - cmpOriginalTitle = moviedb.getOriginalTitle().toLowerCase(); - } + public List getUserLists(String sessionId, int accountId) throws MovieDbException { + return tmdbAccount.getUserLists(sessionId, accountId); + } - if (isValidYear(year) && isValidYear(moviedb.getReleaseDate())) { - // Compare with year - String movieYear = moviedb.getReleaseDate().substring(0, YEAR_LENGTH); - if (movieYear.equals(year)) { - if (compareDistance(cmpOriginalTitle, cmpTitle, maxDistance)) { - return Boolean.TRUE; - } + /** + * Get the account favourite movies + * + * @param sessionId + * @param accountId + * @return + * @throws MovieDbException + */ + public List getFavoriteMovies(String sessionId, int accountId) throws MovieDbException { + return tmdbAccount.getFavoriteMovies(sessionId, accountId); + } - if (compareDistance(cmpOtherTitle, cmpTitle, maxDistance)) { - return Boolean.TRUE; - } - } - } + /** + * Add or remove a movie to an accounts favourite list. + * + * @param sessionId + * @param accountId + * @param mediaId + * @param mediaType + * @param isFavorite + * @return + * @throws MovieDbException + */ + public StatusCode modifyFavoriteStatus(String sessionId, int accountId, Integer mediaId, MediaType mediaType, boolean isFavorite) throws MovieDbException { + return tmdbAccount.modifyFavoriteStatus(sessionId, accountId, mediaType, mediaId, isFavorite); + } - // Compare without year - if (compareDistance(cmpOriginalTitle, cmpTitle, maxDistance)) { - return Boolean.TRUE; - } + /** + * Get the list of rated movies (and associated rating) for an account. + * + * @param sessionId + * @param accountId + * @param page + * @param sortBy + * @param language + * @return + * @throws MovieDbException + */ + public List getRatedMovies(String sessionId, int accountId, Integer page, String sortBy, String language) throws MovieDbException { + return tmdbAccount.getRatedMovies(sessionId, accountId, page, sortBy, language); + } - if (compareDistance(cmpOtherTitle, cmpTitle, maxDistance)) { - return Boolean.TRUE; - } + /** + * Get the list of rated TV shows (and associated rating) for an account. + * + * @param sessionId + * @param accountId + * @param page + * @param sortBy + * @param language + * @return + * @throws MovieDbException + */ + public List getRatedTV(String sessionId, int accountId, Integer page, String sortBy, String language) throws MovieDbException { + return tmdbAccount.getRatedTV(sessionId, accountId, page, sortBy, language); + } - return Boolean.FALSE; + /** + * Get the list of movies on an accounts watchlist. + * + * @param sessionId + * @param accountId + * @param page + * @param sortBy + * @param language + * @return The watchlist of the user + * @throws MovieDbException + */ + public List getWatchListMovie(String sessionId, int accountId, Integer page, String sortBy, String language) throws MovieDbException { + return tmdbAccount.getWatchListMovie(sessionId, accountId, page, sortBy, language); } /** - * Compare the MovieDB object with a title & year, case sensitive + * Get the list of movies on an accounts watchlist. * - * @param moviedb - * @param title - * @param year - * @param maxDistance - * @return + * @param sessionId + * @param accountId + * @param page + * @param sortBy + * @param language + * @return The watchlist of the user + * @throws MovieDbException */ - public static boolean compareMovies(MovieDb moviedb, String title, String year, int maxDistance) { - return compareMovies(moviedb, title, year, maxDistance, true); + public List getWatchListTV(String sessionId, int accountId, Integer page, String sortBy, String language) throws MovieDbException { + return tmdbAccount.getWatchListTV(sessionId, accountId, page, sortBy, language); } /** - * Compare the Levenshtein Distance between the two strings + * Add a movie to an accounts watch list. * - * @param title1 - * @param title2 - * @param distance + * @param sessionId + * @param accountId + * @param mediaId + * @param mediaType + * @return + * @throws MovieDbException */ - private static boolean compareDistance(String title1, String title2, int distance) { - return StringUtils.getLevenshteinDistance(title1, title2) <= distance; + public StatusCode addToWatchList(String sessionId, int accountId, MediaType mediaType, Integer mediaId) throws MovieDbException { + return tmdbAccount.modifyWatchList(sessionId, accountId, mediaType, mediaId, true); } /** - * Check the year is not blank or UNKNOWN + * Remove a movie from an accounts watch list. * - * @param year + * @param sessionId + * @param accountId + * @param mediaId + * @param mediaType + * @return + * @throws MovieDbException */ - private static boolean isValidYear(String year) { - return StringUtils.isNotBlank(year) && !"UNKNOWN".equals(year); + public StatusCode removeFromWatchList(String sessionId, int accountId, MediaType mediaType, Integer mediaId) throws MovieDbException { + return tmdbAccount.modifyWatchList(sessionId, accountId, mediaType, mediaId, false); } - // /** - * Get the configuration information + * Get the list of favorite TV series for an account. * + * @param sessionId + * @param accountId * @return + * @throws MovieDbException */ - public TmdbConfiguration getConfiguration() { - return tmdbConfig; + public List getFavoriteTv(String sessionId, int accountId) throws MovieDbException { + return tmdbAccount.getFavoriteTv(sessionId, accountId); } /** - * Generate the full image URL from the size and image path + * Get a list of rated movies for a specific guest session id. * - * @param imagePath - * @param requiredSize + * @param guestSessionId + * @param language + * @param page + * @param sortBy only CREATED_AT_ASC or CREATED_AT_DESC is supported * @return * @throws MovieDbException */ - public URL createImageUrl(String imagePath, String requiredSize) throws MovieDbException { - if (!tmdbConfig.isValidSize(requiredSize)) { - throw new MovieDbException(ApiExceptionType.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(), ex); - throw new MovieDbException(ApiExceptionType.INVALID_URL, sb.toString(), "", ex); - } + public List getGuestRatedMovies(String guestSessionId, String language, Integer page, SortBy sortBy) throws MovieDbException { + return tmdbAccount.getGuestRatedMovies(guestSessionId, language, page, sortBy); } // - // + // /** - * This method is used to generate a valid request token for user based authentication. + * 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. + * 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. + * As soon as a valid session id has been created the token will be + * destroyed. * * @return * @throws MovieDbException */ public TokenAuthorisation getAuthorisationToken() throws MovieDbException { - TmdbParameters parameters = new TmdbParameters(); - URL url = new ApiUrl(apiKey, MethodBase.AUTH).setSubMethod(MethodSub.TOKEN_NEW).buildUrl(parameters); - - String webpage = httpTools.getRequest(url); - - try { - return mapper.readValue(webpage, TokenAuthorisation.class); - } catch (IOException ex) { - LOG.warn("Failed to get Authorisation Token: {}", ex.getMessage(), ex); - throw new MovieDbException(ApiExceptionType.AUTH_FAILURE, webpage, url, ex); - } + return tmdbAuth.getAuthorisationToken(); } /** - * This method is used to generate a session id for user based authentication. + * 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. * @@ -319,27 +371,12 @@ public class TheMovieDbApi { * @throws MovieDbException */ public TokenSession getSessionToken(TokenAuthorisation token) throws MovieDbException { - TmdbParameters parameters = new TmdbParameters(); - - if (!token.getSuccess()) { - LOG.warn("Session token was not successful!"); - throw new MovieDbException(ApiExceptionType.AUTH_FAILURE, "Authorisation token was not successful!"); - } - - parameters.add(Param.TOKEN, token.getRequestToken()); - URL url = new ApiUrl(apiKey, MethodBase.AUTH).setSubMethod(MethodSub.SESSION_NEW).buildUrl(parameters); - String webpage = httpTools.getRequest(url); - - try { - return mapper.readValue(webpage, TokenSession.class); - } catch (IOException ex) { - LOG.warn("Failed to get Session Token: {}", ex.getMessage(), ex); - throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, webpage, url, ex); - } + return tmdbAuth.getSessionToken(token); } /** - * This method is used to generate a session id for user based authentication. User must provide their username and password + * This method is used to generate a session id for user based + * authentication. User must provide their username and password * * A session id is required in order to use any of the write methods. * @@ -350,174 +387,449 @@ public class TheMovieDbApi { * @throws MovieDbException */ public TokenAuthorisation getSessionTokenLogin(TokenAuthorisation token, String username, String password) throws MovieDbException { - TmdbParameters parameters = new TmdbParameters(); + return tmdbAuth.getSessionTokenLogin(token, username, password); + } - if (!token.getSuccess()) { - LOG.warn("Session token was not successful!"); - throw new MovieDbException(ApiExceptionType.AUTH_FAILURE, "Authorisation token was not successful!"); - } + /** + * 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. + * + * @return + * @throws MovieDbException + */ + public TokenSession getGuestSessionToken() throws MovieDbException { + return tmdbAuth.getGuestSessionToken(); + } + // - parameters.add(Param.TOKEN, token.getRequestToken()); - parameters.add(Param.USERNAME, username); - parameters.add(Param.PASSWORD, password); + // + /** + * Get a list of movies certification. + * + * @return + * @throws MovieDbException + */ + public TmdbResultsMap> getMoviesCertification() throws MovieDbException { + return tmdbCertifications.getMoviesCertification(); + } - URL url = new ApiUrl(apiKey, MethodBase.AUTH).setSubMethod(MethodSub.TOKEN_VALIDATE).buildUrl(parameters); - String webpage = httpTools.getRequest(url); + /** + * Get a list of tv certification. + * + * @return + * @throws MovieDbException + */ + public TmdbResultsMap> getTvCertification() throws MovieDbException { + return tmdbCertifications.getTvCertification(); + } + // - try { - return mapper.readValue(webpage, TokenAuthorisation.class); - } catch (IOException ex) { - LOG.warn("Failed to get Session Token: {}", ex.getMessage(), ex); - throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, webpage, url, ex); - } + // + /** + * Get a list of Movie IDs that have been edited. + * + * You can then use the movie changes API to get the actual data that has + * been changed. + * + * @param page + * @param startDate the start date of the changes, optional + * @param endDate the end date of the changes, optional + * @return List of changed movie + * @throws MovieDbException + */ + public List getMovieChangeList(Integer page, String startDate, String endDate) throws MovieDbException { + return tmdbChanges.getChangeList(MethodBase.MOVIE, page, startDate, endDate); } /** - * This method is used to generate a guest session id. + * Get a list of TV IDs that have been edited. * - * A guest session can be used to rate movies without having a registered TMDb user account. + * You can then use the TV changes API to get the actual data that has been + * changed. + * + * @param page + * @param startDate the start date of the changes, optional + * @param endDate the end date of the changes, optional + * @return List of changed movie + * @throws MovieDbException + */ + public List getTvChangeList(Integer page, String startDate, String endDate) throws MovieDbException { + return tmdbChanges.getChangeList(MethodBase.TV, page, startDate, endDate); + } + + /** + * Get a list of Person IDs that have been edited. * - * 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. + * You can then use the person changes API to get the actual data that has + * been changed. * - * There are also IP limits in place so you should always make sure it's the end user doing the guest session actions. + * @param page + * @param startDate the start date of the changes, optional + * @param endDate the end date of the changes, optional + * @return List of changed movie + * @throws MovieDbException + */ + public List getPersonChangeList(Integer page, String startDate, String endDate) throws MovieDbException { + return tmdbChanges.getChangeList(MethodBase.PERSON, page, startDate, endDate); + } + // + + // + /** + * This method is used to retrieve all of the basic information about a + * movie collection. * - * If a guest session is not used for the first time within 24 hours, it will be automatically discarded. + * 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 TokenSession getGuestSessionToken() throws MovieDbException { - URL url = new ApiUrl(apiKey, MethodBase.AUTH).setSubMethod(MethodSub.GUEST_SESSION).buildUrl(); - String webpage = httpTools.getRequest(url); + public CollectionInfo getCollectionInfo(int collectionId, String language) throws MovieDbException { + return tmdbCollections.getCollectionInfo(collectionId, language); + } - try { - return mapper.readValue(webpage, TokenSession.class); - } catch (IOException ex) { - LOG.warn("Failed to get Guest Session Token: {}", ex.getMessage(), ex); - throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, webpage, url, ex); - } + /** + * Get all of the images for a particular collection by collection id. + * + * @param collectionId + * @param language + * @return + * @throws MovieDbException + */ + public TmdbResultsList getCollectionImages(int collectionId, String language) throws MovieDbException { + return tmdbCollections.getCollectionImages(collectionId, language); } + // + // /** - * Get the basic information for an account. You will need to have a valid session id. + * This method is used to retrieve the basic information about a production + * company on TMDb. * - * @param sessionId + * @param companyId * @return * @throws MovieDbException */ - public Account getAccount(String sessionId) throws MovieDbException { - TmdbParameters parameters = new TmdbParameters(); - parameters.add(Param.SESSION, sessionId); + public Company getCompanyInfo(int companyId) throws MovieDbException { + return tmdbCompany.getCompanyInfo(companyId); + } - URL url = new ApiUrl(apiKey, MethodBase.ACCOUNT).buildUrl(parameters); - String webpage = httpTools.getRequest(url); + /** + * 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. + * + * @param companyId + * @param language + * @param page + * @return + * @throws MovieDbException + */ + public List getCompanyMovies(int companyId, String language, Integer page) throws MovieDbException { + return tmdbCompany.getCompanyMovies(companyId, language, page); + } + // - try { - return mapper.readValue(webpage, Account.class); - } catch (IOException ex) { - LOG.warn("Failed to get Account: {}", ex.getMessage(), ex); - throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, webpage, url, ex); - } + // + /** + * Get the configuration information + * + * @return + * @throws com.omertron.themoviedbapi.MovieDbException + */ + public Configuration getConfiguration() throws MovieDbException { + return tmdbConfiguration.getConfig(); } /** - * Get the account favourite movies + * Generate the full image URL from the size and image path * - * @param sessionId - * @param accountId + * @param imagePath + * @param requiredSize * @return * @throws MovieDbException */ - public List getFavoriteMovies(String sessionId, int accountId) throws MovieDbException { - TmdbParameters parameters = new TmdbParameters(); - parameters.add(Param.SESSION, sessionId); + public URL createImageUrl(String imagePath, String requiredSize) throws MovieDbException { + return tmdbConfiguration.getConfig().createImageUrl(imagePath, requiredSize); + } - URL url = new ApiUrl(apiKey, MethodBase.ACCOUNT).setSubMethod(accountId, MethodSub.FAVORITE_MOVIES).buildUrl(parameters); - String webpage = httpTools.getRequest(url); + /** + * Get a list of valid jobs + * + * @return + * @throws MovieDbException + */ + public TmdbResultsList getJobs() throws MovieDbException { + return tmdbConfiguration.getJobs(); + } - try { - return mapper.readValue(webpage, WrapperMovie.class).getMovies(); - } catch (IOException ex) { - LOG.warn("Failed to get favorite movies: {}", ex.getMessage(), ex); - throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, webpage, url, ex); - } + /** + * Get the list of supported timezones for the API methods that support + * them. + * + * @return @throws MovieDbException + */ + public Map> getTimezones() throws MovieDbException { + return tmdbConfiguration.getTimezones(); } + // - public StatusCode changeFavoriteStatus(String sessionId, int accountId, Integer movieId, boolean isFavorite) throws MovieDbException { - TmdbParameters parameters = new TmdbParameters(); - parameters.add(Param.SESSION, sessionId); + // + /** + * Get the detailed information about a particular credit record. + *

+ * This is currently only supported with the new credit model found in TV. + *
+ * These IDs can be found from any TV credit response as well as the + * TV_credits and combined_credits methods for people.
+ * The episodes object returns a list of episodes and are generally going to + * be guest stars.
+ * The season array will return a list of season numbers.
+ * Season credits are credits that were marked with the "add to every + * season" option in the editing interface and are assumed to be "season + * regulars". + * + * @param creditId + * @param language + * @return + * @throws MovieDbException + */ + public CreditInfo getCreditInfo(String creditId, String language) throws MovieDbException { + return tmdbCredits.getCreditInfo(creditId, language); + } + // - Map body = new HashMap(); - body.put(MOVIE_ID, movieId); - body.put("favorite", isFavorite); - String jsonBody = convertToJson(body); + // + /** + * 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 List getDiscoverMovies(Discover discover) throws MovieDbException { + return tmdbDiscover.getDiscoverMovies(discover); + } - URL url = new ApiUrl(apiKey, MethodBase.ACCOUNT).setSubMethod(accountId, MethodSub.FAVORITE).buildUrl(parameters); - String webpage = httpTools.postRequest(url, jsonBody); + /** + * 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 List getDiscoverTV(Discover discover) throws MovieDbException { + return tmdbDiscover.getDiscoverTV(discover); + } + // - try { - return mapper.readValue(webpage, StatusCode.class); - } catch (IOException ex) { - LOG.warn("Failed to get favorite status: {}", ex.getMessage(), ex); - throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, webpage, url, ex); - } + // + /** + * You con use this method to find movies, tv series or persons using + * external ids. + * + * Supported query ids are + *

+ * + * For details see http://docs.themoviedb.apiary.io/#find + * + * @param id the external id + * @param externalSource one of {@link ExternalSource}. + * @param language the language + * @return + * @throws MovieDbException + */ + public FindResults find(String id, ExternalSource externalSource, String language) throws MovieDbException { + return tmdbFind.find(id, externalSource, language); } + // + // /** - * Add a movie to an accounts watch list. + * Get the list of Movie genres. * - * @param sessionId - * @param accountId - * @param movieId + * @param language * @return * @throws MovieDbException */ - public StatusCode addToWatchList(String sessionId, int accountId, Integer movieId) throws MovieDbException { - return modifyWatchList(sessionId, accountId, movieId, true); + public TmdbResultsList getGenreMovieList(String language) throws MovieDbException { + return tmdbGenre.getGenreMovieList(language); } /** - * Remove a movie from an accounts watch list. + * Get the list of TV genres.. * - * @param sessionId - * @param accountId - * @param movieId + * @param language * @return * @throws MovieDbException */ - public StatusCode removeFromWatchList(String sessionId, int accountId, Integer movieId) throws MovieDbException { - return modifyWatchList(sessionId, accountId, movieId, false); + public TmdbResultsList getGenreTVList(String language) throws MovieDbException { + return tmdbGenre.getGenreTVList(language); } - private StatusCode modifyWatchList(String sessionId, int accountId, Integer movieId, boolean add) throws MovieDbException { - TmdbParameters parameters = new TmdbParameters(); - parameters.add(Param.SESSION, sessionId); + /** + * 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 + * @param includeAllMovies + * @param includeAdult + * @return + * @throws MovieDbException + */ + public List getGenreMovies(int genreId, String language, Integer page, Boolean includeAllMovies, Boolean includeAdult) throws MovieDbException { + return tmdbGenre.getGenreMovies(genreId, language, page, includeAllMovies, includeAdult); + } + // - Map body = new HashMap(); - body.put(MOVIE_ID, movieId); - body.put("movie_watchlist", add); - String jsonBody = convertToJson(body); + // + /** + * Get the basic information for a specific keyword id. + * + * @param keywordId + * @return + * @throws MovieDbException + */ + public Keyword getKeyword(String keywordId) throws MovieDbException { + return tmdbKeywords.getKeyword(keywordId); + } - URL url = new ApiUrl(apiKey, MethodBase.ACCOUNT).setSubMethod(accountId, MethodSub.MOVIE_WATCHLIST).buildUrl(parameters); - String webpage = httpTools.postRequest(url, jsonBody); + /** + * 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, Integer page) throws MovieDbException { + return tmdbKeywords.getKeywordMovies(keywordId, language, page); + } + // + + // + /** + * Get a list by its ID + * + * @param listId + * @return The list and its items + * @throws MovieDbException + */ + public ListItem getList(String listId) throws MovieDbException { + return tmdbList.getList(listId); + } - try { - return mapper.readValue(webpage, StatusCode.class); - } catch (IOException ex) { - LOG.warn("Failed to modify watch list: {}", ex.getMessage(), ex); - throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, webpage, url, ex); - } + /** + * This method lets users create a new list. A valid session id is required. + * + * @param sessionId + * @param name + * @param description + * @return The list id + * @throws MovieDbException + */ + public String createList(String sessionId, String name, String description) throws MovieDbException { + return tmdbList.createList(sessionId, name, description); + } + + /** + * This method lets users delete a list that they created. A valid session + * id is required. + * + * @param sessionId + * @param listId + * @return + * @throws MovieDbException + */ + public StatusCode deleteList(String sessionId, String listId) throws MovieDbException { + return tmdbList.deleteList(sessionId, listId); + } + + /** + * Check to see if an item is already on a list. + * + * @param listId + * @param mediaId + * @return true if the item is on the list + * @throws MovieDbException + */ + public boolean checkItemStatus(String listId, Integer mediaId) throws MovieDbException { + return tmdbList.checkItemStatus(listId, mediaId); + } + + /** + * This method lets users add new items to a list that they created. + * + * A valid session id is required. + * + * @param sessionId + * @param listId + * @param mediaId + * @return true if the movie is on the list + * @throws MovieDbException + */ + public StatusCode addItemToList(String sessionId, String listId, Integer mediaId) throws MovieDbException { + return tmdbList.addItem(sessionId, listId, mediaId); + } + + /** + * This method lets users remove items from a list that they created. + * + * A valid session id is required. + * + * @param sessionId + * @param listId + * @param mediaId + * @return true if the movie is on the list + * @throws MovieDbException + */ + public StatusCode removeItemFromList(String sessionId, String listId, Integer mediaId) throws MovieDbException { + return tmdbList.removeItem(sessionId, listId, mediaId); } // - // + // /** * This method is used to retrieve all of the basic movie information. * * It will return the single highest rated poster and backdrop. * - * ApiExceptionType.MOVIE_ID_NOT_FOUND will be thrown if there are no movies found. + * ApiExceptionType.MOVIE_ID_NOT_FOUND will be thrown if there are no movies + * found. * * @param movieId * @param language @@ -526,24 +838,7 @@ public class TheMovieDbApi { * @throws MovieDbException */ public MovieDb getMovieInfo(int movieId, String language, String... appendToResponse) throws MovieDbException { - TmdbParameters parameters = new TmdbParameters(); - parameters.add(Param.ID, movieId); - parameters.add(Param.LANGUAGE, language); - parameters.add(Param.APPEND, appendToResponse); - - URL url = new ApiUrl(apiKey, MethodBase.MOVIE).buildUrl(parameters); - String webpage = httpTools.getRequest(url); - try { - MovieDb movie = mapper.readValue(webpage, MovieDb.class); - if (movie == null || movie.getId() == 0) { - LOG.warn("No movie found for ID '{}'", movieId); - throw new MovieDbException(ApiExceptionType.ID_NOT_FOUND, "No movie found for ID: " + movieId, url); - } - return movie; - } catch (IOException ex) { - LOG.warn("Failed to get movie info: {}", ex.getMessage(), ex); - throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, webpage, url, ex); - } + return tmdbMovies.getMovieInfo(movieId, language, appendToResponse); } /** @@ -551,7 +846,8 @@ public class TheMovieDbApi { * * It will return the single highest rated poster and backdrop. * - * ApiExceptionType.MOVIE_ID_NOT_FOUND will be thrown if there are no movies found. + * ApiExceptionType.MOVIE_ID_NOT_FOUND will be thrown if there are no movies + * found. * * @param imdbId * @param language @@ -560,29 +856,27 @@ public class TheMovieDbApi { * @throws MovieDbException */ public MovieDb getMovieInfoImdb(String imdbId, String language, String... appendToResponse) throws MovieDbException { - TmdbParameters parameters = new TmdbParameters(); - parameters.add(Param.ID, imdbId); - parameters.add(Param.LANGUAGE, language); - parameters.add(Param.APPEND, appendToResponse); - - URL url = new ApiUrl(apiKey, MethodBase.MOVIE).buildUrl(parameters); - String webpage = httpTools.getRequest(url); + return tmdbMovies.getMovieInfoImdb(imdbId, language, appendToResponse); + } - try { - MovieDb movie = mapper.readValue(webpage, MovieDb.class); - if (movie == null || movie.getId() == 0) { - LOG.warn("No movie found for IMDB ID: '{}'", imdbId); - throw new MovieDbException(ApiExceptionType.ID_NOT_FOUND, "No movie found for IMDB ID: " + imdbId, url); - } - return movie; - } catch (IOException ex) { - LOG.warn("Failed to get movie info: {}", ex.getMessage(), ex); - throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, "Failed to get movie info", url, ex); - } + /** + * This method lets a user get the status of whether or not the movie has + * been rated or added to their favourite or movie watch list. + * + * A valid session id is required. + * + * @param movieId + * @param sessionId + * @return + * @throws MovieDbException + */ + public MediaState getMovieAccountState(int movieId, String sessionId) throws MovieDbException { + return tmdbMovies.getMovieAccountState(movieId, sessionId); } /** - * 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 @@ -591,55 +885,24 @@ public class TheMovieDbApi { * @throws MovieDbException */ public TmdbResultsList getMovieAlternativeTitles(int movieId, String country, String... appendToResponse) throws MovieDbException { - TmdbParameters parameters = new TmdbParameters(); - parameters.add(Param.ID, movieId); - parameters.add(Param.COUNTRY, country); - parameters.add(Param.APPEND, appendToResponse); - - URL url = new ApiUrl(apiKey, MethodBase.MOVIE).setSubMethod(MethodSub.ALT_TITLES).buildUrl(parameters); - String webpage = httpTools.getRequest(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(), ex); - throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, webpage, url, ex); - } + return tmdbMovies.getMovieAlternativeTitles(movieId, country, appendToResponse); } /** - * Get the cast information for a specific movie id. - * - * TODO: Add a function to enrich the data with the people methods + * Get the cast and crew information for a specific movie id. * * @param movieId * @param appendToResponse * @return * @throws MovieDbException */ - public TmdbResultsList getMovieCasts(int movieId, String... appendToResponse) throws MovieDbException { - TmdbParameters parameters = new TmdbParameters(); - parameters.add(Param.ID, movieId); - parameters.add(Param.APPEND, appendToResponse); - - URL url = new ApiUrl(apiKey, MethodBase.MOVIE).setSubMethod(MethodSub.CASTS).buildUrl(parameters); - String webpage = httpTools.getRequest(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(), ex); - throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, webpage, url, ex); - } + public MediaCreditList getMovieCredits(int movieId, String... appendToResponse) throws MovieDbException { + return tmdbMovies.getMovieCredits(movieId, appendToResponse); } /** - * 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 @@ -648,27 +911,12 @@ public class TheMovieDbApi { * @throws MovieDbException */ public TmdbResultsList getMovieImages(int movieId, String language, String... appendToResponse) throws MovieDbException { - TmdbParameters parameters = new TmdbParameters(); - parameters.add(Param.ID, movieId); - parameters.add(Param.LANGUAGE, language); - parameters.add(Param.APPEND, appendToResponse); - - URL url = new ApiUrl(apiKey, MethodBase.MOVIE).setSubMethod(MethodSub.IMAGES).buildUrl(parameters); - String webpage = httpTools.getRequest(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(), ex); - throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, webpage, url, ex); - } + return tmdbMovies.getMovieImages(movieId, language, appendToResponse); } /** - * 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. * @@ -678,26 +926,12 @@ public class TheMovieDbApi { * @throws MovieDbException */ public TmdbResultsList getMovieKeywords(int movieId, String... appendToResponse) throws MovieDbException { - TmdbParameters parameters = new TmdbParameters(); - parameters.add(Param.ID, movieId); - parameters.add(Param.APPEND, appendToResponse); - - URL url = new ApiUrl(apiKey, MethodBase.MOVIE).setSubMethod(MethodSub.KEYWORDS).buildUrl(parameters); - String webpage = httpTools.getRequest(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(), ex); - throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, webpage, url, ex); - } + return tmdbMovies.getMovieKeywords(movieId, appendToResponse); } /** - * 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 @@ -706,27 +940,12 @@ public class TheMovieDbApi { * @throws MovieDbException */ public TmdbResultsList getMovieReleaseInfo(int movieId, String language, String... appendToResponse) throws MovieDbException { - TmdbParameters parameters = new TmdbParameters(); - parameters.add(Param.ID, movieId); - parameters.add(Param.LANGUAGE, language); - parameters.add(Param.APPEND, appendToResponse); - - URL url = new ApiUrl(apiKey, MethodBase.MOVIE).setSubMethod(MethodSub.RELEASES).buildUrl(parameters); - String webpage = httpTools.getRequest(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(), ex); - throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, webpage, url, ex); - } + return tmdbMovies.getMovieReleaseInfo(movieId, language, appendToResponse); } /** - * 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. * @@ -736,28 +955,13 @@ public class TheMovieDbApi { * @return * @throws MovieDbException */ - public TmdbResultsList + // /** - * Get the list of rated movies (and associated rating) for an account. + * This method is used to retrieve the basic information about a TV network. + *

+ * You can use this ID to search for TV shows with the discover method. * - * @param sessionId - * @param accountId + * @param networkId * @return * @throws MovieDbException */ - public List getRatedMovies(String sessionId, int accountId) throws MovieDbException { - TmdbParameters parameters = new TmdbParameters(); - parameters.add(Param.SESSION, sessionId); - - URL url = new ApiUrl(apiKey, MethodBase.ACCOUNT).setSubMethod(accountId, MethodSub.RATED_MOVIES).buildUrl(parameters); - String webpage = httpTools.getRequest(url); - - try { - return mapper.readValue(webpage, WrapperMovie.class).getMovies(); - } catch (IOException ex) { - LOG.warn("Failed to get rated movies: {}", ex.getMessage(), ex); - throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, webpage, url, ex); - } + public Network getNetworkInfo(int networkId) throws MovieDbException { + return tmdbNetworks.getNetworkInfo(networkId); } + // + // /** - * This method lets users rate a movie. + * Get the general person information for a specific id. * - * A valid session id is required. - * - * @param sessionId - * @param movieId - * @param rating + * @param personId + * @param appendToResponse * @return * @throws MovieDbException */ - public boolean postMovieRating(String sessionId, Integer movieId, Integer rating) throws MovieDbException { - if (rating < 0 || rating > RATING_MAX) { - throw new MovieDbException(ApiExceptionType.UNKNOWN_CAUSE, "Rating out of range"); - } - - TmdbParameters parameters = new TmdbParameters(); - parameters.add(Param.SESSION, sessionId); - - URL url = new ApiUrl(apiKey, MethodBase.MOVIE).setSubMethod(movieId, MethodSub.RATING).buildUrl(parameters); - - String jsonBody = convertToJson(Collections.singletonMap("value", rating)); - String webpage = httpTools.postRequest(url, jsonBody); - - try { - StatusCode status = mapper.readValue(webpage, StatusCode.class); - LOG.info("Status: {}", status); - int code = status.getStatusCode(); - return code == POST_SUCCESS_STATUS_CODE; - } catch (IOException ex) { - LOG.warn("Failed to post movie rating: {}", ex.getMessage(), ex); - throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, webpage, url, ex); - } + public Person getPersonInfo(int personId, String... appendToResponse) throws MovieDbException { + return tmdbPeople.getPersonInfo(personId, appendToResponse); } - // - // /** - * This method is used to retrieve all of the basic information about a movie collection. + * Get the movie credits for a specific person id. * - * You can get the ID needed for this method by making a getMovieInfo request for the belongs_to_collection. - * - * @param collectionId + * @param personId * @param language + * @param appendToResponse * @return * @throws MovieDbException */ - public CollectionInfo getCollectionInfo(int collectionId, String language) throws MovieDbException { - TmdbParameters parameters = new TmdbParameters(); - parameters.add(Param.ID, collectionId); - parameters.add(Param.LANGUAGE, language); - - URL url = new ApiUrl(apiKey, MethodBase.COLLECTION).buildUrl(parameters); - String webpage = httpTools.getRequest(url); - - try { - return mapper.readValue(webpage, CollectionInfo.class); - } catch (IOException ex) { - LOG.warn("Failed to get collection information: {}", ex.getMessage(), ex); - throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, webpage, url, ex); - } + public PersonCredits getPersonMovieCredits(int personId, String language, String... appendToResponse) throws MovieDbException { + return tmdbPeople.getPersonMovieCredits(personId, language, appendToResponse); } /** - * Get all of the images for a particular collection by collection id. + * Get the TV credits for a specific person id. * - * @param collectionId + * To get the expanded details for each record, call the /credit method with + * the provided credit_id. + * + * This will provide details about which episode and/or season the credit is + * for. + * + * @param personId * @param language + * @param appendToResponse * @return * @throws MovieDbException */ - public TmdbResultsList getCollectionImages(int collectionId, String language) throws MovieDbException { - TmdbParameters parameters = new TmdbParameters(); - parameters.add(Param.ID, collectionId); - parameters.add(Param.LANGUAGE, language); - - URL url = new ApiUrl(apiKey, MethodBase.COLLECTION).setSubMethod(MethodSub.IMAGES).buildUrl(parameters); - String webpage = httpTools.getRequest(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(), ex); - throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, webpage, url, ex); - } + public PersonCredits getPersonTVCredits(int personId, String language, String... appendToResponse) throws MovieDbException { + return tmdbPeople.getPersonTVCredits(personId, language, appendToResponse); } - // - // /** - * This method is used to retrieve all of the basic person information. + * Get the combined (movie and TV) credits for a specific person id. * - * It will return the single highest rated profile image. + * To get the expanded details for each TV record, call the /credit method + * with the provided credit_id. + * + * This will provide details about which episode and/or season the credit is + * for. * * @param personId + * @param language * @param appendToResponse * @return * @throws MovieDbException */ - public Person getPersonInfo(int personId, String... appendToResponse) throws MovieDbException { - TmdbParameters parameters = new TmdbParameters(); - parameters.add(Param.ID, personId); - parameters.add(Param.APPEND, appendToResponse); - - URL url = new ApiUrl(apiKey, MethodBase.PERSON).buildUrl(parameters); - String webpage = httpTools.getRequest(url); - - try { - return mapper.readValue(webpage, Person.class); - } catch (IOException ex) { - LOG.warn("Failed to get person info: {}", ex.getMessage(), ex); - throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, webpage, url, ex); - } + public PersonCredits getPersonCombinedCredits(int personId, String language, String... appendToResponse) throws MovieDbException { + return tmdbPeople.getPersonCombinedCredits(personId, language, appendToResponse); } /** - * 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. + * Get the external ids for a specific person id. * * @param personId - * @param appendToResponse * @return * @throws MovieDbException */ - public TmdbResultsList getPersonCredits(int personId, String... appendToResponse) throws MovieDbException { - TmdbParameters parameters = new TmdbParameters(); - parameters.add(Param.ID, personId); - parameters.add(Param.APPEND, appendToResponse); - - URL url = new ApiUrl(apiKey, MethodBase.PERSON).setSubMethod(MethodSub.CREDITS).buildUrl(parameters); - String webpage = httpTools.getRequest(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(), ex); - throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, webpage, url, ex); - } + public ExternalID getPersonExternalIds(int personId) throws MovieDbException { + return tmdbPeople.getPersonExternalIds(personId); } /** - * This method is used to retrieve all of the profile images for a person. + * Get the images for a specific person id. * * @param personId * @return * @throws MovieDbException */ public TmdbResultsList getPersonImages(int personId) throws MovieDbException { - TmdbParameters parameters = new TmdbParameters(); - parameters.add(Param.ID, personId); - - URL url = new ApiUrl(apiKey, MethodBase.PERSON).setSubMethod(MethodSub.IMAGES).buildUrl(parameters); - String webpage = httpTools.getRequest(url); + return tmdbPeople.getPersonImages(personId); + } - 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(), ex); - throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, webpage, url, ex); - } + /** + * Get the images that have been tagged with a specific person id. + * + * We return all of the image results with a media object mapped for each + * image. + * + * @param personId + * @param page + * @param language + * @return + * @throws com.omertron.themoviedbapi.MovieDbException + */ + public TmdbResultsList getPersonTaggedImages(int personId, Integer page, String language) throws MovieDbException { + return tmdbPeople.getPersonTaggedImages(personId, page, language); } /** @@ -1258,30 +1253,19 @@ public class TheMovieDbApi { * * 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 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 { - LOG.trace("getPersonChanges: id: {}, start: {}, end: {}", personId, startDate, endDate); - throw new MovieDbException(ApiExceptionType.UNKNOWN_CAUSE, "Not implemented yet", ""); - } - - /** - * Get the list of popular people on The Movie Database. - * - * This list refreshes every day. - * * @return - * @throws MovieDbException + * @throws com.omertron.themoviedbapi.MovieDbException */ - public TmdbResultsList getPersonPopular() throws MovieDbException { - return getPersonPopular(0); + public WrapperChanges getPersonChanges(int personId, String startDate, String endDate) throws MovieDbException { + return tmdbPeople.getPersonChanges(personId, startDate, endDate); } /** @@ -1293,22 +1277,8 @@ public class TheMovieDbApi { * @return * @throws MovieDbException */ - public TmdbResultsList getPersonPopular(int page) throws MovieDbException { - TmdbParameters parameters = new TmdbParameters(); - parameters.add(Param.PAGE, page); - - URL url = new ApiUrl(apiKey, MethodBase.PERSON).setSubMethod(MethodSub.POPULAR).buildUrl(parameters); - String webpage = httpTools.getRequest(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 popular person: {}", ex.getMessage(), ex); - throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, webpage, url, ex); - } + public TmdbResultsList getPersonPopular(Integer page) throws MovieDbException { + return tmdbPeople.getPersonPopular(page); } /** @@ -1318,711 +1288,397 @@ public class TheMovieDbApi { * @throws MovieDbException */ public Person getPersonLatest() throws MovieDbException { - URL url = new ApiUrl(apiKey, MethodBase.PERSON).setSubMethod(MethodSub.LATEST).buildUrl(); - String webpage = httpTools.getRequest(url); - - try { - return mapper.readValue(webpage, Person.class); - } catch (IOException ex) { - LOG.warn("Failed to get latest person: {}", ex.getMessage(), ex); - throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, webpage, url, ex); - } + return tmdbPeople.getPersonLatest(); } // - // + // /** - * This method is used to retrieve the basic information about a production company on TMDb. * - * @param companyId - * @return - * @throws MovieDbException + * @param reviewId + * @return @throws MovieDbException */ - public Company getCompanyInfo(int companyId) throws MovieDbException { - TmdbParameters parameters = new TmdbParameters(); - parameters.add(Param.ID, companyId); - - URL url = new ApiUrl(apiKey, MethodBase.COMPANY).buildUrl(parameters); - String webpage = httpTools.getRequest(url); - - try { - return mapper.readValue(webpage, Company.class); - } catch (IOException ex) { - LOG.warn("Failed to get company information: {}", ex.getMessage(), ex); - throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, webpage, url, ex); - } + public Review getReviews(String reviewId) throws MovieDbException { + return tmdbReviews.getReview(reviewId); } + // + // /** - * This method is used to retrieve the movies associated with a company. + * Search Companies. * - * These movies are returned in order of most recently released to oldest. The default response will return 20 movies per page. + * 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. * - * TODO: Implement more than 20 movies + * http://help.themoviedb.org/kb/api/search-companies * - * @param companyId - * @param language + * @param query * @param page * @return * @throws MovieDbException */ - public TmdbResultsList getCompanyMovies(int companyId, String language, int page) throws MovieDbException { - TmdbParameters parameters = new TmdbParameters(); - parameters.add(Param.ID, companyId); - parameters.add(Param.LANGUAGE, language); - parameters.add(Param.PAGE, page); - - URL url = new ApiUrl(apiKey, MethodBase.COMPANY).setSubMethod(MethodSub.MOVIES).buildUrl(parameters); - String webpage = httpTools.getRequest(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(), ex); - throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, webpage, url, ex); - } + public TmdbResultsList searchCompanies(String query, Integer page) throws MovieDbException { + return tmdbSearch.searchCompanies(query, page); } - // - // /** - * You can use this method to retrieve the list of genres used on TMDb. - * - * These IDs will correspond to those found in movie calls. + * Search for collections by name. * + * @param query * @param language + * @param page * @return * @throws MovieDbException */ - public TmdbResultsList getGenreList(String language) throws MovieDbException { - TmdbParameters parameters = new TmdbParameters(); - parameters.add(Param.LANGUAGE, language); - - URL url = new ApiUrl(apiKey, MethodBase.GENRE).setSubMethod(MethodSub.LIST).buildUrl(parameters); - String webpage = httpTools.getRequest(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(), ex); - throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, webpage, url, ex); - } + public TmdbResultsList searchCollection(String query, Integer page, String language) throws MovieDbException { + return tmdbSearch.searchCollection(query, page, language); } /** - * 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. + * Search for keywords by name * - * @param genreId - * @param language + * @param query * @param page - * @param includeAllMovies * @return * @throws MovieDbException */ - public TmdbResultsList getGenreMovies(int genreId, String language, int page, boolean includeAllMovies) throws MovieDbException { - TmdbParameters parameters = new TmdbParameters(); - parameters.add(Param.ID, genreId); - parameters.add(Param.LANGUAGE, language); - parameters.add(Param.PAGE, page); - parameters.add(Param.INCLUDE_ALL_MOVIES, includeAllMovies); - - URL url = new ApiUrl(apiKey, MethodBase.GENRE).setSubMethod(MethodSub.MOVIES).buildUrl(parameters); - String webpage = httpTools.getRequest(url); + public TmdbResultsList searchKeyword(String query, Integer page) throws MovieDbException { + return tmdbSearch.searchKeyword(query, page); + } - 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(), ex); - throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, webpage, url, ex); - } + /** + * Search for lists by name and description. + * + * @param query + * @param includeAdult + * @param page + * @return + * @throws MovieDbException + */ + public TmdbResultsList searchList(String query, Integer page, Boolean includeAdult) throws MovieDbException { + return tmdbSearch.searchList(query, page, includeAdult); } - // - // /** - * Search Movies This is a good starting point to start finding movies on TMDb. + * 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 query + * @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) + * @param page The page of results to return. 0 to get the default (first + * page) + * @param primaryReleaseYear + * @param searchType * @return * @throws MovieDbException */ - public TmdbResultsList searchMovie(String movieName, int searchYear, String language, boolean includeAdult, int page) throws MovieDbException { - TmdbParameters parameters = new TmdbParameters(); - parameters.add(Param.QUERY, movieName); - parameters.add(Param.YEAR, searchYear); - parameters.add(Param.LANGUAGE, language); - parameters.add(Param.ADULT, includeAdult); - parameters.add(Param.PAGE, page); - - URL url = new ApiUrl(apiKey, MethodBase.SEARCH).setSubMethod(MethodSub.MOVIE).buildUrl(parameters); - String webpage = httpTools.getRequest(url); - - try { - WrapperMovie wrapper = mapper.readValue(webpage, WrapperMovie.class); - TmdbResultsList results = new TmdbResultsList(wrapper.getMovies()); - results.copyWrapper(wrapper); - return results; - } catch (IOException ex) { - LOG.warn("Failed to find movie: {}", ex.getMessage(), ex); - throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, webpage, url, ex); - } - + public TmdbResultsList searchMovie(String query, + Integer page, + String language, + Boolean includeAdult, + Integer searchYear, + Integer primaryReleaseYear, + SearchType searchType) throws MovieDbException { + return tmdbSearch.searchMovie(query, page, language, includeAdult, searchYear, primaryReleaseYear, searchType); } /** - * Search for collections by name. + * Search the movie, tv show and person collections with a single query. + * + * Each item returned in the result array has a media_type field that maps + * to either movie, tv or person. + * + * Each mapped result is the same response you would get from each + * independent search * * @param query - * @param language * @param page + * @param language + * @param includeAdult * @return * @throws MovieDbException */ - public TmdbResultsList searchCollection(String query, String language, int page) throws MovieDbException { - TmdbParameters parameters = new TmdbParameters(); - parameters.add(Param.QUERY, query); - parameters.add(Param.LANGUAGE, language); - parameters.add(Param.PAGE, page); - - URL url = new ApiUrl(apiKey, MethodBase.SEARCH).setSubMethod(MethodSub.COLLECTION).buildUrl(parameters); - String webpage = httpTools.getRequest(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(), ex); - throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, webpage, url, ex); - } + public TmdbResultsList searchMulti(String query, Integer page, String language, Boolean includeAdult) throws MovieDbException { + return tmdbSearch.searchMulti(query, page, language, includeAdult); } /** * 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. + * The idea is to be a quick and light method so you can iterate through + * people quickly. * - * @param personName + * @param query * @param includeAdult * @param page + * @param searchType * @return * @throws MovieDbException */ - public TmdbResultsList searchPeople(String personName, boolean includeAdult, int page) throws MovieDbException { - TmdbParameters parameters = new TmdbParameters(); - parameters.add(Param.QUERY, personName); - parameters.add(Param.ADULT, includeAdult); - parameters.add(Param.PAGE, page); - - URL url = new ApiUrl(apiKey, MethodBase.SEARCH).setSubMethod(MethodSub.PERSON).buildUrl(parameters); - String webpage = httpTools.getRequest(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(), ex); - throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, webpage, url, ex); - } + public TmdbResultsList searchPeople(String query, Integer page, Boolean includeAdult, SearchType searchType) throws MovieDbException { + return tmdbSearch.searchPeople(query, page, includeAdult, searchType); } /** - * Search for lists by name and description. + * Search for TV shows by title. * * @param query - * @param language * @param page + * @param language + * @param firstAirDateYear + * @param searchType * @return - * @throws MovieDbException + * @throws com.omertron.themoviedbapi.MovieDbException */ - public TmdbResultsList searchList(String query, String language, int page) throws MovieDbException { - TmdbParameters parameters = new TmdbParameters(); - parameters.add(Param.QUERY, query); - parameters.add(Param.LANGUAGE, language); - parameters.add(Param.PAGE, page); - - URL url = new ApiUrl(apiKey, MethodBase.SEARCH).setSubMethod(MethodSub.LIST).buildUrl(parameters); - String webpage = httpTools.getRequest(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(), ex); - throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, webpage, url, ex); - } + public TmdbResultsList searchTV(String query, Integer page, String language, Integer firstAirDateYear, SearchType searchType) throws MovieDbException { + return tmdbSearch.searchTV(query, page, language, firstAirDateYear, searchType); } + // + // /** - * 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 + * Get the primary information about a TV series by id. * - * @param companyName - * @param page + * @param tvID + * @param language + * @param appendToResponse * @return - * @throws MovieDbException + * @throws com.omertron.themoviedbapi.MovieDbException */ - public TmdbResultsList searchCompanies(String companyName, int page) throws MovieDbException { - TmdbParameters parameters = new TmdbParameters(); - parameters.add(Param.QUERY, companyName); - parameters.add(Param.PAGE, page); - - URL url = new ApiUrl(apiKey, MethodBase.SEARCH).setSubMethod(MethodSub.COMPANY).buildUrl(parameters); - String webpage = httpTools.getRequest(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(), ex); - throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, webpage, url, ex); - } + public TVInfo getTVInfo(int tvID, String language, String... appendToResponse) throws MovieDbException { + return tmdbTv.getTVInfo(tvID, language, appendToResponse); } /** - * Search for keywords by name + * This method lets users get the status of whether or not the TV show has + * been rated or added to their favourite or watch lists. * - * @param query - * @param page + * A valid session id is required. + * + * @param tvID + * @param sessionID * @return - * @throws MovieDbException + * @throws com.omertron.themoviedbapi.MovieDbException */ - public TmdbResultsList searchKeyword(String query, int page) throws MovieDbException { - TmdbParameters parameters = new TmdbParameters(); - parameters.add(Param.QUERY, query); - parameters.add(Param.PAGE, page); - - URL url = new ApiUrl(apiKey, MethodBase.SEARCH).setSubMethod(MethodSub.KEYWORD).buildUrl(parameters); - String webpage = httpTools.getRequest(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(), ex); - throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, webpage, url, ex); - } + public MediaState getTVAccountState(int tvID, String sessionID) throws MovieDbException { + return tmdbTv.getTVAccountState(tvID, sessionID); } - // - // /** - * Get a list by its ID + * Get the alternative titles for a specific show ID. * - * @param listId - * @return The list and its items - * @throws MovieDbException + * @param tvID + * @return + * @throws com.omertron.themoviedbapi.MovieDbException */ - public MovieDbList getList(String listId) throws MovieDbException { - TmdbParameters parameters = new TmdbParameters(); - parameters.add(Param.ID, listId); - - URL url = new ApiUrl(apiKey, MethodBase.LIST).buildUrl(parameters); - String webpage = httpTools.getRequest(url); - - try { - return mapper.readValue(webpage, MovieDbList.class); - } catch (IOException ex) { - LOG.warn("Failed to get list: {}", ex.getMessage(), ex); - throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, webpage, url, ex); - } + public TmdbResultsList getTVAlternativeTitles(int tvID) throws MovieDbException { + return tmdbTv.getTVAlternativeTitles(tvID); } /** - * Get all lists of a given user + * Get the changes for a specific TV show id. * - * @param sessionId - * @param accountID - * @return The lists - * @throws MovieDbException + * @param tvID + * @param startDate + * @param endDate + * @return + * @throws com.omertron.themoviedbapi.MovieDbException */ - public List getUserLists(String sessionId, int accountID) throws MovieDbException { - TmdbParameters parameters = new TmdbParameters(); - parameters.add(Param.SESSION, sessionId); - - URL url = new ApiUrl(apiKey, MethodBase.ACCOUNT).setSubMethod(accountID, MethodSub.LISTS).buildUrl(parameters); - String webpage = httpTools.getRequest(url); - - try { - return mapper.readValue(webpage, WrapperMovieDbList.class).getLists(); - } catch (IOException ex) { - LOG.warn("Failed to get user list: {}", ex.getMessage(), ex); - throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, webpage, url, ex); - } + public WrapperChanges getTVChanges(int tvID, String startDate, String endDate) throws MovieDbException { + return tmdbTv.getTVChanges(tvID, startDate, endDate); } /** - * This method lets users create a new list. A valid session id is required. + * Get the content ratings for a specific TV show id. * - * @param sessionId - * @param name - * @param description - * @return The list id - * @throws MovieDbException + * @param tvID + * @return + * @throws com.omertron.themoviedbapi.MovieDbException */ - public String createList(String sessionId, String name, String description) throws MovieDbException { - TmdbParameters parameters = new TmdbParameters(); - parameters.add(Param.SESSION, sessionId); - - Map body = new HashMap(); - body.put("name", StringUtils.trimToEmpty(name)); - body.put("description", StringUtils.trimToEmpty(description)); - String jsonBody = convertToJson(body); - - URL url = new ApiUrl(apiKey, MethodBase.LIST).buildUrl(parameters); - String webpage = httpTools.postRequest(url, jsonBody); - - try { - return mapper.readValue(webpage, MovieDbListStatus.class).getListId(); - } catch (IOException ex) { - LOG.warn("Failed to create list: {}", ex.getMessage(), ex); - throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, webpage, url, ex); - } + public TmdbResultsList getTVContentRatings(int tvID) throws MovieDbException { + return tmdbTv.getTVContentRatings(tvID); } /** - * Check to see if a movie ID is already added to a list. + * Get the cast & crew information about a TV series. * - * @param listId - * @param movieId - * @return true if the movie is on the list - * @throws MovieDbException + * @param tvID + * @param language + * @param appendToResponse + * @return + * @throws com.omertron.themoviedbapi.MovieDbException */ - public boolean isMovieOnList(String listId, Integer movieId) throws MovieDbException { - TmdbParameters parameters = new TmdbParameters(); - parameters.add(Param.ID, movieId); - - URL url = new ApiUrl(apiKey, MethodBase.LIST).setSubMethod(listId, MethodSub.ITEM_STATUS).buildUrl(parameters); - String webpage = httpTools.getRequest(url); - - try { - return mapper.readValue(webpage, ListItemStatus.class).isItemPresent(); - } catch (IOException ex) { - LOG.warn("Failed to get item status: {}", ex.getMessage(), ex); - throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, webpage, url, ex); - } + public MediaCreditList getTVCredits(int tvID, String language, String... appendToResponse) throws MovieDbException { + return tmdbTv.getTVCredits(tvID, language, appendToResponse); } /** - * This method lets users add new movies to a list that they created. A valid session id is required. + * Get the external ids that we have stored for a TV series. * - * @param sessionId - * @param listId - * @param movieId - * @return true if the movie is on the list - * @throws MovieDbException + * @param tvID + * @param language + * @return + * @throws com.omertron.themoviedbapi.MovieDbException */ - public StatusCode addMovieToList(String sessionId, String listId, Integer movieId) throws MovieDbException { - return modifyMovieList(sessionId, listId, movieId, MethodSub.ADD_ITEM); + public ExternalID getTVExternalIDs(int tvID, String language) throws MovieDbException { + return tmdbTv.getTVExternalIDs(tvID, language); } /** - * This method lets users remove movies from a list that they created. A valid session id is required. + * Get the images (posters and backdrops) for a TV series. * - * @param sessionId - * @param listId - * @param movieId - * @return true if the movie is on the list - * @throws MovieDbException + * @param tvID + * @param language + * @param includeImageLanguage + * @return + * @throws com.omertron.themoviedbapi.MovieDbException */ - public StatusCode removeMovieFromList(String sessionId, String listId, Integer movieId) throws MovieDbException { - return modifyMovieList(sessionId, listId, movieId, MethodSub.REMOVE_ITEM); + public TmdbResultsList getTVImages(int tvID, String language, String... includeImageLanguage) throws MovieDbException { + return tmdbTv.getTVImages(tvID, language, includeImageLanguage); } - private StatusCode modifyMovieList(String sessionId, String listId, Integer movieId, MethodSub operation) throws MovieDbException { - TmdbParameters parameters = new TmdbParameters(); - parameters.add(Param.SESSION, sessionId); - - String jsonBody = convertToJson(Collections.singletonMap("media_id", String.valueOf(movieId))); - - URL url = new ApiUrl(apiKey, MethodBase.LIST).setSubMethod(listId, operation).buildUrl(parameters); - String webpage = httpTools.postRequest(url, jsonBody); - - try { - return mapper.readValue(webpage, StatusCode.class); - } catch (IOException ex) { - LOG.warn("Failed to remove movie from list: {}", ex.getMessage(), ex); - throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, webpage, url, ex); - } + /** + * Get the plot keywords for a specific TV show id. + * + * @param tvID + * @param appendToResponse + * @return + * @throws com.omertron.themoviedbapi.MovieDbException + */ + public TmdbResultsList getTVKeywords(int tvID, String... appendToResponse) throws MovieDbException { + return tmdbTv.getTVKeywords(tvID, appendToResponse); } /** - * Get the list of movies on an accounts watchlist. + * This method lets users rate a TV show. * - * @param sessionId - * @param accountId - * @return The watchlist of the user - * @throws MovieDbException + * A valid session id or guest session id is required. + * + * @param tvID + * @param rating + * @param sessionID + * @param guestSessionID + * @return + * @throws com.omertron.themoviedbapi.MovieDbException */ - public List getWatchList(String sessionId, int accountId) throws MovieDbException { - TmdbParameters parameters = new TmdbParameters(); - parameters.add(Param.SESSION, sessionId); - - URL url = new ApiUrl(apiKey, MethodBase.ACCOUNT).setSubMethod(accountId, MethodSub.MOVIE_WATCHLIST).buildUrl(parameters); - String webpage = httpTools.getRequest(url); - - try { - return mapper.readValue(webpage, WrapperMovie.class).getMovies(); - } catch (IOException ex) { - LOG.warn("Failed to get watch list: {}", ex.getMessage(), ex); - throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, webpage, url, ex); - } + public StatusCode postTVRating(int tvID, int rating, String sessionID, String guestSessionID) throws MovieDbException { + return tmdbTv.postTVRating(tvID, rating, sessionID, guestSessionID); } /** - * This method lets users delete a list that they created. A valid session id is required. + * Get the similar TV shows for a specific tv id. * - * @param sessionId - * @param listId + * @param tvID + * @param page + * @param language + * @param appendToResponse * @return - * @throws MovieDbException + * @throws com.omertron.themoviedbapi.MovieDbException */ - public StatusCode deleteMovieList(String sessionId, String listId) throws MovieDbException { - TmdbParameters parameters = new TmdbParameters(); - parameters.add(Param.ID, listId); - parameters.add(Param.SESSION, sessionId); - - URL url = new ApiUrl(apiKey, MethodBase.LIST).buildUrl(parameters); - String webpage = httpTools.deleteRequest(url); - - try { - return mapper.readValue(webpage, StatusCode.class); - } catch (IOException ex) { - LOG.warn("Failed to delete movie list: {}", ex.getMessage(), ex); - throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, webpage, url, ex); - } + public TmdbResultsList getTVSimilar(int tvID, Integer page, String language, String... appendToResponse) throws MovieDbException { + return tmdbTv.getTVSimilar(tvID, page, language, appendToResponse); } - // - // /** - * Get the basic information for a specific keyword id. + * Get the list of translations that exist for a TV series. These + * translations cascade down to the episode level. * - * @param keywordId + * @param tvID * @return - * @throws MovieDbException + * @throws com.omertron.themoviedbapi.MovieDbException */ - public Keyword getKeyword(String keywordId) throws MovieDbException { - TmdbParameters parameters = new TmdbParameters(); - parameters.add(Param.ID, keywordId); - - URL url = new ApiUrl(apiKey, MethodBase.KEYWORD).buildUrl(parameters); - String webpage = httpTools.getRequest(url); - - try { - return mapper.readValue(webpage, Keyword.class); - } catch (IOException ex) { - LOG.warn("Failed to get keyword: {}", ex.getMessage(), ex); - throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, webpage, url, ex); - } - + public TmdbResultsList getTVTranslations(int tvID) throws MovieDbException { + return tmdbTv.getTVTranslations(tvID); } /** - * Get the list of movies for a particular keyword by id. + * Get the videos that have been added to a TV series (trailers, opening + * credits, etc...) * - * @param keywordId + * @param tvID * @param language - * @param page - * @return List of movies with the keyword - * @throws MovieDbException + * @return + * @throws com.omertron.themoviedbapi.MovieDbException */ - public TmdbResultsList getKeywordMovies(String keywordId, String language, int page) throws MovieDbException { - TmdbParameters parameters = new TmdbParameters(); - parameters.add(Param.ID, keywordId); - parameters.add(Param.LANGUAGE, language); - parameters.add(Param.PAGE, page); - - URL url = new ApiUrl(apiKey, MethodBase.KEYWORD).setSubMethod(MethodSub.MOVIES).buildUrl(parameters); - String webpage = httpTools.getRequest(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 keyword movies: {}", ex.getMessage(), ex); - throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, webpage, url, ex); - } - + public TmdbResultsList - // /** - * Get a list of movie ids that have been edited. By default we show the last 24 hours and only 100 items per page. The maximum - * number of days that can be returned in a single request is 14. You can then use the movie changes API to get the actual data - * that has been changed. Please note that the change log system to support this was changed on October 5, 2012 and will only - * show movies that have been edited since. + * Get the latest TV show id. * - * @param page - * @param startDate the start date of the changes, optional - * @param endDate the end date of the changes, optional - * @return List of changed movie - * @throws MovieDbException + * @return + * @throws com.omertron.themoviedbapi.MovieDbException */ - public TmdbResultsList getMovieChangesList(int page, String startDate, String endDate) throws MovieDbException { - TmdbParameters params = new TmdbParameters(); - params.add(Param.PAGE, page); - params.add(Param.START_DATE, startDate); - params.add(Param.END_DATE, endDate); - - URL url = new ApiUrl(apiKey, MethodBase.MOVIE).setSubMethod(MethodSub.CHANGES).buildUrl(params); - String webpage = httpTools.getRequest(url); - - try { - WrapperMovieChanges wrapper = mapper.readValue(webpage, WrapperMovieChanges.class); - - TmdbResultsList results = new TmdbResultsList(wrapper.getResults()); - results.copyWrapper(wrapper); - return results; - } catch (IOException ex) { - LOG.warn("Failed to get movie changes: {}", ex.getMessage(), ex); - throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, webpage, url, ex); - } + public TVInfo getLatestTV() throws MovieDbException { + return tmdbTv.getLatestTV(); } - public void getPersonChangesList(int page, String startDate, String endDate) throws MovieDbException { - LOG.trace("getPersonChangesList: page: {}, start: {}, end: {}", page, startDate, endDate); - throw new MovieDbException(ApiExceptionType.UNKNOWN_CAUSE, "Not implemented yet", ""); + /** + * Get the list of TV shows that are currently on the air. + * + * This query looks for any TV show that has an episode with an air date in + * the next 7 days. + * + * @param page + * @param language + * @return + * @throws com.omertron.themoviedbapi.MovieDbException + */ + public TmdbResultsList getTVOnTheAir(Integer page, String language) throws MovieDbException { + return tmdbTv.getTVOnTheAir(page, language); } - // - // - public TmdbResultsList getJobs() throws MovieDbException { - URL url = new ApiUrl(apiKey, MethodBase.JOB).setSubMethod(MethodSub.LIST).buildUrl(); - String webpage = httpTools.getRequest(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(), ex); - throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, webpage, url, ex); - } + /** + * Get the list of TV shows that air today. + * + * Without a specified timezone, this query defaults to EST + * + * @param page + * @param language + * @param timezone + * @return + * @throws com.omertron.themoviedbapi.MovieDbException + */ + public TmdbResultsList getTVAiringToday(Integer page, String language, String timezone) throws MovieDbException { + return tmdbTv.getTVAiringToday(page, language, timezone); } - // - // /** - * 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. + * Get the list of top rated TV shows. * - * @param discover A discover object containing the search criteria required + * By default, this list will only include TV shows that have 2 or more + * votes. + * + * This list refreshes every day. + * + * @param page + * @param language * @return - * @throws MovieDbException + * @throws com.omertron.themoviedbapi.MovieDbException */ - public TmdbResultsList getDiscover(Discover discover) throws MovieDbException { - URL url = new ApiUrl(apiKey, MethodBase.DISCOVER).setSubMethod(MethodSub.MOVIE).buildUrl(discover.getParams()); - String webpage = httpTools.getRequest(url); - - try { - WrapperMovie wrapper = mapper.readValue(webpage, WrapperMovie.class); - TmdbResultsList results = new TmdbResultsList(wrapper.getMovies()); - results.copyWrapper(wrapper); - return results; - } catch (IOException ex) { - LOG.warn("Failed to get discover list: {}", ex.getMessage(), ex); - throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, webpage, url, ex); - } + public TmdbResultsList getTVTopRated(Integer page, String language) throws MovieDbException { + return tmdbTv.getTVTopRated(page, language); } - // /** - * Use Jackson to convert Map to JSON string. + * Get the list of popular TV shows. This list refreshes every day. * - * @param map + * @param page + * @param language * @return - * @throws MovieDbException + * @throws com.omertron.themoviedbapi.MovieDbException */ - public static String convertToJson(Map map) throws MovieDbException { - try { - return mapper.writeValueAsString(map); - } catch (JsonProcessingException jpe) { - throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, "JSON conversion failed", "", jpe); - } + public TmdbResultsList getTVPopular(Integer page, String language) throws MovieDbException { + return tmdbTv.getTVPopular(page, language); } + + // + // + // + // + // } diff --git a/src/main/java/com/omertron/themoviedbapi/enumeration/ArtworkType.java b/src/main/java/com/omertron/themoviedbapi/enumeration/ArtworkType.java new file mode 100644 index 000000000..3ade5fcb8 --- /dev/null +++ b/src/main/java/com/omertron/themoviedbapi/enumeration/ArtworkType.java @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2004-2015 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.enumeration; + +import org.apache.commons.lang3.StringUtils; + +/** + * ArtworkType enum List of the artwork types that are available + */ +public enum ArtworkType { + + /** + * Poster artwork + */ + POSTER, + /** + * Fanart/backdrop + */ + BACKDROP, + /** + * Person image + */ + PROFILE, + /** + * Still (Video image) + */ + STILL; + + /** + * Convert a string into an Enum type + * + * @param artworkType + * @return + * @throws IllegalArgumentException If type is not recognised + * + */ + public static ArtworkType fromString(String artworkType) { + if (StringUtils.isNotBlank(artworkType)) { + try { + return ArtworkType.valueOf(artworkType.trim().toUpperCase()); + } catch (IllegalArgumentException ex) { + throw new IllegalArgumentException("ArtworkType " + artworkType + " does not exist.", ex); + } + } + throw new IllegalArgumentException("ArtworkType must not be null"); + } +} diff --git a/src/main/java/com/omertron/themoviedbapi/model/PersonType.java b/src/main/java/com/omertron/themoviedbapi/enumeration/CreditType.java similarity index 80% rename from src/main/java/com/omertron/themoviedbapi/model/PersonType.java rename to src/main/java/com/omertron/themoviedbapi/enumeration/CreditType.java index daf8816ee..72ffd3b31 100644 --- a/src/main/java/com/omertron/themoviedbapi/model/PersonType.java +++ b/src/main/java/com/omertron/themoviedbapi/enumeration/CreditType.java @@ -17,17 +17,14 @@ * along with TheMovieDB API. If not, see . * */ -package com.omertron.themoviedbapi.model; +package com.omertron.themoviedbapi.enumeration; /** - * @author stuart.boston + * + * @author Stuart */ -public enum PersonType { +public enum CreditType { - // A member of the cast CAST, - // A member of the crew - CREW, - // No specific type - PERSON + CREW; } diff --git a/src/main/java/com/omertron/themoviedbapi/enumeration/ExternalSource.java b/src/main/java/com/omertron/themoviedbapi/enumeration/ExternalSource.java new file mode 100644 index 000000000..772e7255b --- /dev/null +++ b/src/main/java/com/omertron/themoviedbapi/enumeration/ExternalSource.java @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2004-2015 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.enumeration; + +/** + * + * @author Stuart + */ +public enum ExternalSource { + + FREEBASE_ID, + FREEBASE_MID, + IMDB_ID, + TVDB_ID, + TVRAGE_ID; + + public String getPropertyString() { + return this.name().toLowerCase(); + } + +} diff --git a/src/main/java/com/omertron/themoviedbapi/enumeration/MediaType.java b/src/main/java/com/omertron/themoviedbapi/enumeration/MediaType.java new file mode 100644 index 000000000..839a41ed6 --- /dev/null +++ b/src/main/java/com/omertron/themoviedbapi/enumeration/MediaType.java @@ -0,0 +1,62 @@ +/* + * Copyright (c) 2004-2015 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.enumeration; + +import org.apache.commons.lang3.StringUtils; + +/** + * Media type options + * + * @author Stuart + */ +public enum MediaType { + + /** + * Movie media type + */ + MOVIE, + /** + * TV Show media type + */ + TV, + /** + * TV Episode media type + */ + EPISODE; + + /** + * Convert a string into an Enum type + * + * @param mediaType + * @return + * @throws IllegalArgumentException If type is not recognised + * + */ + public static MediaType fromString(String mediaType) { + if (StringUtils.isNotBlank(mediaType)) { + try { + return MediaType.valueOf(mediaType.trim().toUpperCase()); + } catch (IllegalArgumentException ex) { + throw new IllegalArgumentException("MediaType " + mediaType + " does not exist.", ex); + } + } + throw new IllegalArgumentException("MediaType must not be null"); + } +} diff --git a/src/main/java/com/omertron/themoviedbapi/model/ArtworkType.java b/src/main/java/com/omertron/themoviedbapi/enumeration/SearchType.java similarity index 77% rename from src/main/java/com/omertron/themoviedbapi/model/ArtworkType.java rename to src/main/java/com/omertron/themoviedbapi/enumeration/SearchType.java index 091319f1a..c367bed08 100644 --- a/src/main/java/com/omertron/themoviedbapi/model/ArtworkType.java +++ b/src/main/java/com/omertron/themoviedbapi/enumeration/SearchType.java @@ -17,17 +17,19 @@ * along with TheMovieDB API. If not, see . * */ -package com.omertron.themoviedbapi.model; +package com.omertron.themoviedbapi.enumeration; /** - * ArtworkType enum List of the artwork types that are available + * + * @author Stuart */ -public enum ArtworkType { +public enum SearchType { + + PHRASE, + NGRAM; + + public String getPropertyString() { + return this.name().toLowerCase(); + } - // Poster artwork - POSTER, - // Fanart/backdrop - BACKDROP, - // Person image - PROFILE } diff --git a/src/main/java/com/omertron/themoviedbapi/enumeration/SortBy.java b/src/main/java/com/omertron/themoviedbapi/enumeration/SortBy.java new file mode 100644 index 000000000..b80de72d9 --- /dev/null +++ b/src/main/java/com/omertron/themoviedbapi/enumeration/SortBy.java @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2004-2015 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.enumeration; + +public enum SortBy { + + CREATED_AT_ASC, + CREATED_AT_DESC, + POPULARITY_ASC, + POPULARITY_DESC, + RELEASE_DATE_ASC, + RELEASE_DATE_DESC, + REVENUE_ASC, + REVENUE_DESC, + PRIMARY_RELEASE_DATE_ASC, + PRIMARY_RELEASE_DATE_DESC, + ORIGINAL_TITLE_ASC, + ORIGINAL_TITLE_DESC, + VOTE_AVERAGE_ASC, + VOTE_AVERAGE_DESC, + VOTE_COUNT_ASC, + VOTE_COUNT_DESC; + + public String getPropertyString() { + return this.name().toLowerCase().replace("_asc", ".asc").replaceAll("_desc", ".desc"); + } +} diff --git a/src/main/java/com/omertron/themoviedbapi/methods/AbstractMethod.java b/src/main/java/com/omertron/themoviedbapi/methods/AbstractMethod.java new file mode 100644 index 000000000..7766c4630 --- /dev/null +++ b/src/main/java/com/omertron/themoviedbapi/methods/AbstractMethod.java @@ -0,0 +1,159 @@ +/* + * Copyright (c) 2004-2015 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.methods; + +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.omertron.themoviedbapi.MovieDbException; +import com.omertron.themoviedbapi.model.artwork.ArtworkMedia; +import com.omertron.themoviedbapi.model.change.ChangeListItem; +import com.omertron.themoviedbapi.model.collection.Collection; +import com.omertron.themoviedbapi.model.company.Company; +import com.omertron.themoviedbapi.model.keyword.Keyword; +import com.omertron.themoviedbapi.model.list.UserList; +import com.omertron.themoviedbapi.model.media.AlternativeTitle; +import com.omertron.themoviedbapi.model.movie.MovieBasic; +import com.omertron.themoviedbapi.model.movie.MovieDb; +import com.omertron.themoviedbapi.model.person.ContentRating; +import com.omertron.themoviedbapi.model.person.Person; +import com.omertron.themoviedbapi.model.person.PersonFind; +import com.omertron.themoviedbapi.model.review.Review; +import com.omertron.themoviedbapi.model.tv.TVBasic; +import com.omertron.themoviedbapi.model.tv.TVInfo; +import com.omertron.themoviedbapi.tools.HttpTools; +import com.omertron.themoviedbapi.wrapper.WrapperGenericList; +import java.io.IOException; +import java.net.URL; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import org.yamj.api.common.exception.ApiExceptionType; + +/** + * Abstract methods + * + * @author Stuart + */ +public class AbstractMethod { + + // The API key to be used + protected final String apiKey; + // The HttpTools to use + protected final HttpTools httpTools; + // Jackson JSON configuration + protected static final ObjectMapper MAPPER = new ObjectMapper(); + private static final Map TYPE_REFS = new HashMap(); + + static { + TYPE_REFS.put(MovieBasic.class, new TypeReference>() { + }); + TYPE_REFS.put(TVBasic.class, new TypeReference>() { + }); + TYPE_REFS.put(UserList.class, new TypeReference>() { + }); + TYPE_REFS.put(Company.class, new TypeReference>() { + }); + TYPE_REFS.put(Collection.class, new TypeReference>() { + }); + TYPE_REFS.put(Keyword.class, new TypeReference>() { + }); + TYPE_REFS.put(MovieDb.class, new TypeReference>() { + }); + TYPE_REFS.put(Person.class, new TypeReference>() { + }); + TYPE_REFS.put(PersonFind.class, new TypeReference>() { + }); + TYPE_REFS.put(Review.class, new TypeReference>() { + }); + TYPE_REFS.put(ChangeListItem.class, new TypeReference>() { + }); + TYPE_REFS.put(ArtworkMedia.class, new TypeReference>() { + }); + TYPE_REFS.put(ContentRating.class, new TypeReference>() { + }); + TYPE_REFS.put(TVInfo.class, new TypeReference>() { + }); + TYPE_REFS.put(AlternativeTitle.class, new TypeReference>() { + }); + + } + + /** + * Default constructor for the methods + * + * @param apiKey + * @param httpTools + */ + public AbstractMethod(String apiKey, HttpTools httpTools) { + this.apiKey = apiKey; + this.httpTools = httpTools; + } + + /** + * Helper function to get a pre-generated TypeReference for a class + * + * @param aClass + * @return + * @throws MovieDbException + */ + protected static TypeReference getTypeReference(Class aClass) throws MovieDbException { + if (TYPE_REFS.containsKey(aClass)) { + return TYPE_REFS.get(aClass); + } else { + throw new MovieDbException(ApiExceptionType.UNKNOWN_CAUSE, "Class type reference for '" + aClass.getSimpleName() + "' not found!"); + } + } + + /** + * Process the wrapper list and return the results + * + * @param Type of list to process + * @param typeRef + * @param url URL of the page (Error output only) + * @param errorMessageSuffix Error message to output (Error output only) + * @return + * @throws MovieDbException + */ + protected List processWrapperList(TypeReference typeRef, URL url, String errorMessageSuffix) throws MovieDbException { + WrapperGenericList val = processWrapper(typeRef, url, errorMessageSuffix); + return val.getResults(); + } + + /** + * Process the wrapper list and return the whole wrapper + * + * @param Type of list to process + * @param typeRef + * @param url URL of the page (Error output only) + * @param errorMessageSuffix Error message to output (Error output only) + * @return + * @throws MovieDbException + */ + protected WrapperGenericList processWrapper(TypeReference typeRef, URL url, String errorMessageSuffix) throws MovieDbException { + String webpage = httpTools.getRequest(url); + try { + // Due to type erasure, this doesn't work + // TypeReference> typeRef = new TypeReference>() {}; + return MAPPER.readValue(webpage, typeRef); + } catch (IOException ex) { + throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, "Failed to get " + errorMessageSuffix, url, ex); + } + } +} diff --git a/src/main/java/com/omertron/themoviedbapi/methods/TmdbAccount.java b/src/main/java/com/omertron/themoviedbapi/methods/TmdbAccount.java new file mode 100644 index 000000000..7f5eab05c --- /dev/null +++ b/src/main/java/com/omertron/themoviedbapi/methods/TmdbAccount.java @@ -0,0 +1,318 @@ +/* + * Copyright (c) 2004-2015 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.methods; + +import com.omertron.themoviedbapi.MovieDbException; +import com.omertron.themoviedbapi.enumeration.MediaType; +import com.omertron.themoviedbapi.enumeration.SortBy; +import com.omertron.themoviedbapi.model.StatusCode; +import com.omertron.themoviedbapi.model.account.Account; +import com.omertron.themoviedbapi.model.list.UserList; +import com.omertron.themoviedbapi.model.movie.MovieBasic; +import com.omertron.themoviedbapi.model.tv.TVBasic; +import com.omertron.themoviedbapi.tools.ApiUrl; +import com.omertron.themoviedbapi.tools.HttpTools; +import com.omertron.themoviedbapi.tools.MethodBase; +import com.omertron.themoviedbapi.tools.MethodSub; +import com.omertron.themoviedbapi.tools.Param; +import com.omertron.themoviedbapi.tools.PostBody; +import com.omertron.themoviedbapi.tools.PostTools; +import com.omertron.themoviedbapi.tools.TmdbParameters; +import java.io.IOException; +import java.net.URL; +import java.util.List; +import org.yamj.api.common.exception.ApiExceptionType; + +/** + * Class to hold the Account Methods + * + * @author stuart.boston + */ +public class TmdbAccount extends AbstractMethod { + + /** + * Constructor + * + * @param apiKey + * @param httpTools + */ + public TmdbAccount(String apiKey, HttpTools httpTools) { + super(apiKey, httpTools); + } + + /** + * Get the basic information for an account. You will need to have a valid + * session id. + * + * @param sessionId + * @return + * @throws MovieDbException + */ + public Account getAccount(String sessionId) throws MovieDbException { + TmdbParameters parameters = new TmdbParameters(); + parameters.add(Param.SESSION_ID, sessionId); + + URL url = new ApiUrl(apiKey, MethodBase.ACCOUNT).buildUrl(parameters); + String webpage = httpTools.getRequest(url); + + try { + return MAPPER.readValue(webpage, Account.class); + } catch (IOException ex) { + throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, "Failed to get Account", url, ex); + } + } + + /** + * Get all lists of a given user + * + * @param sessionId + * @param accountId + * @return The lists + * @throws MovieDbException + */ + public List getUserLists(String sessionId, int accountId) throws MovieDbException { + TmdbParameters parameters = new TmdbParameters(); + parameters.add(Param.SESSION_ID, sessionId); + parameters.add(Param.ID, accountId); + + URL url = new ApiUrl(apiKey, MethodBase.ACCOUNT).subMethod(MethodSub.LISTS).buildUrl(parameters); + return processWrapperList(getTypeReference(UserList.class), url, "user list"); + } + + /** + * Get the list of favorite movies for an account. + * + * @param sessionId + * @param accountId + * @return + * @throws MovieDbException + */ + public List getFavoriteMovies(String sessionId, int accountId) throws MovieDbException { + TmdbParameters parameters = new TmdbParameters(); + parameters.add(Param.SESSION_ID, sessionId); + parameters.add(Param.ID, accountId); + + URL url = new ApiUrl(apiKey, MethodBase.ACCOUNT).subMethod(MethodSub.FAVORITE_MOVIES).buildUrl(parameters); + return processWrapperList(getTypeReference(MovieBasic.class), url, "favorite movies"); + } + + /** + * Get the list of favorite TV series for an account. + * + * @param sessionId + * @param accountId + * @return + * @throws MovieDbException + */ + public List getFavoriteTv(String sessionId, int accountId) throws MovieDbException { + TmdbParameters parameters = new TmdbParameters(); + parameters.add(Param.SESSION_ID, sessionId); + parameters.add(Param.ID, accountId); + + URL url = new ApiUrl(apiKey, MethodBase.ACCOUNT).subMethod(MethodSub.FAVORITE_TV).buildUrl(parameters); + return processWrapperList(getTypeReference(TVBasic.class), url, "favorite TV shows"); + } + + /** + * Add or remove a movie to an accounts favorite list. + * + * @param sessionId + * @param accountId + * @param mediaType + * @param mediaId + * @param setFavorite + * @return + * @throws MovieDbException + */ + public StatusCode modifyFavoriteStatus(String sessionId, int accountId, MediaType mediaType, int mediaId, boolean setFavorite) throws MovieDbException { + TmdbParameters parameters = new TmdbParameters(); + parameters.add(Param.SESSION_ID, sessionId); + parameters.add(Param.ID, accountId); + + String jsonBody = new PostTools() + .add(PostBody.MEDIA_TYPE, mediaType.toString().toLowerCase()) + .add(PostBody.MEDIA_ID, mediaId) + .add(PostBody.FAVORITE, setFavorite) + .build(); + + URL url = new ApiUrl(apiKey, MethodBase.ACCOUNT).subMethod(MethodSub.FAVORITE).buildUrl(parameters); + String webpage = httpTools.postRequest(url, jsonBody); + + try { + return MAPPER.readValue(webpage, StatusCode.class); + } catch (IOException ex) { + throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, "Failed to set favorite status", url, ex); + } + } + + /** + * Get the list of rated movies (and associated rating) for an account. + * + * @param sessionId + * @param accountId + * @param page + * @param sortBy + * @param language + * @return + * @throws MovieDbException + */ + public List getRatedMovies(String sessionId, int accountId, Integer page, String sortBy, String language) throws MovieDbException { + TmdbParameters parameters = new TmdbParameters(); + parameters.add(Param.SESSION_ID, sessionId); + parameters.add(Param.ID, accountId); + parameters.add(Param.PAGE, page); + parameters.add(Param.SORT_BY, sortBy); + parameters.add(Param.LANGUAGE, language); + + URL url = new ApiUrl(apiKey, MethodBase.ACCOUNT).subMethod(MethodSub.RATED_MOVIES).buildUrl(parameters); + return processWrapperList(getTypeReference(MovieBasic.class), url, "rated movies"); + } + + /** + * Get the list of rated TV shows (and associated rating) for an account. + * + * @param sessionId + * @param accountId + * @param page + * @param sortBy + * @param language + * @return + * @throws MovieDbException + */ + public List getRatedTV(String sessionId, int accountId, Integer page, String sortBy, String language) throws MovieDbException { + TmdbParameters parameters = new TmdbParameters(); + parameters.add(Param.SESSION_ID, sessionId); + parameters.add(Param.ID, accountId); + parameters.add(Param.PAGE, page); + parameters.add(Param.SORT_BY, sortBy); + parameters.add(Param.LANGUAGE, language); + + URL url = new ApiUrl(apiKey, MethodBase.ACCOUNT).subMethod(MethodSub.RATED_TV).buildUrl(parameters); + return processWrapperList(getTypeReference(TVBasic.class), url, "rated TV shows"); + } + + /** + * Get the list of movies on an accounts watch list. + * + * @param sessionId + * @param accountId + * @param page + * @param sortBy + * @param language + * @return The watch list of the user + * @throws MovieDbException + */ + public List getWatchListMovie(String sessionId, int accountId, Integer page, String sortBy, String language) throws MovieDbException { + TmdbParameters parameters = new TmdbParameters(); + parameters.add(Param.SESSION_ID, sessionId); + parameters.add(Param.ID, accountId); + parameters.add(Param.PAGE, page); + parameters.add(Param.SORT_BY, sortBy); + parameters.add(Param.LANGUAGE, language); + + URL url = new ApiUrl(apiKey, MethodBase.ACCOUNT).subMethod(MethodSub.WATCHLIST_MOVIES).buildUrl(parameters); + return processWrapperList(getTypeReference(MovieBasic.class), url, "movie watch list"); + } + + /** + * Get the list of movies on an accounts watch list. + * + * @param sessionId + * @param accountId + * @param page + * @param sortBy + * @param language + * @return The watch list of the user + * @throws MovieDbException + */ + public List getWatchListTV(String sessionId, int accountId, Integer page, String sortBy, String language) throws MovieDbException { + TmdbParameters parameters = new TmdbParameters(); + parameters.add(Param.SESSION_ID, sessionId); + parameters.add(Param.ID, accountId); + parameters.add(Param.PAGE, page); + parameters.add(Param.SORT_BY, sortBy); + parameters.add(Param.LANGUAGE, language); + + URL url = new ApiUrl(apiKey, MethodBase.ACCOUNT).subMethod(MethodSub.WATCHLIST_TV).buildUrl(parameters); + return processWrapperList(getTypeReference(TVBasic.class), url, "TV watch list"); + } + + /** + * Add or remove a movie to an accounts watch list. + * + * @param sessionId + * @param accountId + * @param movieId + * @param mediaType + * @param addToWatchlist + * @return + * @throws MovieDbException + */ + public StatusCode modifyWatchList(String sessionId, int accountId, MediaType mediaType, Integer movieId, boolean addToWatchlist) throws MovieDbException { + TmdbParameters parameters = new TmdbParameters(); + parameters.add(Param.SESSION_ID, sessionId); + parameters.add(Param.ID, accountId); + + String jsonBody = new PostTools() + .add(PostBody.MEDIA_TYPE, mediaType.toString().toLowerCase()) + .add(PostBody.MEDIA_ID, movieId) + .add(PostBody.WATCHLIST, addToWatchlist) + .build(); + + URL url = new ApiUrl(apiKey, MethodBase.ACCOUNT).subMethod(MethodSub.WATCHLIST).buildUrl(parameters); + String webpage = httpTools.postRequest(url, jsonBody); + + try { + return MAPPER.readValue(webpage, StatusCode.class); + } catch (IOException ex) { + throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, "Failed to modify watch list", url, ex); + } + } + + /** + * Get a list of rated movies for a specific guest session id. + * + * @param guestSessionId + * @param language + * @param page + * @param sortBy only CREATED_AT_ASC or CREATED_AT_DESC is supported + * @return + * @throws MovieDbException + */ + public List getGuestRatedMovies(String guestSessionId, String language, Integer page, SortBy sortBy) throws MovieDbException { + TmdbParameters parameters = new TmdbParameters(); + parameters.add(Param.ID, guestSessionId); + parameters.add(Param.LANGUAGE, language); + parameters.add(Param.PAGE, page); + if (sortBy != null) { + // Only created_at is supported + parameters.add(Param.SORT_BY, "created_at"); + + if (sortBy.getPropertyString().endsWith("asc")) { + parameters.add(Param.SORT_ORDER, "asc"); + } else { + parameters.add(Param.SORT_ORDER, "desc"); + } + } + + URL url = new ApiUrl(apiKey, MethodBase.GUEST_SESSION).subMethod(MethodSub.RATED_MOVIES_GUEST).buildUrl(parameters); + return processWrapperList(getTypeReference(MovieBasic.class), url, "Guest Session Movies"); + } +} diff --git a/src/main/java/com/omertron/themoviedbapi/methods/TmdbAuthentication.java b/src/main/java/com/omertron/themoviedbapi/methods/TmdbAuthentication.java new file mode 100644 index 000000000..247cc7dec --- /dev/null +++ b/src/main/java/com/omertron/themoviedbapi/methods/TmdbAuthentication.java @@ -0,0 +1,160 @@ +/* + * Copyright (c) 2004-2015 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.methods; + +import com.omertron.themoviedbapi.MovieDbException; +import com.omertron.themoviedbapi.model.authentication.TokenAuthorisation; +import com.omertron.themoviedbapi.model.authentication.TokenSession; +import com.omertron.themoviedbapi.tools.ApiUrl; +import com.omertron.themoviedbapi.tools.HttpTools; +import com.omertron.themoviedbapi.tools.MethodBase; +import com.omertron.themoviedbapi.tools.MethodSub; +import com.omertron.themoviedbapi.tools.Param; +import com.omertron.themoviedbapi.tools.TmdbParameters; +import java.io.IOException; +import java.net.URL; +import org.yamj.api.common.exception.ApiExceptionType; + +/** + * Class to hold the Authentication Methods + * + * @author stuart.boston + */ +public class TmdbAuthentication extends AbstractMethod { + + /** + * Constructor + * + * @param apiKey + * @param httpTools + */ + public TmdbAuthentication(String apiKey, HttpTools httpTools) { + super(apiKey, httpTools); + } + + /** + * 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. + * + * @return + * @throws MovieDbException + */ + public TokenAuthorisation getAuthorisationToken() throws MovieDbException { + TmdbParameters parameters = new TmdbParameters(); + URL url = new ApiUrl(apiKey, MethodBase.AUTH).subMethod(MethodSub.TOKEN_NEW).buildUrl(parameters); + + String webpage = httpTools.getRequest(url); + + try { + return MAPPER.readValue(webpage, TokenAuthorisation.class); + } catch (IOException ex) { + throw new MovieDbException(ApiExceptionType.AUTH_FAILURE, "Failed to get Authorisation Token", url, 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 + * @return + * @throws MovieDbException + */ + public TokenSession getSessionToken(TokenAuthorisation token) throws MovieDbException { + TmdbParameters parameters = new TmdbParameters(); + + if (!token.getSuccess()) { + throw new MovieDbException(ApiExceptionType.AUTH_FAILURE, "Authorisation token was not successful!"); + } + + parameters.add(Param.TOKEN, token.getRequestToken()); + URL url = new ApiUrl(apiKey, MethodBase.AUTH).subMethod(MethodSub.SESSION_NEW).buildUrl(parameters); + String webpage = httpTools.getRequest(url); + + try { + return MAPPER.readValue(webpage, TokenSession.class); + } catch (IOException ex) { + throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, "Failed to get Session Token", url, ex); + } + } + + /** + * This method is used to generate a session id for user based authentication. User must provide their username and password + * + * A session id is required in order to use any of the write methods. + * + * @param token Session token + * @param username User's username + * @param password User's password + * @return + * @throws MovieDbException + */ + public TokenAuthorisation getSessionTokenLogin(TokenAuthorisation token, String username, String password) throws MovieDbException { + TmdbParameters parameters = new TmdbParameters(); + + if (!token.getSuccess()) { + throw new MovieDbException(ApiExceptionType.AUTH_FAILURE, "Authorisation token was not successful!"); + } + + parameters.add(Param.TOKEN, token.getRequestToken()); + parameters.add(Param.USERNAME, username); + parameters.add(Param.PASSWORD, password); + + URL url = new ApiUrl(apiKey, MethodBase.AUTH).subMethod(MethodSub.TOKEN_VALIDATE).buildUrl(parameters); + String webpage = httpTools.getRequest(url); + + try { + return MAPPER.readValue(webpage, TokenAuthorisation.class); + } catch (IOException ex) { + throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, "Failed to get Session Token", url, 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. + * + * @return + * @throws MovieDbException + */ + public TokenSession getGuestSessionToken() throws MovieDbException { + URL url = new ApiUrl(apiKey, MethodBase.AUTH).subMethod(MethodSub.GUEST_SESSION).buildUrl(); + String webpage = httpTools.getRequest(url); + + try { + return MAPPER.readValue(webpage, TokenSession.class); + } catch (IOException ex) { + throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, "Failed to get Guest Session Token", url, ex); + } + }} diff --git a/src/main/java/com/omertron/themoviedbapi/methods/TmdbCertifications.java b/src/main/java/com/omertron/themoviedbapi/methods/TmdbCertifications.java new file mode 100644 index 000000000..addd2a02e --- /dev/null +++ b/src/main/java/com/omertron/themoviedbapi/methods/TmdbCertifications.java @@ -0,0 +1,93 @@ +/* + * Copyright (c) 2004-2015 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.methods; + +import com.omertron.themoviedbapi.MovieDbException; +import com.omertron.themoviedbapi.results.TmdbResultsMap; +import com.omertron.themoviedbapi.tools.HttpTools; +import java.util.List; +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.JsonNode; +import com.omertron.themoviedbapi.model.Certification; +import com.omertron.themoviedbapi.tools.ApiUrl; +import com.omertron.themoviedbapi.tools.MethodBase; +import com.omertron.themoviedbapi.tools.MethodSub; +import java.io.IOException; +import java.net.URL; +import java.util.Map; +import org.yamj.api.common.exception.ApiExceptionType; + +/** + * Class to hold the Certification Methods + * + * @author stuart.boston + */ +public class TmdbCertifications extends AbstractMethod { + + /** + * Constructor + * + * @param apiKey + * @param httpTools + */ + public TmdbCertifications(String apiKey, HttpTools httpTools) { + super(apiKey, httpTools); + } + + /** + * Get a list of movies certification. + * + * @return + * @throws MovieDbException + */ + public TmdbResultsMap> getMoviesCertification() throws MovieDbException { + URL url = new ApiUrl(apiKey, MethodBase.CERTIFICATION).subMethod(MethodSub.MOVIE_LIST).buildUrl(); + String webpage = httpTools.getRequest(url); + + try { + JsonNode node = MAPPER.readTree(webpage); + Map> results = MAPPER.readValue(node.elements().next().traverse(), new TypeReference>>() { + }); + return new TmdbResultsMap>(results); + } catch (IOException ex) { + throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, "Failed to get movie certifications", url, ex); + } + } + + /** + * Get a list of tv certification. + * + * @return + * @throws MovieDbException + */ + public TmdbResultsMap> getTvCertification() throws MovieDbException { + URL url = new ApiUrl(apiKey, MethodBase.CERTIFICATION).subMethod(MethodSub.TV_LIST).buildUrl(); + String webpage = httpTools.getRequest(url); + + try { + JsonNode node = MAPPER.readTree(webpage); + Map> results = MAPPER.readValue(node.elements().next().traverse(), new TypeReference>>() { + }); + return new TmdbResultsMap>(results); + } catch (IOException ex) { + throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, "Failed to get TV certifications", url, ex); + } + } +} diff --git a/src/main/java/com/omertron/themoviedbapi/methods/TmdbChanges.java b/src/main/java/com/omertron/themoviedbapi/methods/TmdbChanges.java new file mode 100644 index 000000000..2a83580cf --- /dev/null +++ b/src/main/java/com/omertron/themoviedbapi/methods/TmdbChanges.java @@ -0,0 +1,73 @@ +/* + * Copyright (c) 2004-2015 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.methods; + +import com.omertron.themoviedbapi.MovieDbException; +import com.omertron.themoviedbapi.model.change.ChangeListItem; +import com.omertron.themoviedbapi.tools.ApiUrl; +import com.omertron.themoviedbapi.tools.HttpTools; +import com.omertron.themoviedbapi.tools.MethodBase; +import com.omertron.themoviedbapi.tools.MethodSub; +import com.omertron.themoviedbapi.tools.Param; +import com.omertron.themoviedbapi.tools.TmdbParameters; +import java.net.URL; +import java.util.List; + +/** + * Class to hold the Change Methods + * + * @author stuart.boston + */ +public class TmdbChanges extends AbstractMethod { + + /** + * Constructor + * + * @param apiKey + * @param httpTools + */ + public TmdbChanges(String apiKey, HttpTools httpTools) { + super(apiKey, httpTools); + } + + /** + * Get a list of Media IDs that have been edited. + * + * You can then use the movie/TV/person changes API to get the actual data + * that has been changed. + * + * @param method The method base to get + * @param page + * @param startDate the start date of the changes, optional + * @param endDate the end date of the changes, optional + * @return List of changed movie + * @throws MovieDbException + */ + public List getChangeList(MethodBase method, Integer page, String startDate, String endDate) throws MovieDbException { + TmdbParameters params = new TmdbParameters(); + params.add(Param.PAGE, page); + params.add(Param.START_DATE, startDate); + params.add(Param.END_DATE, endDate); + + URL url = new ApiUrl(apiKey, method).subMethod(MethodSub.CHANGES).buildUrl(params); + return processWrapperList(getTypeReference(ChangeListItem.class), url, "changes"); + } + +} diff --git a/src/main/java/com/omertron/themoviedbapi/methods/TmdbCollections.java b/src/main/java/com/omertron/themoviedbapi/methods/TmdbCollections.java new file mode 100644 index 000000000..501961607 --- /dev/null +++ b/src/main/java/com/omertron/themoviedbapi/methods/TmdbCollections.java @@ -0,0 +1,107 @@ +/* + * Copyright (c) 2004-2015 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.methods; + +import com.omertron.themoviedbapi.MovieDbException; +import com.omertron.themoviedbapi.model.artwork.Artwork; +import com.omertron.themoviedbapi.enumeration.ArtworkType; +import com.omertron.themoviedbapi.model.collection.CollectionInfo; +import com.omertron.themoviedbapi.results.TmdbResultsList; +import com.omertron.themoviedbapi.tools.ApiUrl; +import com.omertron.themoviedbapi.tools.HttpTools; +import com.omertron.themoviedbapi.tools.MethodBase; +import com.omertron.themoviedbapi.tools.MethodSub; +import com.omertron.themoviedbapi.tools.Param; +import com.omertron.themoviedbapi.tools.TmdbParameters; +import com.omertron.themoviedbapi.wrapper.WrapperImages; +import java.io.IOException; +import java.net.URL; +import org.yamj.api.common.exception.ApiExceptionType; + +/** + * Class to hold the Collections Methods + * + * @author stuart.boston + */ +public class TmdbCollections extends AbstractMethod { + + /** + * Constructor + * + * @param apiKey + * @param httpTools + */ + public TmdbCollections(String apiKey, HttpTools httpTools) { + super(apiKey, httpTools); + } + + /** + * 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 { + TmdbParameters parameters = new TmdbParameters(); + parameters.add(Param.ID, collectionId); + parameters.add(Param.LANGUAGE, language); + + URL url = new ApiUrl(apiKey, MethodBase.COLLECTION).buildUrl(parameters); + String webpage = httpTools.getRequest(url); + + try { + return MAPPER.readValue(webpage, CollectionInfo.class); + } catch (IOException ex) { + throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, "Failed to get collection information", url, ex); + } + } + + /** + * Get all of the images for a particular collection by collection id. + * + * @param collectionId + * @param language + * @return + * @throws MovieDbException + */ + public TmdbResultsList getCollectionImages(int collectionId, String language) throws MovieDbException { + TmdbParameters parameters = new TmdbParameters(); + parameters.add(Param.ID, collectionId); + parameters.add(Param.LANGUAGE, language); + + URL url = new ApiUrl(apiKey, MethodBase.COLLECTION).subMethod(MethodSub.IMAGES).buildUrl(parameters); + String webpage = httpTools.getRequest(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) { + throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, "Failed to get collection images", url, ex); + } + } +} diff --git a/src/main/java/com/omertron/themoviedbapi/methods/TmdbCompanies.java b/src/main/java/com/omertron/themoviedbapi/methods/TmdbCompanies.java new file mode 100644 index 000000000..52ce18075 --- /dev/null +++ b/src/main/java/com/omertron/themoviedbapi/methods/TmdbCompanies.java @@ -0,0 +1,96 @@ +/* + * Copyright (c) 2004-2015 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.methods; + +import com.omertron.themoviedbapi.MovieDbException; +import com.omertron.themoviedbapi.model.company.Company; +import com.omertron.themoviedbapi.model.movie.MovieBasic; +import com.omertron.themoviedbapi.tools.ApiUrl; +import com.omertron.themoviedbapi.tools.HttpTools; +import com.omertron.themoviedbapi.tools.MethodBase; +import com.omertron.themoviedbapi.tools.MethodSub; +import com.omertron.themoviedbapi.tools.Param; +import com.omertron.themoviedbapi.tools.TmdbParameters; +import java.io.IOException; +import java.net.URL; +import java.util.List; +import org.yamj.api.common.exception.ApiExceptionType; + +/** + * Class to hold the Company Methods + * + * @author stuart.boston + */ +public class TmdbCompanies extends AbstractMethod { + + /** + * Constructor + * + * @param apiKey + * @param httpTools + */ + public TmdbCompanies(String apiKey, HttpTools httpTools) { + super(apiKey, httpTools); + } + + /** + * This method is used to retrieve the basic information about a production company on TMDb. + * + * @param companyId + * @return + * @throws MovieDbException + */ + public Company getCompanyInfo(int companyId) throws MovieDbException { + TmdbParameters parameters = new TmdbParameters(); + parameters.add(Param.ID, companyId); + + URL url = new ApiUrl(apiKey, MethodBase.COMPANY).buildUrl(parameters); + String webpage = httpTools.getRequest(url); + + try { + return MAPPER.readValue(webpage, Company.class); + } catch (IOException ex) { + throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, "Failed to get company information", url, 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. + * + * @param companyId + * @param language + * @param page + * @return + * @throws MovieDbException + */ + public List getCompanyMovies(int companyId, String language, Integer page) throws MovieDbException { + TmdbParameters parameters = new TmdbParameters(); + parameters.add(Param.ID, companyId); + parameters.add(Param.LANGUAGE, language); + parameters.add(Param.PAGE, page); + + URL url = new ApiUrl(apiKey, MethodBase.COMPANY).subMethod(MethodSub.MOVIES).buildUrl(parameters); + String webpage = httpTools.getRequest(url); + return processWrapperList(getTypeReference(MovieBasic.class), url, webpage); + } + +} diff --git a/src/main/java/com/omertron/themoviedbapi/methods/TmdbConfiguration.java b/src/main/java/com/omertron/themoviedbapi/methods/TmdbConfiguration.java new file mode 100644 index 000000000..11c5b89d5 --- /dev/null +++ b/src/main/java/com/omertron/themoviedbapi/methods/TmdbConfiguration.java @@ -0,0 +1,135 @@ +/* + * Copyright (c) 2004-2015 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.methods; + +import com.fasterxml.jackson.core.type.TypeReference; +import com.omertron.themoviedbapi.MovieDbException; +import static com.omertron.themoviedbapi.methods.AbstractMethod.MAPPER; +import com.omertron.themoviedbapi.model.config.Configuration; +import com.omertron.themoviedbapi.model.config.JobDepartment; +import com.omertron.themoviedbapi.results.TmdbResultsList; +import com.omertron.themoviedbapi.tools.ApiUrl; +import com.omertron.themoviedbapi.tools.HttpTools; +import com.omertron.themoviedbapi.tools.MethodBase; +import com.omertron.themoviedbapi.tools.MethodSub; +import com.omertron.themoviedbapi.wrapper.WrapperConfig; +import com.omertron.themoviedbapi.wrapper.WrapperJobList; +import java.io.IOException; +import java.net.URL; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import org.yamj.api.common.exception.ApiExceptionType; + +/** + * Class to hold the Configuration Methods + * + * @author stuart.boston + */ +public class TmdbConfiguration extends AbstractMethod { + + /** + * Cache the configuration in memory
+ * It rarely changes, so this should be safe. + */ + private static Configuration config = null; + + /** + * Constructor + * + * @param apiKey + * @param httpTools + */ + public TmdbConfiguration(String apiKey, HttpTools httpTools) { + super(apiKey, httpTools); + } + + /** + * Get the configuration
+ * If the configuration has been previously retrieved, use that instead + * + * @return + * @throws MovieDbException + */ + public Configuration getConfig() throws MovieDbException { + if (config == null) { + URL configUrl = new ApiUrl(apiKey, MethodBase.CONFIGURATION).buildUrl(); + String webpage = httpTools.getRequest(configUrl); + + try { + WrapperConfig wc = MAPPER.readValue(webpage, WrapperConfig.class); + config = wc.getTmdbConfiguration(); + } catch (IOException ex) { + throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, "Failed to read configuration", configUrl, ex); + } + } + return config; + } + + /** + * Get a list of valid jobs + * + * @return + * @throws MovieDbException + */ + public TmdbResultsList getJobs() throws MovieDbException { + URL url = new ApiUrl(apiKey, MethodBase.JOB).subMethod(MethodSub.LIST).buildUrl(); + String webpage = httpTools.getRequest(url); + + try { + WrapperJobList wrapper = MAPPER.readValue(webpage, WrapperJobList.class); + TmdbResultsList results = new TmdbResultsList(wrapper.getJobs()); + results.copyWrapper(wrapper); + return results; + } catch (IOException ex) { + throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, "Failed to get job list", url, ex); + } + } + + /** + * Get the list of supported timezones for the API methods that support them + * + * @return + * @throws MovieDbException + */ + public Map> getTimezones() throws MovieDbException { + URL url = new ApiUrl(apiKey, MethodBase.TIMEZONES).subMethod(MethodSub.LIST).buildUrl(); + String webpage = httpTools.getRequest(url); + + List>> tzList; + try { + tzList = MAPPER.readValue(webpage, new TypeReference>>>() { + }); + } catch (IOException ex) { + throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, "Failed to get timezone list", url, ex); + } + + Map> timezones = new HashMap>(); + + for (Map> tzMap : tzList) { + for (Map.Entry> x : tzMap.entrySet()) { + timezones.put(x.getKey(), x.getValue()); + } + } + + return timezones; + } + +} diff --git a/src/main/java/com/omertron/themoviedbapi/methods/TmdbCredits.java b/src/main/java/com/omertron/themoviedbapi/methods/TmdbCredits.java new file mode 100644 index 000000000..e0c6ac6c3 --- /dev/null +++ b/src/main/java/com/omertron/themoviedbapi/methods/TmdbCredits.java @@ -0,0 +1,84 @@ +/* + * Copyright (c) 2004-2015 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.methods; + +import com.omertron.themoviedbapi.MovieDbException; +import com.omertron.themoviedbapi.model.person.CreditInfo; +import com.omertron.themoviedbapi.tools.ApiUrl; +import com.omertron.themoviedbapi.tools.HttpTools; +import com.omertron.themoviedbapi.tools.MethodBase; +import com.omertron.themoviedbapi.tools.Param; +import com.omertron.themoviedbapi.tools.TmdbParameters; +import java.io.IOException; +import java.net.URL; +import org.yamj.api.common.exception.ApiExceptionType; + +/** + * Class to hold the Credit Methods + * + * @author stuart.boston + */ +public class TmdbCredits extends AbstractMethod { + + /** + * Constructor + * + * @param apiKey + * @param httpTools + */ + public TmdbCredits(String apiKey, HttpTools httpTools) { + super(apiKey, httpTools); + } + + /** + * Get the detailed information about a particular credit record. + *

+ * This is currently only supported with the new credit model found in TV. + *
+ * These IDs can be found from any TV credit response as well as the + * TV_credits and combined_credits methods for people.
+ * The episodes object returns a list of episodes and are generally going to + * be guest stars.
+ * The season array will return a list of season numbers.
+ * Season credits are credits that were marked with the "add to every + * season" option in the editing interface and are assumed to be "season + * regulars". + * + * @param creditId + * @param language + * @return + * @throws MovieDbException + */ + public CreditInfo getCreditInfo(String creditId, String language) throws MovieDbException { + TmdbParameters parameters = new TmdbParameters(); + parameters.add(Param.ID, creditId); + parameters.add(Param.LANGUAGE, language); + + URL url = new ApiUrl(apiKey, MethodBase.CREDIT).buildUrl(parameters); + String webpage = httpTools.getRequest(url); + + try { + return MAPPER.readValue(webpage, CreditInfo.class); + } catch (IOException ex) { + throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, "Failed to get credit info", url, ex); + } + } + +} diff --git a/src/main/java/com/omertron/themoviedbapi/methods/TmdbDiscover.java b/src/main/java/com/omertron/themoviedbapi/methods/TmdbDiscover.java new file mode 100644 index 000000000..625f5db07 --- /dev/null +++ b/src/main/java/com/omertron/themoviedbapi/methods/TmdbDiscover.java @@ -0,0 +1,75 @@ +/* + * Copyright (c) 2004-2015 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.methods; + +import com.omertron.themoviedbapi.MovieDbException; +import com.omertron.themoviedbapi.model.discover.Discover; +import com.omertron.themoviedbapi.model.movie.MovieBasic; +import com.omertron.themoviedbapi.model.tv.TVBasic; +import com.omertron.themoviedbapi.tools.ApiUrl; +import com.omertron.themoviedbapi.tools.HttpTools; +import com.omertron.themoviedbapi.tools.MethodBase; +import com.omertron.themoviedbapi.tools.MethodSub; +import java.net.URL; +import java.util.List; + +/** + * Class to hold the Discover Methods + * + * @author stuart.boston + */ +public class TmdbDiscover extends AbstractMethod { + + /** + * Constructor + * + * @param apiKey + * @param httpTools + */ + public TmdbDiscover(String apiKey, HttpTools httpTools) { + super(apiKey, httpTools); + } + + /** + * 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 List getDiscoverMovies(Discover discover) throws MovieDbException { + URL url = new ApiUrl(apiKey, MethodBase.DISCOVER).subMethod(MethodSub.MOVIE).buildUrl(discover.getParams()); + String webpage = httpTools.getRequest(url); + return processWrapperList(getTypeReference(MovieBasic.class), url, webpage); + } + + /** + * 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 List getDiscoverTV(Discover discover) throws MovieDbException { + URL url = new ApiUrl(apiKey, MethodBase.DISCOVER).subMethod(MethodSub.TV).buildUrl(discover.getParams()); + String webpage = httpTools.getRequest(url); + return processWrapperList(getTypeReference(TVBasic.class), url, webpage); + } +} diff --git a/src/main/java/com/omertron/themoviedbapi/methods/TmdbFind.java b/src/main/java/com/omertron/themoviedbapi/methods/TmdbFind.java new file mode 100644 index 000000000..0e9198875 --- /dev/null +++ b/src/main/java/com/omertron/themoviedbapi/methods/TmdbFind.java @@ -0,0 +1,87 @@ +/* + * Copyright (c) 2004-2015 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.methods; + +import com.omertron.themoviedbapi.MovieDbException; +import com.omertron.themoviedbapi.enumeration.ExternalSource; +import com.omertron.themoviedbapi.model.FindResults; +import com.omertron.themoviedbapi.tools.ApiUrl; +import com.omertron.themoviedbapi.tools.HttpTools; +import com.omertron.themoviedbapi.tools.MethodBase; +import com.omertron.themoviedbapi.tools.Param; +import com.omertron.themoviedbapi.tools.TmdbParameters; +import java.io.IOException; +import java.net.URL; +import org.yamj.api.common.exception.ApiExceptionType; + +/** + * Class to hold the Find Methods + * + * @author stuart.boston + */ +public class TmdbFind extends AbstractMethod { + + /** + * Constructor + * + * @param apiKey + * @param httpTools + */ + public TmdbFind(String apiKey, HttpTools httpTools) { + super(apiKey, httpTools); + } + + /** + * You con use this method to find movies, tv series or persons using external ids. + * + * Supported query ids are + *

    + *
  • Movies: imdb_id
  • + *
  • People: imdb_id, freebase_mid, freebase_id, tvrage_id
  • + *
  • TV Series: imdb_id, freebase_mid, freebase_id, tvdb_id, tvrage_id
  • + *
  • TV Seasons: freebase_mid, freebase_id, tvdb_id, tvrage_id
  • + *
  • TV Episodes: imdb_id, freebase_mid, freebase_id, tvdb_id, tvrage_idimdb_id, freebase_mid, freebase_id, tvrage_id, + * tvdb_id. + *
+ * + * For details see http://docs.themoviedb.apiary.io/#find + * + * @param id the external id + * @param externalSource one of {@link ExternalSource}. + * @param language the language + * @return + * @throws MovieDbException + */ + public FindResults find(String id, ExternalSource externalSource, String language) throws MovieDbException { + TmdbParameters parameters = new TmdbParameters(); + parameters.add(Param.ID, id); + parameters.add(Param.LANGUAGE, language); + parameters.add(Param.EXTERNAL_SOURCE, externalSource.getPropertyString()); + + URL url = new ApiUrl(apiKey, MethodBase.FIND).buildUrl(parameters); + String webpage = httpTools.getRequest(url); + + try { + return MAPPER.readValue(webpage, FindResults.class); + } catch (IOException ex) { + throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, "Failed to get find results", url, ex); + } + } +} diff --git a/src/main/java/com/omertron/themoviedbapi/methods/TmdbGenres.java b/src/main/java/com/omertron/themoviedbapi/methods/TmdbGenres.java new file mode 100644 index 000000000..ebc35917a --- /dev/null +++ b/src/main/java/com/omertron/themoviedbapi/methods/TmdbGenres.java @@ -0,0 +1,127 @@ +/* + * Copyright (c) 2004-2015 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.methods; + +import com.omertron.themoviedbapi.MovieDbException; +import com.omertron.themoviedbapi.model.Genre; +import com.omertron.themoviedbapi.model.movie.MovieBasic; +import com.omertron.themoviedbapi.results.TmdbResultsList; +import com.omertron.themoviedbapi.tools.ApiUrl; +import com.omertron.themoviedbapi.tools.HttpTools; +import com.omertron.themoviedbapi.tools.MethodBase; +import com.omertron.themoviedbapi.tools.MethodSub; +import com.omertron.themoviedbapi.tools.Param; +import com.omertron.themoviedbapi.tools.TmdbParameters; +import com.omertron.themoviedbapi.wrapper.WrapperGenres; +import java.io.IOException; +import java.net.URL; +import java.util.List; +import org.yamj.api.common.exception.ApiExceptionType; + +/** + * Class to hold the Genre Methods + * + * @author stuart.boston + */ +public class TmdbGenres extends AbstractMethod { + + /** + * Constructor + * + * @param apiKey + * @param httpTools + */ + public TmdbGenres(String apiKey, HttpTools httpTools) { + super(apiKey, httpTools); + } + + /** + * Get the list of movie genres. + * + * @param language + * @return + * @throws MovieDbException + */ + public TmdbResultsList getGenreMovieList(String language) throws MovieDbException { + return getGenreList(language, MethodSub.MOVIE_LIST); + } + + /** + * Get the list of TV genres. + * + * @param language + * @return + * @throws MovieDbException + */ + public TmdbResultsList getGenreTVList(String language) throws MovieDbException { + return getGenreList(language, MethodSub.TV_LIST); + } + + /** + * Get the list of genres for movies or TV + * @param language + * @param sub + * @return + * @throws MovieDbException + */ + private TmdbResultsList getGenreList(String language, MethodSub sub) throws MovieDbException { + TmdbParameters parameters = new TmdbParameters(); + parameters.add(Param.LANGUAGE, language); + + URL url = new ApiUrl(apiKey, MethodBase.GENRE).subMethod(sub).buildUrl(parameters); + String webpage = httpTools.getRequest(url); + + try { + WrapperGenres wrapper = MAPPER.readValue(webpage, WrapperGenres.class); + TmdbResultsList results = new TmdbResultsList(wrapper.getGenres()); + results.copyWrapper(wrapper); + return results; + } catch (IOException ex) { + throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, "Failed to get genre " + sub.toString(), url, ex); + } + } + + /** + * Get the list of movies for a particular genre by id. + * + * By default, only movies with 10 or more votes are included. + * + * @param genreId + * @param language + * @param page + * @param includeAllMovies + * @param includeAdult + * @return + * @throws MovieDbException + */ + public List getGenreMovies(int genreId, String language, Integer page, Boolean includeAllMovies, Boolean includeAdult) throws MovieDbException { + TmdbParameters parameters = new TmdbParameters(); + parameters.add(Param.ID, genreId); + parameters.add(Param.LANGUAGE, language); + parameters.add(Param.PAGE, page); + parameters.add(Param.INCLUDE_ALL_MOVIES, includeAllMovies); + parameters.add(Param.INCLUDE_ADULT, includeAdult); + + URL url = new ApiUrl(apiKey, MethodBase.GENRE).subMethod(MethodSub.MOVIES).buildUrl(parameters); + String webpage = httpTools.getRequest(url); + + return processWrapperList(getTypeReference(MovieBasic.class), url, webpage); + } +} diff --git a/src/main/java/com/omertron/themoviedbapi/methods/TmdbKeywords.java b/src/main/java/com/omertron/themoviedbapi/methods/TmdbKeywords.java new file mode 100644 index 000000000..ba049dce4 --- /dev/null +++ b/src/main/java/com/omertron/themoviedbapi/methods/TmdbKeywords.java @@ -0,0 +1,96 @@ +/* + * Copyright (c) 2004-2015 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.methods; + +import com.omertron.themoviedbapi.MovieDbException; +import com.omertron.themoviedbapi.model.keyword.Keyword; +import com.omertron.themoviedbapi.model.movie.MovieBasic; +import com.omertron.themoviedbapi.results.TmdbResultsList; +import com.omertron.themoviedbapi.tools.ApiUrl; +import com.omertron.themoviedbapi.tools.HttpTools; +import com.omertron.themoviedbapi.tools.MethodBase; +import com.omertron.themoviedbapi.tools.MethodSub; +import com.omertron.themoviedbapi.tools.Param; +import com.omertron.themoviedbapi.tools.TmdbParameters; +import com.omertron.themoviedbapi.wrapper.WrapperGenericList; +import java.io.IOException; +import java.net.URL; +import org.yamj.api.common.exception.ApiExceptionType; + +/** + * Class to hold the Keyword Methods + * + * @author stuart.boston + */ +public class TmdbKeywords extends AbstractMethod { + + /** + * Constructor + * + * @param apiKey + * @param httpTools + */ + public TmdbKeywords(String apiKey, HttpTools httpTools) { + super(apiKey, httpTools); + } + + /** + * Get the basic information for a specific keyword id. + * + * @param keywordId + * @return + * @throws MovieDbException + */ + public Keyword getKeyword(String keywordId) throws MovieDbException { + TmdbParameters parameters = new TmdbParameters(); + parameters.add(Param.ID, keywordId); + + URL url = new ApiUrl(apiKey, MethodBase.KEYWORD).buildUrl(parameters); + String webpage = httpTools.getRequest(url); + + try { + return MAPPER.readValue(webpage, Keyword.class); + } catch (IOException ex) { + throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, "Failed to get keyword " + keywordId, url, 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, Integer page) throws MovieDbException { + TmdbParameters parameters = new TmdbParameters(); + parameters.add(Param.ID, keywordId); + parameters.add(Param.LANGUAGE, language); + parameters.add(Param.PAGE, page); + + URL url = new ApiUrl(apiKey, MethodBase.KEYWORD).subMethod(MethodSub.MOVIES).buildUrl(parameters); + WrapperGenericList wrapper = processWrapper(getTypeReference(MovieBasic.class), url, "keyword movies"); + return wrapper.getTmdbResultsList(); + } + +} diff --git a/src/main/java/com/omertron/themoviedbapi/methods/TmdbLists.java b/src/main/java/com/omertron/themoviedbapi/methods/TmdbLists.java new file mode 100644 index 000000000..9d316351c --- /dev/null +++ b/src/main/java/com/omertron/themoviedbapi/methods/TmdbLists.java @@ -0,0 +1,246 @@ +/* + * Copyright (c) 2004-2015 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.methods; + +import com.fasterxml.jackson.core.type.TypeReference; +import com.omertron.themoviedbapi.MovieDbException; +import com.omertron.themoviedbapi.model.list.ListStatusCode; +import com.omertron.themoviedbapi.model.StatusCode; +import com.omertron.themoviedbapi.model.list.ListItem; +import com.omertron.themoviedbapi.model.list.ListItemStatus; +import com.omertron.themoviedbapi.model.movie.MovieDb; +import com.omertron.themoviedbapi.tools.ApiUrl; +import com.omertron.themoviedbapi.tools.HttpTools; +import com.omertron.themoviedbapi.tools.MethodBase; +import com.omertron.themoviedbapi.tools.MethodSub; +import com.omertron.themoviedbapi.tools.Param; +import com.omertron.themoviedbapi.tools.PostBody; +import com.omertron.themoviedbapi.tools.PostTools; +import com.omertron.themoviedbapi.tools.TmdbParameters; +import java.io.IOException; +import java.net.URL; +import org.apache.commons.lang3.StringUtils; +import org.yamj.api.common.exception.ApiExceptionType; + +/** + * Class to hold the List Methods + * + * @author stuart.boston + */ +public class TmdbLists extends AbstractMethod { + + /** + * Constructor + * + * @param apiKey + * @param httpTools + */ + public TmdbLists(String apiKey, HttpTools httpTools) { + super(apiKey, httpTools); + } + + /** + * Get a list by its ID + * + * @param listId + * @return The list and its items + * @throws MovieDbException + */ + public ListItem getList(String listId) throws MovieDbException { + TmdbParameters parameters = new TmdbParameters(); + parameters.add(Param.ID, listId); + + URL url = new ApiUrl(apiKey, MethodBase.LIST).buildUrl(parameters); + String webpage = httpTools.getRequest(url); + + try { + return MAPPER.readValue(webpage, new TypeReference>() { + }); + } catch (IOException ex) { + throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, "Failed to get list", url, ex); + } + } + + /** + * Check to see if an ID is already on a list. + * + * @param listId + * @param mediaId + * @return true if the movie is on the list + * @throws MovieDbException + */ + public boolean checkItemStatus(String listId, int mediaId) throws MovieDbException { + TmdbParameters parameters = new TmdbParameters(); + parameters.add(Param.ID, listId); + parameters.add(Param.MOVIE_ID, mediaId); + + URL url = new ApiUrl(apiKey, MethodBase.LIST).subMethod(MethodSub.ITEM_STATUS).buildUrl(parameters); + String webpage = httpTools.getRequest(url); + + try { + return MAPPER.readValue(webpage, ListItemStatus.class).isItemPresent(); + } catch (IOException ex) { + throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, "Failed to get item status", url, ex); + } + } + + /** + * This method lets users create a new list. A valid session id is required. + * + * @param sessionId + * @param name + * @param description + * @return The list id + * @throws MovieDbException + */ + public String createList(String sessionId, String name, String description) throws MovieDbException { + TmdbParameters parameters = new TmdbParameters(); + parameters.add(Param.SESSION_ID, sessionId); + + String jsonBody = new PostTools() + .add(PostBody.NAME, StringUtils.trimToEmpty(name)) + .add(PostBody.DESCRIPTION, StringUtils.trimToEmpty(description)) + .build(); + + URL url = new ApiUrl(apiKey, MethodBase.LIST).buildUrl(parameters); + String webpage = httpTools.postRequest(url, jsonBody); + + try { + return MAPPER.readValue(webpage, ListStatusCode.class).getListId(); + } catch (IOException ex) { + throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, "Failed to create list", url, ex); + } + } + + /** + * This method lets users delete a list that they created. A valid session id is required. + * + * @param sessionId + * @param listId + * @return + * @throws MovieDbException + */ + public StatusCode deleteList(String sessionId, String listId) throws MovieDbException { + TmdbParameters parameters = new TmdbParameters(); + parameters.add(Param.ID, listId); + parameters.add(Param.SESSION_ID, sessionId); + + URL url = new ApiUrl(apiKey, MethodBase.LIST).buildUrl(parameters); + String webpage = httpTools.deleteRequest(url); + + try { + return MAPPER.readValue(webpage, StatusCode.class); + } catch (IOException ex) { + throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, "Failed to delete list", url, ex); + } + } + + /** + * Modify a list + * + * This can be used to add or remove an item from the list + * + * @param sessionId + * @param listId + * @param movieId + * @param operation + * @return + * @throws MovieDbException + */ + private StatusCode modifyMovieList(String sessionId, String listId, int movieId, MethodSub operation) throws MovieDbException { + TmdbParameters parameters = new TmdbParameters(); + parameters.add(Param.SESSION_ID, sessionId); + parameters.add(Param.ID, listId); + + String jsonBody = new PostTools() + .add(PostBody.MEDIA_ID, movieId) + .build(); + + URL url = new ApiUrl(apiKey, MethodBase.LIST).subMethod(operation).buildUrl(parameters); + String webpage = httpTools.postRequest(url, jsonBody); + + try { + return MAPPER.readValue(webpage, StatusCode.class); + } catch (IOException ex) { + throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, "Failed to remove item from list", url, ex); + } + } + + /** + * This method lets users add new items to a list that they created. + * + * A valid session id is required. + * + * @param sessionId + * @param listId + * @param mediaId + * @return + * @throws MovieDbException + */ + public StatusCode addItem(String sessionId, String listId, int mediaId) throws MovieDbException { + return modifyMovieList(sessionId, listId, mediaId, MethodSub.ADD_ITEM); + } + + /** + * This method lets users delete items from a list that they created. + * + * A valid session id is required. + * + * @param sessionId + * @param listId + * @param mediaId + * @return + * @throws MovieDbException + */ + public StatusCode removeItem(String sessionId, String listId, int mediaId) throws MovieDbException { + return modifyMovieList(sessionId, listId, mediaId, MethodSub.REMOVE_ITEM); + } + + /** + * Clear all of the items within a list. + * + * This is a irreversible action and should be treated with caution. + * + * A valid session id is required. A call without confirm=true will return status code 29. + * + * @param sessionId + * @param listId + * @param confirm + * @return + * @throws MovieDbException + */ + public StatusCode clear(String sessionId, String listId, boolean confirm) throws MovieDbException { + TmdbParameters parameters = new TmdbParameters(); + parameters.add(Param.SESSION_ID, sessionId); + parameters.add(Param.ID, listId); + parameters.add(Param.CONFIRM, confirm); + + URL url = new ApiUrl(apiKey, MethodBase.LIST).subMethod(MethodSub.CLEAR).buildUrl(parameters); + String webpage = httpTools.postRequest(url, ""); + + try { + return MAPPER.readValue(webpage, StatusCode.class); + } catch (IOException ex) { + throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, "Failed to clear list", url, ex); + } + + } + +} diff --git a/src/main/java/com/omertron/themoviedbapi/methods/TmdbMovies.java b/src/main/java/com/omertron/themoviedbapi/methods/TmdbMovies.java new file mode 100644 index 000000000..cd4cd38e2 --- /dev/null +++ b/src/main/java/com/omertron/themoviedbapi/methods/TmdbMovies.java @@ -0,0 +1,607 @@ +/* + * Copyright (c) 2004-2015 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.methods; + +import com.omertron.themoviedbapi.MovieDbException; +import static com.omertron.themoviedbapi.methods.AbstractMethod.MAPPER; +import com.omertron.themoviedbapi.model.StatusCode; +import com.omertron.themoviedbapi.model.artwork.Artwork; +import com.omertron.themoviedbapi.model.keyword.Keyword; +import com.omertron.themoviedbapi.model.list.UserList; +import com.omertron.themoviedbapi.model.media.MediaCreditList; +import com.omertron.themoviedbapi.model.media.MediaState; +import com.omertron.themoviedbapi.model.media.AlternativeTitle; +import com.omertron.themoviedbapi.model.movie.MovieDb; +import com.omertron.themoviedbapi.model.movie.ReleaseInfo; +import com.omertron.themoviedbapi.model.media.Translation; +import com.omertron.themoviedbapi.model.media.Video; +import com.omertron.themoviedbapi.model.review.Review; +import com.omertron.themoviedbapi.results.TmdbResultsList; +import com.omertron.themoviedbapi.tools.ApiUrl; +import com.omertron.themoviedbapi.tools.HttpTools; +import com.omertron.themoviedbapi.tools.MethodBase; +import com.omertron.themoviedbapi.tools.MethodSub; +import com.omertron.themoviedbapi.tools.Param; +import com.omertron.themoviedbapi.tools.PostBody; +import com.omertron.themoviedbapi.tools.PostTools; +import com.omertron.themoviedbapi.tools.TmdbParameters; +import com.omertron.themoviedbapi.wrapper.WrapperAlternativeTitles; +import com.omertron.themoviedbapi.wrapper.WrapperChanges; +import com.omertron.themoviedbapi.wrapper.WrapperGenericList; +import com.omertron.themoviedbapi.wrapper.WrapperImages; +import com.omertron.themoviedbapi.wrapper.WrapperMovieKeywords; +import com.omertron.themoviedbapi.wrapper.WrapperReleaseInfo; +import com.omertron.themoviedbapi.wrapper.WrapperTranslations; +import com.omertron.themoviedbapi.wrapper.WrapperVideos; +import java.io.IOException; +import java.net.URL; +import org.yamj.api.common.exception.ApiExceptionType; + +/** + * Class to hold the Movie Methods + * + * @author stuart.boston + */ +public class TmdbMovies extends AbstractMethod { + + private static final int RATING_MAX = 10; + + /** + * Constructor + * + * @param apiKey + * @param httpTools + */ + public TmdbMovies(String apiKey, HttpTools httpTools) { + super(apiKey, httpTools); + } + + /** + * This method is used to retrieve all of the basic movie information. + * + * It will return the single highest rated poster and backdrop. + * + * ApiExceptionType.MOVIE_ID_NOT_FOUND will be thrown if there are no movies + * found. + * + * @param movieId + * @param language + * @param appendToResponse + * @return + * @throws MovieDbException + */ + public MovieDb getMovieInfo(int movieId, String language, String... appendToResponse) throws MovieDbException { + TmdbParameters parameters = new TmdbParameters(); + parameters.add(Param.ID, movieId); + parameters.add(Param.LANGUAGE, language); + parameters.add(Param.APPEND, appendToResponse); + + URL url = new ApiUrl(apiKey, MethodBase.MOVIE).buildUrl(parameters); + String webpage = httpTools.getRequest(url); + try { + MovieDb movie = MAPPER.readValue(webpage, MovieDb.class); + if (movie == null || movie.getId() == 0) { + throw new MovieDbException(ApiExceptionType.ID_NOT_FOUND, "No movie found for ID: " + movieId, url); + } + return movie; + } catch (IOException ex) { + throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, "Failed to get info", url, ex); + } + } + + /** + * This method is used to retrieve all of the basic movie information. + * + * It will return the single highest rated poster and backdrop. + * + * ApiExceptionType.MOVIE_ID_NOT_FOUND will be thrown if there are no movies + * found. + * + * @param imdbId + * @param language + * @param appendToResponse + * @return + * @throws MovieDbException + */ + public MovieDb getMovieInfoImdb(String imdbId, String language, String... appendToResponse) throws MovieDbException { + TmdbParameters parameters = new TmdbParameters(); + parameters.add(Param.ID, imdbId); + parameters.add(Param.LANGUAGE, language); + parameters.add(Param.APPEND, appendToResponse); + + URL url = new ApiUrl(apiKey, MethodBase.MOVIE).buildUrl(parameters); + String webpage = httpTools.getRequest(url); + + try { + MovieDb movie = MAPPER.readValue(webpage, MovieDb.class); + if (movie == null || movie.getId() == 0) { + throw new MovieDbException(ApiExceptionType.ID_NOT_FOUND, "No movie found for IMDB ID: " + imdbId, url); + } + return movie; + } catch (IOException ex) { + throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, "Failed to get info (IMDB)", url, ex); + } + } + + /** + * This method lets a user get the status of whether or not the movie has + * been rated or added to their favourite or movie watch list. + * + * A valid session id is required. + * + * @param movieId + * @param sessionId + * @return + * @throws MovieDbException + */ + public MediaState getMovieAccountState(int movieId, String sessionId) throws MovieDbException { + TmdbParameters parameters = new TmdbParameters(); + parameters.add(Param.ID, movieId); + parameters.add(Param.SESSION_ID, sessionId); + + URL url = new ApiUrl(apiKey, MethodBase.MOVIE).subMethod(MethodSub.ACCOUNT_STATES).buildUrl(parameters); + String webpage = httpTools.getRequest(url); + + try { + return MAPPER.readValue(webpage, MediaState.class); + } catch (IOException ex) { + throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, "Failed to get account state", url, ex); + } + } + + /** + * This method is used to retrieve all of the alternative titles we have for + * a particular movie. + * + * @param movieId + * @param country + * @param appendToResponse + * @return + * @throws MovieDbException + */ + public TmdbResultsList getMovieAlternativeTitles(int movieId, String country, String... appendToResponse) throws MovieDbException { + TmdbParameters parameters = new TmdbParameters(); + parameters.add(Param.ID, movieId); + parameters.add(Param.COUNTRY, country); + parameters.add(Param.APPEND, appendToResponse); + + URL url = new ApiUrl(apiKey, MethodBase.MOVIE).subMethod(MethodSub.ALT_TITLES).buildUrl(parameters); + String webpage = httpTools.getRequest(url); + try { + WrapperAlternativeTitles wrapper = MAPPER.readValue(webpage, WrapperAlternativeTitles.class); + TmdbResultsList results = new TmdbResultsList(wrapper.getTitles()); + results.copyWrapper(wrapper); + return results; + } catch (IOException ex) { + throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, "Failed to get alternative titles", url, ex); + } + } + + /** + * Get the cast and crew information for a specific movie id. + * + * @param movieId + * @param appendToResponse + * @return + * @throws MovieDbException + */ + public MediaCreditList getMovieCredits(int movieId, String... appendToResponse) throws MovieDbException { + TmdbParameters parameters = new TmdbParameters(); + parameters.add(Param.ID, movieId); + parameters.add(Param.APPEND, appendToResponse); + + URL url = new ApiUrl(apiKey, MethodBase.MOVIE).subMethod(MethodSub.CREDITS).buildUrl(parameters); + String webpage = httpTools.getRequest(url); + try { + return MAPPER.readValue(webpage, MediaCreditList.class); + } catch (IOException ex) { + throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, "Failed to get credits", url, ex); + } + } + + /** + * This method should be used when you’re wanting to retrieve all of the + * images for a particular movie. + * + * @param movieId + * @param language + * @param appendToResponse + * @return + * @throws MovieDbException + */ + public TmdbResultsList getMovieImages(int movieId, String language, String... appendToResponse) throws MovieDbException { + TmdbParameters parameters = new TmdbParameters(); + parameters.add(Param.ID, movieId); + parameters.add(Param.LANGUAGE, language); + parameters.add(Param.APPEND, appendToResponse); + + URL url = new ApiUrl(apiKey, MethodBase.MOVIE).subMethod(MethodSub.IMAGES).buildUrl(parameters); + String webpage = httpTools.getRequest(url); + + try { + WrapperImages wrapper = MAPPER.readValue(webpage, WrapperImages.class); + TmdbResultsList results = new TmdbResultsList(wrapper.getAll()); + results.copyWrapper(wrapper); + return results; + } catch (IOException ex) { + throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, "Failed to get images", url, 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 + * @param appendToResponse + * @return + * @throws MovieDbException + */ + public TmdbResultsList getMovieKeywords(int movieId, String... appendToResponse) throws MovieDbException { + TmdbParameters parameters = new TmdbParameters(); + parameters.add(Param.ID, movieId); + parameters.add(Param.APPEND, appendToResponse); + + URL url = new ApiUrl(apiKey, MethodBase.MOVIE).subMethod(MethodSub.KEYWORDS).buildUrl(parameters); + String webpage = httpTools.getRequest(url); + + try { + WrapperMovieKeywords wrapper = MAPPER.readValue(webpage, WrapperMovieKeywords.class); + TmdbResultsList results = new TmdbResultsList(wrapper.getKeywords()); + results.copyWrapper(wrapper); + return results; + } catch (IOException ex) { + throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, "Failed to get keywords", url, ex); + } + } + + /** + * This method is used to retrieve all of the release and certification data + * we have for a specific movie. + * + * @param movieId + * @param language + * @param appendToResponse + * @return + * @throws MovieDbException + */ + public TmdbResultsList getMovieReleaseInfo(int movieId, String language, String... appendToResponse) throws MovieDbException { + TmdbParameters parameters = new TmdbParameters(); + parameters.add(Param.ID, movieId); + parameters.add(Param.LANGUAGE, language); + parameters.add(Param.APPEND, appendToResponse); + + URL url = new ApiUrl(apiKey, MethodBase.MOVIE).subMethod(MethodSub.RELEASES).buildUrl(parameters); + String webpage = httpTools.getRequest(url); + + try { + WrapperReleaseInfo wrapper = MAPPER.readValue(webpage, WrapperReleaseInfo.class); + TmdbResultsList results = new TmdbResultsList(wrapper.getCountries()); + results.copyWrapper(wrapper); + return results; + } catch (IOException ex) { + throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, "Failed to get release information", url, 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 + * @param appendToResponse + * @return + * @throws MovieDbException + */ + public TmdbResultsList