diff --git a/src/main/java/com/omertron/themoviedbapi/TheMovieDbApi.java b/src/main/java/com/omertron/themoviedbapi/TheMovieDbApi.java index ffa53281b..b0cda7140 100644 --- a/src/main/java/com/omertron/themoviedbapi/TheMovieDbApi.java +++ b/src/main/java/com/omertron/themoviedbapi/TheMovieDbApi.java @@ -470,25 +470,12 @@ public class TheMovieDbApi { apiUrl.appendToResponse(appendToResponse); - List artwork = new ArrayList(); URL url = apiUrl.buildUrl(); String webpage = WebBrowser.request(url); + try { WrapperImages wrapper = mapper.readValue(webpage, WrapperImages.class); - - // Add all the posters to the list - for (Artwork poster : wrapper.getPosters()) { - poster.setArtworkType(ArtworkType.POSTER); - artwork.add(poster); - } - - // Add all the backdrops to the list - for (Artwork backdrop : wrapper.getBackdrops()) { - backdrop.setArtworkType(ArtworkType.BACKDROP); - artwork.add(backdrop); - } - - return artwork; + return wrapper.getAll(); } catch (IOException ex) { LOG.warn("Failed to get movie images: {}", ex.getMessage()); throw new MovieDbException(MovieDbExceptionType.MAPPING_FAILED, webpage, ex); @@ -557,8 +544,6 @@ public class TheMovieDbApi { * @throws MovieDbException */ public List getMovieTrailers(int movieId, String language, String... appendToResponse) throws MovieDbException { - List trailers = new ArrayList(); - ApiUrl apiUrl = new ApiUrl(apiKey, BASE_MOVIE, "/trailers"); apiUrl.addArgument(PARAM_ID, movieId); @@ -573,18 +558,7 @@ public class TheMovieDbApi { try { WrapperTrailers wrapper = mapper.readValue(webpage, WrapperTrailers.class); - - // Add the trailer to the return list along with it's source - for (Trailer trailer : wrapper.getQuicktime()) { - trailer.setWebsite(Trailer.WEBSITE_QUICKTIME); - trailers.add(trailer); - } - // Add the trailer to the return list along with it's source - for (Trailer trailer : wrapper.getYoutube()) { - trailer.setWebsite(Trailer.WEBSITE_YOUTUBE); - trailers.add(trailer); - } - return trailers; + return wrapper.getAll(); } catch (IOException ex) { LOG.warn("Failed to get movie trailers: {}", ex.getMessage()); throw new MovieDbException(MovieDbExceptionType.MAPPING_FAILED, webpage, ex); @@ -653,6 +627,32 @@ public class TheMovieDbApi { } } + public List getReviews(int movieId, String language, int page, String... appendToResponse) throws MovieDbException { + ApiUrl apiUrl = new ApiUrl(apiKey, BASE_MOVIE, "/reviews"); + apiUrl.addArgument(PARAM_ID, movieId); + + if (StringUtils.isNotBlank(language)) { + apiUrl.addArgument(PARAM_LANGUAGE, language); + } + + if (page > 0) { + apiUrl.addArgument(PARAM_PAGE, page); + } + + apiUrl.appendToResponse(appendToResponse); + + URL url = apiUrl.buildUrl(); + String webpage = WebBrowser.request(url); + + try { + WrapperReviews wrapper = mapper.readValue(webpage, WrapperReviews.class); + return wrapper.getReviews(); + } catch (IOException ex) { + LOG.warn("Failed to get reviews: {}", ex.getMessage()); + throw new MovieDbException(MovieDbExceptionType.MAPPING_FAILED, webpage, ex); + } + } + /** * Get the lists that the movie belongs to * diff --git a/src/main/java/com/omertron/themoviedbapi/wrapper/WrapperImages.java b/src/main/java/com/omertron/themoviedbapi/wrapper/WrapperImages.java index a6fbaa172..74dde658e 100644 --- a/src/main/java/com/omertron/themoviedbapi/wrapper/WrapperImages.java +++ b/src/main/java/com/omertron/themoviedbapi/wrapper/WrapperImages.java @@ -1,74 +1,121 @@ -/* - * 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.wrapper; - -import com.fasterxml.jackson.annotation.JsonProperty; -import com.omertron.themoviedbapi.model.Artwork; -import java.util.List; -import org.slf4j.LoggerFactory; - -/** - * - * @author Stuart - */ -public class WrapperImages extends WrapperBase { - /* - * Properties - */ - - @JsonProperty("backdrops") - private List backdrops; - @JsonProperty("posters") - private List posters; - @JsonProperty("profiles") - private List profiles; - - public WrapperImages() { - super(LoggerFactory.getLogger(WrapperImages.class)); - } - - // - public List getBackdrops() { - return backdrops; - } - - public List getPosters() { - return posters; - } - - public List getProfiles() { - return profiles; - } - // - - // - public void setBackdrops(List backdrops) { - this.backdrops = backdrops; - } - - public void setPosters(List posters) { - this.posters = posters; - } - - public void setProfiles(List profiles) { - this.profiles = profiles; - } - // -} +/* + * 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.wrapper; + +import com.fasterxml.jackson.annotation.JsonProperty; +import com.omertron.themoviedbapi.model.Artwork; +import com.omertron.themoviedbapi.model.ArtworkType; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import org.slf4j.LoggerFactory; + +/** + * + * @author Stuart + */ +public class WrapperImages extends WrapperBase { + /* + * Properties + */ + + @JsonProperty("backdrops") + private List backdrops; + @JsonProperty("posters") + private List posters; + @JsonProperty("profiles") + private List profiles; + + public WrapperImages() { + super(LoggerFactory.getLogger(WrapperImages.class)); + } + + // + public List getBackdrops() { + return backdrops; + } + + public List getPosters() { + return posters; + } + + public List getProfiles() { + return profiles; + } + // + + // + public void setBackdrops(List backdrops) { + this.backdrops = backdrops; + } + + public void setPosters(List posters) { + this.posters = posters; + } + + public void setProfiles(List profiles) { + this.profiles = profiles; + } + // + + /** + * Return a list of all the artwork with their types. + * + * Leaving the parameters blank will return all types + * + * @return + */ + public List getAll(ArtworkType... artworkList) { + List artwork = new ArrayList(); + List types; + + if (artworkList.length > 0) { + types = new ArrayList(Arrays.asList(artworkList)); + } else { + types = new ArrayList(Arrays.asList(ArtworkType.values())); + } + + if (types.contains(ArtworkType.POSTER)) { + // Add all the posters to the list + for (Artwork poster : posters) { + poster.setArtworkType(ArtworkType.POSTER); + artwork.add(poster); + } + } + + if (types.contains(ArtworkType.BACKDROP)) { + // Add all the backdrops to the list + for (Artwork backdrop : backdrops) { + backdrop.setArtworkType(ArtworkType.BACKDROP); + artwork.add(backdrop); + } + } + + if (types.contains(ArtworkType.PROFILE)) { + // Add all the backdrops to the list + for (Artwork backdrop : profiles) { + backdrop.setArtworkType(ArtworkType.PROFILE); + artwork.add(backdrop); + } + } + + return artwork; + } +} diff --git a/src/main/java/com/omertron/themoviedbapi/wrapper/WrapperTrailers.java b/src/main/java/com/omertron/themoviedbapi/wrapper/WrapperTrailers.java index 0f19e69bc..2b3098ca6 100644 --- a/src/main/java/com/omertron/themoviedbapi/wrapper/WrapperTrailers.java +++ b/src/main/java/com/omertron/themoviedbapi/wrapper/WrapperTrailers.java @@ -1,90 +1,112 @@ -/* - * 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.wrapper; - -import com.fasterxml.jackson.annotation.JsonAnySetter; -import com.fasterxml.jackson.annotation.JsonProperty; -import com.omertron.themoviedbapi.model.Trailer; -import java.util.List; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * - * @author Stuart - */ -public class WrapperTrailers { - /* - * Logger - */ - - private static final Logger LOG = LoggerFactory.getLogger(WrapperTrailers.class); - /* - * Properties - */ - @JsonProperty("id") - private int id; - @JsonProperty("quicktime") - private List quicktime; - @JsonProperty("youtube") - private List youtube; - - // - public int getId() { - return id; - } - - public List getQuicktime() { - return quicktime; - } - - public List getYoutube() { - return youtube; - } - // - - // - public void setId(int id) { - this.id = id; - } - - public void setQuicktime(List quicktime) { - this.quicktime = quicktime; - } - - public void setYoutube(List youtube) { - this.youtube = youtube; - } - // - - /** - * 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("'"); - LOG.trace(sb.toString()); - } -} +/* + * 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.wrapper; + +import com.fasterxml.jackson.annotation.JsonAnySetter; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.omertron.themoviedbapi.model.Trailer; +import java.util.ArrayList; +import java.util.List; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * + * @author Stuart + */ +public class WrapperTrailers { + /* + * Logger + */ + + private static final Logger LOG = LoggerFactory.getLogger(WrapperTrailers.class); + /* + * Properties + */ + @JsonProperty("id") + private int id; + @JsonProperty("quicktime") + private List quicktime; + @JsonProperty("youtube") + private List youtube; + + // + public int getId() { + return id; + } + + public List getQuicktime() { + return quicktime; + } + + public List getYoutube() { + return youtube; + } + // + + // + public void setId(int id) { + this.id = id; + } + + public void setQuicktime(List quicktime) { + this.quicktime = quicktime; + } + + public void setYoutube(List youtube) { + this.youtube = youtube; + } + // + + /** + * Get a combined list of the trailers with their source website + * @return + */ + public List getAll() { + List trailers = new ArrayList(); + + // Add the trailer to the return list along with it's source + for (Trailer trailer : quicktime) { + trailer.setWebsite(Trailer.WEBSITE_QUICKTIME); + trailers.add(trailer); + } + // Add the trailer to the return list along with it's source + for (Trailer trailer : youtube) { + trailer.setWebsite(Trailer.WEBSITE_YOUTUBE); + trailers.add(trailer); + } + + return trailers; + } + + /** + * 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("'"); + LOG.trace(sb.toString()); + } +}