master
Stuart Boston 11 years ago
parent bbda0eec72
commit 97f3313d8c

File diff suppressed because it is too large Load Diff

@ -19,14 +19,10 @@
*/
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
@ -55,19 +51,4 @@ public class AbstractMethod {
this.httpTools = httpTools;
}
/**
* Use Jackson to convert Map to JSON string.
*
* @param map
* @return
* @throws MovieDbException
*/
protected static String convertToJson(Map<String, ?> map) throws MovieDbException {
try {
return MAPPER.writeValueAsString(map);
} catch (JsonProcessingException ex) {
throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, "JSON conversion failed", "", ex);
}
}
}

@ -30,14 +30,14 @@ 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.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;
/**
@ -47,11 +47,6 @@ import org.yamj.api.common.exception.ApiExceptionType;
*/
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
*
@ -63,7 +58,8 @@ public class TmdbAccount extends AbstractMethod {
}
/**
* Get the basic information for an account. You will need to have a valid session id.
* Get the basic information for an account. You will need to have a valid
* session id.
*
* @param sessionId
* @return
@ -79,8 +75,7 @@ public class TmdbAccount extends AbstractMethod {
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);
throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, "Failed to get Account", url, ex);
}
}
@ -102,8 +97,7 @@ public class TmdbAccount extends AbstractMethod {
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);
throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, "Failed to get user list", url, ex);
}
}
@ -125,8 +119,7 @@ public class TmdbAccount extends AbstractMethod {
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);
throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, "Failed to get favorite movies", url, ex);
}
}
@ -157,11 +150,11 @@ public class TmdbAccount extends AbstractMethod {
TmdbParameters parameters = new TmdbParameters();
parameters.add(Param.SESSION, sessionId);
Map<String, Object> body = new HashMap<String, Object>();
body.put(MEDIA_TYPE, mediaType.toString().toLowerCase());
body.put(MEDIA_ID, mediaId);
body.put(FAVORITE, isFavorite);
String jsonBody = convertToJson(body);
String jsonBody = new PostTools()
.add(PostBody.MEDIA_TYPE, mediaType.toString().toLowerCase())
.add(PostBody.MEDIA_ID, mediaId)
.add(PostBody.FAVORITE, isFavorite)
.build();
URL url = new ApiUrl(apiKey, MethodBase.ACCOUNT).setSubMethod(accountId, MethodSub.FAVORITE).buildUrl(parameters);
String webpage = httpTools.postRequest(url, jsonBody);
@ -169,8 +162,7 @@ public class TmdbAccount extends AbstractMethod {
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);
throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, "Failed to get favorite status", url, ex);
}
}
@ -192,8 +184,7 @@ public class TmdbAccount extends AbstractMethod {
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);
throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, "Failed to get rated movies", url, ex);
}
}
@ -227,8 +218,7 @@ public class TmdbAccount extends AbstractMethod {
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);
throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, "Failed to get Movie watch list", url, ex);
}
}
@ -250,8 +240,7 @@ public class TmdbAccount extends AbstractMethod {
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);
throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, "Failed to get TV watch list", url, ex);
}
}
@ -270,11 +259,11 @@ public class TmdbAccount extends AbstractMethod {
TmdbParameters parameters = new TmdbParameters();
parameters.add(Param.SESSION, sessionId);
Map<String, Object> body = new HashMap<String, Object>();
body.put(MEDIA_TYPE, mediaType.toString().toLowerCase());
body.put(MEDIA_ID, movieId);
body.put(WATCHLIST, addToWatchlist);
String jsonBody = convertToJson(body);
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).setSubMethod(accountId, MethodSub.WATCHLIST).buildUrl(parameters);
String webpage = httpTools.postRequest(url, jsonBody);

@ -20,10 +20,8 @@
package com.omertron.themoviedbapi.methods;
import com.omertron.themoviedbapi.MovieDbException;
import com.omertron.themoviedbapi.model.ChangedItem;
import com.omertron.themoviedbapi.model.ChangedMedia;
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;
@ -33,7 +31,6 @@ import com.omertron.themoviedbapi.tools.TmdbParameters;
import com.omertron.themoviedbapi.wrapper.WrapperMediaChanges;
import java.io.IOException;
import java.net.URL;
import java.util.List;
import org.yamj.api.common.exception.ApiExceptionType;
/**
@ -53,54 +50,11 @@ public class TmdbChanges extends AbstractMethod {
super(apiKey, httpTools);
}
/**
* 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<String, List<ChangedItem>> getMovieChanges(int movieId, String startDate, String endDate) throws MovieDbException {
return null;
}
/**
* 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 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.
* 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
@ -128,4 +82,46 @@ public class TmdbChanges extends AbstractMethod {
throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, "Failed to get changes", url, ex);
}
}
/**
* Get the changes for a specific movie id.
*
* @param id
* @param startDate the start date of the changes, optional
* @param endDate the end date of the changes, optional
* @return
* @throws MovieDbException
*/
public TmdbResultsList getMovieChanges(int id, String startDate, String endDate) throws MovieDbException {
LOG.trace("Movie Changes: id: {}, start: {}, end: {}", id, startDate, endDate);
throw new MovieDbException(ApiExceptionType.UNKNOWN_CAUSE, "Not implemented yet");
}
/**
* Get the changes for a specific TV Show
*
* @param id
* @param startDate
* @param endDate
* @return
* @throws MovieDbException
*/
public TmdbResultsList getTvChanges(int id, String startDate, String endDate) throws MovieDbException {
LOG.trace("TV Changes: id: {}, start: {}, end: {}", id, startDate, endDate);
throw new MovieDbException(ApiExceptionType.UNKNOWN_CAUSE, "Not implemented yet");
}
/**
* Get the changes for a specific person id.
*
* @param id
* @param startDate
* @param endDate
* @return
* @throws MovieDbException
*/
public TmdbResultsList getPersonChanges(int id, String startDate, String endDate) throws MovieDbException {
LOG.trace("Person Changes: id: {}, start: {}, end: {}", id, startDate, endDate);
throw new MovieDbException(ApiExceptionType.UNKNOWN_CAUSE, "Not implemented yet", "");
}
}

