diff --git a/src/main/java/com/omertron/themoviedbapi/TheMovieDbApi.java b/src/main/java/com/omertron/themoviedbapi/TheMovieDbApi.java index 3172aac00..f0369814d 100644 --- a/src/main/java/com/omertron/themoviedbapi/TheMovieDbApi.java +++ b/src/main/java/com/omertron/themoviedbapi/TheMovieDbApi.java @@ -21,50 +21,12 @@ package com.omertron.themoviedbapi; import com.fasterxml.jackson.databind.ObjectMapper; import com.omertron.themoviedbapi.MovieDbException.MovieDbExceptionType; -import com.omertron.themoviedbapi.model.AlternativeTitle; -import com.omertron.themoviedbapi.model.Artwork; -import com.omertron.themoviedbapi.model.ArtworkType; -import com.omertron.themoviedbapi.model.Collection; -import com.omertron.themoviedbapi.model.CollectionInfo; -import com.omertron.themoviedbapi.model.Company; -import com.omertron.themoviedbapi.model.Genre; -import com.omertron.themoviedbapi.model.Keyword; -import com.omertron.themoviedbapi.model.MovieChanges; -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; -import com.omertron.themoviedbapi.model.PersonCrew; -import com.omertron.themoviedbapi.model.PersonType; -import com.omertron.themoviedbapi.model.ReleaseInfo; -import com.omertron.themoviedbapi.model.TmdbConfiguration; -import com.omertron.themoviedbapi.model.TokenAuthorisation; -import com.omertron.themoviedbapi.model.TokenSession; -import com.omertron.themoviedbapi.model.Trailer; -import com.omertron.themoviedbapi.model.Translation; +import com.omertron.themoviedbapi.model.*; import com.omertron.themoviedbapi.tools.ApiUrl; import static com.omertron.themoviedbapi.tools.ApiUrl.*; import com.omertron.themoviedbapi.tools.FilteringLayout; import com.omertron.themoviedbapi.tools.WebBrowser; -import com.omertron.themoviedbapi.wrapper.WrapperAlternativeTitles; -import com.omertron.themoviedbapi.wrapper.WrapperChanges; -import com.omertron.themoviedbapi.wrapper.WrapperCollection; -import com.omertron.themoviedbapi.wrapper.WrapperCompany; -import com.omertron.themoviedbapi.wrapper.WrapperCompanyMovies; -import com.omertron.themoviedbapi.wrapper.WrapperConfig; -import com.omertron.themoviedbapi.wrapper.WrapperGenres; -import com.omertron.themoviedbapi.wrapper.WrapperImages; -import com.omertron.themoviedbapi.wrapper.WrapperKeywords; -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; -import com.omertron.themoviedbapi.wrapper.WrapperTrailers; -import com.omertron.themoviedbapi.wrapper.WrapperTranslations; +import com.omertron.themoviedbapi.wrapper.*; import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; @@ -74,9 +36,7 @@ import org.apache.commons.lang3.StringUtils; import org.apache.log4j.Logger; /** - * The MovieDb API - *

- * This is for version 3 of the API as specified here: http://help.themoviedb.org/kb/api/about-3 + * 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 */ @@ -99,6 +59,8 @@ public class TheMovieDbApi { private static final String BASE_COLLECTION = "collection/"; // private static final String BASE_ACCOUNT = "account/"; private static final String BASE_SEARCH = "search/"; + private static final String BASE_LIST = "list/"; + private static final String BASE_KEYWORD = "keyword/"; // Account /* private final ApiUrl tmdbAccount = new ApiUrl(this, BASE_ACCOUNT); @@ -332,11 +294,10 @@ public class TheMovieDbApi { * * A guest session can be used to rate movies without having a registered TMDb user account. * - * You should only generate a single guest session per user (or device) as you will be able to attach the ratings to - * a TMDb user account in the future. + * You should only generate a single guest session per user (or device) as you will be able to attach the ratings to a TMDb user + * account in the future. * - * There are also IP limits in place so you should always make sure it's the end user doing the guest session - * actions. + * There are also IP limits in place so you should always make sure it's the end user doing the guest session actions. * * If a guest session is not used for the first time within 24 hours, it will be automatically discarded. * @@ -1153,8 +1114,7 @@ public class TheMovieDbApi { /** * This method is used to retrieve the movies associated with a company. * - * These movies are returned in order of most recently released to oldest. The default response will return 20 - * movies per page. + * These movies are returned in order of most recently released to oldest. The default response will return 20 movies per page. * * TODO: Implement more than 20 movies * @@ -1399,8 +1359,8 @@ public class TheMovieDbApi { /** * Search Companies. * - * You can use this method to search for production companies that are part of TMDb. The company IDs will map to - * those returned on movie calls. + * You can use this method to search for production companies that are part of TMDb. The company IDs will map to those returned + * on movie calls. * * http://help.themoviedb.org/kb/api/search-companies * @@ -1458,6 +1418,93 @@ public class TheMovieDbApi { } // // - // List Functions - // Keywords Functions + // + + /** + * Get a list by its ID + * + * @param listId + * @return The list and its items + * @throws MovieDbException + */ + public MovieDbList getList(String listId) throws MovieDbException { + ApiUrl apiUrl = new ApiUrl(this, BASE_LIST); + apiUrl.addArgument(PARAM_ID, listId); + + URL url = apiUrl.buildUrl(); + String webpage = WebBrowser.request(url); + + try { + MovieDbList movieDbList = mapper.readValue(webpage, MovieDbList.class); + return movieDbList; + } catch (IOException ex) { + logger.warn("Failed to get list: " + ex.getMessage()); + throw new MovieDbException(MovieDbExceptionType.MAPPING_FAILED, webpage, ex); + } + } + // + // + // + + /** + * Get the basic information for a specific keyword id. + * + * @param keywordId + * @return + * @throws MovieDbException + */ + public Keyword getKeyword(String keywordId) throws MovieDbException { + ApiUrl apiUrl = new ApiUrl(this, BASE_KEYWORD); + apiUrl.addArgument(PARAM_ID, keywordId); + + URL url = apiUrl.buildUrl(); + String webpage = WebBrowser.request(url); + + try { + Keyword keyword = mapper.readValue(webpage, Keyword.class); + return keyword; + } catch (IOException ex) { + logger.warn("Failed to get keyword: " + ex.getMessage()); + throw new MovieDbException(MovieDbExceptionType.MAPPING_FAILED, webpage, ex); + } + + } + + /** + * Get the list of movies for a particular keyword by id. + * + * @param keywordId + * @param language + * @param page + * @return List of movies with the keyword + * @throws MovieDbException + */ + public List getKeywordMovies(String keywordId, String language, int page) throws MovieDbException { + ApiUrl apiUrl = new ApiUrl(this, BASE_KEYWORD, "/movies"); + apiUrl.addArgument(PARAM_ID, keywordId); + + 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 top rated movies: " + ex.getMessage()); + throw new MovieDbException(MovieDbExceptionType.MAPPING_FAILED, webpage, ex); + } + + } + // + // + // + // } diff --git a/src/main/java/com/omertron/themoviedbapi/model/MovieDbList.java b/src/main/java/com/omertron/themoviedbapi/model/MovieDbList.java new file mode 100644 index 000000000..464f341b2 --- /dev/null +++ b/src/main/java/com/omertron/themoviedbapi/model/MovieDbList.java @@ -0,0 +1,150 @@ +/* + * Copyright (c) 2004-2013 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.util.Collections; +import java.util.List; +import org.apache.log4j.Logger; + +/** + * Wrapper for the MovieDbList function + * @author stuart.boston + */ +public class MovieDbList { + /* + * Logger + */ + + private static final Logger logger = Logger.getLogger(MovieDbList.class); + /* + * Properties + */ + @JsonProperty("id") + private String id; + @JsonProperty("created_by") + private String createdBy; + @JsonProperty("description") + private String description; + @JsonProperty("favorite_count") + private int favoriteCount; + @JsonProperty("items") + private List items = Collections.EMPTY_LIST; + @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 getId() { + return id; + } + + public String getCreatedBy() { + return createdBy; + } + + public String getDescription() { + return description; + } + + public int getFavoriteCount() { + return favoriteCount; + } + + public List getItems() { + return items; + } + + public int getItemCount() { + return itemCount; + } + + public String getLanguage() { + return language; + } + + public String getName() { + return name; + } + + public String getPosterPath() { + return posterPath; + } + // + + // + public void setId(String id) { + this.id = id; + } + + public void setCreatedBy(String createdBy) { + this.createdBy = createdBy; + } + + public void setDescription(String description) { + this.description = description; + } + + public void setFavoriteCount(int favoriteCount) { + this.favoriteCount = favoriteCount; + } + + public void setItems(List items) { + this.items = items; + } + + 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()); + } + +} diff --git a/src/test/java/com/omertron/themoviedbapi/TheMovieDbApiTest.java b/src/test/java/com/omertron/themoviedbapi/TheMovieDbApiTest.java index 225a10550..8ba56b03d 100644 --- a/src/test/java/com/omertron/themoviedbapi/TheMovieDbApiTest.java +++ b/src/test/java/com/omertron/themoviedbapi/TheMovieDbApiTest.java @@ -28,6 +28,7 @@ import com.omertron.themoviedbapi.model.Genre; import com.omertron.themoviedbapi.model.Keyword; import com.omertron.themoviedbapi.model.MovieChanges; import com.omertron.themoviedbapi.model.MovieDb; +import com.omertron.themoviedbapi.model.MovieDbList; import com.omertron.themoviedbapi.model.MovieList; import com.omertron.themoviedbapi.model.Person; import com.omertron.themoviedbapi.model.PersonCredit; @@ -66,6 +67,7 @@ public class TheMovieDbApiTest { private static final int ID_COMPANY_LUCASFILM = 1; private static final String COMPANY_NAME = "Marvel Studios"; private static final int ID_GENRE_ACTION = 28; + private static final String ID_KEYWORD = "1721"; public TheMovieDbApiTest() throws MovieDbException { tmdb = new TheMovieDbApi(API_KEY); @@ -599,4 +601,66 @@ public class TheMovieDbApiTest { assertFalse("No keywords found", result == null); assertTrue("No keywords found", result.size() > 0); } + + /** + * Test of postMovieRating method, of class TheMovieDbApi. + */ + @Test + public void testPostMovieRating() throws Exception { + logger.info("postMovieRating"); + String sessionId = ""; + String rating = ""; + boolean expResult = false; + boolean result = tmdb.postMovieRating(sessionId, rating); + assertEquals(expResult, result); + // TODO review the generated test code and remove the default call to fail. + fail("The test case is a prototype."); + } + + /** + * Test of getPersonChanges method, of class TheMovieDbApi. + */ + @Test + public void testGetPersonChanges() throws Exception { + logger.info("getPersonChanges"); + int personId = 0; + String startDate = ""; + String endDate = ""; + tmdb.getPersonChanges(personId, startDate, endDate); + // TODO review the generated test code and remove the default call to fail. + fail("The test case is a prototype."); + } + + /** + * Test of getList method, of class TheMovieDbApi. + */ + @Test + public void testGetList() throws Exception { + logger.info("getList"); + String listId = "509ec17b19c2950a0600050d"; + MovieDbList result = tmdb.getList(listId); + assertFalse("List not found", result.getItems().isEmpty()); + } + + /** + * Test of getKeyword method, of class TheMovieDbApi. + */ + @Test + public void testGetKeyword() throws Exception { + logger.info("getKeyword"); + Keyword result = tmdb.getKeyword(ID_KEYWORD); + assertEquals("fight", result.getName()); + } + + /** + * Test of getKeywordMovies method, of class TheMovieDbApi. + */ + @Test + public void testGetKeywordMovies() throws Exception { + logger.info("getKeywordMovies"); + String language = ""; + int page = 0; + List result = tmdb.getKeywordMovies(ID_KEYWORD, language, page); + assertFalse("No keyword movies found", result.isEmpty()); + } }