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