@ -19,7 +19,21 @@
*/
package com.omertron.themoviedbapi.methods;
import com.omertron.themoviedbapi.MovieDbException;
import com.omertron.themoviedbapi.model.Artwork;
import com.omertron.themoviedbapi.model.ArtworkType;
import com.omertron.themoviedbapi.model.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
@ -37,4 +51,57 @@ public class TmdbCollections extends AbstractMethod {
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<Artwork> 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<Artwork> results = new TmdbResultsList<Artwork>(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);
}
}
}

@ -19,7 +19,20 @@
*/
package com.omertron.themoviedbapi.methods;
import com.omertron.themoviedbapi.MovieDbException;
import com.omertron.themoviedbapi.model.Company;
import com.omertron.themoviedbapi.model.MovieDb;
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.WrapperCompanyMovies;
import java.io.IOException;
import java.net.URL;
import org.yamj.api.common.exception.ApiExceptionType;
/**
* Class to hold the Company Methods
@ -37,4 +50,60 @@ public class TmdbCompanies extends AbstractMethod {
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.
*
* TODO: Implement more than 20 movies
*
* @param companyId
* @param language
* @param page
* @return
* @throws MovieDbException
*/
public TmdbResultsList<MovieDb> 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<MovieDb> results = new TmdbResultsList<MovieDb>(wrapper.getResults());
results.copyWrapper(wrapper);
return results;
} catch (IOException ex) {
throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, "Failed to get company movies", url, ex);
}
}
}

