diff --git a/MediaBrowser.Providers/TV/TheTVDB/TvDbClientManager.cs b/MediaBrowser.Providers/TV/TheTVDB/TvDbClientManager.cs index e789d2ded..7a83cfa18 100644 --- a/MediaBrowser.Providers/TV/TheTVDB/TvDbClientManager.cs +++ b/MediaBrowser.Providers/TV/TheTVDB/TvDbClientManager.cs @@ -94,7 +94,7 @@ namespace MediaBrowser.Providers.TV public Task> GetSeriesByZap2ItIdAsync(string zap2ItId, CancellationToken cancellationToken) { - return TryGetValue("series" + zap2ItId,() => TvDbClient.Search.SearchSeriesByImdbIdAsync(zap2ItId, cancellationToken)); + return TryGetValue("series" + zap2ItId,() => TvDbClient.Search.SearchSeriesByZap2ItIdAsync(zap2ItId, cancellationToken)); } public Task> GetActorsAsync(int tvdbId, CancellationToken cancellationToken) { @@ -111,6 +111,18 @@ namespace MediaBrowser.Providers.TV return TryGetValue("languages",() => TvDbClient.Languages.GetAllAsync(cancellationToken)); } + public Task> GetSeriesEpisodeSummaryAsync(int tvdbId, CancellationToken cancellationToken) + { + return TryGetValue("seriesepisodesummary" + tvdbId, + () => TvDbClient.Series.GetEpisodesSummaryAsync(tvdbId, cancellationToken)); + } + + public Task> GetEpisodesPageAsync(int tvdbId, EpisodeQuery episodeQuery, CancellationToken cancellationToken) + { + return TryGetValue("episodespage" + tvdbId + episodeQuery.AiredSeason, + () => TvDbClient.Series.GetEpisodesAsync(tvdbId, 1, episodeQuery, cancellationToken)); + } + private async Task TryGetValue(object key, Func> resultFactory) { if (_cache.TryGetValue(key, out T cachedValue)) diff --git a/MediaBrowser.Providers/TV/TheTVDB/TvdbSeriesProvider.cs b/MediaBrowser.Providers/TV/TheTVDB/TvdbSeriesProvider.cs index 9940cf7e4..6b124c96d 100644 --- a/MediaBrowser.Providers/TV/TheTVDB/TvdbSeriesProvider.cs +++ b/MediaBrowser.Providers/TV/TheTVDB/TvdbSeriesProvider.cs @@ -329,7 +329,7 @@ namespace MediaBrowser.Providers.TV.TheTVDB return name.Trim(); } - private static void MapSeriesToResult(MetadataResult result, TvDbSharper.Dto.Series tvdbSeries) + private void MapSeriesToResult(MetadataResult result, TvDbSharper.Dto.Series tvdbSeries) { Series series = result.Item; series.SetProviderId(MetadataProviders.Tvdb, tvdbSeries.Id.ToString()); @@ -363,11 +363,30 @@ namespace MediaBrowser.Providers.TV.TheTVDB series.AddStudio(tvdbSeries.Network); - // TODO is this necessary? - // if (result.Item.Status.HasValue && result.Item.Status.Value == SeriesStatus.Ended && episodeAirDates.Count > 0) - // { - // result.Item.EndDate = episodeAirDates.Max(); - // } + if (result.Item.Status.HasValue && result.Item.Status.Value == SeriesStatus.Ended) + { + try + { + var episodeSummary = _tvDbClientManager + .GetSeriesEpisodeSummaryAsync(tvdbSeries.Id, CancellationToken.None).Result.Data; + var maxSeasonNumber = episodeSummary.AiredSeasons.Select(s => Convert.ToInt32(s)).Max(); + var episodeQuery = new EpisodeQuery + { + AiredSeason = maxSeasonNumber + }; + var episodesPage = + _tvDbClientManager.GetEpisodesPageAsync(tvdbSeries.Id, episodeQuery, CancellationToken.None).Result.Data; + result.Item.EndDate = episodesPage.Select(e => + { + DateTime.TryParse(e.FirstAired, out var firstAired); + return firstAired; + }).Max(); + } + catch (TvDbServerException e) + { + _logger.LogError(e, "Failed to find series end date for series {TvdbId}", tvdbSeries.Id); + } + } } private static void MapActorsToResult(MetadataResult result, IEnumerable actors)