From 608c0a9b05ab0fedb826188c23530cd7381c893d Mon Sep 17 00:00:00 2001 From: Stuart Boston Date: Tue, 18 Dec 2012 16:07:10 +0000 Subject: [PATCH] Added getMovieLists method --- .../omertron/themoviedbapi/TheMovieDbApi.java | 34 +++++ .../themoviedbapi/model/MovieList.java | 137 ++++++++++++++++++ .../wrapper/WrapperMovieList.java | 110 ++++++++++++++ .../themoviedbapi/TheMovieDbApiTest.java | 86 ++++++----- 4 files changed, 329 insertions(+), 38 deletions(-) create mode 100644 src/main/java/com/omertron/themoviedbapi/model/MovieList.java create mode 100644 src/main/java/com/omertron/themoviedbapi/wrapper/WrapperMovieList.java diff --git a/src/main/java/com/omertron/themoviedbapi/TheMovieDbApi.java b/src/main/java/com/omertron/themoviedbapi/TheMovieDbApi.java index 9213f5573..d8da1c629 100644 --- a/src/main/java/com/omertron/themoviedbapi/TheMovieDbApi.java +++ b/src/main/java/com/omertron/themoviedbapi/TheMovieDbApi.java @@ -19,6 +19,8 @@ */ package com.omertron.themoviedbapi; +import com.fasterxml.jackson.core.JsonParseException; +import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.omertron.themoviedbapi.MovieDbException.MovieDbExceptionType; import com.omertron.themoviedbapi.model.AlternativeTitle; @@ -29,6 +31,7 @@ import com.omertron.themoviedbapi.model.Company; import com.omertron.themoviedbapi.model.Genre; import com.omertron.themoviedbapi.model.Keyword; import com.omertron.themoviedbapi.model.MovieDb; +import com.omertron.themoviedbapi.model.MovieList; import com.omertron.themoviedbapi.model.Person; import com.omertron.themoviedbapi.model.PersonCast; import com.omertron.themoviedbapi.model.PersonCredit; @@ -53,6 +56,7 @@ import com.omertron.themoviedbapi.wrapper.WrapperImages; import com.omertron.themoviedbapi.wrapper.WrapperMovie; import com.omertron.themoviedbapi.wrapper.WrapperMovieCasts; import com.omertron.themoviedbapi.wrapper.WrapperMovieKeywords; +import com.omertron.themoviedbapi.wrapper.WrapperMovieList; import com.omertron.themoviedbapi.wrapper.WrapperPerson; import com.omertron.themoviedbapi.wrapper.WrapperPersonCredits; import com.omertron.themoviedbapi.wrapper.WrapperReleaseInfo; @@ -63,6 +67,7 @@ import java.net.MalformedURLException; import java.net.URL; import java.util.ArrayList; import java.util.List; +import java.util.logging.Level; import org.apache.commons.lang3.StringUtils; import org.apache.log4j.Logger; @@ -695,6 +700,35 @@ public class TheMovieDbApi { } } + //lists + public List getMovieLists(int movieId, String language, int page) throws MovieDbException { + ApiUrl apiUrl = new ApiUrl(this, BASE_MOVIE, "/lists"); + apiUrl.addArgument(PARAM_ID, movieId); + + if (StringUtils.isNotBlank(language)) { + apiUrl.addArgument(PARAM_LANGUAGE, language); + } + + if (page > 0) { + apiUrl.addArgument(PARAM_PAGE, page); + } + + URL url = apiUrl.buildUrl(); + String webpage = WebBrowser.request(url); + + try { + WrapperMovieList wrapper = mapper.readValue(webpage, WrapperMovieList.class); + return wrapper.getMovieList(); + } catch (IOException ex) { + logger.warn("Failed to get movie lists: " + ex.getMessage()); + throw new MovieDbException(MovieDbExceptionType.MAPPING_FAILED, webpage, ex); + } + } + + //changes + public void getMovieChanges() throws MovieDbException { + } + /** * This method is used to retrieve the newest movie that was added to TMDb. * diff --git a/src/main/java/com/omertron/themoviedbapi/model/MovieList.java b/src/main/java/com/omertron/themoviedbapi/model/MovieList.java new file mode 100644 index 000000000..29f2fac3b --- /dev/null +++ b/src/main/java/com/omertron/themoviedbapi/model/MovieList.java @@ -0,0 +1,137 @@ +/* + * Copyright (c) 2004-2012 Stuart Boston + * + * This file is part of TheMovieDB API. + * + * TheMovieDB API is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * any later version. + * + * TheMovieDB API is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with TheMovieDB API. If not, see . + * + */ +package com.omertron.themoviedbapi.model; + +import com.fasterxml.jackson.annotation.JsonAnySetter; +import com.fasterxml.jackson.annotation.JsonProperty; +import java.io.Serializable; +import org.apache.log4j.Logger; + +/** + * + * @author Stuart + */ +public class MovieList implements Serializable { + + private static final long serialVersionUID = 1L; + + /* + * Logger + */ + private static final Logger logger = Logger.getLogger(MovieList.class); + /* + * Properties + */ + @JsonProperty("description") + private String description; + @JsonProperty("favorite_count") + private int favoriteCount; + @JsonProperty("id") + private String id; + @JsonProperty("item_count") + private int itemCount; + @JsonProperty("iso_639_1") + private String language; + @JsonProperty("name") + private String name; + @JsonProperty("poster_path") + private String posterPath; + + // + public String getDescription() { + return description; + } + + public int getFavoriteCount() { + return favoriteCount; + } + + public String getId() { + return id; + } + + public int getItemCount() { + return itemCount; + } + + public String getLanguage() { + return language; + } + + public String getName() { + return name; + } + + public String getPosterPath() { + return posterPath; + } + + // + + // + public void setDescription(String description) { + this.description = description; + } + + public void setFavoriteCount(int favoriteCount) { + this.favoriteCount = favoriteCount; + } + + public void setId(String id) { + this.id = id; + } + + public void setItemCount(int itemCount) { + this.itemCount = itemCount; + } + + public void setLanguage(String language) { + this.language = language; + } + + public void setName(String name) { + this.name = name; + } + + public void setPosterPath(String posterPath) { + this.posterPath = posterPath; + } + + // + + /** + * 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.trace(sb.toString()); + } + + @Override + public String toString() { + return "MovieList{" + "description=" + description + ", favoriteCount=" + favoriteCount + ", id=" + id + ", itemCount=" + itemCount + ", language=" + language + ", name=" + name + ", posterPath=" + posterPath + '}'; + } +} diff --git a/src/main/java/com/omertron/themoviedbapi/wrapper/WrapperMovieList.java b/src/main/java/com/omertron/themoviedbapi/wrapper/WrapperMovieList.java new file mode 100644 index 000000000..80f025b31 --- /dev/null +++ b/src/main/java/com/omertron/themoviedbapi/wrapper/WrapperMovieList.java @@ -0,0 +1,110 @@ +/* + * Copyright (c) 2004-2012 Stuart Boston + * + * This file is part of TheMovieDB API. + * + * TheMovieDB API is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * any later version. + * + * TheMovieDB API is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with TheMovieDB API. If not, see . + * + */ +package com.omertron.themoviedbapi.wrapper; + +import com.fasterxml.jackson.annotation.JsonAnySetter; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.omertron.themoviedbapi.model.AlternativeTitle; +import com.omertron.themoviedbapi.model.MovieList; +import java.util.List; +import org.apache.log4j.Logger; + +/** + * + * @author Stuart + */ +public class WrapperMovieList { + /* + * Logger + */ + + private static final Logger logger = Logger.getLogger(WrapperMovieList.class); + /* + * Properties + */ + @JsonProperty("id") + private int id; + @JsonProperty("page") + private int page; + @JsonProperty("results") + private List movieList; + @JsonProperty("total_pages") + private int totalPages; + @JsonProperty("total_results") + private int totalResults; + + // + public int getId() { + return id; + } + + public int getPage() { + return page; + } + + public List getMovieList() { + return movieList; + } + + public int getTotalPages() { + return totalPages; + } + + public int getTotalResults() { + return totalResults; + } + // + + // + public void setId(int id) { + this.id = id; + } + + public void setPage(int page) { + this.page = page; + } + + public void setMovieList(List movieList) { + this.movieList = movieList; + } + + public void setTotalPages(int totalPages) { + this.totalPages = totalPages; + } + + public void setTotalResults(int totalResults) { + this.totalResults = totalResults; + } + // + + /** + * 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.trace(sb.toString()); + } +} diff --git a/src/test/java/com/omertron/themoviedbapi/TheMovieDbApiTest.java b/src/test/java/com/omertron/themoviedbapi/TheMovieDbApiTest.java index c16c870a5..79f55cd87 100644 --- a/src/test/java/com/omertron/themoviedbapi/TheMovieDbApiTest.java +++ b/src/test/java/com/omertron/themoviedbapi/TheMovieDbApiTest.java @@ -26,6 +26,7 @@ import com.omertron.themoviedbapi.model.Company; import com.omertron.themoviedbapi.model.Genre; import com.omertron.themoviedbapi.model.Keyword; import com.omertron.themoviedbapi.model.MovieDb; +import com.omertron.themoviedbapi.model.MovieList; import com.omertron.themoviedbapi.model.Person; import com.omertron.themoviedbapi.model.PersonCredit; import com.omertron.themoviedbapi.model.ReleaseInfo; @@ -90,7 +91,7 @@ public class TheMovieDbApiTest { /** * Test of getConfiguration method, of class TheMovieDbApi. */ - @Test + //@Test public void testConfiguration() throws IOException { logger.info("Test Configuration"); @@ -106,7 +107,7 @@ public class TheMovieDbApiTest { /** * Test of searchMovie method, of class TheMovieDbApi. */ - @Test + //@Test public void testSearchMovie() throws MovieDbException { logger.info("searchMovie"); @@ -127,7 +128,7 @@ public class TheMovieDbApiTest { /** * Test of getMovieInfo method, of class TheMovieDbApi. */ - @Test + //@Test public void testGetMovieInfo() throws MovieDbException { logger.info("getMovieInfo"); String language = "en"; @@ -138,7 +139,7 @@ public class TheMovieDbApiTest { /** * Test of getMovieAlternativeTitles method, of class TheMovieDbApi. */ - @Test + //@Test public void testGetMovieAlternativeTitles() throws MovieDbException { logger.info("getMovieAlternativeTitles"); String country = ""; @@ -154,7 +155,7 @@ public class TheMovieDbApiTest { /** * Test of getMovieCasts method, of class TheMovieDbApi. */ - @Test + //@Test public void testGetMovieCasts() throws MovieDbException { logger.info("getMovieCasts"); List people = tmdb.getMovieCasts(ID_MOVIE_BLADE_RUNNER); @@ -181,7 +182,7 @@ public class TheMovieDbApiTest { /** * Test of getMovieImages method, of class TheMovieDbApi. */ - @Test + //@Test public void testGetMovieImages() throws MovieDbException { logger.info("getMovieImages"); String language = ""; @@ -192,7 +193,7 @@ public class TheMovieDbApiTest { /** * Test of getMovieKeywords method, of class TheMovieDbApi. */ - @Test + //@Test public void testGetMovieKeywords() throws MovieDbException { logger.info("getMovieKeywords"); List result = tmdb.getMovieKeywords(ID_MOVIE_BLADE_RUNNER); @@ -202,7 +203,7 @@ public class TheMovieDbApiTest { /** * Test of getMovieReleaseInfo method, of class TheMovieDbApi. */ - @Test + //@Test public void testGetMovieReleaseInfo() throws MovieDbException { logger.info("getMovieReleaseInfo"); List result = tmdb.getMovieReleaseInfo(ID_MOVIE_BLADE_RUNNER, ""); @@ -212,7 +213,7 @@ public class TheMovieDbApiTest { /** * Test of getMovieTrailers method, of class TheMovieDbApi. */ - @Test + //@Test public void testGetMovieTrailers() throws MovieDbException { logger.info("getMovieTrailers"); List result = tmdb.getMovieTrailers(ID_MOVIE_BLADE_RUNNER, ""); @@ -222,7 +223,7 @@ public class TheMovieDbApiTest { /** * Test of getMovieTranslations method, of class TheMovieDbApi. */ - @Test + //@Test public void testGetMovieTranslations() throws MovieDbException { logger.info("getMovieTranslations"); List result = tmdb.getMovieTranslations(ID_MOVIE_BLADE_RUNNER); @@ -232,7 +233,7 @@ public class TheMovieDbApiTest { /** * Test of getCollectionInfo method, of class TheMovieDbApi. */ - @Test + //@Test public void testGetCollectionInfo() throws MovieDbException { logger.info("getCollectionInfo"); String language = ""; @@ -245,7 +246,7 @@ public class TheMovieDbApiTest { * * @throws MovieDbException */ - @Test + //@Test public void testCreateImageUrl() throws MovieDbException { logger.info("createImageUrl"); MovieDb movie = tmdb.getMovieInfo(ID_MOVIE_BLADE_RUNNER, ""); @@ -256,7 +257,7 @@ public class TheMovieDbApiTest { /** * Test of getMovieInfoImdb method, of class TheMovieDbApi. */ - @Test + //@Test public void testGetMovieInfoImdb() throws MovieDbException { logger.info("getMovieInfoImdb"); MovieDb result = tmdb.getMovieInfoImdb("tt0076759", "en-US"); @@ -266,7 +267,7 @@ public class TheMovieDbApiTest { /** * Test of getApiKey method, of class TheMovieDbApi. */ - @Test + //@Test public void testGetApiKey() { // Not required } @@ -274,7 +275,7 @@ public class TheMovieDbApiTest { /** * Test of getApiBase method, of class TheMovieDbApi. */ - @Test + //@Test public void testGetApiBase() { // Not required } @@ -282,7 +283,7 @@ public class TheMovieDbApiTest { /** * Test of getConfiguration method, of class TheMovieDbApi. */ - @Test + //@Test public void testGetConfiguration() { // Not required } @@ -290,7 +291,7 @@ public class TheMovieDbApiTest { /** * Test of searchPeople method, of class TheMovieDbApi. */ - @Test + //@Test public void testSearchPeople() throws MovieDbException { logger.info("searchPeople"); String personName = "Bruce Willis"; @@ -302,7 +303,7 @@ public class TheMovieDbApiTest { /** * Test of getPersonInfo method, of class TheMovieDbApi. */ - @Test + //@Test public void testGetPersonInfo() throws MovieDbException { logger.info("getPersonInfo"); Person result = tmdb.getPersonInfo(ID_PERSON_BRUCE_WILLIS); @@ -312,7 +313,7 @@ public class TheMovieDbApiTest { /** * Test of getPersonCredits method, of class TheMovieDbApi. */ - @Test + //@Test public void testGetPersonCredits() throws MovieDbException { logger.info("getPersonCredits"); @@ -323,7 +324,7 @@ public class TheMovieDbApiTest { /** * Test of getPersonImages method, of class TheMovieDbApi. */ - @Test + //@Test public void testGetPersonImages() throws MovieDbException { logger.info("getPersonImages"); @@ -334,7 +335,7 @@ public class TheMovieDbApiTest { /** * Test of getLatestMovie method, of class TheMovieDbApi. */ - @Test + //@Test public void testGetLatestMovie() throws MovieDbException { logger.info("getLatestMovie"); MovieDb result = tmdb.getLatestMovie(); @@ -345,7 +346,7 @@ public class TheMovieDbApiTest { /** * Test of compareMovies method, of class TheMovieDbApi. */ - @Test + //@Test public void testCompareMovies() { // Not required } @@ -353,7 +354,7 @@ public class TheMovieDbApiTest { /** * Test of setProxy method, of class TheMovieDbApi. */ - @Test + //@Test public void testSetProxy() { // Not required } @@ -361,7 +362,7 @@ public class TheMovieDbApiTest { /** * Test of setTimeout method, of class TheMovieDbApi. */ - @Test + //@Test public void testSetTimeout() { // Not required } @@ -369,7 +370,7 @@ public class TheMovieDbApiTest { /** * Test of getNowPlayingMovies method, of class TheMovieDbApi. */ - @Test + //@Test public void testGetNowPlayingMovies() throws MovieDbException { logger.info("getNowPlayingMovies"); List results = tmdb.getNowPlayingMovies("", true); @@ -379,7 +380,7 @@ public class TheMovieDbApiTest { /** * Test of getPopularMovieList method, of class TheMovieDbApi. */ - @Test + //@Test public void testGetPopularMovieList() throws MovieDbException { logger.info("getPopularMovieList"); List results = tmdb.getPopularMovieList("", true); @@ -389,7 +390,7 @@ public class TheMovieDbApiTest { /** * Test of getTopRatedMovies method, of class TheMovieDbApi. */ - @Test + //@Test public void testGetTopRatedMovies() throws MovieDbException { logger.info("getTopRatedMovies"); List results = tmdb.getTopRatedMovies("", true); @@ -399,7 +400,7 @@ public class TheMovieDbApiTest { /** * Test of getCompanyInfo method, of class TheMovieDbApi. */ - @Test + //@Test public void testGetCompanyInfo() throws MovieDbException { logger.info("getCompanyInfo"); Company company = tmdb.getCompanyInfo(ID_COMPANY_LUCASFILM); @@ -409,7 +410,7 @@ public class TheMovieDbApiTest { /** * Test of getCompanyMovies method, of class TheMovieDbApi. */ - @Test + //@Test public void testGetCompanyMovies() throws MovieDbException { logger.info("getCompanyMovies"); List results = tmdb.getCompanyMovies(ID_COMPANY_LUCASFILM, "", true); @@ -419,7 +420,7 @@ public class TheMovieDbApiTest { /** * Test of searchCompanies method, of class TheMovieDbApi. */ - @Test + //@Test public void testSearchCompanies() throws MovieDbException { logger.info("searchCompanies"); List results = tmdb.searchCompanies(COMPANY_NAME, "", true); @@ -429,7 +430,7 @@ public class TheMovieDbApiTest { /** * Test of getSimilarMovies method, of class TheMovieDbApi. */ - @Test + //@Test public void testGetSimilarMovies() throws MovieDbException { logger.info("getSimilarMovies"); List results = tmdb.getSimilarMovies(ID_MOVIE_BLADE_RUNNER, "", true); @@ -439,7 +440,7 @@ public class TheMovieDbApiTest { /** * Test of getGenreList method, of class TheMovieDbApi. */ - @Test + //@Test public void testGetGenreList() throws MovieDbException { logger.info("getGenreList"); List results = tmdb.getGenreList(""); @@ -449,7 +450,7 @@ public class TheMovieDbApiTest { /** * Test of getGenreMovies method, of class TheMovieDbApi. */ - @Test + //@Test public void testGetGenreMovies() throws MovieDbException { logger.info("getGenreMovies"); List results = tmdb.getGenreMovies(ID_GENRE_ACTION, "", true); @@ -459,7 +460,7 @@ public class TheMovieDbApiTest { /** * Test of getUpcoming method, of class TheMovieDbApi. */ - @Test + //@Test public void testGetUpcoming() throws Exception { logger.info("getUpcoming"); List results = tmdb.getUpcoming(""); @@ -469,7 +470,7 @@ public class TheMovieDbApiTest { /** * Test of getCollectionImages method, of class TheMovieDbApi. */ - @Test + //@Test public void testGetCollectionImages() throws Exception { logger.info("getCollectionImages"); String language = ""; @@ -480,7 +481,7 @@ public class TheMovieDbApiTest { /** * Test of getAuthorisationToken method, of class TheMovieDbApi. */ - @Test + //@Test public void testGetAuthorisationToken() throws Exception { logger.info("getAuthorisationToken"); TokenAuthorisation result = tmdb.getAuthorisationToken(); @@ -492,7 +493,7 @@ public class TheMovieDbApiTest { /** * Test of getSessionToken method, of class TheMovieDbApi. */ - @Test + //@Test public void testGetSessionToken() throws Exception { logger.info("getSessionToken"); TokenAuthorisation token = tmdb.getAuthorisationToken(); @@ -509,11 +510,20 @@ public class TheMovieDbApiTest { /** * Test of getGuestSessionToken method, of class TheMovieDbApi. */ - @Test + //@Test public void testGetGuestSessionToken() throws Exception { logger.info("getGuestSessionToken"); TokenSession result = tmdb.getGuestSessionToken(); assertTrue("Failed to get guest session", result.getSuccess()); } + + @Test + public void testGetMovieLists() throws Exception { + logger.info("getMovieLists"); + String language = "en"; + List results = tmdb.getMovieLists(ID_MOVIE_BLADE_RUNNER, language, 0); + assertNotNull("No results found", results); + assertTrue("No results found", results.size() > 0); + } }