@ -19,7 +19,21 @@
*/
package com.omertron.themoviedbapi.methods;
import com.omertron.themoviedbapi.MovieDbException;
import static com.omertron.themoviedbapi.methods.AbstractMethod.MAPPER;
import com.omertron.themoviedbapi.model.Configuration;
import com.omertron.themoviedbapi.model.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.MalformedURLException;
import java.net.URL;
import org.yamj.api.common.exception.ApiExceptionType;
/**
* Class to hold the Configuration Methods
@ -28,6 +42,12 @@ import com.omertron.themoviedbapi.tools.HttpTools;
*/
public class TmdbConfiguration extends AbstractMethod {
/**
* Cache the configuration in memory<br/>
* It rarely changes, so this should be safe.
*/
private static Configuration config = null;
/**
* Constructor
*
@ -37,4 +57,71 @@ public class TmdbConfiguration extends AbstractMethod {
public TmdbConfiguration(String apiKey, HttpTools httpTools) {
super(apiKey, httpTools);
}
/**
* Get the configuration<br/>
* 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;
}
/**
* Generate the full image URL from the size and image path
*
* @param imagePath
* @param requiredSize
* @return
* @throws MovieDbException
*/
public URL createImageUrl(String imagePath, String requiredSize) throws MovieDbException {
if (!config.isValidSize(requiredSize)) {
throw new MovieDbException(ApiExceptionType.INVALID_IMAGE, "Required size '" + requiredSize + "' is not valid");
}
StringBuilder sb = new StringBuilder(config.getBaseUrl());
sb.append(requiredSize);
sb.append(imagePath);
try {
return new URL(sb.toString());
} catch (MalformedURLException ex) {
throw new MovieDbException(ApiExceptionType.INVALID_URL, "Failed to create image URL", sb.toString(), ex);
}
}
/**
* Get a list of valid jobs
*
* @return
* @throws MovieDbException
*/
public TmdbResultsList<JobDepartment> 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<JobDepartment> results = new TmdbResultsList<JobDepartment>(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);
}
}
}

@ -19,7 +19,16 @@
*/
package com.omertron.themoviedbapi.methods;
import com.omertron.themoviedbapi.MovieDbException;
import com.omertron.themoviedbapi.model.TBD_PersonCredits;
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
@ -37,4 +46,39 @@ public class TmdbCredits extends AbstractMethod {
public TmdbCredits(String apiKey, HttpTools httpTools) {
super(apiKey, httpTools);
}
/**
* Get the detailed information about a particular credit record.
* <p>
* This is currently only supported with the new credit model found in TV.
* <br/>
* These IDs can be found from any TV credit response as well as the
* TV_credits and combined_credits methods for people. <br/>
* The episodes object returns a list of episodes and are generally going to
* be guest stars. <br/>
* The season array will return a list of season numbers. <br/>
* 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 TBD_PersonCredits 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, TBD_PersonCredits.class);
} catch (IOException ex) {
throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, "Failed to get credit info", url, ex);
}
}
}

@ -19,7 +19,18 @@
*/
package com.omertron.themoviedbapi.methods;
import com.omertron.themoviedbapi.MovieDbException;
import com.omertron.themoviedbapi.model.Discover;
import com.omertron.themoviedbapi.model.MovieDb;
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.WrapperMovie;
import java.io.IOException;
import java.net.URL;
import org.yamj.api.common.exception.ApiExceptionType;
/**
* Class to hold the Discover Methods
@ -37,4 +48,39 @@ public class TmdbDiscover extends AbstractMethod {
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 TmdbResultsList<MovieDb> getDiscoverMovies(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<MovieDb> results = new TmdbResultsList<MovieDb>(wrapper.getMovies());
results.copyWrapper(wrapper);
return results;
} catch (IOException ex) {
throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, "Failed to get movie discover list", url, ex);
}
}
/**
* Discover movies by different types of data like average rating, number of
* votes, genres and certifications.
*
* @param discover A discover object containing the search criteria required
* @return
* @throws MovieDbException
*/
public TmdbResultsList<MovieDb> getDiscoverTV(Discover discover) throws MovieDbException {
URL url = new ApiUrl(apiKey, MethodBase.DISCOVER).setSubMethod(MethodSub.TV).buildUrl(discover.getParams());
throw new MovieDbException(ApiExceptionType.UNKNOWN_CAUSE, "Not implemented yet");
}
}

