jellyfin-server/MediaBrowser.Providers/Plugins/TheTvdb/TvdbPersonImageProvider.cs

117 lines
4.1 KiB
C#
Raw Normal View History

#pragma warning disable CS1591
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using MediaBrowser.Common.Net;
2017-05-21 07:25:49 +00:00
using MediaBrowser.Controller.Dto;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.TV;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Providers;
2019-02-08 12:30:50 +00:00
using Microsoft.Extensions.Logging;
using TvDbSharper;
namespace MediaBrowser.Providers.Plugins.TheTvdb
{
2014-01-31 19:55:21 +00:00
public class TvdbPersonImageProvider : IRemoteImageProvider, IHasOrder
{
private readonly IHttpClient _httpClient;
2020-06-06 00:15:56 +00:00
private readonly ILogger<TvdbPersonImageProvider> _logger;
2019-02-08 12:30:50 +00:00
private readonly ILibraryManager _libraryManager;
private readonly TvdbClientManager _tvdbClientManager;
public TvdbPersonImageProvider(ILibraryManager libraryManager, IHttpClient httpClient, ILogger<TvdbPersonImageProvider> logger, TvdbClientManager tvdbClientManager)
{
2015-06-21 03:35:22 +00:00
_libraryManager = libraryManager;
_httpClient = httpClient;
2019-02-08 12:30:50 +00:00
_logger = logger;
_tvdbClientManager = tvdbClientManager;
}
2019-09-10 20:37:53 +00:00
/// <inheritdoc />
public string Name => "TheTVDB";
2019-09-10 20:37:53 +00:00
/// <inheritdoc />
public int Order => 1;
2019-09-10 20:37:53 +00:00
/// <inheritdoc />
public bool Supports(BaseItem item) => item is Person;
2019-09-10 20:37:53 +00:00
/// <inheritdoc />
2018-09-12 17:26:21 +00:00
public IEnumerable<ImageType> GetSupportedImages(BaseItem item)
{
2019-09-10 20:37:53 +00:00
yield return ImageType.Primary;
}
2019-09-10 20:37:53 +00:00
/// <inheritdoc />
2019-02-08 12:30:50 +00:00
public async Task<IEnumerable<RemoteImageInfo>> GetImages(BaseItem item, CancellationToken cancellationToken)
{
2016-03-20 06:46:51 +00:00
var seriesWithPerson = _libraryManager.GetItemList(new InternalItemsQuery
2015-07-07 02:25:23 +00:00
{
IncludeItemTypes = new[] { typeof(Series).Name },
2018-09-12 17:26:21 +00:00
PersonIds = new[] { item.Id },
2017-05-21 07:25:49 +00:00
DtoOptions = new DtoOptions(false)
{
EnableImages = false
}
2016-03-20 06:46:51 +00:00
}).Cast<Series>()
2015-11-24 18:45:52 +00:00
.Where(i => TvdbSeriesProvider.IsValidSeries(i.ProviderIds))
.ToList();
2019-02-20 18:35:47 +00:00
var infos = (await Task.WhenAll(seriesWithPerson.Select(async i =>
await GetImageFromSeriesData(i, item.Name, cancellationToken).ConfigureAwait(false)))
.ConfigureAwait(false))
.Where(i => i != null)
2019-02-12 20:44:10 +00:00
.Take(1);
2019-02-08 12:30:50 +00:00
return infos;
}
2019-02-08 12:30:50 +00:00
private async Task<RemoteImageInfo> GetImageFromSeriesData(Series series, string personName, CancellationToken cancellationToken)
{
2020-06-06 19:17:49 +00:00
var tvdbId = Convert.ToInt32(series.GetProviderId(MetadataProvider.Tvdb));
2019-02-08 12:30:50 +00:00
try
{
var actorsResult = await _tvdbClientManager
2019-02-20 18:35:47 +00:00
.GetActorsAsync(tvdbId, series.GetPreferredMetadataLanguage(), cancellationToken)
.ConfigureAwait(false);
2019-02-08 12:30:50 +00:00
var actor = actorsResult.Data.FirstOrDefault(a =>
string.Equals(a.Name, personName, StringComparison.OrdinalIgnoreCase) &&
!string.IsNullOrEmpty(a.Image));
if (actor == null)
{
2019-02-08 12:30:50 +00:00
return null;
}
return new RemoteImageInfo
{
Url = TvdbUtils.BannerUrl + actor.Image,
Type = ImageType.Primary,
ProviderName = Name
};
}
2019-02-08 12:30:50 +00:00
catch (TvDbServerException e)
{
_logger.LogError(e, "Failed to retrieve actor {ActorName} from series {SeriesTvdbId}", personName, tvdbId);
return null;
}
}
2019-09-10 20:37:53 +00:00
/// <inheritdoc />
public Task<HttpResponseInfo> GetImageResponse(string url, CancellationToken cancellationToken)
{
return _httpClient.GetResponse(new HttpRequestOptions
{
CancellationToken = cancellationToken,
2016-10-31 18:39:41 +00:00
Url = url
});
}
}
}