jellyfin/MediaBrowser.Providers/Plugins/TheTvdb/TvdbEpisodeImageProvider.cs

123 lines
4.5 KiB
C#
Raw Normal View History

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