Fix episode provider

pull/776/head
Claus Vium 6 years ago
parent b997b12d27
commit 83d98ac92d

@ -1,8 +1,11 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using MediaBrowser.Controller.Library; using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Entities;
using Microsoft.Extensions.Caching.Memory; using Microsoft.Extensions.Caching.Memory;
using TvDbSharper; using TvDbSharper;
using TvDbSharper.Dto; using TvDbSharper.Dto;
@ -94,8 +97,13 @@ namespace MediaBrowser.Providers.TV
var episodes = new List<EpisodeRecord>(); var episodes = new List<EpisodeRecord>();
var episodePage = await GetEpisodesPageAsync(tvdbId, new EpisodeQuery(), cancellationToken); var episodePage = await GetEpisodesPageAsync(tvdbId, new EpisodeQuery(), cancellationToken);
episodes.AddRange(episodePage.Data); episodes.AddRange(episodePage.Data);
int next = episodePage.Links.Next.GetValueOrDefault(0); if (!episodePage.Links.Next.HasValue || !episodePage.Links.Last.HasValue)
int last = episodePage.Links.Last.GetValueOrDefault(0); {
return episodes;
}
int next = episodePage.Links.Next.Value;
int last = episodePage.Links.Last.Value;
for (var page = next; page <= last; ++page) for (var page = next; page <= last; ++page)
{ {
@ -122,7 +130,8 @@ namespace MediaBrowser.Providers.TV
public Task<TvDbResponse<Image[]>> GetImagesAsync(int tvdbId, ImagesQuery imageQuery, CancellationToken cancellationToken) public Task<TvDbResponse<Image[]>> 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<TvDbResponse<Language[]>> GetLanguagesAsync(CancellationToken cancellationToken) public Task<TvDbResponse<Language[]>> GetLanguagesAsync(CancellationToken cancellationToken)
@ -144,10 +153,36 @@ namespace MediaBrowser.Providers.TV
{ {
cacheKey += "airedseason" + episodeQuery.AiredSeason.Value; cacheKey += "airedseason" + episodeQuery.AiredSeason.Value;
} }
if (episodeQuery.AiredEpisode.HasValue)
{
cacheKey += "airedepisode" + episodeQuery.AiredEpisode.Value;
}
return TryGetValue(cacheKey, return TryGetValue(cacheKey,
() => TvDbClient.Series.GetEpisodesAsync(tvdbId, page, episodeQuery, cancellationToken)); () => TvDbClient.Series.GetEpisodesAsync(tvdbId, page, episodeQuery, cancellationToken));
} }
public Task<string> 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<string> 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<TvDbResponse<EpisodeRecord[]>> GetEpisodesPageAsync(int tvdbId, EpisodeQuery episodeQuery, CancellationToken cancellationToken) public Task<TvDbResponse<EpisodeRecord[]>> GetEpisodesPageAsync(int tvdbId, EpisodeQuery episodeQuery, CancellationToken cancellationToken)
{ {
return GetEpisodesPageAsync(tvdbId, 1, episodeQuery, cancellationToken); return GetEpisodesPageAsync(tvdbId, 1, episodeQuery, cancellationToken);

@ -53,13 +53,28 @@ namespace MediaBrowser.Providers.TV.TheTVDB
if (series != null && TvdbSeriesProvider.IsValidSeries(series.ProviderIds)) if (series != null && TvdbSeriesProvider.IsValidSeries(series.ProviderIds))
{ {
var tvdbId = episode.GetProviderId(MetadataProviders.Tvdb); var episodeTvdbId = episode.GetProviderId(MetadataProviders.Tvdb);
// Process images // Process images
try 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 = var episodeResult =
await _tvDbClientManager.GetEpisodesAsync(Convert.ToInt32(tvdbId), cancellationToken); await _tvDbClientManager.GetEpisodesAsync(Convert.ToInt32(episodeTvdbId), cancellationToken);
var image = GetImageInfo(episodeResult.Data); var image = GetImageInfo(episodeResult.Data);
if (image != null) if (image != null)
@ -69,7 +84,7 @@ namespace MediaBrowser.Providers.TV.TheTVDB
} }
catch (TvDbServerException e) 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);
} }
} }

@ -20,7 +20,6 @@ namespace MediaBrowser.Providers.TV
/// </summary> /// </summary>
class TvdbEpisodeProvider : IRemoteMetadataProvider<Episode, EpisodeInfo>, IHasOrder class TvdbEpisodeProvider : IRemoteMetadataProvider<Episode, EpisodeInfo>, IHasOrder
{ {
internal static TvdbEpisodeProvider Current;
private readonly IHttpClient _httpClient; private readonly IHttpClient _httpClient;
private readonly ILogger _logger; private readonly ILogger _logger;
private readonly TvDbClientManager _tvDbClientManager; private readonly TvDbClientManager _tvDbClientManager;
@ -29,7 +28,6 @@ namespace MediaBrowser.Providers.TV
{ {
_httpClient = httpClient; _httpClient = httpClient;
_logger = logger; _logger = logger;
Current = this;
_tvDbClientManager = TvDbClientManager.Instance; _tvDbClientManager = TvDbClientManager.Instance;
} }
@ -48,8 +46,18 @@ namespace MediaBrowser.Providers.TV
{ {
try try
{ {
var episodeResult = var episodeTvdbId = searchInfo.GetProviderId(MetadataProviders.Tvdb);
await _tvDbClientManager.GetEpisodesAsync((int)searchInfo.IndexNumber, cancellationToken); 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); var metadataResult = MapEpisodeToResult(searchInfo, episodeResult.Data);
if (metadataResult.HasMetadata) if (metadataResult.HasMetadata)
@ -93,6 +101,17 @@ namespace MediaBrowser.Providers.TV
var tvdbId = searchInfo.GetProviderId(MetadataProviders.Tvdb); var tvdbId = searchInfo.GetProviderId(MetadataProviders.Tvdb);
try 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( var episodeResult = await _tvDbClientManager.GetEpisodesAsync(
Convert.ToInt32(tvdbId), Convert.ToInt32(tvdbId),
cancellationToken); cancellationToken);

@ -81,9 +81,9 @@ namespace MediaBrowser.Providers.TV.TheTVDB
var imageResults = await _tvDbClientManager.GetImagesAsync(tvdbId, imageQuery, cancellationToken); var imageResults = await _tvDbClientManager.GetImagesAsync(tvdbId, imageQuery, cancellationToken);
remoteImages.AddRange(GetImages(imageResults.Data, language)); 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);
} }
} }

@ -75,9 +75,9 @@ namespace MediaBrowser.Providers.TV.TheTVDB
remoteImages.AddRange(GetImages(imageResults.Data, language)); 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); tvdbId);
} }
} }

Loading…
Cancel
Save