using System; using System.Globalization; using System.Linq; using System.Threading; using System.Threading.Tasks; using TMDbLib.Objects.Authentication; using TMDbLib.Objects.General; using TMDbLib.Objects.TvShows; using TMDbLib.Rest; using TMDbLib.Utilities; using Credits = TMDbLib.Objects.TvShows.Credits; namespace TMDbLib.Client { public partial class TMDbClient { private async Task GetTvSeasonMethodInternal(int tvShowId, int seasonNumber, TvSeasonMethods tvShowMethod, string dateFormat = null, string language = null, CancellationToken cancellationToken = default) where T : new() { RestRequest req = _client.Create("tv/{id}/season/{season_number}/{method}"); req.AddUrlSegment("id", tvShowId.ToString(CultureInfo.InvariantCulture)); req.AddUrlSegment("season_number", seasonNumber.ToString(CultureInfo.InvariantCulture)); req.AddUrlSegment("method", tvShowMethod.GetDescription()); // TODO: Dateformat? //if (dateFormat != null) // req.DateFormat = dateFormat; language ??= DefaultLanguage; if (!string.IsNullOrWhiteSpace(language)) req.AddParameter("language", language); T response = await req.GetOfT(cancellationToken).ConfigureAwait(false); return response; } public async Task> GetTvSeasonAccountStateAsync(int tvShowId, int seasonNumber, CancellationToken cancellationToken = default) { RequireSessionId(SessionType.UserSession); RestRequest req = _client.Create("tv/{id}/season/{season_number}/account_states"); req.AddUrlSegment("id", tvShowId.ToString(CultureInfo.InvariantCulture)); req.AddUrlSegment("season_number", seasonNumber.ToString(CultureInfo.InvariantCulture)); req.AddUrlSegment("method", TvEpisodeMethods.AccountStates.GetDescription()); AddSessionId(req, SessionType.UserSession); using RestResponse> response = await req.Get>(cancellationToken).ConfigureAwait(false); return await response.GetDataObject().ConfigureAwait(false); } /// /// Retrieve a season for a specifc tv Show by id. /// /// TMDb id of the tv show the desired season belongs to. /// The season number of the season you want to retrieve. Note use 0 for specials. /// Enum flags indicating any additional data that should be fetched in the same request. /// If specified the api will attempt to return a localized result. ex: en,it,es /// If specified the api will attempt to return localized image results eg. en,it,es. /// A cancellation token /// The requested season for the specified tv show public async Task GetTvSeasonAsync(int tvShowId, int seasonNumber, TvSeasonMethods extraMethods = TvSeasonMethods.Undefined, string language = null, string includeImageLanguage = null, CancellationToken cancellationToken = default) { if (extraMethods.HasFlag(TvSeasonMethods.AccountStates)) RequireSessionId(SessionType.UserSession); RestRequest req = _client.Create("tv/{id}/season/{season_number}"); req.AddUrlSegment("id", tvShowId.ToString(CultureInfo.InvariantCulture)); req.AddUrlSegment("season_number", seasonNumber.ToString(CultureInfo.InvariantCulture)); if (extraMethods.HasFlag(TvSeasonMethods.AccountStates)) AddSessionId(req, SessionType.UserSession); language ??= DefaultLanguage; if (!string.IsNullOrWhiteSpace(language)) req.AddParameter("language", language); includeImageLanguage ??= DefaultImageLanguage; if (!string.IsNullOrWhiteSpace(includeImageLanguage)) req.AddParameter("include_image_language", includeImageLanguage); string appends = string.Join(",", Enum.GetValues(typeof(TvSeasonMethods)) .OfType() .Except(new[] { TvSeasonMethods.Undefined }) .Where(s => extraMethods.HasFlag(s)) .Select(s => s.GetDescription())); if (appends != string.Empty) req.AddParameter("append_to_response", appends); using RestResponse response = await req.Get(cancellationToken).ConfigureAwait(false); if (!response.IsValid) return null; TvSeason item = await response.GetDataObject().ConfigureAwait(false); // Nothing to patch up if (item == null) return null; if (item.Images != null) item.Images.Id = item.Id ?? 0; if (item.Credits != null) item.Credits.Id = item.Id ?? 0; if (item.ExternalIds != null) item.ExternalIds.Id = item.Id ?? 0; if (item.AccountStates != null) item.AccountStates.Id = item.Id ?? 0; if (item.Videos != null) item.Videos.Id = item.Id ?? 0; return item; } /// /// Returns a credits object for the season of the tv show associated with the provided TMDb id. /// /// The TMDb id of the target tv show. /// The season number of the season you want to retrieve information for. Note use 0 for specials. /// If specified the api will attempt to return a localized result. ex: en,it,es /// A cancellation token public async Task GetTvSeasonCreditsAsync(int tvShowId, int seasonNumber, string language = null, CancellationToken cancellationToken = default) { return await GetTvSeasonMethodInternal(tvShowId, seasonNumber, TvSeasonMethods.Credits, dateFormat: "yyyy-MM-dd", language: language, cancellationToken: cancellationToken).ConfigureAwait(false); } /// /// Returns an object that contains all known exteral id's for the season of the tv show related to the specified TMDB id. /// /// The TMDb id of the target tv show. /// The season number of the season you want to retrieve information for. Note use 0 for specials. /// A cancellation token public async Task GetTvSeasonExternalIdsAsync(int tvShowId, int seasonNumber, CancellationToken cancellationToken = default) { return await GetTvSeasonMethodInternal(tvShowId, seasonNumber, TvSeasonMethods.ExternalIds, cancellationToken: cancellationToken).ConfigureAwait(false); } /// /// Retrieves all images all related to the season of specified tv show. /// /// The TMDb id of the target tv show. /// The season number of the season you want to retrieve information for. Note use 0 for specials. /// /// If specified the api will attempt to return a localized result. ex: en,it,es. /// For images this means that the image might contain language specifc text /// /// A cancellation token public async Task GetTvSeasonImagesAsync(int tvShowId, int seasonNumber, string language = null, CancellationToken cancellationToken = default) { return await GetTvSeasonMethodInternal(tvShowId, seasonNumber, TvSeasonMethods.Images, language: language, cancellationToken: cancellationToken).ConfigureAwait(false); } public async Task> GetTvSeasonVideosAsync(int tvShowId, int seasonNumber, string language = null, CancellationToken cancellationToken = default) { return await GetTvSeasonMethodInternal>(tvShowId, seasonNumber, TvSeasonMethods.Videos, language: language, cancellationToken: cancellationToken).ConfigureAwait(false); } } }