@ -19,7 +19,11 @@
*/
package com.omertron.themoviedbapi.methods;
import com.omertron.themoviedbapi.MovieDbException;
import com.omertron.themoviedbapi.model.TBD_ExternalSource;
import com.omertron.themoviedbapi.model.TBD_FindResults;
import com.omertron.themoviedbapi.tools.HttpTools;
import org.yamj.api.common.exception.ApiExceptionType;
/**
* Class to hold the Find Methods
@ -37,4 +41,31 @@ public class TmdbFind extends AbstractMethod {
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
* <ul>
* <li>Movies: imdb_id</li>
* <li>People: imdb_id, freebase_mid, freebase_id, tvrage_id</li>
* <li>TV Series: imdb_id, freebase_mid, freebase_id, tvdb_id,
* tvrage_id</li>
* <li>TV Seasons: freebase_mid, freebase_id, tvdb_id, tvrage_id</li>
* <li>TV Episodes: imdb_id, freebase_mid, freebase_id, tvdb_id,
* tvrage_idimdb_id, freebase_mid, freebase_id, tvrage_id, tvdb_id.
* </ul>
*
* 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 TBD_FindResults find(String id, TBD_ExternalSource externalSource, String language) throws MovieDbException {
throw new MovieDbException(ApiExceptionType.UNKNOWN_CAUSE, "not implemented yet");
}
}

@ -19,7 +19,21 @@
*/
package com.omertron.themoviedbapi.methods;
import com.omertron.themoviedbapi.MovieDbException;
import com.omertron.themoviedbapi.model.Genre;
import com.omertron.themoviedbapi.model.MovieDb;
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 com.omertron.themoviedbapi.wrapper.WrapperMovie;
import java.io.IOException;
import java.net.URL;
import org.yamj.api.common.exception.ApiExceptionType;
/**
* Class to hold the Genre Methods
@ -37,4 +51,66 @@ public class TmdbGenres extends AbstractMethod {
public TmdbGenres(String apiKey, HttpTools httpTools) {
super(apiKey, httpTools);
}
/**
* You can use this method to retrieve the list of genres used on TMDb.
*
* These IDs will correspond to those found in movie calls.
*
* @param language
* @return
* @throws MovieDbException
*/
public TmdbResultsList<Genre> 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<Genre> results = new TmdbResultsList<Genre>(wrapper.getGenres());
results.copyWrapper(wrapper);
return results;
} catch (IOException ex) {
throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, "Failed to get genre list", url, ex);
}
}
/**
* Get a list of movies per genre.
*
* It is important to understand that only movies with more than 10 votes
* get listed.
*
* This prevents movies from 1 10/10 rating from being listed first and for
* the first 5 pages.
*
* @param genreId
* @param language
* @param page
* @param includeAllMovies
* @return
* @throws MovieDbException
*/
public TmdbResultsList<MovieDb> 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);
try {
WrapperMovie wrapper = MAPPER.readValue(webpage, WrapperMovie.class);
TmdbResultsList<MovieDb> results = new TmdbResultsList<MovieDb>(wrapper.getMovies());
results.copyWrapper(wrapper);
return results;
} catch (IOException ex) {
throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, "Failed to get genre movie list", url, ex);
}
}
}

@ -19,7 +19,20 @@
*/
package com.omertron.themoviedbapi.methods;
import com.omertron.themoviedbapi.MovieDbException;
import com.omertron.themoviedbapi.model.Keyword;
import com.omertron.themoviedbapi.model.KeywordMovie;
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.WrapperKeywordMovies;
import java.io.IOException;
import java.net.URL;
import org.yamj.api.common.exception.ApiExceptionType;
/**
* Class to hold the Keyword Methods
@ -37,4 +50,57 @@ public class TmdbKeywords extends AbstractMethod {
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) {
LOG.warn("Failed to get keyword: {}", ex.getMessage(), ex);
throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, webpage, 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<KeywordMovie> 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<KeywordMovie> results = new TmdbResultsList<KeywordMovie>(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);
}
}
}

@ -19,7 +19,23 @@
*/
package com.omertron.themoviedbapi.methods;
import com.omertron.themoviedbapi.MovieDbException;
import com.omertron.themoviedbapi.model.ListItemStatus;
import com.omertron.themoviedbapi.model.MovieDbList;
import com.omertron.themoviedbapi.model.MovieDbListStatus;
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.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
@ -37,4 +53,127 @@ public class TmdbLists extends AbstractMethod {
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 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) {
throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, "Failed to get list", 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, 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, MovieDbListStatus.class).getListId();
} catch (IOException ex) {
throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, "Failed to create list", url, ex);
}
}
/**
* Check to see if a movie ID is already added to a list.
*
* @param listId
* @param movieId
* @return true if the movie is on the list
* @throws 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) {
throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, "Failed to get media status", url, ex);
}
}
/**
* Modify a list
*
* @param sessionId
* @param listId
* @param movieId
* @param operation
* @return
* @throws MovieDbException
*/
public StatusCode modifyMovieList(String sessionId, String listId, Integer movieId, MethodSub operation) throws MovieDbException {
TmdbParameters parameters = new TmdbParameters();
parameters.add(Param.SESSION, sessionId);
String jsonBody = new PostTools()
.add(PostBody.MEDIA_ID, movieId)
.build();
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) {
throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, "Failed to remove movie from 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 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) {
throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, "Failed to delete movie list", url, ex);
}
}
}

