Finished the ApiUrl class to be more generic for TMDb and IMDb ID values

master
Omertron 14 years ago
parent 6133742768
commit 5c809e77fd

@ -1,366 +0,0 @@
/*
* Copyright (c) 2004-2012 YAMJ Members
* http://code.google.com/p/moviejukebox/people/list
*
* Web: http://code.google.com/p/moviejukebox/
*
* This software is licensed under a Creative Commons License
* See this page: http://code.google.com/p/moviejukebox/wiki/License
*
* For any reuse or distribution, you must make clear to others the
* license terms of this work.
*/
package com.moviejukebox.themoviedb;
import com.moviejukebox.themoviedb.model.*;
import com.moviejukebox.themoviedb.tools.ApiUrl;
import com.moviejukebox.themoviedb.tools.FilteringLayout;
import com.moviejukebox.themoviedb.wrapper.*;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.Logger;
import org.codehaus.jackson.map.DeserializationConfig;
import org.codehaus.jackson.map.ObjectMapper;
/**
* The MovieDB API. This is for version 3 of the API as specified here:
* http://help.themoviedb.org/kb/api/about-3
*
* @author stuart.boston
*/
public class TheMovieDB {
private static final Logger LOGGER = Logger.getLogger(TheMovieDB.class);
private static String apiKey;
private static TmdbConfiguration tmdbConfig;
/*
* TheMovieDB API URLs
*/
protected static final String TMDB_API_BASE = "http://api.themoviedb.org/3/";
/*
* API Methods
*/
protected static final ApiUrl TMDB_CONFIG_URL = new ApiUrl("configuration");
protected static final ApiUrl TMDB_SEARCH_MOVIE = new ApiUrl("search/movie");
protected static final ApiUrl TMDB_SEARCH_PEOPLE = new ApiUrl("search/person");
protected static final ApiUrl TMDB_COLLECTION_INFO = new ApiUrl("collection/");
protected static final ApiUrl TMDB_MOVIE_INFO = new ApiUrl("movie/");
protected static final ApiUrl TMDB_MOVIE_ALT_TITLES = new ApiUrl("movie/", "/alternative_titles");
protected static final ApiUrl TMDB_MOVIE_CASTS = new ApiUrl("movie/", "/casts");
protected static final ApiUrl TMDB_MOVIE_IMAGES = new ApiUrl("movie/", "/images");
protected static final ApiUrl TMDB_MOVIE_KEYWORDS = new ApiUrl("movie/", "/keywords");
protected static final ApiUrl TMDB_MOVIE_RELEASE_INFO = new ApiUrl("movie/", "/releases");
protected static final ApiUrl TMDB_MOVIE_TRAILERS = new ApiUrl("movie/", "/trailers");
protected static final ApiUrl TMDB_MOVIE_TRANSLATIONS = new ApiUrl("movie/", "/translations");
protected static final ApiUrl TMDB_PERSON_INFO = new ApiUrl("person");
protected static final ApiUrl TMDB_PERSON_CREDITS = new ApiUrl("person/", "/credits");
protected static final ApiUrl TMDB_PERSON_IMAGES = new ApiUrl("person/", "/images");
protected static final ApiUrl TMDB_LATEST_MOVIE = new ApiUrl("latest/movie");
/*
* Jackson JSON configuration
*/
private static ObjectMapper mapper = new ObjectMapper();
public TheMovieDB(String apiKey) throws IOException {
TheMovieDB.apiKey = apiKey;
URL configUrl = TMDB_CONFIG_URL.getQueryUrl("");
mapper.configure(DeserializationConfig.Feature.UNWRAP_ROOT_VALUE, true);
tmdbConfig = mapper.readValue(configUrl, TmdbConfiguration.class);
mapper.configure(DeserializationConfig.Feature.UNWRAP_ROOT_VALUE, false);
FilteringLayout.addApiKey(apiKey);
}
public static String getApiKey() {
return apiKey;
}
public static String getApiBase() {
return TMDB_API_BASE;
}
/**
* Search Movies This is a good starting point to start finding movies on
* TMDb. The idea is to be a quick and light method so you can iterate
* through movies quickly. http://help.themoviedb.org/kb/api/search-movies
*/
public List<MovieDB> searchMovie(String movieName, String language, boolean allResults) {
try {
URL url = TMDB_SEARCH_MOVIE.getQueryUrl(movieName, language, 1);
WrapperResultList resultList = mapper.readValue(url, WrapperResultList.class);
return resultList.getResults();
} catch (IOException ex) {
LOGGER.warn("Failed to find movie: " + ex.getMessage());
return new ArrayList<MovieDB>();
}
}
/**
* This method is used to retrieve all of the basic movie information. It
* will return the single highest rated poster and backdrop.
*
* @param movieId
* @param language
* @return
*/
public MovieDB getMovieInfo(int movieId, String language) {
try {
URL url = TMDB_MOVIE_INFO.getIdUrl(movieId, language);
return mapper.readValue(url, MovieDB.class);
} catch (IOException ex) {
LOGGER.warn("Failed to get movie info: " + ex.getMessage());
}
return new MovieDB();
}
/**
* This method is used to retrieve all of the basic movie information. It
* will return the single highest rated poster and backdrop.
*
* @param movieId
* @param language
* @return
*/
public MovieDB getMovieInfoImdb(String imdbId, String language) {
try {
URL url = TMDB_MOVIE_INFO.getIdUrl(imdbId, language);
return mapper.readValue(url, MovieDB.class);
} catch (IOException ex) {
LOGGER.warn("Failed to get movie info: " + ex.getMessage());
}
return new MovieDB();
}
/**
* This method is used to retrieve all of the alternative titles we have for
* a particular movie.
*
* @param movieId
* @param country
* @return
*/
public List<AlternativeTitle> getMovieAlternativeTitles(int movieId, String country) {
try {
URL url = TMDB_MOVIE_ALT_TITLES.getIdUrl(movieId, country);
WrapperAlternativeTitles at = mapper.readValue(url, WrapperAlternativeTitles.class);
return at.getTitles();
} catch (IOException ex) {
LOGGER.warn("Failed to get movie alternative titles: " + ex.getMessage());
}
return new ArrayList<AlternativeTitle>();
}
/**
* This method is used to retrieve all of the movie cast information.
*
* @param movieId
* @return
*/
public List<Person> getMovieCasts(int movieId) {
List<Person> people = new ArrayList<Person>();
try {
URL url = TMDB_MOVIE_CASTS.getIdUrl(movieId);
WrapperMovieCasts mc = mapper.readValue(url, WrapperMovieCasts.class);
// Add a cast member
for (PersonCast cast : mc.getCast()) {
Person person = new Person();
person.addCast(cast.getId(), cast.getName(), cast.getProfilePath(), cast.getCharacter(), cast.getOrder());
people.add(person);
}
// Add a crew member
for (PersonCrew crew : mc.getCrew()) {
Person person = new Person();
person.addCrew(crew.getId(), crew.getName(), crew.getProfilePath(), crew.getDepartment(), crew.getJob());
people.add(person);
}
return people;
} catch (IOException ex) {
LOGGER.warn("Failed to get movie casts: " + ex.getMessage());
}
return people;
}
/**
* This method should be used when youre wanting to retrieve all of the
* images for a particular movie.
*
* @param movieId
* @param language
* @return
*/
public List<Artwork> getMovieImages(int movieId, String language) {
List<Artwork> artwork = new ArrayList<Artwork>();
try {
URL url = TMDB_MOVIE_IMAGES.getIdUrl(movieId, language);
WrapperMovieImages mi = mapper.readValue(url, WrapperMovieImages.class);
// Add all the posters to the list
for (Artwork poster : mi.getPosters()) {
poster.setArtworkType(ArtworkType.POSTER);
artwork.add(poster);
}
// Add all the backdrops to the list
for (Artwork backdrop : mi.getBackdrops()) {
backdrop.setArtworkType(ArtworkType.BACKDROP);
artwork.add(backdrop);
}
return artwork;
} catch (IOException ex) {
LOGGER.warn("Failed to get movie images: " + ex.getMessage());
}
return artwork;
}
/**
* This method is used to retrieve all of the keywords that have been added
* to a particular movie. Currently, only English keywords exist.
*
* @param movieId
* @return
*/
public List<Keyword> getMovieKeywords(int movieId) {
try {
URL url = TMDB_MOVIE_KEYWORDS.getIdUrl(movieId);
WrapperMovieKeywords mk = mapper.readValue(url, WrapperMovieKeywords.class);
return mk.getKeywords();
} catch (IOException ex) {
LOGGER.warn("Failed to get movie keywords: " + ex.getMessage());
}
return new ArrayList<Keyword>();
}
/**
* This method is used to retrieve all of the release and certification data
* we have for a specific movie.
*
* @param movieId
* @param language
* @return
*/
public List<ReleaseInfo> getMovieReleaseInfo(int movieId, String language) {
try {
URL url = TMDB_MOVIE_RELEASE_INFO.getIdUrl(movieId);
WrapperReleaseInfo ri = mapper.readValue(url, WrapperReleaseInfo.class);
return ri.getCountries();
} catch (IOException ex) {
LOGGER.warn("Failed to get movie release information: " + ex.getMessage());
}
return new ArrayList<ReleaseInfo>();
}
/**
* This method is used to retrieve all of the trailers for a particular
* movie. Supported sites are YouTube and QuickTime.
*
* @param movieId
* @param language
* @return
*/
public List<Trailer> getMovieTrailers(int movieId, String language) {
List<Trailer> trailers = new ArrayList<Trailer>();
try {
URL url = TMDB_MOVIE_TRAILERS.getIdUrl(movieId);
WrapperTrailers wt = mapper.readValue(url, WrapperTrailers.class);
// Add the trailer to the return list along with it's source
for (Trailer trailer : wt.getQuicktime()) {
trailer.setWebsite(Trailer.WEBSITE_QUICKTIME);
trailers.add(trailer);
}
// Add the trailer to the return list along with it's source
for (Trailer trailer : wt.getYoutube()) {
trailer.setWebsite(Trailer.WEBSITE_YOUTUBE);
trailers.add(trailer);
}
return trailers;
} catch (IOException ex) {
LOGGER.warn("Failed to get movie trailers: " + ex.getMessage());
}
return trailers;
}
/**
* This method is used to retrieve a list of the available translations for
* a specific movie.
*
* @param movieId
* @return
*/
public List<Translation> getMovieTranslations(int movieId) {
try {
URL url = TMDB_MOVIE_TRANSLATIONS.getIdUrl(movieId);
WrapperTranslations wt = mapper.readValue(url, WrapperTranslations.class);
return wt.getTranslations();
} catch (IOException ex) {
LOGGER.warn("Failed to get movie tranlations: " + ex.getMessage());
}
return new ArrayList<Translation>();
}
/**
* 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 movieId
* @param language
* @return
*/
public CollectionInfo getCollectionInfo(int movieId, String language) {
try {
URL url = TMDB_COLLECTION_INFO.getIdUrl(movieId);
return mapper.readValue(url, CollectionInfo.class);
} catch (IOException ex) {
return new CollectionInfo();
}
}
/**
* Get the configuration information
*
* @return
*/
public TmdbConfiguration getConfiguration() {
return tmdbConfig;
}
/**
* Generate the full image URL from the size and image path
*
* @param imagePath
* @param requiredSize
* @return
*/
public URL createImageUrl(String imagePath, String requiredSize) {
URL returnUrl = null;
StringBuilder sb;
if (!tmdbConfig.isValidSize(requiredSize)) {
sb = new StringBuilder();
sb.append(" - Invalid size requested: ").append(requiredSize);
LOGGER.warn(sb.toString());
return returnUrl;
}
try {
sb = new StringBuilder(tmdbConfig.getBaseUrl());
sb.append(requiredSize);
sb.append(imagePath);
returnUrl = new URL(sb.toString());
} catch (MalformedURLException ex) {
LOGGER.warn("Failed to create image URL: " + ex.getMessage());
}
return returnUrl;
}
}

