diff --git a/themoviedbapi/src/main/java/com/moviejukebox/themoviedb/TheMovieDb.java b/themoviedbapi/src/main/java/com/moviejukebox/themoviedb/TheMovieDb.java index dc83eb73a..984b61b36 100644 --- a/themoviedbapi/src/main/java/com/moviejukebox/themoviedb/TheMovieDb.java +++ b/themoviedbapi/src/main/java/com/moviejukebox/themoviedb/TheMovieDb.java @@ -1,518 +1,522 @@ -/* - * 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.commons.lang.StringUtils; -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 String apiKey; - private TmdbConfiguration tmdbConfig; - /* - * API Methods These are not set to static so that multiple instances of the - * API can co-exist - */ - private final String BASE_MOVIE = "movie/"; - private final String BASE_PERSON = "person/"; - private final ApiUrl TMDB_CONFIG_URL = new ApiUrl(this, "configuration"); - private final ApiUrl TMDB_SEARCH_MOVIE = new ApiUrl(this, "search/movie"); - private final ApiUrl TMDB_SEARCH_PEOPLE = new ApiUrl(this, "search/person"); - private final ApiUrl TMDB_COLLECTION_INFO = new ApiUrl(this, "collection/"); - private final ApiUrl TMDB_MOVIE_INFO = new ApiUrl(this, BASE_MOVIE); - private final ApiUrl TMDB_MOVIE_ALT_TITLES = new ApiUrl(this, BASE_MOVIE, "/alternative_titles"); - private final ApiUrl TMDB_MOVIE_CASTS = new ApiUrl(this, BASE_MOVIE, "/casts"); - private final ApiUrl TMDB_MOVIE_IMAGES = new ApiUrl(this, BASE_MOVIE, "/images"); - private final ApiUrl TMDB_MOVIE_KEYWORDS = new ApiUrl(this, BASE_MOVIE, "/keywords"); - private final ApiUrl TMDB_MOVIE_RELEASE_INFO = new ApiUrl(this, BASE_MOVIE, "/releases"); - private final ApiUrl TMDB_MOVIE_TRAILERS = new ApiUrl(this, BASE_MOVIE, "/trailers"); - private final ApiUrl TMDB_MOVIE_TRANSLATIONS = new ApiUrl(this, BASE_MOVIE, "/translations"); - private final ApiUrl TMDB_PERSON_INFO = new ApiUrl(this, BASE_PERSON); - private final ApiUrl TMDB_PERSON_CREDITS = new ApiUrl(this, BASE_PERSON, "/credits"); - private final ApiUrl TMDB_PERSON_IMAGES = new ApiUrl(this, BASE_PERSON, "/images"); - private final ApiUrl TMDB_LATEST_MOVIE = new ApiUrl(this, "latest/movie"); - - /* - * Jackson JSON configuration - */ - private static ObjectMapper mapper = new ObjectMapper(); - - /** - * API for The Movie Db. - * - * @param apiKey - * @throws IOException - */ - public TheMovieDb(String apiKey) throws IOException { - this.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 String getApiKey() { - return apiKey; - } - - /** - * 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 - * TODO: Make the allResults work - */ - public List 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(); - } - } - - /** - * 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 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(); - } - - /** - * This method is used to retrieve all of the movie cast information. TODO: - * Add a function to enrich the data with the people methods - * - * @param movieId - * @return - */ - public List getMovieCasts(int movieId) { - List people = new ArrayList(); - - 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 you’re wanting to retrieve all of the - * images for a particular movie. - * - * @param movieId - * @param language - * @return - */ - public List getMovieImages(int movieId, String language) { - List artwork = new ArrayList(); - try { - URL url = TMDB_MOVIE_IMAGES.getIdUrl(movieId, language); - WrapperImages mi = mapper.readValue(url, WrapperImages.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 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(); - } - - /** - * 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 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(); - } - - /** - * 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 getMovieTrailers(int movieId, String language) { - List trailers = new ArrayList(); - 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 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(); - } - - /** - * 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; - } - - /** - * This is a good starting point to start finding people on TMDb. The idea - * is to be a quick and light method so you can iterate through people - * quickly. TODO: Fix allResults - */ - public List searchPeople(String personName, boolean allResults) { - - try { - URL url = TMDB_SEARCH_PEOPLE.getQueryUrl(personName, "", 1); - WrapperPerson resultList = mapper.readValue(url, WrapperPerson.class); - return resultList.getResults(); - } catch (IOException ex) { - LOGGER.warn("Failed to find person: " + ex.getMessage()); - return new ArrayList(); - } - } - - /** - * This method is used to retrieve all of the basic person information. It - * will return the single highest rated profile image. - * - * @param personId - * @return - */ - public Person getPersonInfo(int personId) { - try { - URL url = TMDB_PERSON_INFO.getIdUrl(personId); - return mapper.readValue(url, Person.class); - } catch (IOException ex) { - LOGGER.warn("Failed to get movie info: " + ex.getMessage()); - return new Person(); - } - } - - /** - * This method is used to retrieve all of the cast & crew information for - * the person. It will return the single highest rated poster for each movie - * record. - * - * @param personId - * @return - */ - public List getPersonCredits(int personId) { - List personCredits = new ArrayList(); - - try { - URL url = TMDB_PERSON_CREDITS.getIdUrl(personId); - WrapperPersonCredits pc = mapper.readValue(url, WrapperPersonCredits.class); - - // Add a cast member - for (PersonCredit cast : pc.getCast()) { - cast.setPersonType(PersonType.CAST); - personCredits.add(cast); - } - - // Add a crew member - for (PersonCredit crew : pc.getCrew()) { - crew.setPersonType(PersonType.CREW); - personCredits.add(crew); - } - - return personCredits; - } catch (IOException ex) { - LOGGER.warn("Failed to get person credits: " + ex.getMessage()); - return personCredits; - } - } - - /** - * This method is used to retrieve all of the profile images for a person. - * - * @param personId - * @return - */ - public List getPersonImages(int personId) { - List personImages = new ArrayList(); - - try { - URL url = TMDB_PERSON_IMAGES.getIdUrl(personId); - WrapperImages images = mapper.readValue(url, WrapperImages.class); - - // Update the image type - for (Artwork artwork : images.getProfiles()) { - artwork.setArtworkType(ArtworkType.PROFILE); - personImages.add(artwork); - } - - return personImages; - } catch (IOException ex) { - LOGGER.warn("Failed to get person images: " + ex.getMessage()); - return personImages; - } - } - - /** - * This method is used to retrieve the newest movie that was added to TMDb. - * @return - */ - public MovieDb getLatestMovie() { - try { - URL url = TMDB_LATEST_MOVIE.getIdUrl(""); - return mapper.readValue(url, MovieDb.class); - } catch (IOException ex) { - LOGGER.warn("Failed to get latest movie: " + ex.getMessage()); - return new MovieDb(); - } - } - - /** - * Compare the MovieDB object with a title & year - * - * @param moviedb The moviedb object to compare too - * @param title The title of the movie to compare - * @param year The year of the movie to compare - * @return True if there is a match, False otherwise. - */ - public static boolean compareMovies(MovieDb moviedb, String title, String year) { - if ((moviedb == null) || (StringUtils.isBlank(title))) { - return false; - } - - if (StringUtils.isNotBlank(year)) { - if (StringUtils.isNotBlank(moviedb.getReleaseDate())) { - // Compare with year - String movieYear = moviedb.getReleaseDate().substring(0, 4); - if (movieYear.equals(year)) { - if (moviedb.getOriginalTitle().equalsIgnoreCase(title)) { - return true; - } - - if (moviedb.getTitle().equalsIgnoreCase(title)) { - return true; - } - } - } - } else { - // Compare without year - if (moviedb.getOriginalTitle().equalsIgnoreCase(title)) { - return true; - } - - if (moviedb.getTitle().equalsIgnoreCase(title)) { - return true; - } - } - return false; - } -} +/* + * 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.commons.lang.StringUtils; +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 String apiKey; + private TmdbConfiguration tmdbConfig; + /* + * API Methods These are not set to static so that multiple instances of the + * API can co-exist + */ + private static final String BASE_MOVIE = "movie/"; + private static final String BASE_PERSON = "person/"; + private final ApiUrl tmdbConfigUrl = new ApiUrl(this, "configuration"); + private final ApiUrl tmdbSearchMovie = new ApiUrl(this, "search/movie"); + private final ApiUrl tmdbSearchPeople = new ApiUrl(this, "search/person"); + private final ApiUrl tmdbCollectionInfo = new ApiUrl(this, "collection/"); + private final ApiUrl tmdbMovieInfo = new ApiUrl(this, BASE_MOVIE); + private final ApiUrl tmdbMovieAltTitles = new ApiUrl(this, BASE_MOVIE, "/alternative_titles"); + private final ApiUrl tmdbMovieCasts = new ApiUrl(this, BASE_MOVIE, "/casts"); + private final ApiUrl tmdbMovieImages = new ApiUrl(this, BASE_MOVIE, "/images"); + private final ApiUrl tmdbMovieKeywords = new ApiUrl(this, BASE_MOVIE, "/keywords"); + private final ApiUrl tmdbMovieReleaseInfo = new ApiUrl(this, BASE_MOVIE, "/releases"); + private final ApiUrl tmdbMovieTrailers = new ApiUrl(this, BASE_MOVIE, "/trailers"); + private final ApiUrl tmdbMovieTranslations = new ApiUrl(this, BASE_MOVIE, "/translations"); + private final ApiUrl tmdbPersonInfo = new ApiUrl(this, BASE_PERSON); + private final ApiUrl tmdbPersonCredits = new ApiUrl(this, BASE_PERSON, "/credits"); + private final ApiUrl tmdbPersonImages = new ApiUrl(this, BASE_PERSON, "/images"); + private final ApiUrl tmdbLatestMovie = new ApiUrl(this, "latest/movie"); + + /* + * Jackson JSON configuration + */ + private static ObjectMapper mapper = new ObjectMapper(); + + /** + * API for The Movie Db. + * + * @param apiKey + * @throws IOException + */ + public TheMovieDb(String apiKey) throws IOException { + this.apiKey = apiKey; + URL configUrl = tmdbConfigUrl.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); + } + + /** + * Get the API key that is to be used + * @return + */ + public String getApiKey() { + return apiKey; + } + + /** + * 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 + * TODO: Make the allResults work + */ + public List searchMovie(String movieName, String language, boolean allResults) { + try { + URL url = tmdbSearchMovie.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(); + } + } + + /** + * 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 = tmdbMovieInfo.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 = tmdbMovieInfo.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 getMovieAlternativeTitles(int movieId, String country) { + try { + URL url = tmdbMovieAltTitles.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(); + } + + /** + * This method is used to retrieve all of the movie cast information. TODO: + * Add a function to enrich the data with the people methods + * + * @param movieId + * @return + */ + public List getMovieCasts(int movieId) { + List people = new ArrayList(); + + try { + URL url = tmdbMovieCasts.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 you’re wanting to retrieve all of the + * images for a particular movie. + * + * @param movieId + * @param language + * @return + */ + public List getMovieImages(int movieId, String language) { + List artwork = new ArrayList(); + try { + URL url = tmdbMovieImages.getIdUrl(movieId, language); + WrapperImages mi = mapper.readValue(url, WrapperImages.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 getMovieKeywords(int movieId) { + try { + URL url = tmdbMovieKeywords.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(); + } + + /** + * 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 getMovieReleaseInfo(int movieId, String language) { + try { + URL url = tmdbMovieReleaseInfo.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(); + } + + /** + * 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 getMovieTrailers(int movieId, String language) { + List trailers = new ArrayList(); + try { + URL url = tmdbMovieTrailers.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 getMovieTranslations(int movieId) { + try { + URL url = tmdbMovieTranslations.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(); + } + + /** + * 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 = tmdbCollectionInfo.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; + } + + /** + * This is a good starting point to start finding people on TMDb. The idea + * is to be a quick and light method so you can iterate through people + * quickly. TODO: Fix allResults + */ + public List searchPeople(String personName, boolean allResults) { + + try { + URL url = tmdbSearchPeople.getQueryUrl(personName, "", 1); + WrapperPerson resultList = mapper.readValue(url, WrapperPerson.class); + return resultList.getResults(); + } catch (IOException ex) { + LOGGER.warn("Failed to find person: " + ex.getMessage()); + return new ArrayList(); + } + } + + /** + * This method is used to retrieve all of the basic person information. It + * will return the single highest rated profile image. + * + * @param personId + * @return + */ + public Person getPersonInfo(int personId) { + try { + URL url = tmdbPersonInfo.getIdUrl(personId); + return mapper.readValue(url, Person.class); + } catch (IOException ex) { + LOGGER.warn("Failed to get movie info: " + ex.getMessage()); + return new Person(); + } + } + + /** + * This method is used to retrieve all of the cast & crew information for + * the person. It will return the single highest rated poster for each movie + * record. + * + * @param personId + * @return + */ + public List getPersonCredits(int personId) { + List personCredits = new ArrayList(); + + try { + URL url = tmdbPersonCredits.getIdUrl(personId); + WrapperPersonCredits pc = mapper.readValue(url, WrapperPersonCredits.class); + + // Add a cast member + for (PersonCredit cast : pc.getCast()) { + cast.setPersonType(PersonType.CAST); + personCredits.add(cast); + } + + // Add a crew member + for (PersonCredit crew : pc.getCrew()) { + crew.setPersonType(PersonType.CREW); + personCredits.add(crew); + } + + return personCredits; + } catch (IOException ex) { + LOGGER.warn("Failed to get person credits: " + ex.getMessage()); + return personCredits; + } + } + + /** + * This method is used to retrieve all of the profile images for a person. + * + * @param personId + * @return + */ + public List getPersonImages(int personId) { + List personImages = new ArrayList(); + + try { + URL url = tmdbPersonImages.getIdUrl(personId); + WrapperImages images = mapper.readValue(url, WrapperImages.class); + + // Update the image type + for (Artwork artwork : images.getProfiles()) { + artwork.setArtworkType(ArtworkType.PROFILE); + personImages.add(artwork); + } + + return personImages; + } catch (IOException ex) { + LOGGER.warn("Failed to get person images: " + ex.getMessage()); + return personImages; + } + } + + /** + * This method is used to retrieve the newest movie that was added to TMDb. + * @return + */ + public MovieDb getLatestMovie() { + try { + URL url = tmdbLatestMovie.getIdUrl(""); + return mapper.readValue(url, MovieDb.class); + } catch (IOException ex) { + LOGGER.warn("Failed to get latest movie: " + ex.getMessage()); + return new MovieDb(); + } + } + + /** + * Compare the MovieDB object with a title & year + * + * @param moviedb The moviedb object to compare too + * @param title The title of the movie to compare + * @param year The year of the movie to compare + * @return True if there is a match, False otherwise. + */ + public static boolean compareMovies(MovieDb moviedb, String title, String year) { + if ((moviedb == null) || (StringUtils.isBlank(title))) { + return false; + } + + if (StringUtils.isNotBlank(year)) { + if (StringUtils.isNotBlank(moviedb.getReleaseDate())) { + // Compare with year + String movieYear = moviedb.getReleaseDate().substring(0, 4); + if (movieYear.equals(year)) { + if (moviedb.getOriginalTitle().equalsIgnoreCase(title)) { + return true; + } + + if (moviedb.getTitle().equalsIgnoreCase(title)) { + return true; + } + } + } + } else { + // Compare without year + if (moviedb.getOriginalTitle().equalsIgnoreCase(title)) { + return true; + } + + if (moviedb.getTitle().equalsIgnoreCase(title)) { + return true; + } + } + return false; + } +} diff --git a/themoviedbapi/src/main/java/com/moviejukebox/themoviedb/model/Collection.java b/themoviedbapi/src/main/java/com/moviejukebox/themoviedb/model/Collection.java index 969710a71..4667edc08 100644 --- a/themoviedbapi/src/main/java/com/moviejukebox/themoviedb/model/Collection.java +++ b/themoviedbapi/src/main/java/com/moviejukebox/themoviedb/model/Collection.java @@ -1,173 +1,167 @@ -/* - * 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.model; - -import org.apache.commons.lang.StringUtils; -import org.apache.log4j.Logger; -import org.codehaus.jackson.annotate.JsonAnySetter; -import org.codehaus.jackson.annotate.JsonProperty; -import org.codehaus.jackson.map.annotate.JsonRootName; - -/** - * - * @author stuart.boston - */ -@JsonRootName("collection") -public class Collection { - - /* - * Logger - */ - private static final Logger LOGGER = Logger.getLogger(Collection.class); - /* - * Properties - */ - @JsonProperty("id") - private int id; - @JsonProperty("title") - private String title; - @JsonProperty("name") - private String name; - @JsonProperty("poster_path") - private String posterPath; - @JsonProperty("backdrop_path") - private String backdropPath; - @JsonProperty("release_date") - private String releaseDate; - - // - public String getBackdropPath() { - return backdropPath; - } - - public int getId() { - return id; - } - - public String getPosterPath() { - return posterPath; - } - - public String getReleaseDate() { - return releaseDate; - } - - public String getTitle() { - if (StringUtils.isBlank(title)) { - return name; - } - return title; - } - - public String getName() { - if (StringUtils.isBlank(name)) { - return title; - } - return name; - } - // - - // - public void setBackdropPath(String backdropPath) { - this.backdropPath = backdropPath; - } - - public void setId(int id) { - this.id = id; - } - - public void setPosterPath(String posterPath) { - this.posterPath = posterPath; - } - - public void setReleaseDate(String releaseDate) { - this.releaseDate = releaseDate; - } - - public void setTitle(String title) { - this.title = title; - } - - public void setName(String name) { - this.name = name; - } - // - - /** - * Handle unknown properties and print a message - * @param key - * @param value - */ - @JsonAnySetter - public void handleUnknown(String key, Object value) { - StringBuilder sb = new StringBuilder(); - sb.append("Unknown property: '").append(key); - sb.append("' value: '").append(value).append("'"); - LOGGER.warn(sb.toString()); - } - - @Override - public boolean equals(Object obj) { - if (obj == null) { - return false; - } - if (getClass() != obj.getClass()) { - return false; - } - final Collection other = (Collection) obj; - if ((this.backdropPath == null) ? (other.backdropPath != null) : !this.backdropPath.equals(other.backdropPath)) { - return false; - } - if (this.id != other.id) { - return false; - } - if ((this.title == null) ? (other.title != null) : !this.title.equals(other.title)) { - return false; - } - if ((this.name == null) ? (other.name != null) : !this.name.equals(other.name)) { - return false; - } - if ((this.posterPath == null) ? (other.posterPath != null) : !this.posterPath.equals(other.posterPath)) { - return false; - } - if ((this.releaseDate == null) ? (other.releaseDate != null) : !this.releaseDate.equals(other.releaseDate)) { - return false; - } - return true; - } - - @Override - public int hashCode() { - int hash = 7; - hash = 19 * hash + (this.backdropPath != null ? this.backdropPath.hashCode() : 0); - hash = 19 * hash + this.id; - hash = 19 * hash + (this.title != null ? this.title.hashCode() : 0); - hash = 19 * hash + (this.name != null ? this.name.hashCode() : 0); - hash = 19 * hash + (this.posterPath != null ? this.posterPath.hashCode() : 0); - hash = 19 * hash + (this.releaseDate != null ? this.releaseDate.hashCode() : 0); - return hash; - } - - @Override - public String toString() { - StringBuilder sb = new StringBuilder("[Collection="); - sb.append("[id=").append(id); - sb.append("],[title=").append(title); - sb.append("],[name=").append(name); - sb.append("],[posterPath=").append(posterPath); - sb.append("],[backdropPath=").append(backdropPath); - sb.append("],[releaseDate=").append(releaseDate); - sb.append("]]"); - return sb.toString(); - } -} +/* + * 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.model; + +import org.apache.commons.lang.StringUtils; +import org.apache.log4j.Logger; +import org.codehaus.jackson.annotate.JsonAnySetter; +import org.codehaus.jackson.annotate.JsonProperty; +import org.codehaus.jackson.map.annotate.JsonRootName; + +/** + * + * @author stuart.boston + */ +@JsonRootName("collection") +public class Collection { + + /* + * Logger + */ + private static final Logger LOGGER = Logger.getLogger(Collection.class); + /* + * Properties + */ + @JsonProperty("id") + private int id; + @JsonProperty("title") + private String title; + @JsonProperty("name") + private String name; + @JsonProperty("poster_path") + private String posterPath; + @JsonProperty("backdrop_path") + private String backdropPath; + @JsonProperty("release_date") + private String releaseDate; + + // + public String getBackdropPath() { + return backdropPath; + } + + public int getId() { + return id; + } + + public String getPosterPath() { + return posterPath; + } + + public String getReleaseDate() { + return releaseDate; + } + + public String getTitle() { + if (StringUtils.isBlank(title)) { + return name; + } + return title; + } + + public String getName() { + if (StringUtils.isBlank(name)) { + return title; + } + return name; + } + // + + // + public void setBackdropPath(String backdropPath) { + this.backdropPath = backdropPath; + } + + public void setId(int id) { + this.id = id; + } + + public void setPosterPath(String posterPath) { + this.posterPath = posterPath; + } + + public void setReleaseDate(String releaseDate) { + this.releaseDate = releaseDate; + } + + public void setTitle(String title) { + this.title = title; + } + + public void setName(String name) { + this.name = name; + } + // + + /** + * Handle unknown properties and print a message + * @param key + * @param value + */ + @JsonAnySetter + public void handleUnknown(String key, Object value) { + StringBuilder sb = new StringBuilder(); + sb.append("Unknown property: '").append(key); + sb.append("' value: '").append(value).append("'"); + LOGGER.warn(sb.toString()); + } + + @Override + public boolean equals(Object obj) { + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + final Collection other = (Collection) obj; + if ((this.backdropPath == null) ? (other.backdropPath != null) : !this.backdropPath.equals(other.backdropPath)) { + return false; + } + if (this.id != other.id) { + return false; + } + if ((this.title == null) ? (other.title != null) : !this.title.equals(other.title)) { + return false; + } + if ((this.name == null) ? (other.name != null) : !this.name.equals(other.name)) { + return false; + } + return true; + } + + @Override + public int hashCode() { + int hash = 7; + hash = 19 * hash + (this.backdropPath != null ? this.backdropPath.hashCode() : 0); + hash = 19 * hash + this.id; + hash = 19 * hash + (this.title != null ? this.title.hashCode() : 0); + hash = 19 * hash + (this.name != null ? this.name.hashCode() : 0); + hash = 19 * hash + (this.posterPath != null ? this.posterPath.hashCode() : 0); + hash = 19 * hash + (this.releaseDate != null ? this.releaseDate.hashCode() : 0); + return hash; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder("[Collection="); + sb.append("[id=").append(id); + sb.append("],[title=").append(title); + sb.append("],[name=").append(name); + sb.append("],[posterPath=").append(posterPath); + sb.append("],[backdropPath=").append(backdropPath); + sb.append("],[releaseDate=").append(releaseDate); + sb.append("]]"); + return sb.toString(); + } +} diff --git a/themoviedbapi/src/main/java/com/moviejukebox/themoviedb/model/PersonCrew.java b/themoviedbapi/src/main/java/com/moviejukebox/themoviedb/model/PersonCrew.java index 117ec79b2..69456f3fa 100644 --- a/themoviedbapi/src/main/java/com/moviejukebox/themoviedb/model/PersonCrew.java +++ b/themoviedbapi/src/main/java/com/moviejukebox/themoviedb/model/PersonCrew.java @@ -1,149 +1,146 @@ -/* - * 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.model; - -import org.apache.log4j.Logger; -import org.codehaus.jackson.annotate.JsonAnySetter; -import org.codehaus.jackson.annotate.JsonProperty; - -/** - * - * @author Stuart - */ -public class PersonCrew { - /* - * Logger - */ - - private static final Logger LOGGER = Logger.getLogger(PersonCrew.class); - /* - * Properties - */ - @JsonProperty("id") - private int id; - @JsonProperty("department") - private String department; - @JsonProperty("job") - private String job; - @JsonProperty("name") - private String name; - @JsonProperty("profile_path") - private String profilePath; - - // - public String getDepartment() { - return department; - } - - public int getId() { - return id; - } - - public String getJob() { - return job; - } - - public String getName() { - return name; - } - - public String getProfilePath() { - return profilePath; - } - // - - // - public void setDepartment(String department) { - this.department = department; - } - - public void setId(int id) { - this.id = id; - } - - public void setJob(String job) { - this.job = job; - } - - public void setName(String name) { - this.name = name; - } - - public void setProfilePath(String profilePath) { - this.profilePath = profilePath; - } - // - - /** - * Handle unknown properties and print a message - * @param key - * @param value - */ - @JsonAnySetter - public void handleUnknown(String key, Object value) { - StringBuilder sb = new StringBuilder(); - sb.append("Unknown property: '").append(key); - sb.append("' value: '").append(value).append("'"); - LOGGER.warn(sb.toString()); - } - - @Override - public boolean equals(Object obj) { - if (obj == null) { - return false; - } - if (getClass() != obj.getClass()) { - return false; - } - final PersonCrew other = (PersonCrew) obj; - if (this.id != other.id) { - return false; - } - if ((this.department == null) ? (other.department != null) : !this.department.equals(other.department)) { - return false; - } - if ((this.job == null) ? (other.job != null) : !this.job.equals(other.job)) { - return false; - } - if ((this.name == null) ? (other.name != null) : !this.name.equals(other.name)) { - return false; - } - if ((this.profilePath == null) ? (other.profilePath != null) : !this.profilePath.equals(other.profilePath)) { - return false; - } - return true; - } - - @Override - public int hashCode() { - int hash = 7; - hash = 59 * hash + this.id; - hash = 59 * hash + (this.department != null ? this.department.hashCode() : 0); - hash = 59 * hash + (this.job != null ? this.job.hashCode() : 0); - hash = 59 * hash + (this.name != null ? this.name.hashCode() : 0); - hash = 59 * hash + (this.profilePath != null ? this.profilePath.hashCode() : 0); - return hash; - } - - @Override - public String toString() { - StringBuilder sb = new StringBuilder("[PersonCrew="); - sb.append("id=").append(id); - sb.append("],[department=").append(department); - sb.append("],[job=").append(job); - sb.append("],[name=").append(name); - sb.append("],[profilePath=").append(profilePath); - sb.append("]]"); - return sb.toString(); - } -} +/* + * 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.model; + +import org.apache.log4j.Logger; +import org.codehaus.jackson.annotate.JsonAnySetter; +import org.codehaus.jackson.annotate.JsonProperty; + +/** + * + * @author Stuart + */ +public class PersonCrew { + /* + * Logger + */ + + private static final Logger LOGGER = Logger.getLogger(PersonCrew.class); + /* + * Properties + */ + @JsonProperty("id") + private int id; + @JsonProperty("department") + private String department; + @JsonProperty("job") + private String job; + @JsonProperty("name") + private String name; + @JsonProperty("profile_path") + private String profilePath; + + // + public String getDepartment() { + return department; + } + + public int getId() { + return id; + } + + public String getJob() { + return job; + } + + public String getName() { + return name; + } + + public String getProfilePath() { + return profilePath; + } + // + + // + public void setDepartment(String department) { + this.department = department; + } + + public void setId(int id) { + this.id = id; + } + + public void setJob(String job) { + this.job = job; + } + + public void setName(String name) { + this.name = name; + } + + public void setProfilePath(String profilePath) { + this.profilePath = profilePath; + } + // + + /** + * Handle unknown properties and print a message + * @param key + * @param value + */ + @JsonAnySetter + public void handleUnknown(String key, Object value) { + StringBuilder sb = new StringBuilder(); + sb.append("Unknown property: '").append(key); + sb.append("' value: '").append(value).append("'"); + LOGGER.warn(sb.toString()); + } + + @Override + public boolean equals(Object obj) { + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + final PersonCrew other = (PersonCrew) obj; + if (this.id != other.id) { + return false; + } + if ((this.department == null) ? (other.department != null) : !this.department.equals(other.department)) { + return false; + } + if ((this.job == null) ? (other.job != null) : !this.job.equals(other.job)) { + return false; + } + if ((this.name == null) ? (other.name != null) : !this.name.equals(other.name)) { + return false; + } + return true; + } + + @Override + public int hashCode() { + int hash = 7; + hash = 59 * hash + this.id; + hash = 59 * hash + (this.department != null ? this.department.hashCode() : 0); + hash = 59 * hash + (this.job != null ? this.job.hashCode() : 0); + hash = 59 * hash + (this.name != null ? this.name.hashCode() : 0); + hash = 59 * hash + (this.profilePath != null ? this.profilePath.hashCode() : 0); + return hash; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder("[PersonCrew="); + sb.append("id=").append(id); + sb.append("],[department=").append(department); + sb.append("],[job=").append(job); + sb.append("],[name=").append(name); + sb.append("],[profilePath=").append(profilePath); + sb.append("]]"); + return sb.toString(); + } +} diff --git a/themoviedbapi/src/main/java/com/moviejukebox/themoviedb/model/Trailer.java b/themoviedbapi/src/main/java/com/moviejukebox/themoviedb/model/Trailer.java index dfa07f5e3..3985fae3d 100644 --- a/themoviedbapi/src/main/java/com/moviejukebox/themoviedb/model/Trailer.java +++ b/themoviedbapi/src/main/java/com/moviejukebox/themoviedb/model/Trailer.java @@ -1,134 +1,131 @@ -/* - * 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.model; - -import org.apache.log4j.Logger; -import org.codehaus.jackson.annotate.JsonAnySetter; - -/** - * - * @author Stuart - */ -public class Trailer { - /* - * Logger - */ - - private static final Logger LOGGER = Logger.getLogger(Trailer.class); - /* - * Website sources - */ - public static final String WEBSITE_YOUTUBE = "youtube"; - public static final String WEBSITE_QUICKTIME = "quicktime"; - /* - * Properties - */ - private String name; - private String size; - private String source; - private String website; // The website of the trailer - - // - public String getName() { - return name; - } - - public String getSize() { - return size; - } - - public String getSource() { - return source; - } - - public String getWebsite() { - return website; - } - // - - // - public void setName(String name) { - this.name = name; - } - - public void setSize(String size) { - this.size = size; - } - - public void setSource(String source) { - this.source = source; - } - - public void setWebsite(String website) { - this.website = website; - } - // - - /** - * Handle unknown properties and print a message - * @param key - * @param value - */ - @JsonAnySetter - public void handleUnknown(String key, Object value) { - StringBuilder sb = new StringBuilder(); - sb.append("Unknown property: '").append(key); - sb.append("' value: '").append(value).append("'"); - LOGGER.warn(sb.toString()); - } - - @Override - public boolean equals(Object obj) { - if (obj == null) { - return false; - } - if (getClass() != obj.getClass()) { - return false; - } - final Trailer other = (Trailer) obj; - if ((this.name == null) ? (other.name != null) : !this.name.equals(other.name)) { - return false; - } - if ((this.size == null) ? (other.size != null) : !this.size.equals(other.size)) { - return false; - } - if ((this.source == null) ? (other.source != null) : !this.source.equals(other.source)) { - return false; - } - if ((this.website == null) ? (other.website != null) : !this.website.equals(other.website)) { - return false; - } - return true; - } - - @Override - public int hashCode() { - int hash = 7; - hash = 61 * hash + (this.name != null ? this.name.hashCode() : 0); - hash = 61 * hash + (this.size != null ? this.size.hashCode() : 0); - hash = 61 * hash + (this.source != null ? this.source.hashCode() : 0); - hash = 61 * hash + (this.website != null ? this.website.hashCode() : 0); - return hash; - } - - @Override - public String toString() { - StringBuilder sb = new StringBuilder("[Trailer="); - sb.append("name=").append(name); - sb.append("],[size=").append(size); - sb.append("],[source=").append(source); - sb.append("],[website=").append(website); - sb.append("]]"); - return sb.toString(); - } -} +/* + * 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.model; + +import org.apache.log4j.Logger; +import org.codehaus.jackson.annotate.JsonAnySetter; + +/** + * + * @author Stuart + */ +public class Trailer { + /* + * Logger + */ + + private static final Logger LOGGER = Logger.getLogger(Trailer.class); + /* + * Website sources + */ + public static final String WEBSITE_YOUTUBE = "youtube"; + public static final String WEBSITE_QUICKTIME = "quicktime"; + /* + * Properties + */ + private String name; + private String size; + private String source; + private String website; // The website of the trailer + + // + public String getName() { + return name; + } + + public String getSize() { + return size; + } + + public String getSource() { + return source; + } + + public String getWebsite() { + return website; + } + // + + // + public void setName(String name) { + this.name = name; + } + + public void setSize(String size) { + this.size = size; + } + + public void setSource(String source) { + this.source = source; + } + + public void setWebsite(String website) { + this.website = website; + } + // + + /** + * Handle unknown properties and print a message + * @param key + * @param value + */ + @JsonAnySetter + public void handleUnknown(String key, Object value) { + StringBuilder sb = new StringBuilder(); + sb.append("Unknown property: '").append(key); + sb.append("' value: '").append(value).append("'"); + LOGGER.warn(sb.toString()); + } + + @Override + public boolean equals(Object obj) { + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + final Trailer other = (Trailer) obj; + if ((this.name == null) ? (other.name != null) : !this.name.equals(other.name)) { + return false; + } + if ((this.size == null) ? (other.size != null) : !this.size.equals(other.size)) { + return false; + } + if ((this.source == null) ? (other.source != null) : !this.source.equals(other.source)) { + return false; + } + return true; + } + + @Override + public int hashCode() { + int hash = 7; + hash = 61 * hash + (this.name != null ? this.name.hashCode() : 0); + hash = 61 * hash + (this.size != null ? this.size.hashCode() : 0); + hash = 61 * hash + (this.source != null ? this.source.hashCode() : 0); + hash = 61 * hash + (this.website != null ? this.website.hashCode() : 0); + return hash; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder("[Trailer="); + sb.append("name=").append(name); + sb.append("],[size=").append(size); + sb.append("],[source=").append(source); + sb.append("],[website=").append(website); + sb.append("]]"); + return sb.toString(); + } +} diff --git a/themoviedbapi/src/main/java/com/moviejukebox/themoviedb/tools/ApiUrl.java b/themoviedbapi/src/main/java/com/moviejukebox/themoviedb/tools/ApiUrl.java index 7d49c50a2..649816b88 100644 --- a/themoviedbapi/src/main/java/com/moviejukebox/themoviedb/tools/ApiUrl.java +++ b/themoviedbapi/src/main/java/com/moviejukebox/themoviedb/tools/ApiUrl.java @@ -1,248 +1,248 @@ -/* - * 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.tools; - -import com.moviejukebox.themoviedb.TheMovieDb; -import java.io.UnsupportedEncodingException; -import java.net.MalformedURLException; -import java.net.URL; -import java.net.URLEncoder; -import org.apache.commons.lang.StringUtils; -import org.apache.log4j.Logger; - -/** - * The API URL that is used to construct the API call - * - * @author Stuart - */ -public class ApiUrl { - - /* - * Logger - */ - private static final Logger LOGGER = Logger.getLogger(ApiUrl.class); - /* - * TheMovieDb API Base URL - */ - private final String TMDB_API_BASE = "http://api.themoviedb.org/3/"; - /* - * Parameter configuration - */ - private static final String DELIMITER_FIRST = "?"; - private static final String DELIMITER_SUBSEQUENT = "&"; - private static final String PARAMETER_API_KEY = "api_key="; // The API Key is always needed and always first - private static final String PARAMETER_QUERY = "query="; - private static final String PARAMETER_LANGUAGE = DELIMITER_SUBSEQUENT + "language="; - private static final String PARAMETER_COUNTRY = DELIMITER_SUBSEQUENT + "country="; - private static final String PARAMETER_PAGE = DELIMITER_SUBSEQUENT + "page="; - private static final String DEFAULT_STRING = ""; - private static final int DEFAULT_INT = -1; - /* - * Properties - */ - private String method; - private String submethod; - private TheMovieDb TMDb; - - // - /** - * Constructor for the simple API URL method without a sub-method - * @param method - */ - public ApiUrl(TheMovieDb TMDb, String method) { - this.TMDb = TMDb; - this.method = method; - this.submethod = DEFAULT_STRING; - } - - /** - * Constructor for the API URL with a sub-method - * @param method - * @param submethod - */ - public ApiUrl(TheMovieDb TMDb, String method, String submethod) { - this.TMDb = TMDb; - this.method = method; - this.submethod = submethod; - } - // - - /** - * Create the full URL with the API. - * - * @param query - * @param tmdbId - * @param language - * @param country - * @param page - * @return - */ - private URL getFullUrl(String query, String movieId, String language, String country, int page) { - StringBuilder urlString = new StringBuilder(TMDB_API_BASE); - - // Get the start of the URL - urlString.append(method); - - // Append the search term if required - if (StringUtils.isNotBlank(query)) { - urlString.append(DELIMITER_FIRST); - urlString.append(PARAMETER_QUERY); - - try { - urlString.append(URLEncoder.encode(query, "UTF-8")); - } catch (UnsupportedEncodingException ex) { - // If we can't encode it, try it raw - urlString.append(query); - } - } - - // Append the ID if provided - if (StringUtils.isNotBlank(movieId)) { - urlString.append(movieId); - } - - // Append the suffix of the API URL - urlString.append(submethod); - - // Append the key information - if (StringUtils.isBlank(query)) { - // This is the first parameter - urlString.append(DELIMITER_FIRST); - } else { - // The first parameter was the query - urlString.append(DELIMITER_SUBSEQUENT); - } - urlString.append(PARAMETER_API_KEY); - urlString.append(TMDb.getApiKey()); - - // Append the language to the URL - if (StringUtils.isNotBlank(language)) { - urlString.append(PARAMETER_LANGUAGE); - urlString.append(language); - } - - // Append the country to the URL - if (StringUtils.isNotBlank(country)) { - urlString.append(PARAMETER_COUNTRY); - urlString.append(country); - } - - // Append the page to the URL - if (page > DEFAULT_INT) { - urlString.append(PARAMETER_PAGE); - urlString.append(page); - } - - try { - LOGGER.trace("URL: " + urlString.toString()); - return new URL(urlString.toString()); - } catch (MalformedURLException ex) { - LOGGER.warn("Failed to create URL " + urlString.toString()); - return null; - } - } - - /** - * 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_STRING, language, null, page); - } - - /** - * Create an URL using the query (string) - * @param query - * @return - */ - public URL getQueryUrl(String query) { - return getQueryUrl(query, DEFAULT_STRING, DEFAULT_INT); - } - - /** - * Create an URL using the query (string) and language - * @param query - * @param language - * @return - */ - public URL getQueryUrl(String query, String language) { - return getQueryUrl(query, language, DEFAULT_INT); - } - - /** - * Create an URL using the movie ID, language and country code - * - * @param movieId - * @param language - * @param country - * @return - */ - public URL getIdUrl(String movieId, String language, String country) { - return getFullUrl(DEFAULT_STRING, movieId, language, country, DEFAULT_INT); - } - - /** - * Create an URL using the movie ID and language - * @param movieId - * @param language - * @return - */ - public URL getIdUrl(String movieId, String language) { - return getIdUrl(movieId, language, DEFAULT_STRING); - } - - /** - * Create an URL using the movie ID - * @param movieId - * @return - */ - public URL getIdUrl(String movieId) { - return getIdUrl(movieId, DEFAULT_STRING, DEFAULT_STRING); - } - - /** - * Create an URL using the movie ID, language and country code - * - * @param movieId - * @param language - * @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(int movieId) { - return getIdUrl(String.valueOf(movieId), DEFAULT_STRING, DEFAULT_STRING); - } - -} +/* + * 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.tools; + +import com.moviejukebox.themoviedb.TheMovieDb; +import java.io.UnsupportedEncodingException; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLEncoder; +import org.apache.commons.lang.StringUtils; +import org.apache.log4j.Logger; + +/** + * The API URL that is used to construct the API call + * + * @author Stuart + */ +public class ApiUrl { + + /* + * Logger + */ + private static final Logger LOGGER = Logger.getLogger(ApiUrl.class); + /* + * TheMovieDb API Base URL + */ + private static final String TMDB_API_BASE = "http://api.themoviedb.org/3/"; + /* + * Parameter configuration + */ + private static final String DELIMITER_FIRST = "?"; + private static final String DELIMITER_SUBSEQUENT = "&"; + private static final String PARAMETER_API_KEY = "api_key="; // The API Key is always needed and always first + private static final String PARAMETER_QUERY = "query="; + private static final String PARAMETER_LANGUAGE = DELIMITER_SUBSEQUENT + "language="; + private static final String PARAMETER_COUNTRY = DELIMITER_SUBSEQUENT + "country="; + private static final String PARAMETER_PAGE = DELIMITER_SUBSEQUENT + "page="; + private static final String DEFAULT_STRING = ""; + private static final int DEFAULT_INT = -1; + /* + * Properties + */ + private String method; + private String submethod; + private TheMovieDb tmdb; + + // + /** + * Constructor for the simple API URL method without a sub-method + * @param method + */ + public ApiUrl(TheMovieDb tmdb, String method) { + this.tmdb = tmdb; + this.method = method; + this.submethod = DEFAULT_STRING; + } + + /** + * Constructor for the API URL with a sub-method + * @param method + * @param submethod + */ + public ApiUrl(TheMovieDb tmdb, String method, String submethod) { + this.tmdb = tmdb; + this.method = method; + this.submethod = submethod; + } + // + + /** + * Create the full URL with the API. + * + * @param query + * @param tmdbId + * @param language + * @param country + * @param page + * @return + */ + private URL getFullUrl(String query, String movieId, String language, String country, int page) { + StringBuilder urlString = new StringBuilder(TMDB_API_BASE); + + // Get the start of the URL + urlString.append(method); + + // Append the search term if required + if (StringUtils.isNotBlank(query)) { + urlString.append(DELIMITER_FIRST); + urlString.append(PARAMETER_QUERY); + + try { + urlString.append(URLEncoder.encode(query, "UTF-8")); + } catch (UnsupportedEncodingException ex) { + // If we can't encode it, try it raw + urlString.append(query); + } + } + + // Append the ID if provided + if (StringUtils.isNotBlank(movieId)) { + urlString.append(movieId); + } + + // Append the suffix of the API URL + urlString.append(submethod); + + // Append the key information + if (StringUtils.isBlank(query)) { + // This is the first parameter + urlString.append(DELIMITER_FIRST); + } else { + // The first parameter was the query + urlString.append(DELIMITER_SUBSEQUENT); + } + urlString.append(PARAMETER_API_KEY); + urlString.append(tmdb.getApiKey()); + + // Append the language to the URL + if (StringUtils.isNotBlank(language)) { + urlString.append(PARAMETER_LANGUAGE); + urlString.append(language); + } + + // Append the country to the URL + if (StringUtils.isNotBlank(country)) { + urlString.append(PARAMETER_COUNTRY); + urlString.append(country); + } + + // Append the page to the URL + if (page > DEFAULT_INT) { + urlString.append(PARAMETER_PAGE); + urlString.append(page); + } + + try { + LOGGER.trace("URL: " + urlString.toString()); + return new URL(urlString.toString()); + } catch (MalformedURLException ex) { + LOGGER.warn("Failed to create URL " + urlString.toString()); + return null; + } + } + + /** + * 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_STRING, language, null, page); + } + + /** + * Create an URL using the query (string) + * @param query + * @return + */ + public URL getQueryUrl(String query) { + return getQueryUrl(query, DEFAULT_STRING, DEFAULT_INT); + } + + /** + * Create an URL using the query (string) and language + * @param query + * @param language + * @return + */ + public URL getQueryUrl(String query, String language) { + return getQueryUrl(query, language, DEFAULT_INT); + } + + /** + * Create an URL using the movie ID, language and country code + * + * @param movieId + * @param language + * @param country + * @return + */ + public URL getIdUrl(String movieId, String language, String country) { + return getFullUrl(DEFAULT_STRING, movieId, language, country, DEFAULT_INT); + } + + /** + * Create an URL using the movie ID and language + * @param movieId + * @param language + * @return + */ + public URL getIdUrl(String movieId, String language) { + return getIdUrl(movieId, language, DEFAULT_STRING); + } + + /** + * Create an URL using the movie ID + * @param movieId + * @return + */ + public URL getIdUrl(String movieId) { + return getIdUrl(movieId, DEFAULT_STRING, DEFAULT_STRING); + } + + /** + * Create an URL using the movie ID, language and country code + * + * @param movieId + * @param language + * @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(int movieId) { + return getIdUrl(String.valueOf(movieId), DEFAULT_STRING, DEFAULT_STRING); + } + +} diff --git a/themoviedbapi/src/test/java/com/moviejukebox/themoviedb/TheMovieDbTest.java b/themoviedbapi/src/test/java/com/moviejukebox/themoviedb/TheMovieDbTest.java index 99b34ac9f..a09e185cd 100644 --- a/themoviedbapi/src/test/java/com/moviejukebox/themoviedb/TheMovieDbTest.java +++ b/themoviedbapi/src/test/java/com/moviejukebox/themoviedb/TheMovieDbTest.java @@ -1,299 +1,317 @@ -/* - * 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 java.io.IOException; -import java.io.UnsupportedEncodingException; -import java.util.List; -import org.apache.commons.lang.StringUtils; -import org.apache.log4j.Logger; -import static org.junit.Assert.*; -import org.junit.*; - -/** - * Test cases for TheMovieDb API - * - * @author stuart.boston - */ -public class TheMovieDbTest { - - private static final Logger LOGGER = Logger.getLogger(TheMovieDbTest.class); - private static final String API_KEY = "5a1a77e2eba8984804586122754f969f"; - private static TheMovieDb tmdb; - /* - * Test data - */ - private static final int ID_BLADE_RUNNER = 78; - private static final int ID_STAR_WARS_COLLECTION = 10; - private static final int ID_BRUCE_WILLIS = 62; - - public TheMovieDbTest() throws IOException { - tmdb = new TheMovieDb(API_KEY); - } - - @BeforeClass - public static void setUpClass() throws Exception { - } - - @AfterClass - public static void tearDownClass() throws Exception { - } - - @Before - public void setUp() { - } - - @After - public void tearDown() { - } - - /** - * Test of getConfiguration method, of class TheMovieDb. - */ - @Test - public void testConfiguration() throws IOException { - LOGGER.info("Test Configuration"); - - TmdbConfiguration tmdbConfig = tmdb.getConfiguration(); - assertNotNull("Configuration failed", tmdbConfig); - assertTrue("No base URL", StringUtils.isNotBlank(tmdbConfig.getBaseUrl())); - assertTrue("No backdrop sizes", tmdbConfig.getBackdropSizes().size() > 0); - assertTrue("No poster sizes", tmdbConfig.getPosterSizes().size() > 0); - assertTrue("No profile sizes", tmdbConfig.getProfileSizes().size() > 0); - LOGGER.info(tmdbConfig.toString()); - } - - /** - * Test of searchMovie method, of class TheMovieDb. - */ - @Test - public void testSearchMovie() throws UnsupportedEncodingException { - LOGGER.info("searchMovie"); - - // Try a movie with less than 1 page of results - List movieList = tmdb.searchMovie("Blade Runner", "", true); - assertTrue("No movies found, should be at least 1", movieList.size() > 0); - - // Try a russian langugage movie - movieList = tmdb.searchMovie("О чём говорят мужчины", "ru", true); - assertTrue("No movies found, should be at least 1", movieList.size() > 0); - - // Try a movie with more than 20 results - movieList = tmdb.searchMovie("Star Wars", "en", false); - assertTrue("Not enough movies found, should be 20", movieList.size() == 20); - } - - /** - * Test of getMovieInfo method, of class TheMovieDb. - */ - @Test - public void testGetMovieInfo() { - LOGGER.info("getMovieInfo"); - String language = "en"; - MovieDb result = tmdb.getMovieInfo(ID_BLADE_RUNNER, language); - assertEquals("Incorrect movie information", "Blade Runner", result.getOriginalTitle()); - } - - /** - * Test of getMovieAlternativeTitles method, of class TheMovieDb. - */ - @Test - public void testGetMovieAlternativeTitles() { - LOGGER.info("getMovieAlternativeTitles"); - String country = ""; - List results = tmdb.getMovieAlternativeTitles(ID_BLADE_RUNNER, country); - assertTrue("No alternative titles found", results.size() > 0); - - country = "US"; - results = tmdb.getMovieAlternativeTitles(ID_BLADE_RUNNER, country); - assertTrue("No alternative titles found", results.size() > 0); - - } - - /** - * Test of getMovieCasts method, of class TheMovieDb. - */ - @Test - public void testGetMovieCasts() { - LOGGER.info("getMovieCasts"); - List people = tmdb.getMovieCasts(ID_BLADE_RUNNER); - assertTrue("No cast information", people.size() > 0); - - String name1 = "Harrison Ford"; - String name2 = "Charles Knode"; - boolean foundName1 = Boolean.FALSE; - boolean foundName2 = Boolean.FALSE; - - for (Person person : people) { - if (!foundName1 && person.getName().equalsIgnoreCase(name1)) { - foundName1 = Boolean.TRUE; - } - - if (!foundName2 && person.getName().equalsIgnoreCase(name2)) { - foundName2 = Boolean.TRUE; - } - } - assertTrue("Couldn't find " + name1, foundName1); - assertTrue("Couldn't find " + name2, foundName2); - } - - /** - * Test of getMovieImages method, of class TheMovieDb. - */ - @Test - public void testGetMovieImages() { - LOGGER.info("getMovieImages"); - String language = ""; - List result = tmdb.getMovieImages(ID_BLADE_RUNNER, language); - assertFalse("No artwork found", result.isEmpty()); - } - - /** - * Test of getMovieKeywords method, of class TheMovieDb. - */ - @Test - public void testGetMovieKeywords() { - LOGGER.info("getMovieKeywords"); - List result = tmdb.getMovieKeywords(ID_BLADE_RUNNER); - assertFalse("No keywords found", result.isEmpty()); - } - - /** - * Test of getMovieReleaseInfo method, of class TheMovieDb. - */ - @Test - public void testGetMovieReleaseInfo() { - LOGGER.info("getMovieReleaseInfo"); - List result = tmdb.getMovieReleaseInfo(ID_BLADE_RUNNER, ""); - assertFalse("Release information missing", result.isEmpty()); - } - - /** - * Test of getMovieTrailers method, of class TheMovieDb. - */ - @Test - public void testGetMovieTrailers() { - LOGGER.info("getMovieTrailers"); - List result = tmdb.getMovieTrailers(ID_BLADE_RUNNER, ""); - assertFalse("Movie trailers missing", result.isEmpty()); - } - - /** - * Test of getMovieTranslations method, of class TheMovieDb. - */ - @Test - public void testGetMovieTranslations() { - LOGGER.info("getMovieTranslations"); - List result = tmdb.getMovieTranslations(ID_BLADE_RUNNER); - assertFalse("No translations found", result.isEmpty()); - } - - /** - * Test of getCollectionInfo method, of class TheMovieDb. - */ - @Test - public void testGetCollectionInfo() { - LOGGER.info("getCollectionInfo"); - String language = ""; - CollectionInfo result = tmdb.getCollectionInfo(ID_STAR_WARS_COLLECTION, language); - assertFalse("No collection information", result.getParts().isEmpty()); - } - - @Test - public void testCreateImageUrl() { - LOGGER.info("createImageUrl"); - MovieDb movie = tmdb.getMovieInfo(ID_BLADE_RUNNER, ""); - String result = tmdb.createImageUrl(movie.getPosterPath(), "original").toString(); - assertTrue("Error compiling image URL", !result.isEmpty()); - } - - /** - * Test of getMovieInfoImdb method, of class TheMovieDb. - */ - @Test - public void testGetMovieInfoImdb() { - LOGGER.info("getMovieInfoImdb"); - MovieDb result = tmdb.getMovieInfoImdb("tt0076759", "en-US"); - assertTrue("Error getting the movie from IMDB ID", result.getId() == 11); - } - - /** - * Test of getApiKey method, of class TheMovieDb. - */ - @Test - public void testGetApiKey() { - // Not required - } - - /** - * Test of getApiBase method, of class TheMovieDb. - */ - @Test - public void testGetApiBase() { - // Not required - } - - /** - * Test of getConfiguration method, of class TheMovieDb. - */ - @Test - public void testGetConfiguration() { - // Not required - } - - /** - * Test of searchPeople method, of class TheMovieDb. - */ - @Test - public void testSearchPeople() { - LOGGER.info("searchPeople"); - String personName = "Bruce Willis"; - boolean allResults = false; - List result = tmdb.searchPeople(personName, allResults); - assertTrue("Couldn't find the person", result.size() > 0); - } - - /** - * Test of getPersonInfo method, of class TheMovieDb. - */ - @Test - public void testGetPersonInfo() { - LOGGER.info("getPersonInfo"); - Person result = tmdb.getPersonInfo(ID_BRUCE_WILLIS); - assertTrue("Wrong actor returned", result.getId() == ID_BRUCE_WILLIS); - } - - /** - * Test of getPersonCredits method, of class TheMovieDb. - */ - @Test - public void testGetPersonCredits() { - LOGGER.info("getPersonCredits"); - - List people = tmdb.getPersonCredits(ID_BRUCE_WILLIS); - assertTrue("No cast information", people.size() > 0); - } - - /** - * Test of getPersonImages method, of class TheMovieDb. - */ - @Test - public void testGetPersonImages() { - LOGGER.info("getPersonImages"); - - List artwork = tmdb.getPersonImages(ID_BRUCE_WILLIS); - assertTrue("No cast information", artwork.size() > 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 java.io.IOException; +import java.io.UnsupportedEncodingException; +import java.util.List; +import org.apache.commons.lang.StringUtils; +import org.apache.log4j.Logger; +import static org.junit.Assert.*; +import org.junit.*; + +/** + * Test cases for TheMovieDb API + * + * @author stuart.boston + */ +public class TheMovieDbTest { + + private static final Logger LOGGER = Logger.getLogger(TheMovieDbTest.class); + private static final String API_KEY = "5a1a77e2eba8984804586122754f969f"; + private static TheMovieDb tmdb; + /* + * Test data + */ + private static final int ID_BLADE_RUNNER = 78; + private static final int ID_STAR_WARS_COLLECTION = 10; + private static final int ID_BRUCE_WILLIS = 62; + + public TheMovieDbTest() throws IOException { + tmdb = new TheMovieDb(API_KEY); + } + + @BeforeClass + public static void setUpClass() throws Exception { + } + + @AfterClass + public static void tearDownClass() throws Exception { + } + + @Before + public void setUp() { + } + + @After + public void tearDown() { + } + + /** + * Test of getConfiguration method, of class TheMovieDb. + */ + @Test + public void testConfiguration() throws IOException { + LOGGER.info("Test Configuration"); + + TmdbConfiguration tmdbConfig = tmdb.getConfiguration(); + assertNotNull("Configuration failed", tmdbConfig); + assertTrue("No base URL", StringUtils.isNotBlank(tmdbConfig.getBaseUrl())); + assertTrue("No backdrop sizes", tmdbConfig.getBackdropSizes().size() > 0); + assertTrue("No poster sizes", tmdbConfig.getPosterSizes().size() > 0); + assertTrue("No profile sizes", tmdbConfig.getProfileSizes().size() > 0); + LOGGER.info(tmdbConfig.toString()); + } + + /** + * Test of searchMovie method, of class TheMovieDb. + */ + @Test + public void testSearchMovie() throws UnsupportedEncodingException { + LOGGER.info("searchMovie"); + + // Try a movie with less than 1 page of results + List movieList = tmdb.searchMovie("Blade Runner", "", true); + assertTrue("No movies found, should be at least 1", movieList.size() > 0); + + // Try a russian langugage movie + movieList = tmdb.searchMovie("О чём говорят мужчины", "ru", true); + assertTrue("No movies found, should be at least 1", movieList.size() > 0); + + // Try a movie with more than 20 results + movieList = tmdb.searchMovie("Star Wars", "en", false); + assertTrue("Not enough movies found, should be 20", movieList.size() == 20); + } + + /** + * Test of getMovieInfo method, of class TheMovieDb. + */ + @Test + public void testGetMovieInfo() { + LOGGER.info("getMovieInfo"); + String language = "en"; + MovieDb result = tmdb.getMovieInfo(ID_BLADE_RUNNER, language); + assertEquals("Incorrect movie information", "Blade Runner", result.getOriginalTitle()); + } + + /** + * Test of getMovieAlternativeTitles method, of class TheMovieDb. + */ + @Test + public void testGetMovieAlternativeTitles() { + LOGGER.info("getMovieAlternativeTitles"); + String country = ""; + List results = tmdb.getMovieAlternativeTitles(ID_BLADE_RUNNER, country); + assertTrue("No alternative titles found", results.size() > 0); + + country = "US"; + results = tmdb.getMovieAlternativeTitles(ID_BLADE_RUNNER, country); + assertTrue("No alternative titles found", results.size() > 0); + + } + + /** + * Test of getMovieCasts method, of class TheMovieDb. + */ + @Test + public void testGetMovieCasts() { + LOGGER.info("getMovieCasts"); + List people = tmdb.getMovieCasts(ID_BLADE_RUNNER); + assertTrue("No cast information", people.size() > 0); + + String name1 = "Harrison Ford"; + String name2 = "Charles Knode"; + boolean foundName1 = Boolean.FALSE; + boolean foundName2 = Boolean.FALSE; + + for (Person person : people) { + if (!foundName1 && person.getName().equalsIgnoreCase(name1)) { + foundName1 = Boolean.TRUE; + } + + if (!foundName2 && person.getName().equalsIgnoreCase(name2)) { + foundName2 = Boolean.TRUE; + } + } + assertTrue("Couldn't find " + name1, foundName1); + assertTrue("Couldn't find " + name2, foundName2); + } + + /** + * Test of getMovieImages method, of class TheMovieDb. + */ + @Test + public void testGetMovieImages() { + LOGGER.info("getMovieImages"); + String language = ""; + List result = tmdb.getMovieImages(ID_BLADE_RUNNER, language); + assertFalse("No artwork found", result.isEmpty()); + } + + /** + * Test of getMovieKeywords method, of class TheMovieDb. + */ + @Test + public void testGetMovieKeywords() { + LOGGER.info("getMovieKeywords"); + List result = tmdb.getMovieKeywords(ID_BLADE_RUNNER); + assertFalse("No keywords found", result.isEmpty()); + } + + /** + * Test of getMovieReleaseInfo method, of class TheMovieDb. + */ + @Test + public void testGetMovieReleaseInfo() { + LOGGER.info("getMovieReleaseInfo"); + List result = tmdb.getMovieReleaseInfo(ID_BLADE_RUNNER, ""); + assertFalse("Release information missing", result.isEmpty()); + } + + /** + * Test of getMovieTrailers method, of class TheMovieDb. + */ + @Test + public void testGetMovieTrailers() { + LOGGER.info("getMovieTrailers"); + List result = tmdb.getMovieTrailers(ID_BLADE_RUNNER, ""); + assertFalse("Movie trailers missing", result.isEmpty()); + } + + /** + * Test of getMovieTranslations method, of class TheMovieDb. + */ + @Test + public void testGetMovieTranslations() { + LOGGER.info("getMovieTranslations"); + List result = tmdb.getMovieTranslations(ID_BLADE_RUNNER); + assertFalse("No translations found", result.isEmpty()); + } + + /** + * Test of getCollectionInfo method, of class TheMovieDb. + */ + @Test + public void testGetCollectionInfo() { + LOGGER.info("getCollectionInfo"); + String language = ""; + CollectionInfo result = tmdb.getCollectionInfo(ID_STAR_WARS_COLLECTION, language); + assertFalse("No collection information", result.getParts().isEmpty()); + } + + @Test + public void testCreateImageUrl() { + LOGGER.info("createImageUrl"); + MovieDb movie = tmdb.getMovieInfo(ID_BLADE_RUNNER, ""); + String result = tmdb.createImageUrl(movie.getPosterPath(), "original").toString(); + assertTrue("Error compiling image URL", !result.isEmpty()); + } + + /** + * Test of getMovieInfoImdb method, of class TheMovieDb. + */ + @Test + public void testGetMovieInfoImdb() { + LOGGER.info("getMovieInfoImdb"); + MovieDb result = tmdb.getMovieInfoImdb("tt0076759", "en-US"); + assertTrue("Error getting the movie from IMDB ID", result.getId() == 11); + } + + /** + * Test of getApiKey method, of class TheMovieDb. + */ + @Test + public void testGetApiKey() { + // Not required + } + + /** + * Test of getApiBase method, of class TheMovieDb. + */ + @Test + public void testGetApiBase() { + // Not required + } + + /** + * Test of getConfiguration method, of class TheMovieDb. + */ + @Test + public void testGetConfiguration() { + // Not required + } + + /** + * Test of searchPeople method, of class TheMovieDb. + */ + @Test + public void testSearchPeople() { + LOGGER.info("searchPeople"); + String personName = "Bruce Willis"; + boolean allResults = false; + List result = tmdb.searchPeople(personName, allResults); + assertTrue("Couldn't find the person", result.size() > 0); + } + + /** + * Test of getPersonInfo method, of class TheMovieDb. + */ + @Test + public void testGetPersonInfo() { + LOGGER.info("getPersonInfo"); + Person result = tmdb.getPersonInfo(ID_BRUCE_WILLIS); + assertTrue("Wrong actor returned", result.getId() == ID_BRUCE_WILLIS); + } + + /** + * Test of getPersonCredits method, of class TheMovieDb. + */ + @Test + public void testGetPersonCredits() { + LOGGER.info("getPersonCredits"); + + List people = tmdb.getPersonCredits(ID_BRUCE_WILLIS); + assertTrue("No cast information", people.size() > 0); + } + + /** + * Test of getPersonImages method, of class TheMovieDb. + */ + @Test + public void testGetPersonImages() { + LOGGER.info("getPersonImages"); + + List artwork = tmdb.getPersonImages(ID_BRUCE_WILLIS); + assertTrue("No cast information", artwork.size() > 0); + } + + /** + * Test of getLatestMovie method, of class TheMovieDb. + */ + @Test + public void testGetLatestMovie() { + LOGGER.info("getLatestMovie"); + MovieDb result = tmdb.getLatestMovie(); + LOGGER.info(result.toString()); + assertTrue("No latest movie found", result.getId() > 0); +} + + /** + * Test of compareMovies method, of class TheMovieDb. + */ + @Test + public void testCompareMovies() { + // Not required + } +}