diff --git a/src/main/java/com/omertron/themoviedbapi/methods/TmdbTVEpisodes.java b/src/main/java/com/omertron/themoviedbapi/methods/TmdbTVEpisodes.java index 011e91119..3137d4422 100644 --- a/src/main/java/com/omertron/themoviedbapi/methods/TmdbTVEpisodes.java +++ b/src/main/java/com/omertron/themoviedbapi/methods/TmdbTVEpisodes.java @@ -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 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 results = new TmdbResultsList(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