diff --git a/src/main/java/com/omertron/themoviedbapi/TheMovieDbApi.java b/src/main/java/com/omertron/themoviedbapi/TheMovieDbApi.java
index 9a7a9d5eb..1b4c6c7b4 100644
--- a/src/main/java/com/omertron/themoviedbapi/TheMovieDbApi.java
+++ b/src/main/java/com/omertron/themoviedbapi/TheMovieDbApi.java
@@ -21,6 +21,27 @@ package com.omertron.themoviedbapi;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
+import com.omertron.themoviedbapi.enumeration.MediaType;
+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.TmdbJobs;
+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.*;
import com.omertron.themoviedbapi.results.TmdbResultsList;
import com.omertron.themoviedbapi.results.TmdbResultsMap;
@@ -56,15 +77,36 @@ public class TheMovieDbApi {
private static final Logger LOG = LoggerFactory.getLogger(TheMovieDbApi.class);
private String apiKey;
- private TmdbConfiguration tmdbConfig;
+ private Configuration tmdbConfig;
private HttpTools httpTools;
// Jackson JSON configuration
- private static ObjectMapper mapper = new ObjectMapper();
+ private static final 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 TmdbJobs tmdbJobs;
+ private static TmdbKeywords tmdbKeyword;
+ 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.
@@ -91,13 +133,42 @@ public class TheMovieDbApi {
String webpage = httpTools.getRequest(configUrl);
try {
- WrapperConfig wc = mapper.readValue(webpage, WrapperConfig.class);
+ 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 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);
+ tmdbJobs = new TmdbJobs(apiKey, httpTools);
+ tmdbKeyword = 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);
+ }
+
/**
* Compare the MovieDB object with a title & year
*
@@ -195,13 +266,12 @@ public class TheMovieDbApi {
return StringUtils.isNotBlank(year) && !"UNKNOWN".equals(year);
}
- //
/**
* Get the configuration information
*
* @return
*/
- public TmdbConfiguration getConfiguration() {
+ public Configuration getConfiguration() {
return tmdbConfig;
}
@@ -228,238 +298,7 @@ public class TheMovieDbApi {
throw new MovieDbException(ApiExceptionType.INVALID_URL, sb.toString(), "", ex);
}
}
- //
- //
- /**
- * This method is used to generate a valid request token for user based authentication.
- *
- * A request token is required in order to request a session id.
- *
- * You can generate any number of request tokens but they will expire after 60 minutes.
- *
- * As soon as a valid session id has been created the token will be destroyed.
- *
- * @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);
- }
- }
-
- /**
- * 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()) {
- 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);
- }
- }
-
- /**
- * 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()) {
- LOG.warn("Session token was not successful!");
- 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).setSubMethod(MethodSub.TOKEN_VALIDATE).buildUrl(parameters);
- String webpage = httpTools.getRequest(url);
-
- 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);
- }
- }
-
- /**
- * 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).setSubMethod(MethodSub.GUEST_SESSION).buildUrl();
- String webpage = httpTools.getRequest(url);
-
- 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 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, 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) {
- LOG.warn("Failed to get Account: {}", ex.getMessage(), ex);
- throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, webpage, url, ex);
- }
- }
-
- /**
- * Get the account favourite movies
- *
- * @param sessionId
- * @param accountId
- * @return
- * @throws MovieDbException
- */
- public List getFavoriteMovies(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.FAVORITE_MOVIES).buildUrl(parameters);
- String webpage = httpTools.getRequest(url);
-
- 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);
- }
- }
-
- public StatusCode changeFavoriteStatus(String sessionId, int accountId, Integer movieId, boolean isFavorite) throws MovieDbException {
- TmdbParameters parameters = new TmdbParameters();
- parameters.add(Param.SESSION, sessionId);
-
- Map body = new HashMap();
- body.put(MOVIE_ID, movieId);
- body.put("favorite", isFavorite);
- String jsonBody = convertToJson(body);
-
- URL url = new ApiUrl(apiKey, MethodBase.ACCOUNT).setSubMethod(accountId, MethodSub.FAVORITE).buildUrl(parameters);
- String webpage = httpTools.postRequest(url, jsonBody);
-
- 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);
- }
- }
-
- /**
- * Add a movie to an accounts watch list.
- *
- * @param sessionId
- * @param accountId
- * @param movieId
- * @return
- * @throws MovieDbException
- */
- public StatusCode addToWatchList(String sessionId, int accountId, Integer movieId) throws MovieDbException {
- return modifyWatchList(sessionId, accountId, movieId, true);
- }
-
- /**
- * Remove a movie from an accounts watch list.
- *
- * @param sessionId
- * @param accountId
- * @param movieId
- * @return
- * @throws MovieDbException
- */
- public StatusCode removeFromWatchList(String sessionId, int accountId, Integer movieId) throws MovieDbException {
- return modifyWatchList(sessionId, accountId, movieId, false);
- }
-
- private StatusCode modifyWatchList(String sessionId, int accountId, Integer movieId, boolean add) throws MovieDbException {
- TmdbParameters parameters = new TmdbParameters();
- parameters.add(Param.SESSION, sessionId);
-
- Map body = new HashMap();
- body.put(MOVIE_ID, movieId);
- body.put("movie_watchlist", add);
- String jsonBody = convertToJson(body);
-
- URL url = new ApiUrl(apiKey, MethodBase.ACCOUNT).setSubMethod(accountId, MethodSub.MOVIE_WATCHLIST).buildUrl(parameters);
- String webpage = httpTools.postRequest(url, jsonBody);
-
- 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 is used to retrieve all of the basic movie information.
*
@@ -482,7 +321,7 @@ public class TheMovieDbApi {
URL url = new ApiUrl(apiKey, MethodBase.MOVIE).buildUrl(parameters);
String webpage = httpTools.getRequest(url);
try {
- MovieDb movie = mapper.readValue(webpage, MovieDb.class);
+ 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);
@@ -517,7 +356,7 @@ public class TheMovieDbApi {
String webpage = httpTools.getRequest(url);
try {
- MovieDb movie = mapper.readValue(webpage, MovieDb.class);
+ 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);
@@ -547,7 +386,7 @@ public class TheMovieDbApi {
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);
+ WrapperAlternativeTitles wrapper = MAPPER.readValue(webpage, WrapperAlternativeTitles.class);
TmdbResultsList results = new TmdbResultsList(wrapper.getTitles());
results.copyWrapper(wrapper);
return results;
@@ -576,7 +415,7 @@ public class TheMovieDbApi {
String webpage = httpTools.getRequest(url);
try {
- WrapperMovieCasts wrapper = mapper.readValue(webpage, WrapperMovieCasts.class);
+ WrapperMovieCasts wrapper = MAPPER.readValue(webpage, WrapperMovieCasts.class);
TmdbResultsList results = new TmdbResultsList(wrapper.getAll());
results.copyWrapper(wrapper);
return results;
@@ -605,7 +444,7 @@ public class TheMovieDbApi {
String webpage = httpTools.getRequest(url);
try {
- WrapperImages wrapper = mapper.readValue(webpage, WrapperImages.class);
+ WrapperImages wrapper = MAPPER.readValue(webpage, WrapperImages.class);
TmdbResultsList results = new TmdbResultsList(wrapper.getAll());
results.copyWrapper(wrapper);
return results;
@@ -634,7 +473,7 @@ public class TheMovieDbApi {
String webpage = httpTools.getRequest(url);
try {
- WrapperMovieKeywords wrapper = mapper.readValue(webpage, WrapperMovieKeywords.class);
+ WrapperMovieKeywords wrapper = MAPPER.readValue(webpage, WrapperMovieKeywords.class);
TmdbResultsList results = new TmdbResultsList(wrapper.getKeywords());
results.copyWrapper(wrapper);
return results;
@@ -663,7 +502,7 @@ public class TheMovieDbApi {
String webpage = httpTools.getRequest(url);
try {
- WrapperReleaseInfo wrapper = mapper.readValue(webpage, WrapperReleaseInfo.class);
+ WrapperReleaseInfo wrapper = MAPPER.readValue(webpage, WrapperReleaseInfo.class);
TmdbResultsList results = new TmdbResultsList(wrapper.getCountries());
results.copyWrapper(wrapper);
return results;
@@ -694,7 +533,7 @@ public class TheMovieDbApi {
String webpage = httpTools.getRequest(url);
try {
- WrapperVideos wrapper = mapper.readValue(webpage, WrapperVideos.class);
+ WrapperVideos wrapper = MAPPER.readValue(webpage, WrapperVideos.class);
TmdbResultsList
- //
/**
* This method is used to retrieve all of the basic information about a movie collection.
*
@@ -1083,7 +854,7 @@ public class TheMovieDbApi {
String webpage = httpTools.getRequest(url);
try {
- return mapper.readValue(webpage, CollectionInfo.class);
+ 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);
@@ -1107,7 +878,7 @@ public class TheMovieDbApi {
String webpage = httpTools.getRequest(url);
try {
- WrapperImages wrapper = mapper.readValue(webpage, WrapperImages.class);
+ WrapperImages wrapper = MAPPER.readValue(webpage, WrapperImages.class);
TmdbResultsList results = new TmdbResultsList(wrapper.getAll(ArtworkType.POSTER, ArtworkType.BACKDROP));
results.copyWrapper(wrapper);
return results;
@@ -1116,9 +887,7 @@ public class TheMovieDbApi {
throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, webpage, url, ex);
}
}
- //
- //
/**
* This method is used to retrieve all of the basic person information.
*
@@ -1138,7 +907,7 @@ public class TheMovieDbApi {
String webpage = httpTools.getRequest(url);
try {
- return mapper.readValue(webpage, Person.class);
+ 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);
@@ -1164,7 +933,7 @@ public class TheMovieDbApi {
String webpage = httpTools.getRequest(url);
try {
- WrapperPersonCredits wrapper = mapper.readValue(webpage, WrapperPersonCredits.class);
+ WrapperPersonCredits wrapper = MAPPER.readValue(webpage, WrapperPersonCredits.class);
TmdbResultsList results = new TmdbResultsList(wrapper.getAll());
results.copyWrapper(wrapper);
return results;
@@ -1189,7 +958,7 @@ public class TheMovieDbApi {
String webpage = httpTools.getRequest(url);
try {
- WrapperImages wrapper = mapper.readValue(webpage, WrapperImages.class);
+ WrapperImages wrapper = MAPPER.readValue(webpage, WrapperImages.class);
TmdbResultsList results = new TmdbResultsList(wrapper.getAll(ArtworkType.PROFILE));
results.copyWrapper(wrapper);
return results;
@@ -1199,27 +968,6 @@ public class TheMovieDbApi {
}
}
- /**
- * Get the changes for a specific person id.
- *
- * Changes are grouped by key, and ordered by date in descending order.
- *
- * By default, only the last 24 hours of changes are returned.
- *
- * The maximum number of days that can be returned in a single request is 14.
- *
- * The language is present on fields that are translatable.
- *
- * @param personId
- * @param startDate
- * @param endDate
- * @throws MovieDbException
- */
- public void getPersonChanges(int personId, String startDate, String endDate) throws MovieDbException {
- 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.
*
@@ -1249,7 +997,7 @@ public class TheMovieDbApi {
String webpage = httpTools.getRequest(url);
try {
- WrapperPersonList wrapper = mapper.readValue(webpage, WrapperPersonList.class);
+ WrapperPersonList wrapper = MAPPER.readValue(webpage, WrapperPersonList.class);
TmdbResultsList results = new TmdbResultsList(wrapper.getPersonList());
results.copyWrapper(wrapper);
return results;
@@ -1270,15 +1018,13 @@ public class TheMovieDbApi {
String webpage = httpTools.getRequest(url);
try {
- return mapper.readValue(webpage, Person.class);
+ 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);
}
}
- //
- //
/**
* This method is used to retrieve the basic information about a production company on TMDb.
*
@@ -1294,7 +1040,7 @@ public class TheMovieDbApi {
String webpage = httpTools.getRequest(url);
try {
- return mapper.readValue(webpage, Company.class);
+ 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);
@@ -1324,7 +1070,7 @@ public class TheMovieDbApi {
String webpage = httpTools.getRequest(url);
try {
- WrapperCompanyMovies wrapper = mapper.readValue(webpage, WrapperCompanyMovies.class);
+ WrapperCompanyMovies wrapper = MAPPER.readValue(webpage, WrapperCompanyMovies.class);
TmdbResultsList results = new TmdbResultsList(wrapper.getResults());
results.copyWrapper(wrapper);
return results;
@@ -1333,9 +1079,7 @@ public class TheMovieDbApi {
throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, webpage, url, ex);
}
}
- //
- //
/**
* You can use this method to retrieve the list of genres used on TMDb.
*
@@ -1353,7 +1097,7 @@ public class TheMovieDbApi {
String webpage = httpTools.getRequest(url);
try {
- WrapperGenres wrapper = mapper.readValue(webpage, WrapperGenres.class);
+ WrapperGenres wrapper = MAPPER.readValue(webpage, WrapperGenres.class);
TmdbResultsList results = new TmdbResultsList(wrapper.getGenres());
results.copyWrapper(wrapper);
return results;
@@ -1388,7 +1132,7 @@ public class TheMovieDbApi {
String webpage = httpTools.getRequest(url);
try {
- WrapperMovie wrapper = mapper.readValue(webpage, WrapperMovie.class);
+ WrapperMovie wrapper = MAPPER.readValue(webpage, WrapperMovie.class);
TmdbResultsList results = new TmdbResultsList(wrapper.getMovies());
results.copyWrapper(wrapper);
return results;
@@ -1397,9 +1141,7 @@ public class TheMovieDbApi {
throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, webpage, url, ex);
}
}
- //
- //
/**
* Search Movies This is a good starting point to start finding movies on TMDb.
*
@@ -1423,7 +1165,7 @@ public class TheMovieDbApi {
String webpage = httpTools.getRequest(url);
try {
- WrapperMovie wrapper = mapper.readValue(webpage, WrapperMovie.class);
+ WrapperMovie wrapper = MAPPER.readValue(webpage, WrapperMovie.class);
TmdbResultsList results = new TmdbResultsList(wrapper.getMovies());
results.copyWrapper(wrapper);
return results;
@@ -1453,7 +1195,7 @@ public class TheMovieDbApi {
String webpage = httpTools.getRequest(url);
try {
- WrapperCollection wrapper = mapper.readValue(webpage, WrapperCollection.class);
+ WrapperCollection wrapper = MAPPER.readValue(webpage, WrapperCollection.class);
TmdbResultsList results = new TmdbResultsList(wrapper.getResults());
results.copyWrapper(wrapper);
return results;
@@ -1484,7 +1226,7 @@ public class TheMovieDbApi {
String webpage = httpTools.getRequest(url);
try {
- WrapperPerson wrapper = mapper.readValue(webpage, WrapperPerson.class);
+ WrapperPerson wrapper = MAPPER.readValue(webpage, WrapperPerson.class);
TmdbResultsList results = new TmdbResultsList(wrapper.getResults());
results.copyWrapper(wrapper);
return results;
@@ -1513,7 +1255,7 @@ public class TheMovieDbApi {
String webpage = httpTools.getRequest(url);
try {
- WrapperMovieList wrapper = mapper.readValue(webpage, WrapperMovieList.class);
+ WrapperMovieList wrapper = MAPPER.readValue(webpage, WrapperMovieList.class);
TmdbResultsList results = new TmdbResultsList(wrapper.getMovieList());
results.copyWrapper(wrapper);
return results;
@@ -1545,7 +1287,7 @@ public class TheMovieDbApi {
String webpage = httpTools.getRequest(url);
try {
- WrapperCompany wrapper = mapper.readValue(webpage, WrapperCompany.class);
+ WrapperCompany wrapper = MAPPER.readValue(webpage, WrapperCompany.class);
TmdbResultsList results = new TmdbResultsList(wrapper.getResults());
results.copyWrapper(wrapper);
return results;
@@ -1572,7 +1314,7 @@ public class TheMovieDbApi {
String webpage = httpTools.getRequest(url);
try {
- WrapperKeywords wrapper = mapper.readValue(webpage, WrapperKeywords.class);
+ WrapperKeywords wrapper = MAPPER.readValue(webpage, WrapperKeywords.class);
TmdbResultsList results = new TmdbResultsList(wrapper.getResults());
results.copyWrapper(wrapper);
return results;
@@ -1581,9 +1323,7 @@ public class TheMovieDbApi {
throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, webpage, url, ex);
}
}
- //
- //
/**
* Get a list by its ID
*
@@ -1599,36 +1339,13 @@ public class TheMovieDbApi {
String webpage = httpTools.getRequest(url);
try {
- return mapper.readValue(webpage, MovieDbList.class);
+ 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);
}
}
- /**
- * 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, 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);
- }
- }
-
/**
* This method lets users create a new list. A valid session id is required.
*
@@ -1651,7 +1368,7 @@ public class TheMovieDbApi {
String webpage = httpTools.postRequest(url, jsonBody);
try {
- return mapper.readValue(webpage, MovieDbListStatus.class).getListId();
+ 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);
@@ -1674,7 +1391,7 @@ public class TheMovieDbApi {
String webpage = httpTools.getRequest(url);
try {
- return mapper.readValue(webpage, ListItemStatus.class).isItemPresent();
+ 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);
@@ -1717,36 +1434,13 @@ public class TheMovieDbApi {
String webpage = httpTools.postRequest(url, jsonBody);
try {
- return mapper.readValue(webpage, StatusCode.class);
+ 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 list of movies on an accounts watchlist.
- *
- * @param sessionId
- * @param accountId
- * @return The watchlist of the user
- * @throws 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);
- }
- }
-
/**
* This method lets users delete a list that they created. A valid session id is required.
*
@@ -1764,15 +1458,13 @@ public class TheMovieDbApi {
String webpage = httpTools.deleteRequest(url);
try {
- return mapper.readValue(webpage, StatusCode.class);
+ 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);
}
}
- //
- //
/**
* Get the basic information for a specific keyword id.
*
@@ -1788,7 +1480,7 @@ public class TheMovieDbApi {
String webpage = httpTools.getRequest(url);
try {
- return mapper.readValue(webpage, Keyword.class);
+ 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);
@@ -1815,7 +1507,7 @@ public class TheMovieDbApi {
String webpage = httpTools.getRequest(url);
try {
- WrapperKeywordMovies wrapper = mapper.readValue(webpage, WrapperKeywordMovies.class);
+ WrapperKeywordMovies wrapper = MAPPER.readValue(webpage, WrapperKeywordMovies.class);
TmdbResultsList results = new TmdbResultsList(wrapper.getResults());
results.copyWrapper(wrapper);
return results;
@@ -1825,55 +1517,18 @@ public class TheMovieDbApi {
}
}
- //
-
- //
- /**
- * 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.
- *
- * @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 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 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", "");
}
- //
- //
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);
+ WrapperJobList wrapper = MAPPER.readValue(webpage, WrapperJobList.class);
TmdbResultsList results = new TmdbResultsList(wrapper.getJobs());
results.copyWrapper(wrapper);
return results;
@@ -1882,9 +1537,7 @@ public class TheMovieDbApi {
throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, webpage, url, ex);
}
}
- //
- //
/**
* Discover movies by different types of data like average rating, number of votes, genres and certifications.
*
@@ -1948,7 +1601,7 @@ public class TheMovieDbApi {
String webpage = httpTools.getRequest(url);
try {
- WrapperMovie wrapper = mapper.readValue(webpage, WrapperMovie.class);
+ WrapperMovie wrapper = MAPPER.readValue(webpage, WrapperMovie.class);
TmdbResultsList results = new TmdbResultsList(wrapper.getMovies());
results.copyWrapper(wrapper);
return results;
@@ -1957,7 +1610,6 @@ public class TheMovieDbApi {
throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, webpage, url, ex);
}
}
- //
/**
* Use Jackson to convert Map to JSON string.
@@ -1968,9 +1620,359 @@ public class TheMovieDbApi {
*/
public static String convertToJson(Map map) throws MovieDbException {
try {
- return mapper.writeValueAsString(map);
+ return MAPPER.writeValueAsString(map);
} catch (JsonProcessingException jpe) {
throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, "JSON conversion failed", "", jpe);
}
}
+
+ //
+ /**
+ * 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 {
+ return tmdbAccount.getAccount(sessionId);
+ }
+
+ /**
+ * 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 {
+ return tmdbAccount.getUserLists(sessionId, accountId);
+ }
+
+ /**
+ * 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);
+ }
+
+ /**
+ * 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 changeFavoriteStatus(String sessionId, int accountId, Integer mediaId, MediaType mediaType, boolean isFavorite) throws MovieDbException {
+ return tmdbAccount.changeFavoriteStatus(sessionId, accountId, mediaId, mediaType, isFavorite);
+ }
+
+ /**
+ * Get the list of rated movies (and associated rating) for an account.
+ *
+ * @param sessionId
+ * @param accountId
+ * @return
+ * @throws MovieDbException
+ */
+ public List getRatedMovies(String sessionId, int accountId) throws MovieDbException {
+ return tmdbAccount.getRatedMovies(sessionId, accountId);
+ }
+
+ /**
+ * Get the list of rated TV shows (and associated rating) for an account.
+ *
+ * @param sessionId
+ * @param accountId
+ * @return
+ * @throws MovieDbException
+ */
+ public List getRatedTV(String sessionId, int accountId) throws MovieDbException {
+ return tmdbAccount.getRatedTV(sessionId, accountId);
+ }
+
+ /**
+ * Get the list of movies on an accounts watchlist.
+ *
+ * @param sessionId
+ * @param accountId
+ * @return The watchlist of the user
+ * @throws MovieDbException
+ */
+ public List getWatchListMovie(String sessionId, int accountId) throws MovieDbException {
+ return tmdbAccount.getWatchListMovie(sessionId, accountId);
+ }
+
+ /**
+ * Get the list of movies on an accounts watchlist.
+ *
+ * @param sessionId
+ * @param accountId
+ * @return The watchlist of the user
+ * @throws MovieDbException
+ */
+ public List getWatchListTV(String sessionId, int accountId) throws MovieDbException {
+ return tmdbAccount.getWatchListTV(sessionId, accountId);
+ }
+
+ /**
+ * Add a movie to an accounts watch list.
+ *
+ * @param sessionId
+ * @param accountId
+ * @param mediaId
+ * @param mediaType
+ * @return
+ * @throws MovieDbException
+ */
+ public StatusCode addToWatchList(String sessionId, int accountId, Integer mediaId, MediaType mediaType) throws MovieDbException {
+ return tmdbAccount.modifyWatchList(sessionId, accountId, mediaId, mediaType, true);
+ }
+
+ /**
+ * Remove a movie from an accounts watch list.
+ *
+ * @param sessionId
+ * @param accountId
+ * @param mediaId
+ * @param mediaType
+ * @return
+ * @throws MovieDbException
+ */
+ public StatusCode removeFromWatchList(String sessionId, int accountId, Integer mediaId, MediaType mediaType) throws MovieDbException {
+ return tmdbAccount.modifyWatchList(sessionId, accountId, mediaId, mediaType, false);
+ }
+
+ /**
+ * 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 {
+ return tmdbAccount.getFavoriteTv(sessionId, accountId);
+ }
+ //
+
+ //
+ /**
+ * 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 {
+ return tmdbAuth.getAuthorisationToken();
+ }
+
+ /**
+ * 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 {
+ return tmdbAuth.getSessionToken(token);
+ }
+
+ /**
+ * 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 {
+ return tmdbAuth.getSessionTokenLogin(token, username, password);
+ }
+
+ /**
+ * 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();
+ }
+ //
+
+ //
+ /**
+ * Get a list of movies certification.
+ *
+ * @return
+ * @throws MovieDbException
+ */
+ public TmdbResultsMap> getMoviesCertification() throws MovieDbException {
+ return tmdbCertifications.getMoviesCertification();
+ }
+
+ /**
+ * Get a list of tv certification.
+ *
+ * @return
+ * @throws MovieDbException
+ */
+ public TmdbResultsMap> getTvCertification() throws MovieDbException {
+ return tmdbCertifications.getTvCertification();
+ }
+ //
+
+ //
+ /**
+ * Get the changes for a specific movie id.
+ *
+ * Changes are grouped by key, and ordered by date in descending order.
+ *
+ * By default, only the last 24 hours of changes are returned.
+ *
+ * The maximum number of days that can be returned in a single request is 14.
+ *
+ * The language is present on fields that are translatable.
+ *
+ * TODO: DOES NOT WORK AT THE MOMENT. This is due to the "value" item changing type in the ChangeItem
+ *
+ * @param movieId
+ * @param startDate the start date of the changes, optional
+ * @param endDate the end date of the changes, optional
+ * @return
+ * @throws MovieDbException
+ */
+ public TmdbResultsMap> getMovieChanges(int movieId, String startDate, String endDate) throws MovieDbException {
+ return tmdbChanges.getMovieChanges(movieId, startDate, endDate);
+ }
+
+ /**
+ * Get the changes for a specific person id.
+ *
+ * Changes are grouped by key, and ordered by date in descending order.
+ *
+ * By default, only the last 24 hours of changes are returned.
+ *
+ * The maximum number of days that can be returned in a single request is 14.
+ *
+ * The language is present on fields that are translatable.
+ *
+ * @param personId
+ * @param startDate
+ * @param endDate
+ * @throws MovieDbException
+ */
+ public void getPersonChanges(int personId, String startDate, String endDate) throws MovieDbException {
+ tmdbChanges.getPersonChanges(personId, startDate, endDate);
+ }
+
+ /**
+ * 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 TmdbResultsList getMovieChangeList(int page, String startDate, String endDate) throws MovieDbException {
+ return tmdbChanges.getChangeList(MethodBase.MOVIE, page, startDate, endDate);
+ }
+
+ /**
+ * Get a list of TV IDs that have been edited.
+ *
+ * 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 TmdbResultsList getTvChangeList(int 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 can then use the person 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 TmdbResultsList getPersonChangeList(int page, String startDate, String endDate) throws MovieDbException {
+ return tmdbChanges.getChangeList(MethodBase.PERSON, page, startDate, endDate);
+ }
+ //
+
+ //
+ //
+ //
+ //
+ //
+ //
+ //
+ //
+ //
+ //
+ //
+ //
+ //
+ //
+ //
+ //
+ //
+ //
+ //
+ //
+ //
+ //
+ //
+ //
+ //
+ //
+ //
+ //
+ //
+ //
+ //
+ //
}
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..015ec5e2b
--- /dev/null
+++ b/src/main/java/com/omertron/themoviedbapi/enumeration/MediaType.java
@@ -0,0 +1,37 @@
+/*
+ * 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;
+
+/**
+ * Media type options
+ *
+ * @author Stuart
+ */
+public enum MediaType {
+
+ /**
+ * Movie media type
+ */
+ MOVIE,
+ /**
+ * TV Show media type
+ */
+ TV;
+}
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..f554e2e97
--- /dev/null
+++ b/src/main/java/com/omertron/themoviedbapi/methods/AbstractMethod.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.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.omertron.themoviedbapi.MovieDbException;
+import com.omertron.themoviedbapi.tools.HttpTools;
+import java.util.Map;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+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();
+ // Logger
+ protected static final Logger LOG = LoggerFactory.getLogger(TmdbAccount.class);
+
+ /**
+ * Default constructor for the methods
+ *
+ * @param apiKey
+ * @param httpTools
+ */
+ public AbstractMethod(String apiKey, HttpTools httpTools) {
+ this.apiKey = apiKey;
+ this.httpTools = httpTools;
+ }
+
+ /**
+ * Use Jackson to convert Map to JSON string.
+ *
+ * @param map
+ * @return
+ * @throws MovieDbException
+ */
+ protected static String convertToJson(Map map) throws MovieDbException {
+ try {
+ return MAPPER.writeValueAsString(map);
+ } catch (JsonProcessingException ex) {
+ throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, "JSON conversion failed", "", 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..1341fb27b
--- /dev/null
+++ b/src/main/java/com/omertron/themoviedbapi/methods/TmdbAccount.java
@@ -0,0 +1,289 @@
+/*
+ * 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.model.Account;
+import com.omertron.themoviedbapi.model.MovieDb;
+import com.omertron.themoviedbapi.model.MovieDbList;
+import com.omertron.themoviedbapi.model.StatusCode;
+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.WrapperMovie;
+import com.omertron.themoviedbapi.wrapper.WrapperMovieDbList;
+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 Account Methods
+ *
+ * @author stuart.boston
+ */
+public class TmdbAccount extends AbstractMethod {
+
+ private static final String MEDIA_ID = "media_id";
+ private static final String MEDIA_TYPE = "media_type";
+ private static final String FAVORITE = "favorite";
+ private static final String WATCHLIST = "watchlist";
+
+ /**
+ * 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, 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) {
+ LOG.warn("Failed to get Account: {}", ex.getMessage(), ex);
+ throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, webpage, 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, 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);
+ }
+ }
+
+ /**
+ * 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, sessionId);
+
+ URL url = new ApiUrl(apiKey, MethodBase.ACCOUNT).setSubMethod(accountId, MethodSub.FAVORITE_MOVIES).buildUrl(parameters);
+ String webpage = httpTools.getRequest(url);
+
+ 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 favorite TV series for an account.
+ *
+ * @param sessionId
+ * @param accountId
+ * @return
+ * @throws MovieDbException
+ */
+ public List getFavoriteTv(String sessionId, int accountId) throws MovieDbException {
+ throw new MovieDbException(ApiExceptionType.UNKNOWN_CAUSE, "Not implemented yet");
+ }
+
+ /**
+ * Add or remove a movie to an accounts favorite list.
+ *
+ * @param sessionId
+ * @param accountId
+ * @param mediaType
+ * @param mediaId
+ * @param isFavorite
+ * @return
+ * @throws MovieDbException
+ */
+ public StatusCode changeFavoriteStatus(String sessionId, int accountId, Integer mediaId, MediaType mediaType, boolean isFavorite) throws MovieDbException {
+ TmdbParameters parameters = new TmdbParameters();
+ parameters.add(Param.SESSION, sessionId);
+
+ Map body = new HashMap();
+ body.put(MEDIA_TYPE, mediaType.toString().toLowerCase());
+ body.put(MEDIA_ID, mediaId);
+ body.put(FAVORITE, isFavorite);
+ String jsonBody = convertToJson(body);
+
+ URL url = new ApiUrl(apiKey, MethodBase.ACCOUNT).setSubMethod(accountId, MethodSub.FAVORITE).buildUrl(parameters);
+ String webpage = httpTools.postRequest(url, jsonBody);
+
+ 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);
+ }
+ }
+
+ /**
+ * Get the list of rated movies (and associated rating) for an account.
+ *
+ * @param sessionId
+ * @param accountId
+ * @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);
+ }
+ }
+
+ /**
+ * Get the list of rated TV shows (and associated rating) for an account.
+ *
+ * @param sessionId
+ * @param accountId
+ * @return
+ * @throws MovieDbException
+ */
+ public List getRatedTV(String sessionId, int accountId) throws MovieDbException {
+ throw new MovieDbException(ApiExceptionType.UNKNOWN_CAUSE, "Not implemented yet");
+ }
+
+ /**
+ * Get the list of movies on an accounts watch list.
+ *
+ * @param sessionId
+ * @param accountId
+ * @return The watch list of the user
+ * @throws MovieDbException
+ */
+ public List getWatchListMovie(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.WATCHLIST_MOVIES).buildUrl(parameters);
+ String webpage = httpTools.getRequest(url);
+
+ try {
+ return MAPPER.readValue(webpage, WrapperMovie.class).getMovies();
+ } catch (IOException ex) {
+ LOG.warn("Failed to get Movie watch list: {}", ex.getMessage(), ex);
+ throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, webpage, url, ex);
+ }
+ }
+
+ /**
+ * Get the list of movies on an accounts watch list.
+ *
+ * @param sessionId
+ * @param accountId
+ * @return The watch list of the user
+ * @throws MovieDbException
+ */
+ public List getWatchListTV(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.WATCHLIST_TV).buildUrl(parameters);
+ String webpage = httpTools.getRequest(url);
+
+ try {
+ return MAPPER.readValue(webpage, WrapperMovie.class).getMovies();
+ } catch (IOException ex) {
+ LOG.warn("Failed to get TV watch list: {}", ex.getMessage(), ex);
+ throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, webpage, url, ex);
+ }
+ }
+
+ /**
+ * 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, Integer movieId, MediaType mediaType, boolean addToWatchlist) throws MovieDbException {
+ TmdbParameters parameters = new TmdbParameters();
+ parameters.add(Param.SESSION, sessionId);
+
+ Map body = new HashMap();
+ body.put(MEDIA_TYPE, mediaType.toString().toLowerCase());
+ body.put(MEDIA_ID, movieId);
+ body.put(WATCHLIST, addToWatchlist);
+ String jsonBody = convertToJson(body);
+
+ URL url = new ApiUrl(apiKey, MethodBase.ACCOUNT).setSubMethod(accountId, MethodSub.WATCHLIST).buildUrl(parameters);
+ String webpage = httpTools.postRequest(url, jsonBody);
+
+ 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);
+ }
+ }
+}
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..788d87fc8
--- /dev/null
+++ b/src/main/java/com/omertron/themoviedbapi/methods/TmdbAuthentication.java
@@ -0,0 +1,166 @@
+/*
+ * 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.TokenAuthorisation;
+import com.omertron.themoviedbapi.model.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).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);
+ }
+ }
+
+ /**
+ * 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()) {
+ 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);
+ }
+ }
+
+ /**
+ * 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()) {
+ LOG.warn("Session token was not successful!");
+ 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).setSubMethod(MethodSub.TOKEN_VALIDATE).buildUrl(parameters);
+ String webpage = httpTools.getRequest(url);
+
+ 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);
+ }
+ }
+
+ /**
+ * 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).setSubMethod(MethodSub.GUEST_SESSION).buildUrl();
+ String webpage = httpTools.getRequest(url);
+
+ 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);
+ }
+ }}
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..f17ab64ee
--- /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).setSubMethod(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