@ -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 <http://www.gnu.org/licenses/>.
*
*/
package com.omertron.themoviedbapi.model;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonRootName;
/**
* @author stuart.boston
*/
@JsonRootName("???")
public class TBD_ExternalSource extends AbstractJsonMapping {
private static final long serialVersionUID = 1L;
// Properties
@JsonProperty("id")
private int id;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}

@ -17,24 +17,28 @@
* along with TheMovieDB API. If not, see <http://www.gnu.org/licenses/>.
*
*/
package com.omertron.themoviedbapi.methods;
package com.omertron.themoviedbapi.model;
import com.omertron.themoviedbapi.tools.HttpTools;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonRootName;
/**
* Class to hold the Job Methods
*
* @author stuart.boston
*/
public class TmdbJobs extends AbstractMethod {
@JsonRootName("???")
public class TBD_FindResults extends AbstractJsonMapping {
private static final long serialVersionUID = 1L;
// Properties
@JsonProperty("id")
private int id;
public int getId() {
return id;
}
/**
* Constructor
*
* @param apiKey
* @param httpTools
*/
public TmdbJobs(String apiKey, HttpTools httpTools) {
super(apiKey, httpTools);
public void setId(int id) {
this.id = id;
}
}

@ -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 <http://www.gnu.org/licenses/>.
*
*/
package com.omertron.themoviedbapi.model;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonRootName;
/**
* @author stuart.boston
*/
@JsonRootName("???")
public class TBD_PersonCredits extends AbstractJsonMapping {
private static final long serialVersionUID = 1L;
// Properties
@JsonProperty("id")
private int id;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}

@ -30,6 +30,7 @@ public enum MethodBase {
COLLECTION("collection"),
COMPANY("company"),
CONFIGURATION("configuration"),
CREDIT("credit"),
DISCOVER("discover"),
GENRE("genre"),
JOB("job"),

@ -63,7 +63,8 @@ public enum MethodSub {
ADD_ITEM("add_item"),
REMOVE_ITEM("remove_item"),
MOVIE_LIST("movie/list"),
TV_LIST("tv/list");
TV_LIST("tv/list"),
TV("tv");
private final String value;

@ -0,0 +1,50 @@
/*
* 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 <http://www.gnu.org/licenses/>.
*
*/
package com.omertron.themoviedbapi.tools;
/**
* List of values to use for the POST requests
*
* @author Stuart.Boston
*/
public enum PostBody {
MEDIA_ID("media_id"),
MEDIA_TYPE("media_type"),
FAVORITE("favorite"),
WATCHLIST("watchlist"),
NAME("name"),
DESCRIPTION("description");
private final String value;
private PostBody(String value) {
this.value = value;
}
/**
* Get the URL parameter to use
*
* @return
*/
public String getValue() {
return this.value;
}
}

@ -0,0 +1,71 @@
/*
* 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 <http://www.gnu.org/licenses/>.
*
*/
package com.omertron.themoviedbapi.tools;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.omertron.themoviedbapi.MovieDbException;
import java.util.HashMap;
import java.util.Map;
import org.yamj.api.common.exception.ApiExceptionType;
/**
*
* @author Stuart.Boston
*/
public class PostTools {
// Jackson JSON configuration
protected static final ObjectMapper MAPPER = new ObjectMapper();
private final Map<String, Object> values = new HashMap<String, Object>();
public PostTools() {
}
public PostTools add(PostBody key, Object value) {
values.put(key.getValue(), value);
return this;
}
public PostTools add(String key, Object value) {
values.put(key, value);
return this;
}
public String build() throws MovieDbException {
return convertToJson(values);
}
/**
* Use Jackson to convert Map to JSON string.
*
* @param map
* @return
* @throws MovieDbException
*/
private String convertToJson(Map<String, ?> map) throws MovieDbException {
try {
return MAPPER.writeValueAsString(map);
} catch (JsonProcessingException ex) {
throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, "JSON conversion failed", "", ex);
}
}
}
Loading…
Cancel
Save