Add "getAll" method to Images and Trailers

This commit is contained in:
Stuart Boston
2013-05-15 13:18:14 +01:00
parent 00063cc85d
commit 9bf7175ee8
3 changed files with 262 additions and 193 deletions
@@ -470,25 +470,12 @@ public class TheMovieDbApi {
apiUrl.appendToResponse(appendToResponse);
List<Artwork> artwork = new ArrayList<Artwork>();
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<Trailer> getMovieTrailers(int movieId, String language, String... appendToResponse) throws MovieDbException {
List<Trailer> trailers = new ArrayList<Trailer>();
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<Reviews> 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
*
@@ -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 <http://www.gnu.org/licenses/>.
*
*/
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<Artwork> backdrops;
@JsonProperty("posters")
private List<Artwork> posters;
@JsonProperty("profiles")
private List<Artwork> profiles;
public WrapperImages() {
super(LoggerFactory.getLogger(WrapperImages.class));
}
//<editor-fold defaultstate="collapsed" desc="Getter methods">
public List<Artwork> getBackdrops() {
return backdrops;
}
public List<Artwork> getPosters() {
return posters;
}
public List<Artwork> getProfiles() {
return profiles;
}
//</editor-fold>
//<editor-fold defaultstate="collapsed" desc="Setter methods">
public void setBackdrops(List<Artwork> backdrops) {
this.backdrops = backdrops;
}
public void setPosters(List<Artwork> posters) {
this.posters = posters;
}
public void setProfiles(List<Artwork> profiles) {
this.profiles = profiles;
}
//</editor-fold>
}
/*
* 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 <http://www.gnu.org/licenses/>.
*
*/
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<Artwork> backdrops;
@JsonProperty("posters")
private List<Artwork> posters;
@JsonProperty("profiles")
private List<Artwork> profiles;
public WrapperImages() {
super(LoggerFactory.getLogger(WrapperImages.class));
}
//<editor-fold defaultstate="collapsed" desc="Getter methods">
public List<Artwork> getBackdrops() {
return backdrops;
}
public List<Artwork> getPosters() {
return posters;
}
public List<Artwork> getProfiles() {
return profiles;
}
//</editor-fold>
//<editor-fold defaultstate="collapsed" desc="Setter methods">
public void setBackdrops(List<Artwork> backdrops) {
this.backdrops = backdrops;
}
public void setPosters(List<Artwork> posters) {
this.posters = posters;
}
public void setProfiles(List<Artwork> profiles) {
this.profiles = profiles;
}
//</editor-fold>
/**
* Return a list of all the artwork with their types.
*
* Leaving the parameters blank will return all types
*
* @return
*/
public List<Artwork> getAll(ArtworkType... artworkList) {
List<Artwork> artwork = new ArrayList<Artwork>();
List<ArtworkType> types;
if (artworkList.length > 0) {
types = new ArrayList<ArtworkType>(Arrays.asList(artworkList));
} else {
types = new ArrayList<ArtworkType>(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;
}
}
@@ -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 <http://www.gnu.org/licenses/>.
*
*/
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<Trailer> quicktime;
@JsonProperty("youtube")
private List<Trailer> youtube;
//<editor-fold defaultstate="collapsed" desc="Getter methods">
public int getId() {
return id;
}
public List<Trailer> getQuicktime() {
return quicktime;
}
public List<Trailer> getYoutube() {
return youtube;
}
//</editor-fold>
//<editor-fold defaultstate="collapsed" desc="Setter methods">
public void setId(int id) {
this.id = id;
}
public void setQuicktime(List<Trailer> quicktime) {
this.quicktime = quicktime;
}
public void setYoutube(List<Trailer> youtube) {
this.youtube = youtube;
}
//</editor-fold>
/**
* 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 <http://www.gnu.org/licenses/>.
*
*/
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<Trailer> quicktime;
@JsonProperty("youtube")
private List<Trailer> youtube;
//<editor-fold defaultstate="collapsed" desc="Getter methods">
public int getId() {
return id;
}
public List<Trailer> getQuicktime() {
return quicktime;
}
public List<Trailer> getYoutube() {
return youtube;
}
//</editor-fold>
//<editor-fold defaultstate="collapsed" desc="Setter methods">
public void setId(int id) {
this.id = id;
}
public void setQuicktime(List<Trailer> quicktime) {
this.quicktime = quicktime;
}
public void setYoutube(List<Trailer> youtube) {
this.youtube = youtube;
}
//</editor-fold>
/**
* Get a combined list of the trailers with their source website
* @return
*/
public List<Trailer> getAll() {
List<Trailer> trailers = new ArrayList<Trailer>();
// 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());
}
}