2020-06-19 18:24:13 +00:00
|
|
|
#pragma warning disable CS1591
|
|
|
|
|
2019-01-30 20:23:23 +00:00
|
|
|
using System;
|
2019-01-13 20:03:10 +00:00
|
|
|
using System.Collections.Generic;
|
2020-08-17 19:10:02 +00:00
|
|
|
using System.Net.Http;
|
2020-08-07 17:26:28 +00:00
|
|
|
using System.Globalization;
|
2019-01-13 19:26:31 +00:00
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
2020-08-31 17:05:21 +00:00
|
|
|
using MediaBrowser.Common.Net;
|
2013-11-01 15:55:25 +00:00
|
|
|
using MediaBrowser.Controller.Entities;
|
|
|
|
using MediaBrowser.Controller.Entities.TV;
|
|
|
|
using MediaBrowser.Controller.Providers;
|
|
|
|
using MediaBrowser.Model.Entities;
|
2019-01-13 19:26:31 +00:00
|
|
|
using MediaBrowser.Model.Providers;
|
2019-02-07 21:42:02 +00:00
|
|
|
using Microsoft.Extensions.Logging;
|
2019-01-30 20:23:23 +00:00
|
|
|
using TvDbSharper;
|
|
|
|
using TvDbSharper.Dto;
|
2013-11-01 15:55:25 +00:00
|
|
|
|
2020-03-09 14:53:07 +00:00
|
|
|
namespace MediaBrowser.Providers.Plugins.TheTvdb
|
2013-11-01 15:55:25 +00:00
|
|
|
{
|
2016-05-25 00:42:12 +00:00
|
|
|
public class TvdbEpisodeImageProvider : IRemoteImageProvider
|
2013-11-01 15:55:25 +00:00
|
|
|
{
|
2020-08-17 19:10:02 +00:00
|
|
|
private readonly IHttpClientFactory _httpClientFactory;
|
2020-06-06 00:15:56 +00:00
|
|
|
private readonly ILogger<TvdbEpisodeImageProvider> _logger;
|
2020-03-09 14:53:07 +00:00
|
|
|
private readonly TvdbClientManager _tvdbClientManager;
|
2013-11-01 15:55:25 +00:00
|
|
|
|
2020-08-17 19:10:02 +00:00
|
|
|
public TvdbEpisodeImageProvider(IHttpClientFactory httpClientFactory, ILogger<TvdbEpisodeImageProvider> logger, TvdbClientManager tvdbClientManager)
|
2013-11-01 15:55:25 +00:00
|
|
|
{
|
2020-08-17 19:10:02 +00:00
|
|
|
_httpClientFactory = httpClientFactory;
|
2019-02-07 21:42:02 +00:00
|
|
|
_logger = logger;
|
2020-03-09 14:53:07 +00:00
|
|
|
_tvdbClientManager = tvdbClientManager;
|
2013-11-01 15:55:25 +00:00
|
|
|
}
|
|
|
|
|
2019-01-13 20:31:14 +00:00
|
|
|
public string Name => "TheTVDB";
|
2013-11-01 15:55:25 +00:00
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
public bool Supports(BaseItem item)
|
2013-11-01 15:55:25 +00:00
|
|
|
{
|
|
|
|
return item is Episode;
|
|
|
|
}
|
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
public IEnumerable<ImageType> GetSupportedImages(BaseItem item)
|
2014-01-28 18:37:01 +00:00
|
|
|
{
|
|
|
|
return new List<ImageType>
|
|
|
|
{
|
|
|
|
ImageType.Primary
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-01-30 20:23:23 +00:00
|
|
|
public async Task<IEnumerable<RemoteImageInfo>> GetImages(BaseItem item, CancellationToken cancellationToken)
|
2013-11-01 15:55:25 +00:00
|
|
|
{
|
|
|
|
var episode = (Episode)item;
|
2014-02-06 15:58:49 +00:00
|
|
|
var series = episode.Series;
|
2019-02-07 18:54:34 +00:00
|
|
|
var imageResult = new List<RemoteImageInfo>();
|
2019-02-12 18:50:19 +00:00
|
|
|
var language = item.GetPreferredMetadataLanguage();
|
2015-11-24 18:45:52 +00:00
|
|
|
if (series != null && TvdbSeriesProvider.IsValidSeries(series.ProviderIds))
|
2013-11-01 15:55:25 +00:00
|
|
|
{
|
|
|
|
// Process images
|
2019-02-07 21:42:02 +00:00
|
|
|
try
|
|
|
|
{
|
2020-09-27 23:16:19 +00:00
|
|
|
string episodeTvdbId = null;
|
|
|
|
|
|
|
|
if (episode.IndexNumber.HasValue && episode.ParentIndexNumber.HasValue)
|
2019-08-15 11:39:56 +00:00
|
|
|
{
|
2020-09-27 23:16:19 +00:00
|
|
|
var episodeInfo = new EpisodeInfo
|
|
|
|
{
|
|
|
|
IndexNumber = episode.IndexNumber.Value,
|
|
|
|
ParentIndexNumber = episode.ParentIndexNumber.Value,
|
|
|
|
SeriesProviderIds = series.ProviderIds,
|
|
|
|
SeriesDisplayOrder = series.DisplayOrder
|
|
|
|
};
|
|
|
|
|
|
|
|
episodeTvdbId = await _tvdbClientManager
|
|
|
|
.GetEpisodeTvdbId(episodeInfo, language, cancellationToken).ConfigureAwait(false);
|
|
|
|
}
|
|
|
|
|
2019-02-08 18:48:18 +00:00
|
|
|
if (string.IsNullOrEmpty(episodeTvdbId))
|
|
|
|
{
|
2019-08-16 19:10:42 +00:00
|
|
|
_logger.LogError(
|
|
|
|
"Episode {SeasonNumber}x{EpisodeNumber} not found for series {SeriesTvdbId}",
|
2020-09-27 23:16:19 +00:00
|
|
|
episode.ParentIndexNumber,
|
|
|
|
episode.IndexNumber,
|
2020-06-06 19:17:49 +00:00
|
|
|
series.GetProviderId(MetadataProvider.Tvdb));
|
2019-08-15 11:39:56 +00:00
|
|
|
return imageResult;
|
2019-02-08 18:48:18 +00:00
|
|
|
}
|
|
|
|
|
2019-02-07 21:42:02 +00:00
|
|
|
var episodeResult =
|
2020-03-09 14:53:07 +00:00
|
|
|
await _tvdbClientManager
|
2020-08-07 17:26:28 +00:00
|
|
|
.GetEpisodesAsync(Convert.ToInt32(episodeTvdbId, CultureInfo.InvariantCulture), language, cancellationToken)
|
2019-02-20 18:35:47 +00:00
|
|
|
.ConfigureAwait(false);
|
2013-11-01 15:55:25 +00:00
|
|
|
|
2019-02-07 21:42:02 +00:00
|
|
|
var image = GetImageInfo(episodeResult.Data);
|
|
|
|
if (image != null)
|
|
|
|
{
|
|
|
|
imageResult.Add(image);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (TvDbServerException e)
|
2019-01-30 20:23:23 +00:00
|
|
|
{
|
2020-06-06 19:17:49 +00:00
|
|
|
_logger.LogError(e, "Failed to retrieve episode images for series {TvDbId}", series.GetProviderId(MetadataProvider.Tvdb));
|
2019-02-07 18:54:34 +00:00
|
|
|
}
|
2013-11-01 15:55:25 +00:00
|
|
|
}
|
|
|
|
|
2019-02-07 18:54:34 +00:00
|
|
|
return imageResult;
|
2013-11-01 15:55:25 +00:00
|
|
|
}
|
|
|
|
|
2019-01-30 20:23:23 +00:00
|
|
|
private RemoteImageInfo GetImageInfo(EpisodeRecord episode)
|
2013-11-01 15:55:25 +00:00
|
|
|
{
|
2019-01-30 20:23:23 +00:00
|
|
|
if (string.IsNullOrEmpty(episode.Filename))
|
2013-11-01 15:55:25 +00:00
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return new RemoteImageInfo
|
|
|
|
{
|
2020-08-07 17:26:28 +00:00
|
|
|
Width = Convert.ToInt32(episode.ThumbWidth, CultureInfo.InvariantCulture),
|
|
|
|
Height = Convert.ToInt32(episode.ThumbHeight, CultureInfo.InvariantCulture),
|
2013-11-01 15:55:25 +00:00
|
|
|
ProviderName = Name,
|
2019-02-13 10:21:56 +00:00
|
|
|
Url = TvdbUtils.BannerUrl + episode.Filename,
|
2013-11-01 15:55:25 +00:00
|
|
|
Type = ImageType.Primary
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-01-13 20:31:14 +00:00
|
|
|
public int Order => 0;
|
2014-01-28 18:37:01 +00:00
|
|
|
|
2020-08-17 19:10:02 +00:00
|
|
|
public Task<HttpResponseMessage> GetImageResponse(string url, CancellationToken cancellationToken)
|
2014-01-28 18:37:01 +00:00
|
|
|
{
|
2020-08-31 17:05:21 +00:00
|
|
|
return _httpClientFactory.CreateClient(NamedClient.Default).GetAsync(url, cancellationToken);
|
2014-01-28 18:37:01 +00:00
|
|
|
}
|
2013-11-01 15:55:25 +00:00
|
|
|
}
|
|
|
|
}
|