diff --git a/MediaBrowser.Providers/TV/TheTVDB/TvDbClientManager.cs b/MediaBrowser.Providers/TV/TheTVDB/TvDbClientManager.cs index c02661e37..1bf4f6e03 100644 --- a/MediaBrowser.Providers/TV/TheTVDB/TvDbClientManager.cs +++ b/MediaBrowser.Providers/TV/TheTVDB/TvDbClientManager.cs @@ -1,8 +1,11 @@ using System; using System.Collections.Generic; +using System.Linq; using System.Threading; using System.Threading.Tasks; using MediaBrowser.Controller.Library; +using MediaBrowser.Controller.Providers; +using MediaBrowser.Model.Entities; using Microsoft.Extensions.Caching.Memory; using TvDbSharper; using TvDbSharper.Dto; @@ -94,8 +97,13 @@ namespace MediaBrowser.Providers.TV var episodes = new List(); var episodePage = await GetEpisodesPageAsync(tvdbId, new EpisodeQuery(), cancellationToken); episodes.AddRange(episodePage.Data); - int next = episodePage.Links.Next.GetValueOrDefault(0); - int last = episodePage.Links.Last.GetValueOrDefault(0); + if (!episodePage.Links.Next.HasValue || !episodePage.Links.Last.HasValue) + { + return episodes; + } + + int next = episodePage.Links.Next.Value; + int last = episodePage.Links.Last.Value; for (var page = next; page <= last; ++page) { @@ -122,7 +130,8 @@ namespace MediaBrowser.Providers.TV public Task> GetImagesAsync(int tvdbId, ImagesQuery imageQuery, CancellationToken cancellationToken) { - return TryGetValue("images" + tvdbId,() => TvDbClient.Series.GetImagesAsync(tvdbId, imageQuery, cancellationToken)); + var cacheKey = "images" + tvdbId + "keytype" + imageQuery.KeyType + "subkey" + imageQuery.SubKey; + return TryGetValue(cacheKey,() => TvDbClient.Series.GetImagesAsync(tvdbId, imageQuery, cancellationToken)); } public Task> GetLanguagesAsync(CancellationToken cancellationToken) @@ -144,10 +153,36 @@ namespace MediaBrowser.Providers.TV { cacheKey += "airedseason" + episodeQuery.AiredSeason.Value; } + if (episodeQuery.AiredEpisode.HasValue) + { + cacheKey += "airedepisode" + episodeQuery.AiredEpisode.Value; + } return TryGetValue(cacheKey, () => TvDbClient.Series.GetEpisodesAsync(tvdbId, page, episodeQuery, cancellationToken)); } + public Task GetEpisodeTvdbId(EpisodeInfo searchInfo, CancellationToken cancellationToken) + { + searchInfo.SeriesProviderIds.TryGetValue(MetadataProviders.Tvdb.ToString(), + out var seriesTvdbId); + var episodeNumber = searchInfo.IndexNumber.Value; + var seasonNumber = searchInfo.ParentIndexNumber.Value; + + return GetEpisodeTvdbId(Convert.ToInt32(seriesTvdbId), episodeNumber, seasonNumber, cancellationToken); + } + + public async Task GetEpisodeTvdbId(int seriesTvdbId, int episodeNumber, int seasonNumber, CancellationToken cancellationToken) + { + var episodeQuery = new EpisodeQuery + { + AiredSeason = seasonNumber, + AiredEpisode = episodeNumber + }; + var episodePage = await GetEpisodesPageAsync(Convert.ToInt32(seriesTvdbId), + episodeQuery, cancellationToken); + return episodePage.Data.FirstOrDefault()?.Id.ToString(); + } + public Task> GetEpisodesPageAsync(int tvdbId, EpisodeQuery episodeQuery, CancellationToken cancellationToken) { return GetEpisodesPageAsync(tvdbId, 1, episodeQuery, cancellationToken); diff --git a/MediaBrowser.Providers/TV/TheTVDB/TvdbEpisodeImageProvider.cs b/MediaBrowser.Providers/TV/TheTVDB/TvdbEpisodeImageProvider.cs index caa0a5aba..8f02b1377 100644 --- a/MediaBrowser.Providers/TV/TheTVDB/TvdbEpisodeImageProvider.cs +++ b/MediaBrowser.Providers/TV/TheTVDB/TvdbEpisodeImageProvider.cs @@ -53,13 +53,28 @@ namespace MediaBrowser.Providers.TV.TheTVDB if (series != null && TvdbSeriesProvider.IsValidSeries(series.ProviderIds)) { - var tvdbId = episode.GetProviderId(MetadataProviders.Tvdb); + var episodeTvdbId = episode.GetProviderId(MetadataProviders.Tvdb); // Process images try { + if (string.IsNullOrEmpty(episodeTvdbId)) + { + var episodeNumber = episode.IndexNumber.Value; + var seasonNumber = episode.ParentIndexNumber.Value; + episodeTvdbId = await _tvDbClientManager.GetEpisodeTvdbId( + Convert.ToInt32(series.GetProviderId(MetadataProviders.Tvdb)), episodeNumber, seasonNumber, + cancellationToken); + if (string.IsNullOrEmpty(episodeTvdbId)) + { + _logger.LogError("Episode {SeasonNumber}x{EpisodeNumber}found for series {SeriesTvdbId}", + seasonNumber, episodeNumber); + return imageResult; + } + } + var episodeResult = - await _tvDbClientManager.GetEpisodesAsync(Convert.ToInt32(tvdbId), cancellationToken); + await _tvDbClientManager.GetEpisodesAsync(Convert.ToInt32(episodeTvdbId), cancellationToken); var image = GetImageInfo(episodeResult.Data); if (image != null) @@ -69,7 +84,7 @@ namespace MediaBrowser.Providers.TV.TheTVDB } catch (TvDbServerException e) { - _logger.LogError(e, "Failed to retrieve episode images for {TvDbId}", tvdbId); + _logger.LogError(e, "Failed to retrieve episode images for {TvDbId}", episodeTvdbId); } } diff --git a/MediaBrowser.Providers/TV/TheTVDB/TvdbEpisodeProvider.cs b/MediaBrowser.Providers/TV/TheTVDB/TvdbEpisodeProvider.cs index 68125d50a..d7fc9b563 100644 --- a/MediaBrowser.Providers/TV/TheTVDB/TvdbEpisodeProvider.cs +++ b/MediaBrowser.Providers/TV/TheTVDB/TvdbEpisodeProvider.cs @@ -20,7 +20,6 @@ namespace MediaBrowser.Providers.TV /// class TvdbEpisodeProvider : IRemoteMetadataProvider, IHasOrder { - internal static TvdbEpisodeProvider Current; private readonly IHttpClient _httpClient; private readonly ILogger _logger; private readonly TvDbClientManager _tvDbClientManager; @@ -29,7 +28,6 @@ namespace MediaBrowser.Providers.TV { _httpClient = httpClient; _logger = logger; - Current = this; _tvDbClientManager = TvDbClientManager.Instance; } @@ -48,8 +46,18 @@ namespace MediaBrowser.Providers.TV { try { - var episodeResult = - await _tvDbClientManager.GetEpisodesAsync((int)searchInfo.IndexNumber, cancellationToken); + var episodeTvdbId = searchInfo.GetProviderId(MetadataProviders.Tvdb); + if (string.IsNullOrEmpty(episodeTvdbId)) + { + episodeTvdbId = await _tvDbClientManager.GetEpisodeTvdbId(searchInfo, cancellationToken); + if (string.IsNullOrEmpty(episodeTvdbId)) + { + _logger.LogError("Episode {SeasonNumber}x{EpisodeNumber} found for series {SeriesTvdbId}", + searchInfo.ParentIndexNumber, searchInfo.IndexNumber); + return list; + } + } + var episodeResult = await _tvDbClientManager.GetEpisodesAsync(Convert.ToInt32(episodeTvdbId), cancellationToken); var metadataResult = MapEpisodeToResult(searchInfo, episodeResult.Data); if (metadataResult.HasMetadata) @@ -93,6 +101,17 @@ namespace MediaBrowser.Providers.TV var tvdbId = searchInfo.GetProviderId(MetadataProviders.Tvdb); try { + if (string.IsNullOrEmpty(tvdbId)) + { + tvdbId = await _tvDbClientManager.GetEpisodeTvdbId(searchInfo, cancellationToken); + if (string.IsNullOrEmpty(tvdbId)) + { + _logger.LogError("Episode {SeasonNumber}x{EpisodeNumber}found for series {SeriesTvdbId}", + searchInfo.ParentIndexNumber, searchInfo.IndexNumber, tvdbId); + return result; + } + } + var episodeResult = await _tvDbClientManager.GetEpisodesAsync( Convert.ToInt32(tvdbId), cancellationToken); diff --git a/MediaBrowser.Providers/TV/TheTVDB/TvdbSeasonImageProvider.cs b/MediaBrowser.Providers/TV/TheTVDB/TvdbSeasonImageProvider.cs index 42c7bc5c3..932b2ff63 100644 --- a/MediaBrowser.Providers/TV/TheTVDB/TvdbSeasonImageProvider.cs +++ b/MediaBrowser.Providers/TV/TheTVDB/TvdbSeasonImageProvider.cs @@ -81,9 +81,9 @@ namespace MediaBrowser.Providers.TV.TheTVDB var imageResults = await _tvDbClientManager.GetImagesAsync(tvdbId, imageQuery, cancellationToken); remoteImages.AddRange(GetImages(imageResults.Data, language)); } - catch (TvDbServerException e) + catch (TvDbServerException) { - _logger.LogInformation(e, "No images of type {KeyType} found for series {TvdbId}", keyType, tvdbId); + _logger.LogDebug("No images of type {KeyType} found for series {TvdbId}", keyType, tvdbId); } } diff --git a/MediaBrowser.Providers/TV/TheTVDB/TvdbSeriesImageProvider.cs b/MediaBrowser.Providers/TV/TheTVDB/TvdbSeriesImageProvider.cs index b9caf0b3a..38251a5c0 100644 --- a/MediaBrowser.Providers/TV/TheTVDB/TvdbSeriesImageProvider.cs +++ b/MediaBrowser.Providers/TV/TheTVDB/TvdbSeriesImageProvider.cs @@ -75,9 +75,9 @@ namespace MediaBrowser.Providers.TV.TheTVDB remoteImages.AddRange(GetImages(imageResults.Data, language)); } - catch (TvDbServerException e) + catch (TvDbServerException) { - _logger.LogError(e, "Failed to retrieve images of type {KeyType} for series {TvDbId}", keyType, + _logger.LogDebug("No images of type {KeyType} exist for series {TvDbId}", keyType, tvdbId); } }