Add some error handling, grab imdb id and other minor fixes

This commit is contained in:
Claus Vium 2019-02-07 19:54:34 +01:00
parent 2a26760911
commit ecbc0538f6
4 changed files with 58 additions and 89 deletions

View File

@ -28,24 +28,24 @@ namespace MediaBrowser.Controller.Library
{
if (string.Equals(day, "Daily", StringComparison.OrdinalIgnoreCase))
{
return new DayOfWeek[]
{
DayOfWeek.Sunday,
DayOfWeek.Monday,
DayOfWeek.Tuesday,
DayOfWeek.Wednesday,
DayOfWeek.Thursday,
DayOfWeek.Friday,
DayOfWeek.Saturday
};
return new[]
{
DayOfWeek.Sunday,
DayOfWeek.Monday,
DayOfWeek.Tuesday,
DayOfWeek.Wednesday,
DayOfWeek.Thursday,
DayOfWeek.Friday,
DayOfWeek.Saturday
};
}
if (Enum.TryParse(day, true, out DayOfWeek value))
{
return new DayOfWeek[]
{
value
};
return new[]
{
value
};
}
return new DayOfWeek[] { };

View File

@ -18,7 +18,6 @@ namespace MediaBrowser.Providers.TV.TheTVDB
{
public class TvdbEpisodeImageProvider : IRemoteImageProvider
{
private readonly CultureInfo _usCulture = new CultureInfo("en-US");
private readonly IHttpClient _httpClient;
private readonly TvDbClientManager _tvDbClientManager;
@ -47,6 +46,7 @@ namespace MediaBrowser.Providers.TV.TheTVDB
{
var episode = (Episode)item;
var series = episode.Series;
var imageResult = new List<RemoteImageInfo>();
if (series != null && TvdbSeriesProvider.IsValidSeries(series.ProviderIds))
{
@ -55,21 +55,17 @@ namespace MediaBrowser.Providers.TV.TheTVDB
var episodeResult = await _tvDbClientManager.TvDbClient.Episodes.GetAsync(Convert.ToInt32(tvdbId), cancellationToken);
var image = GetImageInfo(episodeResult.Data);
return new List<RemoteImageInfo>
if (image != null)
{
image
};
imageResult.Add(image);
}
}
return new RemoteImageInfo[] { };
return imageResult;
}
private RemoteImageInfo GetImageInfo(EpisodeRecord episode)
{
var height = 225;
var width = 400;
var url = string.Empty;
if (string.IsNullOrEmpty(episode.Filename))
{
return null;

View File

@ -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;
using RatingType = MediaBrowser.Model.Dto.RatingType;
@ -22,11 +23,13 @@ namespace MediaBrowser.Providers.TV.TheTVDB
private static readonly CultureInfo UsCulture = new CultureInfo("en-US");
private readonly IHttpClient _httpClient;
private readonly ILogger _logger;
private readonly TvDbClientManager _tvDbClientManager;
public TvdbSeasonImageProvider(IHttpClient httpClient)
public TvdbSeasonImageProvider(IHttpClient httpClient, ILogger logger)
{
_httpClient = httpClient;
_logger = logger;
_tvDbClientManager = TvDbClientManager.Instance;
}
@ -59,13 +62,14 @@ namespace MediaBrowser.Providers.TV.TheTVDB
return new RemoteImageInfo[] { };
}
var tvdbId = Convert.ToInt32(series.GetProviderId(MetadataProviders.Tvdb));
var seasonNumber = season.IndexNumber.Value;
var language = item.GetPreferredMetadataLanguage();
_tvDbClientManager.TvDbClient.AcceptedLanguage = language;
var remoteImages = new List<RemoteImageInfo>();
var keyTypes = new[] {KeyType.Season, KeyType.Seasonwide, KeyType.Fanart};
// TODO error handling
foreach (KeyType keyType in keyTypes)
foreach (var keyType in keyTypes)
{
var imageQuery = new ImagesQuery
{
@ -74,15 +78,12 @@ namespace MediaBrowser.Providers.TV.TheTVDB
};
try
{
var imageResults =
await _tvDbClientManager.TvDbClient.Series.GetImagesAsync(
Convert.ToInt32(series.GetProviderId(MetadataProviders.Tvdb)), imageQuery,
cancellationToken);
var imageResults = await _tvDbClientManager.TvDbClient.Series.GetImagesAsync(tvdbId, imageQuery, cancellationToken);
remoteImages.AddRange(GetImages(imageResults.Data, language));
}
catch (TvDbServerException e)
{
// TODO log
_logger.LogInformation(e, "No images of type {KeyType} found for series {TvdbId}", keyType, tvdbId);
}
}

View File

@ -95,7 +95,7 @@ namespace MediaBrowser.Providers.TV.TheTVDB
result.Item = new Series();
result.HasMetadata = true;
FetchSeriesData(result, itemId.MetadataLanguage, itemId.ProviderIds, cancellationToken);
await FetchSeriesData(result, itemId.MetadataLanguage, itemId.ProviderIds, cancellationToken);
}
return result;
@ -127,7 +127,7 @@ namespace MediaBrowser.Providers.TV.TheTVDB
var seriesResult = await _tvDbClientManager.TvDbClient.Series.GetAsync(Convert.ToInt32(tvdbId), cancellationToken);
// TODO error handling
MapSeriesToResult(result, seriesResult.Data, cancellationToken);
MapSeriesToResult(result, seriesResult.Data);
cancellationToken.ThrowIfCancellationRequested();
@ -161,51 +161,6 @@ namespace MediaBrowser.Providers.TV.TheTVDB
seriesProviderIds.TryGetValue(MetadataProviders.Zap2It.ToString(), out _);
}
// TODO caching
private bool IsCacheValid(string seriesDataPath, string preferredMetadataLanguage)
{
return true;
// try
// {
// var files = _fileSystem.GetFiles(seriesDataPath, new[] { ".xml" }, true, false)
// .ToList();
//
// var seriesXmlFilename = preferredMetadataLanguage + ".xml";
//
// const int cacheHours = 12;
//
// var seriesFile = files.FirstOrDefault(i => string.Equals(seriesXmlFilename, i.Name, StringComparison.OrdinalIgnoreCase));
// // No need to check age if automatic updates are enabled
// if (seriesFile == null || !seriesFile.Exists || (DateTime.UtcNow - _fileSystem.GetLastWriteTimeUtc(seriesFile)).TotalHours > cacheHours)
// {
// return false;
// }
//
// var actorsXml = files.FirstOrDefault(i => string.Equals("actors.xml", i.Name, StringComparison.OrdinalIgnoreCase));
// // No need to check age if automatic updates are enabled
// if (actorsXml == null || !actorsXml.Exists || (DateTime.UtcNow - _fileSystem.GetLastWriteTimeUtc(actorsXml)).TotalHours > cacheHours)
// {
// return false;
// }
//
// var bannersXml = files.FirstOrDefault(i => string.Equals("banners.xml", i.Name, StringComparison.OrdinalIgnoreCase));
// // No need to check age if automatic updates are enabled
// if (bannersXml == null || !bannersXml.Exists || (DateTime.UtcNow - _fileSystem.GetLastWriteTimeUtc(bannersXml)).TotalHours > cacheHours)
// {
// return false;
// }
// return true;
// }
// catch (FileNotFoundException)
// {
// return false;
// }
// catch (IOException)
// {
// return false;
// }
}
/// <summary>
/// Finds the series.
/// </summary>
@ -246,7 +201,16 @@ namespace MediaBrowser.Providers.TV.TheTVDB
_tvDbClientManager.TvDbClient.AcceptedLanguage = NormalizeLanguage(language);
var comparableName = GetComparableName(name);
var list = new List<Tuple<List<string>, RemoteSearchResult>>();
TvDbResponse<SeriesSearchResult[]> result = await _tvDbClientManager.TvDbClient.Search.SearchSeriesByNameAsync(comparableName, cancellationToken);
TvDbResponse<SeriesSearchResult[]> result;
try
{
result = await _tvDbClientManager.TvDbClient.Search.SearchSeriesByNameAsync(comparableName, cancellationToken);
}
catch (TvDbServerException e)
{
_logger.LogError(e, "No series results found for {Name}", comparableName);
return new List<RemoteSearchResult>();
}
foreach (var seriesSearchResult in result.Data)
{
@ -265,8 +229,18 @@ namespace MediaBrowser.Providers.TV.TheTVDB
ImageUrl = TVUtils.BannerUrl + seriesSearchResult.Banner
};
// TODO requires another query, is it worth it?
// remoteSearchResult.SetProviderId(MetadataProviders.Imdb, seriesSearchResult.Id);
try
{
var seriesSesult =
await _tvDbClientManager.TvDbClient.Series.GetAsync(seriesSearchResult.Id, cancellationToken);
remoteSearchResult.SetProviderId(MetadataProviders.Imdb, seriesSesult.Data.ImdbId);
remoteSearchResult.SetProviderId(MetadataProviders.Zap2It, seriesSesult.Data.Zap2itId);
}
catch (TvDbServerException e)
{
_logger.LogError(e, "Unable to retrieve series with id {TvdbId}", seriesSearchResult.Id);
}
remoteSearchResult.SetProviderId(MetadataProviders.Tvdb, seriesSearchResult.Id.ToString());
list.Add(new Tuple<List<string>, RemoteSearchResult>(tvdbTitles, remoteSearchResult));
}
@ -335,14 +309,13 @@ namespace MediaBrowser.Providers.TV.TheTVDB
return name.Trim();
}
private static void MapSeriesToResult(MetadataResult<Series> result, TvDbSharper.Dto.Series tvdbSeries, CancellationToken cancellationToken)
private static void MapSeriesToResult(MetadataResult<Series> result, TvDbSharper.Dto.Series tvdbSeries)
{
var episodeAirDates = new List<DateTime>();
Series series = result.Item;
series.SetProviderId(MetadataProviders.Tvdb, tvdbSeries.Id.ToString());
series.Name = tvdbSeries.SeriesName;
series.Overview = (tvdbSeries.Overview ?? string.Empty).Trim();
// TODO result.ResultLanguage = (seriesResponse.Data. ?? string.Empty).Trim();
result.ResultLanguage = TvDbClientManager.Instance.TvDbClient.AcceptedLanguage;
series.AirDays = TVUtils.GetAirDays(tvdbSeries.AirsDayOfWeek);
series.AirTime = tvdbSeries.AirsTime;
@ -368,14 +341,13 @@ namespace MediaBrowser.Providers.TV.TheTVDB
series.AddGenre(genre);
}
// TODO is network == studio?
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 && episodeAirDates.Count > 0)
// {
// result.Item.EndDate = episodeAirDates.Max();
// }
}
private static void MapActorsToResult(MetadataResult<Series> result, IEnumerable<Actor> actors)