Add much needed exception handling and logging
This commit is contained in:
parent
1f8e74f3a8
commit
42c233c74e
|
@ -3,7 +3,6 @@ using System.Threading;
|
|||
using System.Threading.Tasks;
|
||||
using MediaBrowser.Controller.Library;
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
using Microsoft.Extensions.Internal;
|
||||
using TvDbSharper;
|
||||
using TvDbSharper.Dto;
|
||||
|
||||
|
@ -75,43 +74,47 @@ namespace MediaBrowser.Providers.TV
|
|||
|
||||
public Task<TvDbResponse<SeriesSearchResult[]>> GetSeriesByNameAsync(string name, CancellationToken cancellationToken)
|
||||
{
|
||||
return TryGetValue(name,() => TvDbClient.Search.SearchSeriesByNameAsync(name, cancellationToken));
|
||||
return TryGetValue("series" + name,() => TvDbClient.Search.SearchSeriesByNameAsync(name, cancellationToken));
|
||||
}
|
||||
|
||||
public Task<TvDbResponse<Series>> GetSeriesByIdAsync(int tvdbId, CancellationToken cancellationToken)
|
||||
{
|
||||
return TryGetValue(tvdbId,() => TvDbClient.Series.GetAsync(tvdbId, cancellationToken));
|
||||
return TryGetValue("series" + tvdbId,() => TvDbClient.Series.GetAsync(tvdbId, cancellationToken));
|
||||
}
|
||||
|
||||
public Task<TvDbResponse<EpisodeRecord>> GetEpisodesAsync(int tvdbId, CancellationToken cancellationToken)
|
||||
public Task<TvDbResponse<EpisodeRecord>> GetEpisodesAsync(int episodeTvdbId, CancellationToken cancellationToken)
|
||||
{
|
||||
return TryGetValue(tvdbId,() => TvDbClient.Episodes.GetAsync(tvdbId, cancellationToken));
|
||||
return TryGetValue("episode" + episodeTvdbId,() => TvDbClient.Episodes.GetAsync(episodeTvdbId, cancellationToken));
|
||||
}
|
||||
|
||||
public Task<TvDbResponse<SeriesSearchResult[]>> GetSeriesByImdbIdAsync(string imdbId, CancellationToken cancellationToken)
|
||||
{
|
||||
return TryGetValue(imdbId,() => TvDbClient.Search.SearchSeriesByImdbIdAsync(imdbId, cancellationToken));
|
||||
return TryGetValue("series" + imdbId,() => TvDbClient.Search.SearchSeriesByImdbIdAsync(imdbId, cancellationToken));
|
||||
}
|
||||
|
||||
public Task<TvDbResponse<SeriesSearchResult[]>> GetSeriesByZap2ItIdAsync(string zap2ItId, CancellationToken cancellationToken)
|
||||
{
|
||||
return TryGetValue(zap2ItId,() => TvDbClient.Search.SearchSeriesByImdbIdAsync(zap2ItId, cancellationToken));
|
||||
return TryGetValue("series" + zap2ItId,() => TvDbClient.Search.SearchSeriesByImdbIdAsync(zap2ItId, cancellationToken));
|
||||
}
|
||||
public Task<TvDbResponse<Actor[]>> GetActorsAsync(int tvdbId, CancellationToken cancellationToken)
|
||||
{
|
||||
return TryGetValue(tvdbId,() => TvDbClient.Series.GetActorsAsync(tvdbId, cancellationToken));
|
||||
return TryGetValue("actors" + tvdbId,() => TvDbClient.Series.GetActorsAsync(tvdbId, cancellationToken));
|
||||
}
|
||||
|
||||
public Task<TvDbResponse<Image[]>> GetImagesAsync(int tvdbId, ImagesQuery imageQuery, CancellationToken cancellationToken)
|
||||
{
|
||||
return TryGetValue(tvdbId,() => TvDbClient.Series.GetImagesAsync(tvdbId, imageQuery, cancellationToken));
|
||||
return TryGetValue("images" + tvdbId,() => TvDbClient.Series.GetImagesAsync(tvdbId, imageQuery, cancellationToken));
|
||||
}
|
||||
|
||||
public Task<TvDbResponse<Language[]>> GetLanguagesAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
return TryGetValue("languages",() => TvDbClient.Languages.GetAllAsync(cancellationToken));
|
||||
}
|
||||
|
||||
private async Task<T> TryGetValue<T>(object key, Func<Task<T>> resultFactory)
|
||||
{
|
||||
if (_cache.TryGetValue(key, out T cachedValue))
|
||||
{
|
||||
Console.WriteLine("Cache hit!!!");
|
||||
return cachedValue;
|
||||
}
|
||||
|
||||
|
|
|
@ -11,6 +11,7 @@ using MediaBrowser.Controller.Library;
|
|||
using MediaBrowser.Controller.Providers;
|
||||
using MediaBrowser.Model.Entities;
|
||||
using MediaBrowser.Model.Providers;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using TvDbSharper;
|
||||
using TvDbSharper.Dto;
|
||||
|
||||
|
@ -19,11 +20,13 @@ namespace MediaBrowser.Providers.TV.TheTVDB
|
|||
public class TvdbEpisodeImageProvider : IRemoteImageProvider
|
||||
{
|
||||
private readonly IHttpClient _httpClient;
|
||||
private readonly ILogger _logger;
|
||||
private readonly TvDbClientManager _tvDbClientManager;
|
||||
|
||||
public TvdbEpisodeImageProvider(IHttpClient httpClient)
|
||||
public TvdbEpisodeImageProvider(IHttpClient httpClient, ILogger logger)
|
||||
{
|
||||
_httpClient = httpClient;
|
||||
_logger = logger;
|
||||
_tvDbClientManager = TvDbClientManager.Instance;
|
||||
}
|
||||
|
||||
|
@ -51,8 +54,12 @@ namespace MediaBrowser.Providers.TV.TheTVDB
|
|||
if (series != null && TvdbSeriesProvider.IsValidSeries(series.ProviderIds))
|
||||
{
|
||||
var tvdbId = episode.GetProviderId(MetadataProviders.Tvdb);
|
||||
|
||||
// Process images
|
||||
var episodeResult = await _tvDbClientManager.GetEpisodesAsync(Convert.ToInt32(tvdbId), cancellationToken);
|
||||
try
|
||||
{
|
||||
var episodeResult =
|
||||
await _tvDbClientManager.GetEpisodesAsync(Convert.ToInt32(tvdbId), cancellationToken);
|
||||
|
||||
var image = GetImageInfo(episodeResult.Data);
|
||||
if (image != null)
|
||||
|
@ -60,6 +67,11 @@ namespace MediaBrowser.Providers.TV.TheTVDB
|
|||
imageResult.Add(image);
|
||||
}
|
||||
}
|
||||
catch (TvDbServerException e)
|
||||
{
|
||||
_logger.LogError(e, "Failed to retrieve episode images for {TvDbId}", tvdbId);
|
||||
}
|
||||
}
|
||||
|
||||
return imageResult;
|
||||
}
|
||||
|
|
|
@ -9,6 +9,7 @@ using MediaBrowser.Controller.Providers;
|
|||
using MediaBrowser.Model.Entities;
|
||||
using MediaBrowser.Model.Providers;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using TvDbSharper;
|
||||
using TvDbSharper.Dto;
|
||||
|
||||
namespace MediaBrowser.Providers.TV
|
||||
|
@ -37,13 +38,15 @@ namespace MediaBrowser.Providers.TV
|
|||
var list = new List<RemoteSearchResult>();
|
||||
|
||||
// The search query must either provide an episode number or date
|
||||
// TODO premieredate functionality is ded
|
||||
// TODO premieredate functionality is ded, could grab all episodes and search for it
|
||||
if (!searchInfo.IndexNumber.HasValue || !searchInfo.PremiereDate.HasValue)
|
||||
{
|
||||
return list;
|
||||
}
|
||||
|
||||
if (TvdbSeriesProvider.IsValidSeries(searchInfo.SeriesProviderIds))
|
||||
{
|
||||
try
|
||||
{
|
||||
var episodeResult =
|
||||
await _tvDbClientManager.GetEpisodesAsync((int)searchInfo.IndexNumber, cancellationToken);
|
||||
|
@ -66,6 +69,11 @@ namespace MediaBrowser.Providers.TV
|
|||
});
|
||||
}
|
||||
}
|
||||
catch (TvDbServerException e)
|
||||
{
|
||||
_logger.LogError(e, "Failed to retrieve episode with id {TvDbId}", searchInfo.IndexNumber);
|
||||
}
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
|
@ -82,11 +90,20 @@ namespace MediaBrowser.Providers.TV
|
|||
if (TvdbSeriesProvider.IsValidSeries(searchInfo.SeriesProviderIds) &&
|
||||
(searchInfo.IndexNumber.HasValue || searchInfo.PremiereDate.HasValue))
|
||||
{
|
||||
var episodeResult = await _tvDbClientManager.GetEpisodesAsync(Convert.ToInt32(searchInfo.GetProviderId(MetadataProviders.Tvdb)),
|
||||
var tvdbId = searchInfo.GetProviderId(MetadataProviders.Tvdb);
|
||||
try
|
||||
{
|
||||
var episodeResult = await _tvDbClientManager.GetEpisodesAsync(
|
||||
Convert.ToInt32(tvdbId),
|
||||
cancellationToken);
|
||||
|
||||
result = MapEpisodeToResult(searchInfo, episodeResult.Data);
|
||||
}
|
||||
catch (TvDbServerException e)
|
||||
{
|
||||
_logger.LogError(e, "Failed to retrieve episode with id {TvDbId}", tvdbId);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger.LogDebug("No series identity found for {0}", searchInfo.Name);
|
||||
|
@ -178,14 +195,8 @@ namespace MediaBrowser.Providers.TV
|
|||
Type = PersonType.Writer
|
||||
});
|
||||
}
|
||||
// TODO result.ResultLanguage = episode.
|
||||
|
||||
// TODO wtf is additional part info?
|
||||
// foreach (var node in xmlNodes.Skip(1))
|
||||
// {
|
||||
// FetchAdditionalPartInfo(result, node, cancellationToken);
|
||||
// }
|
||||
|
||||
result.ResultLanguage = episode.Language.EpisodeName;
|
||||
return result;
|
||||
}
|
||||
|
||||
|
|
|
@ -93,7 +93,7 @@ namespace MediaBrowser.Providers.TV.TheTVDB
|
|||
private static IEnumerable<RemoteImageInfo> GetImages(Image[] images, string preferredLanguage)
|
||||
{
|
||||
var list = new List<RemoteImageInfo>();
|
||||
|
||||
var languages = TvDbClientManager.Instance.GetLanguagesAsync(CancellationToken.None).Result.Data;
|
||||
foreach (Image image in images)
|
||||
{
|
||||
var imageInfo = new RemoteImageInfo
|
||||
|
@ -103,7 +103,7 @@ namespace MediaBrowser.Providers.TV.TheTVDB
|
|||
VoteCount = image.RatingsInfo.Count,
|
||||
Url = TVUtils.BannerUrl + image.FileName,
|
||||
ProviderName = ProviderName,
|
||||
// TODO Language = image.LanguageId,
|
||||
Language = languages.FirstOrDefault(l => l.Id == image.LanguageId)?.Abbreviation,
|
||||
ThumbnailUrl = TVUtils.BannerUrl + image.Thumbnail
|
||||
};
|
||||
|
||||
|
|
|
@ -10,6 +10,7 @@ using MediaBrowser.Controller.Library;
|
|||
using MediaBrowser.Controller.Providers;
|
||||
using MediaBrowser.Model.Entities;
|
||||
using MediaBrowser.Model.Providers;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using TvDbSharper;
|
||||
using TvDbSharper.Dto;
|
||||
using RatingType = MediaBrowser.Model.Dto.RatingType;
|
||||
|
@ -20,12 +21,13 @@ namespace MediaBrowser.Providers.TV.TheTVDB
|
|||
public class TvdbSeriesImageProvider : IRemoteImageProvider, IHasOrder
|
||||
{
|
||||
private readonly IHttpClient _httpClient;
|
||||
private readonly CultureInfo _usCulture = new CultureInfo("en-US");
|
||||
private readonly ILogger _logger;
|
||||
private readonly TvDbClientManager _tvDbClientManager;
|
||||
|
||||
public TvdbSeriesImageProvider(IHttpClient httpClient)
|
||||
public TvdbSeriesImageProvider(IHttpClient httpClient, ILogger logger)
|
||||
{
|
||||
_httpClient = httpClient;
|
||||
_logger = logger;
|
||||
_tvDbClientManager = TvDbClientManager.Instance;
|
||||
}
|
||||
|
||||
|
@ -59,24 +61,33 @@ namespace MediaBrowser.Providers.TV.TheTVDB
|
|||
_tvDbClientManager.TvDbClient.AcceptedLanguage = language;
|
||||
var remoteImages = new List<RemoteImageInfo>();
|
||||
var keyTypes = new[] {KeyType.Poster, KeyType.Series, KeyType.Fanart};
|
||||
// TODO error handling
|
||||
var tvdbId = Convert.ToInt32(item.GetProviderId(MetadataProviders.Tvdb));
|
||||
foreach (KeyType keyType in keyTypes)
|
||||
{
|
||||
var imageQuery = new ImagesQuery
|
||||
{
|
||||
KeyType = keyType
|
||||
};
|
||||
try
|
||||
{
|
||||
var imageResults =
|
||||
await _tvDbClientManager.GetImagesAsync(Convert.ToInt32(item.GetProviderId(MetadataProviders.Tvdb)), imageQuery, cancellationToken);
|
||||
await _tvDbClientManager.GetImagesAsync(tvdbId, imageQuery, cancellationToken);
|
||||
|
||||
remoteImages.AddRange(GetImages(imageResults.Data, language));
|
||||
}
|
||||
catch (TvDbServerException e)
|
||||
{
|
||||
_logger.LogError(e, "Failed to retrieve images of type {KeyType} for series {TvDbId}", keyType,
|
||||
tvdbId);
|
||||
}
|
||||
}
|
||||
return remoteImages;
|
||||
}
|
||||
|
||||
private IEnumerable<RemoteImageInfo> GetImages(Image[] images, string preferredLanguage)
|
||||
{
|
||||
var list = new List<RemoteImageInfo>();
|
||||
var languages = TvDbClientManager.Instance.GetLanguagesAsync(CancellationToken.None).Result.Data;
|
||||
|
||||
foreach (Image image in images)
|
||||
{
|
||||
|
@ -87,7 +98,7 @@ namespace MediaBrowser.Providers.TV.TheTVDB
|
|||
VoteCount = image.RatingsInfo.Count,
|
||||
Url = TVUtils.BannerUrl + image.FileName,
|
||||
ProviderName = Name,
|
||||
// TODO Language = image.LanguageId,
|
||||
Language = languages.FirstOrDefault(l => l.Id == image.LanguageId)?.Abbreviation,
|
||||
ThumbnailUrl = TVUtils.BannerUrl + image.Thumbnail
|
||||
};
|
||||
|
||||
|
|
|
@ -123,23 +123,40 @@ namespace MediaBrowser.Providers.TV.TheTVDB
|
|||
tvdbId = await GetSeriesByRemoteId(zap2It, MetadataProviders.Zap2It.ToString(), metadataLanguage, cancellationToken);
|
||||
}
|
||||
|
||||
var seriesResult = await _tvDbClientManager.GetSeriesByIdAsync(Convert.ToInt32(tvdbId), cancellationToken);
|
||||
|
||||
try
|
||||
{
|
||||
var seriesResult =
|
||||
await _tvDbClientManager.GetSeriesByIdAsync(Convert.ToInt32(tvdbId), cancellationToken);
|
||||
MapSeriesToResult(result, seriesResult.Data);
|
||||
}
|
||||
catch (TvDbServerException e)
|
||||
{
|
||||
_logger.LogError(e, "Failed to retrieve series with id {TvdbId}", tvdbId);
|
||||
return;
|
||||
}
|
||||
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
|
||||
result.ResetPeople();
|
||||
|
||||
try
|
||||
{
|
||||
var actorsResult = await _tvDbClientManager.GetActorsAsync(Convert.ToInt32(tvdbId), cancellationToken);
|
||||
MapActorsToResult(result, actorsResult.Data);
|
||||
}
|
||||
catch (TvDbServerException e)
|
||||
{
|
||||
_logger.LogError(e, "Failed to retrieve actors for series {TvdbId}", tvdbId);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task<string> GetSeriesByRemoteId(string id, string idType, string language, CancellationToken cancellationToken)
|
||||
{
|
||||
_tvDbClientManager.TvDbClient.AcceptedLanguage = NormalizeLanguage(language);
|
||||
TvDbResponse<SeriesSearchResult[]> result;
|
||||
TvDbResponse<SeriesSearchResult[]> result = null;
|
||||
|
||||
try
|
||||
{
|
||||
if (string.Equals(idType, MetadataProviders.Zap2It.ToString(), StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
result = await _tvDbClientManager.GetSeriesByZap2ItIdAsync(id, cancellationToken);
|
||||
|
@ -148,8 +165,13 @@ namespace MediaBrowser.Providers.TV.TheTVDB
|
|||
{
|
||||
result = await _tvDbClientManager.GetSeriesByImdbIdAsync(id, cancellationToken);
|
||||
}
|
||||
}
|
||||
catch (TvDbServerException e)
|
||||
{
|
||||
_logger.LogError(e, "Failed to retrieve series with remote id {RemoteId}", id);
|
||||
}
|
||||
|
||||
return result.Data.First().Id.ToString();
|
||||
return result?.Data.First().Id.ToString();
|
||||
}
|
||||
|
||||
internal static bool IsValidSeries(Dictionary<string, string> seriesProviderIds)
|
||||
|
|
Loading…
Reference in New Issue
Block a user