TV Episodes

This commit is contained in:
Stuart Boston
2015-03-05 15:57:27 +00:00
parent f713bde372
commit 9693aa458d
7 changed files with 282 additions and 114 deletions
@@ -20,7 +20,30 @@
package com.omertron.themoviedbapi.methods;
import com.omertron.themoviedbapi.MovieDbException;
import static com.omertron.themoviedbapi.methods.AbstractMethod.MAPPER;
import com.omertron.themoviedbapi.model.StatusCode;
import com.omertron.themoviedbapi.model.artwork.Artwork;
import com.omertron.themoviedbapi.model.media.MediaCreditList;
import com.omertron.themoviedbapi.model.media.MediaState;
import com.omertron.themoviedbapi.model.media.Video;
import com.omertron.themoviedbapi.model.person.ExternalID;
import com.omertron.themoviedbapi.model.tv.TVEpisodeInfo;
import com.omertron.themoviedbapi.results.TmdbResultsList;
import com.omertron.themoviedbapi.tools.ApiUrl;
import com.omertron.themoviedbapi.tools.HttpTools;
import com.omertron.themoviedbapi.tools.MethodBase;
import com.omertron.themoviedbapi.tools.MethodSub;
import com.omertron.themoviedbapi.tools.Param;
import com.omertron.themoviedbapi.tools.PostBody;
import com.omertron.themoviedbapi.tools.PostTools;
import com.omertron.themoviedbapi.tools.TmdbParameters;
import com.omertron.themoviedbapi.wrapper.WrapperChanges;
import com.omertron.themoviedbapi.wrapper.WrapperImages;
import com.omertron.themoviedbapi.wrapper.WrapperVideos;
import java.io.IOException;
import java.net.URL;
import org.slf4j.LoggerFactory;
import org.yamj.api.common.exception.ApiExceptionType;
/**
* Class to hold the TV Methods
@@ -53,8 +76,23 @@ public class TmdbTVEpisodes extends AbstractMethod {
* @return
* @throws MovieDbException
*/
public String getEpisodeInfo(int tvID, int seasonNumber, int episodeNumber, String language, String... appendToResponse) throws MovieDbException {
return null;
public TVEpisodeInfo getEpisodeInfo(int tvID, int seasonNumber, int episodeNumber, String language, String... appendToResponse) throws MovieDbException {
TmdbParameters parameters = new TmdbParameters();
parameters.add(Param.ID, tvID);
parameters.add(Param.SEASON_NUMBER, seasonNumber);
parameters.add(Param.EPISODE_NUMBER, episodeNumber);
parameters.add(Param.LANGUAGE, language);
parameters.add(Param.APPEND, appendToResponse);
URL url = new ApiUrl(apiKey, MethodBase.EPISODE).buildUrl(parameters);
String webpage = httpTools.getRequest(url);
try {
return MAPPER.readValue(webpage, TVEpisodeInfo.class);
} catch (IOException ex) {
LoggerFactory.getLogger("test").warn("{}", ex);
throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, "Failed to get TV Episode Info", url, ex);
}
}
/**
@@ -66,8 +104,20 @@ public class TmdbTVEpisodes extends AbstractMethod {
* @return
* @throws MovieDbException
*/
public String getEpisodeChanges(int episodeID, String startDate, String endDate) throws MovieDbException {
return null;
public WrapperChanges getEpisodeChanges(int episodeID, String startDate, String endDate) throws MovieDbException {
TmdbParameters parameters = new TmdbParameters();
parameters.add(Param.ID, episodeID);
parameters.add(Param.START_DATE, startDate);
parameters.add(Param.END_DATE, endDate);
URL url = new ApiUrl(apiKey, MethodBase.EPISODE).subMethod(MethodSub.CHANGES).buildUrl(parameters);
String webpage = httpTools.getRequest(url);
try {
return MAPPER.readValue(webpage, WrapperChanges.class);
} catch (IOException ex) {
throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, "Failed to get changes", url, ex);
}
}
/**
@@ -83,8 +133,21 @@ public class TmdbTVEpisodes extends AbstractMethod {
* @return
* @throws MovieDbException
*/
public String getEpisodeAccountState(int tvID, int seasonNumber, int episodeNumber, String sessionID) throws MovieDbException {
return null;
public MediaState getEpisodeAccountState(int tvID, int seasonNumber, int episodeNumber, String sessionID) throws MovieDbException {
TmdbParameters parameters = new TmdbParameters();
parameters.add(Param.ID, tvID);
parameters.add(Param.SESSION_ID, sessionID);
parameters.add(Param.SEASON_NUMBER, seasonNumber);
parameters.add(Param.EPISODE_NUMBER, episodeNumber);
URL url = new ApiUrl(apiKey, MethodBase.EPISODE).subMethod(MethodSub.ACCOUNT_STATES).buildUrl(parameters);
String webpage = httpTools.getRequest(url);
try {
return MAPPER.readValue(webpage, MediaState.class);
} catch (IOException ex) {
throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, "Failed to get account state", url, ex);
}
}
/**
@@ -96,8 +159,19 @@ public class TmdbTVEpisodes extends AbstractMethod {
* @return
* @throws MovieDbException
*/
public String getEpisodeCredits(int tvID, int seasonNumber, int episodeNumber) throws MovieDbException {
return null;
public MediaCreditList getEpisodeCredits(int tvID, int seasonNumber, int episodeNumber) throws MovieDbException {
TmdbParameters parameters = new TmdbParameters();
parameters.add(Param.ID, tvID);
parameters.add(Param.SEASON_NUMBER, seasonNumber);
parameters.add(Param.EPISODE_NUMBER, episodeNumber);
URL url = new ApiUrl(apiKey, MethodBase.EPISODE).subMethod(MethodSub.CREDITS).buildUrl(parameters);
String webpage = httpTools.getRequest(url);
try {
return MAPPER.readValue(webpage, MediaCreditList.class);
} catch (IOException ex) {
throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, "Failed to get credits", url, ex);
}
}
/**
@@ -111,8 +185,21 @@ public class TmdbTVEpisodes extends AbstractMethod {
* @return
* @throws MovieDbException
*/
public String getEpisodeExternalID(int tvID, int seasonNumber, int episodeNumber, String language) throws MovieDbException {
return null;
public ExternalID getEpisodeExternalID(int tvID, int seasonNumber, int episodeNumber, String language) throws MovieDbException {
TmdbParameters parameters = new TmdbParameters();
parameters.add(Param.ID, tvID);
parameters.add(Param.SEASON_NUMBER, seasonNumber);
parameters.add(Param.EPISODE_NUMBER, episodeNumber);
parameters.add(Param.LANGUAGE, language);
URL url = new ApiUrl(apiKey, MethodBase.EPISODE).subMethod(MethodSub.EXTERNAL_IDS).buildUrl(parameters);
String webpage = httpTools.getRequest(url);
try {
return MAPPER.readValue(webpage, ExternalID.class);
} catch (IOException ex) {
throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, "Failed to get external IDs", url, ex);
}
}
/**
@@ -125,8 +212,23 @@ public class TmdbTVEpisodes extends AbstractMethod {
* @return
* @throws MovieDbException
*/
public String getEpisodeImages(int tvID, int seasonNumber, int episodeNumber) throws MovieDbException {
return null;
public TmdbResultsList<Artwork> getEpisodeImages(int tvID, int seasonNumber, int episodeNumber) throws MovieDbException {
TmdbParameters parameters = new TmdbParameters();
parameters.add(Param.ID, tvID);
parameters.add(Param.SEASON_NUMBER, seasonNumber);
parameters.add(Param.EPISODE_NUMBER, episodeNumber);
URL url = new ApiUrl(apiKey, MethodBase.EPISODE).subMethod(MethodSub.IMAGES).buildUrl(parameters);
String webpage = httpTools.getRequest(url);
try {
WrapperImages wrapper = MAPPER.readValue(webpage, WrapperImages.class);
TmdbResultsList<Artwork> results = new TmdbResultsList<Artwork>(wrapper.getAll());
results.copyWrapper(wrapper);
return results;
} catch (IOException ex) {
throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, "Failed to get images", url, ex);
}
}
/**
@@ -142,8 +244,29 @@ public class TmdbTVEpisodes extends AbstractMethod {
* @return
* @throws MovieDbException
*/
public String postEpisodeRating(int tvID, int seasonNumber, int episodeNumber, int rating, String sessionID, String guestSessionID) throws MovieDbException {
return null;
public StatusCode postEpisodeRating(int tvID, int seasonNumber, int episodeNumber, int rating, String sessionID, String guestSessionID) throws MovieDbException {
if (rating < 0 || rating > RATING_MAX) {
throw new MovieDbException(ApiExceptionType.UNKNOWN_CAUSE, "Rating out of range");
}
TmdbParameters parameters = new TmdbParameters();
parameters.add(Param.ID, tvID);
parameters.add(Param.SEASON_NUMBER, seasonNumber);
parameters.add(Param.EPISODE_NUMBER, episodeNumber);
parameters.add(Param.SESSION_ID, sessionID);
parameters.add(Param.GUEST_SESSION_ID, guestSessionID);
URL url = new ApiUrl(apiKey, MethodBase.EPISODE).subMethod(MethodSub.RATING).buildUrl(parameters);
String jsonBody = new PostTools()
.add(PostBody.VALUE, rating)
.build();
String webpage = httpTools.postRequest(url, jsonBody);
try {
return MAPPER.readValue(webpage, StatusCode.class);
} catch (IOException ex) {
throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, "Failed to post rating", url, ex);
}
}
/**
@@ -157,8 +280,24 @@ public class TmdbTVEpisodes extends AbstractMethod {
* @return
* @throws MovieDbException
*/
public String getEpisodeVideos(int tvID, int seasonNumber, int episodeNumber, String language) throws MovieDbException {
return null;
public TmdbResultsList<Video> getEpisodeVideos(int tvID, int seasonNumber, int episodeNumber, String language) throws MovieDbException {
TmdbParameters parameters = new TmdbParameters();
parameters.add(Param.ID, tvID);
parameters.add(Param.SEASON_NUMBER, seasonNumber);
parameters.add(Param.EPISODE_NUMBER, episodeNumber);
parameters.add(Param.LANGUAGE, language);
URL url = new ApiUrl(apiKey, MethodBase.EPISODE).subMethod(MethodSub.VIDEOS).buildUrl(parameters);
String webpage = httpTools.getRequest(url);
try {
WrapperVideos wrapper = MAPPER.readValue(webpage, WrapperVideos.class);
TmdbResultsList<Video> results = new TmdbResultsList<Video>(wrapper.getVideos());
results.copyWrapper(wrapper);
return results;
} catch (IOException ex) {
throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, "Failed to get videos", url, ex);
}
}
}
@@ -39,7 +39,6 @@ import com.omertron.themoviedbapi.wrapper.WrapperImages;
import com.omertron.themoviedbapi.wrapper.WrapperVideos;
import java.io.IOException;
import java.net.URL;
import org.slf4j.LoggerFactory;
import org.yamj.api.common.exception.ApiExceptionType;
/**
@@ -49,8 +48,6 @@ import org.yamj.api.common.exception.ApiExceptionType;
*/
public class TmdbTVSeasons extends AbstractMethod {
private static final int RATING_MAX = 10;
/**
* Constructor
*
@@ -84,8 +81,7 @@ public class TmdbTVSeasons extends AbstractMethod {
try {
return MAPPER.readValue(webpage, TVSeasonInfo.class);
} catch (IOException ex) {
LoggerFactory.getLogger("test").warn("{}", ex);
throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, "Failed to get TV SeasonInfo", url, ex);
throw new MovieDbException(ApiExceptionType.MAPPING_FAILED, "Failed to get TV Season Info", url, ex);
}
}
@@ -34,6 +34,8 @@ public class MediaCreditList extends AbstractJsonMapping {
private int id;
@JsonProperty("cast")
private List<MediaCreditCast> cast;
@JsonProperty("guest_stars")
private List<MediaCreditCast> guestStars;
@JsonProperty("crew")
private List<MediaCreditCrew> crew;
@@ -61,4 +63,12 @@ public class MediaCreditList extends AbstractJsonMapping {
this.crew = crew;
}
public List<MediaCreditCast> getGuestStars() {
return guestStars;
}
public void setGuestStars(List<MediaCreditCast> guestStars) {
this.guestStars = guestStars;
}
}
@@ -36,6 +36,8 @@ public class ExternalID extends AbstractJsonMapping {
private String freebaseMid;
@JsonProperty("freebase_id")
private String freebaseId;
@JsonProperty("tvdb_id")
private String tvdbId;
@JsonProperty("tvrage_id")
private String tvrageId;
@@ -79,4 +81,12 @@ public class ExternalID extends AbstractJsonMapping {
this.tvrageId = tvrageId;
}
public String getTvdbId() {
return tvdbId;
}
public void setTvdbId(String tvdbId) {
this.tvdbId = tvdbId;
}
}
@@ -20,8 +20,8 @@
package com.omertron.themoviedbapi.wrapper;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.omertron.themoviedbapi.model.artwork.Artwork;
import com.omertron.themoviedbapi.enumeration.ArtworkType;
import com.omertron.themoviedbapi.model.artwork.Artwork;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
@@ -41,6 +41,8 @@ public class WrapperImages extends AbstractWrapperAll implements Serializable {
private List<Artwork> posters = Collections.emptyList();
@JsonProperty("profiles")
private List<Artwork> profiles = Collections.emptyList();
@JsonProperty("stills")
private List<Artwork> stills = Collections.emptyList();
public List<Artwork> getBackdrops() {
return backdrops;
@@ -66,6 +68,10 @@ public class WrapperImages extends AbstractWrapperAll implements Serializable {
this.profiles = profiles;
}
public void setStills(List<Artwork> stills) {
this.stills = stills;
}
/**
* Return a list of all the artwork with their types.
*
@@ -102,6 +108,12 @@ public class WrapperImages extends AbstractWrapperAll implements Serializable {
artwork.addAll(profiles);
}
// Add all the stills to the list
if (types.contains(ArtworkType.STILL)) {
updateArtworkType(stills, ArtworkType.STILL);
artwork.addAll(stills);
}
return artwork;
}