@ -12,7 +12,7 @@
*/
package com.moviejukebox.themoviedb.tools;
import com.moviejukebox.themoviedb.TheMovieDB;
import com.moviejukebox.themoviedb.TheMovieDb;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
@ -41,12 +41,8 @@ public class ApiUrl {
private static final String PARAMETER_LANGUAGE = DELIMITER_SUBSEQUENT + "language=";
private static final String PARAMETER_COUNTRY = DELIMITER_SUBSEQUENT + "country=";
private static final String PARAMETER_PAGE = DELIMITER_SUBSEQUENT + "page=";
private static final String DEFAULT_QUERY = "";
private static final int DEFAULT_ID = -1;
private static final int IMDB_ID_TRIGGER = 0; // Use to determine its an IMDB search
private static final String DEFAULT_LANGUAGE = "";
private static final String DEFAULT_COUNTRY = "";
private static final int DEFAULT_PAGE = -1;
private static final String DEFAULT_STRING = "";
private static final int DEFAULT_INT = -1;
/*
* Properties
*/
@ -54,11 +50,20 @@ public class ApiUrl {
private String submethod;
//<editor-fold defaultstate="collapsed" desc="Constructor Methods">
/**
* Constructor for the simple API URL method without a sub-method
* @param method
*/
public ApiUrl(String method) {
this.method = method;
this.submethod = DEFAULT_QUERY;
this.submethod = DEFAULT_STRING;
}
/**
* Constructor for the API URL with a sub-method
* @param method
* @param submethod
*/
public ApiUrl(String method, String submethod) {
this.method = method;
this.submethod = submethod;
@ -75,8 +80,8 @@ public class ApiUrl {
* @param page
* @return
*/
private URL getFullUrl(String query, int tmdbId, String imdbId, String language, String country, int page) {
StringBuilder urlString = new StringBuilder(TheMovieDB.getApiBase());
private URL getFullUrl(String query, String movieId, String language, String country, int page) {
StringBuilder urlString = new StringBuilder(TheMovieDb.getApiBase());
// Get the start of the URL
urlString.append(method);
@ -95,13 +100,8 @@ public class ApiUrl {
}
// Append the ID if provided
if (tmdbId > DEFAULT_ID) {
urlString.append(tmdbId);
}
// Append the IMDB ID if provided
if (StringUtils.isNotBlank(imdbId)) {
urlString.append(imdbId);
if (StringUtils.isNotBlank(movieId)) {
urlString.append(movieId);
}
// Append the suffix of the API URL
@ -116,7 +116,7 @@ public class ApiUrl {
urlString.append(DELIMITER_SUBSEQUENT);
}
urlString.append(PARAMETER_API_KEY);
urlString.append(TheMovieDB.getApiKey());
urlString.append(TheMovieDb.getApiKey());
// Append the language to the URL
if (StringUtils.isNotBlank(language)) {
@ -131,7 +131,7 @@ public class ApiUrl {
}
// Append the page to the URL
if (page > DEFAULT_PAGE) {
if (page > DEFAULT_INT) {
urlString.append(PARAMETER_PAGE);
urlString.append(page);
}
@ -146,52 +146,96 @@ public class ApiUrl {
}
/**
* Create an URL using a query (string) and optional language and page
* Create an URL using the query (string), language and page
*
* @param query
* @param language
* @param page
* @return
*/
public URL getQueryUrl(String query, String language, int page) {
return getFullUrl(query, DEFAULT_ID, DEFAULT_QUERY, language, null, page);
return getFullUrl(query, DEFAULT_STRING, language, null, page);
}
/**
* Create an URL using the query (string)
* @param query
* @return
*/
public URL getQueryUrl(String query) {
return getQueryUrl(query, DEFAULT_LANGUAGE, DEFAULT_PAGE);
return getQueryUrl(query, DEFAULT_STRING, DEFAULT_INT);
}
/**
* Create an URL using the query (string) and language
* @param query
* @param language
* @return
*/
public URL getQueryUrl(String query, String language) {
return getQueryUrl(query, language, DEFAULT_PAGE);
return getQueryUrl(query, language, DEFAULT_INT);
}
/**
* Create an URL using the TheMovieDB ID and optional language an country codes
* @param tmdbId
* Create an URL using the movie ID, language and country code
*
* @param movieId
* @param language
* @param country
* @return
*/
public URL getIdUrl(int tmdbId, String language, String country) {
return getFullUrl(DEFAULT_QUERY, tmdbId, DEFAULT_QUERY, language, country, DEFAULT_PAGE);
public URL getIdUrl(String movieId, String language, String country) {
return getFullUrl(DEFAULT_STRING, movieId, language, country, DEFAULT_INT);
}
public URL getIdUrl(int tmdbId) {
return getIdUrl(tmdbId, DEFAULT_LANGUAGE, DEFAULT_COUNTRY);
/**
* Create an URL using the movie ID and language
* @param movieId
* @param language
* @return
*/
public URL getIdUrl(String movieId, String language) {
return getIdUrl(movieId, language, DEFAULT_STRING);
}
public URL getIdUrl(int tmdbId, String language) {
return getIdUrl(tmdbId, language, DEFAULT_COUNTRY);
/**
* Create an URL using the movie ID
* @param movieId
* @return
*/
public URL getIdUrl(String movieId) {
return getIdUrl(movieId, DEFAULT_STRING, DEFAULT_STRING);
}
/**
* Get the movie info for an IMDB ID.
* Note, this is a special case
* @param imdbId
* Create an URL using the movie ID, language and country code
*
* @param movieId
* @param language
* @return
* @param country
* @return
*/
public URL getIdUrl(int movieId, String language, String country) {
return getIdUrl(String.valueOf(movieId), language, country);
}
/**
* Create an URL using the movie ID and language
* @param movieId
* @param language
* @return
*/
public URL getIdUrl(int movieId, String language) {
return getIdUrl(String.valueOf(movieId), language, DEFAULT_STRING);
}
/**
* Create an URL using the movie ID
* @param movieId
* @return
*/
public URL getIdUrl(String imdbId, String language) {
return getFullUrl(DEFAULT_QUERY, DEFAULT_ID, imdbId, language, DEFAULT_COUNTRY, DEFAULT_PAGE);
public URL getIdUrl(int movieId) {
return getIdUrl(String.valueOf(movieId), DEFAULT_STRING, DEFAULT_STRING);
}
}

@ -22,23 +22,23 @@ import static org.junit.Assert.*;
import org.junit.*;
/**
* Test cases for TheMovieDB API
* Test cases for TheMovieDb API
*
* @author stuart.boston
*/
public class TheMovieDBTest {
public class TheMovieDbTest {
private static final Logger LOGGER = Logger.getLogger(TheMovieDBTest.class);
private static final Logger LOGGER = Logger.getLogger(TheMovieDbTest.class);
private static final String API_KEY = "5a1a77e2eba8984804586122754f969f";
private static TheMovieDB tmdb;
private static TheMovieDb tmdb;
/*
* Test data
*/
private static final int ID_BLADE_RUNNER = 78;
private static final int ID_STAR_WARS_COLLECTION = 10;
public TheMovieDBTest() throws IOException {
tmdb = new TheMovieDB(API_KEY);
public TheMovieDbTest() throws IOException {
tmdb = new TheMovieDb(API_KEY);
}
@BeforeClass
@ -58,7 +58,7 @@ public class TheMovieDBTest {
}
/**
* Test of getConfiguration method, of class TheMovieDB.
* Test of getConfiguration method, of class TheMovieDb.
*/
@Test
public void testConfiguration() throws IOException {
@ -74,7 +74,7 @@ public class TheMovieDBTest {
}
/**
* Test of searchMovie method, of class TheMovieDB.
* Test of searchMovie method, of class TheMovieDb.
*/
@Test
public void testSearchMovie() throws UnsupportedEncodingException {
@ -94,7 +94,7 @@ public class TheMovieDBTest {
}
/**
* Test of getMovieInfo method, of class TheMovieDB.
* Test of getMovieInfo method, of class TheMovieDb.
*/
@Test
public void testGetMovieInfo() {
@ -105,7 +105,7 @@ public class TheMovieDBTest {
}
/**
* Test of getMovieAlternativeTitles method, of class TheMovieDB.
* Test of getMovieAlternativeTitles method, of class TheMovieDb.
*/
@Test
public void testGetMovieAlternativeTitles() {
@ -121,7 +121,7 @@ public class TheMovieDBTest {
}
/**
* Test of getMovieCasts method, of class TheMovieDB.
* Test of getMovieCasts method, of class TheMovieDb.
*/
@Test
public void testGetMovieCasts() {
@ -149,7 +149,7 @@ public class TheMovieDBTest {
}
/**
* Test of getMovieImages method, of class TheMovieDB.
* Test of getMovieImages method, of class TheMovieDb.
*/
@Test
public void testGetMovieImages() {
@ -160,7 +160,7 @@ public class TheMovieDBTest {
}
/**
* Test of getMovieKeywords method, of class TheMovieDB.
* Test of getMovieKeywords method, of class TheMovieDb.
*/
@Test
public void testGetMovieKeywords() {
@ -170,7 +170,7 @@ public class TheMovieDBTest {
}
/**
* Test of getMovieReleaseInfo method, of class TheMovieDB.
* Test of getMovieReleaseInfo method, of class TheMovieDb.
*/
@Test
public void testGetMovieReleaseInfo() {
@ -180,7 +180,7 @@ public class TheMovieDBTest {
}
/**
* Test of getMovieTrailers method, of class TheMovieDB.
* Test of getMovieTrailers method, of class TheMovieDb.
*/
@Test
public void testGetMovieTrailers() {
@ -190,7 +190,7 @@ public class TheMovieDBTest {
}
/**
* Test of getMovieTranslations method, of class TheMovieDB.
* Test of getMovieTranslations method, of class TheMovieDb.
*/
@Test
public void testGetMovieTranslations() {
@ -200,7 +200,7 @@ public class TheMovieDBTest {
}
/**
* Test of getCollectionInfo method, of class TheMovieDB.
* Test of getCollectionInfo method, of class TheMovieDb.
*/
@Test
public void testGetCollectionInfo() {
@ -219,7 +219,7 @@ public class TheMovieDBTest {
}
/**
* Test of getMovieInfoImdb method, of class TheMovieDB.
* Test of getMovieInfoImdb method, of class TheMovieDb.
*/
@Test
public void testGetMovieInfoImdb() {

Loading…
Cancel
Save