2019-01-30 13:23:23 -07:00
|
|
|
using System;
|
2019-01-13 13:03:10 -07:00
|
|
|
using System.Collections.Generic;
|
2019-01-13 12:26:31 -07:00
|
|
|
using System.Globalization;
|
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using MediaBrowser.Common.Net;
|
2014-01-28 11:37:01 -07:00
|
|
|
using MediaBrowser.Controller.Configuration;
|
2013-11-01 08:55:25 -07:00
|
|
|
using MediaBrowser.Controller.Entities;
|
|
|
|
using MediaBrowser.Controller.Entities.TV;
|
|
|
|
using MediaBrowser.Controller.Library;
|
|
|
|
using MediaBrowser.Controller.Providers;
|
|
|
|
using MediaBrowser.Model.Entities;
|
2019-01-13 12:26:31 -07:00
|
|
|
using MediaBrowser.Model.Providers;
|
2019-02-07 14:42:02 -07:00
|
|
|
using Microsoft.Extensions.Logging;
|
2019-01-30 13:23:23 -07:00
|
|
|
using TvDbSharper;
|
|
|
|
using TvDbSharper.Dto;
|
2013-11-01 08:55:25 -07:00
|
|
|
|
2019-02-09 02:10:33 -07:00
|
|
|
namespace MediaBrowser.Providers.TV.TheTVDB
|
2013-11-01 08:55:25 -07:00
|
|
|
{
|
2016-05-24 17:42:12 -07:00
|
|
|
public class TvdbEpisodeImageProvider : IRemoteImageProvider
|
2013-11-01 08:55:25 -07:00
|
|
|
{
|
2014-01-28 11:37:01 -07:00
|
|
|
private readonly IHttpClient _httpClient;
|
2019-02-07 14:42:02 -07:00
|
|
|
private readonly ILogger _logger;
|
2019-01-30 15:28:43 -07:00
|
|
|
private readonly TvDbClientManager _tvDbClientManager;
|
2013-11-01 08:55:25 -07:00
|
|
|
|
2019-02-07 14:42:02 -07:00
|
|
|
public TvdbEpisodeImageProvider(IHttpClient httpClient, ILogger logger)
|
2013-11-01 08:55:25 -07:00
|
|
|
{
|
2014-01-28 11:37:01 -07:00
|
|
|
_httpClient = httpClient;
|
2019-02-07 14:42:02 -07:00
|
|
|
_logger = logger;
|
2019-01-30 15:28:43 -07:00
|
|
|
_tvDbClientManager = TvDbClientManager.Instance;
|
2013-11-01 08:55:25 -07:00
|
|
|
}
|
|
|
|
|
2019-01-13 13:31:14 -07:00
|
|
|
public string Name => "TheTVDB";
|
2013-11-01 08:55:25 -07:00
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
public bool Supports(BaseItem item)
|
2013-11-01 08:55:25 -07:00
|
|
|
{
|
|
|
|
return item is Episode;
|
|
|
|
}
|
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
public IEnumerable<ImageType> GetSupportedImages(BaseItem item)
|
2014-01-28 11:37:01 -07:00
|
|
|
{
|
|
|
|
return new List<ImageType>
|
|
|
|
{
|
|
|
|
ImageType.Primary
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-01-30 13:23:23 -07:00
|
|
|
public async Task<IEnumerable<RemoteImageInfo>> GetImages(BaseItem item, CancellationToken cancellationToken)
|
2013-11-01 08:55:25 -07:00
|
|
|
{
|
|
|
|
var episode = (Episode)item;
|
2014-02-06 08:58:49 -07:00
|
|
|
var series = episode.Series;
|
2019-02-07 11:54:34 -07:00
|
|
|
var imageResult = new List<RemoteImageInfo>();
|
2013-11-01 08:55:25 -07:00
|
|
|
|
2015-11-24 11:45:52 -07:00
|
|
|
if (series != null && TvdbSeriesProvider.IsValidSeries(series.ProviderIds))
|
2013-11-01 08:55:25 -07:00
|
|
|
{
|
2019-02-08 11:48:18 -07:00
|
|
|
var episodeTvdbId = episode.GetProviderId(MetadataProviders.Tvdb);
|
2019-02-07 14:42:02 -07:00
|
|
|
|
2013-11-01 08:55:25 -07:00
|
|
|
// Process images
|
2019-02-07 14:42:02 -07:00
|
|
|
try
|
|
|
|
{
|
2019-02-08 11:48:18 -07:00
|
|
|
if (string.IsNullOrEmpty(episodeTvdbId))
|
|
|
|
{
|
2019-02-11 12:33:44 -07:00
|
|
|
var episodeInfo = new EpisodeInfo
|
|
|
|
{
|
|
|
|
IndexNumber = episode.IndexNumber.Value,
|
|
|
|
ParentIndexNumber = episode.ParentIndexNumber.Value,
|
|
|
|
SeriesProviderIds = series.ProviderIds
|
|
|
|
};
|
|
|
|
episodeTvdbId = await _tvDbClientManager.GetEpisodeTvdbId(episodeInfo, cancellationToken);
|
2019-02-08 11:48:18 -07:00
|
|
|
if (string.IsNullOrEmpty(episodeTvdbId))
|
|
|
|
{
|
2019-02-11 10:47:40 -07:00
|
|
|
_logger.LogError("Episode {SeasonNumber}x{EpisodeNumber} not found for series {SeriesTvdbId}",
|
2019-02-11 12:33:44 -07:00
|
|
|
episodeInfo.ParentIndexNumber, episodeInfo.IndexNumber);
|
2019-02-08 11:48:18 -07:00
|
|
|
return imageResult;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-07 14:42:02 -07:00
|
|
|
var episodeResult =
|
2019-02-08 11:48:18 -07:00
|
|
|
await _tvDbClientManager.GetEpisodesAsync(Convert.ToInt32(episodeTvdbId), cancellationToken);
|
2013-11-01 08:55:25 -07:00
|
|
|
|
2019-02-07 14:42:02 -07:00
|
|
|
var image = GetImageInfo(episodeResult.Data);
|
|
|
|
if (image != null)
|
|
|
|
{
|
|
|
|
imageResult.Add(image);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (TvDbServerException e)
|
2019-01-30 13:23:23 -07:00
|
|
|
{
|
2019-02-08 11:48:18 -07:00
|
|
|
_logger.LogError(e, "Failed to retrieve episode images for {TvDbId}", episodeTvdbId);
|
2019-02-07 11:54:34 -07:00
|
|
|
}
|
2013-11-01 08:55:25 -07:00
|
|
|
}
|
|
|
|
|
2019-02-07 11:54:34 -07:00
|
|
|
return imageResult;
|
2013-11-01 08:55:25 -07:00
|
|
|
}
|
|
|
|
|
2019-01-30 13:23:23 -07:00
|
|
|
private RemoteImageInfo GetImageInfo(EpisodeRecord episode)
|
2013-11-01 08:55:25 -07:00
|
|
|
{
|
2019-01-30 13:23:23 -07:00
|
|
|
if (string.IsNullOrEmpty(episode.Filename))
|
2013-11-01 08:55:25 -07:00
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return new RemoteImageInfo
|
|
|
|
{
|
2019-01-30 13:23:23 -07:00
|
|
|
Width = Convert.ToInt32(episode.ThumbWidth),
|
|
|
|
Height = Convert.ToInt32(episode.ThumbHeight),
|
2013-11-01 08:55:25 -07:00
|
|
|
ProviderName = Name,
|
2019-01-30 13:23:23 -07:00
|
|
|
Url = TVUtils.BannerUrl + episode.Filename,
|
2013-11-01 08:55:25 -07:00
|
|
|
Type = ImageType.Primary
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-01-13 13:31:14 -07:00
|
|
|
public int Order => 0;
|
2014-01-28 11:37:01 -07:00
|
|
|
|
|
|
|
public Task<HttpResponseInfo> GetImageResponse(string url, CancellationToken cancellationToken)
|
|
|
|
{
|
|
|
|
return _httpClient.GetResponse(new HttpRequestOptions
|
|
|
|
{
|
|
|
|
CancellationToken = cancellationToken,
|
2017-04-29 19:37:51 -07:00
|
|
|
Url = url
|
2014-01-28 11:37:01 -07:00
|
|
|
});
|
|
|
|
}
|
2013-11-01 08:55:25 -07:00
|
|
|
}
|
|
|
|
}
|