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;
|
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
2019-01-13 20:26:31 +01: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;
|
|
|
|
using MediaBrowser.Providers.TV;
|
2019-02-08 13:30:50 +01:00
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
using TvDbSharper;
|
2013-11-05 17:08:44 +01:00
|
|
|
|
2014-01-28 19:37:01 +01:00
|
|
|
namespace MediaBrowser.Providers.People
|
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
|
|
|
{
|
2014-01-28 19:37:01 +01:00
|
|
|
private readonly IHttpClient _httpClient;
|
2019-02-08 13:30:50 +01:00
|
|
|
private readonly ILogger _logger;
|
|
|
|
private readonly ILibraryManager _libraryManager;
|
|
|
|
private readonly TvDbClientManager _tvDbClientManager;
|
2013-11-05 17:08:44 +01:00
|
|
|
|
2019-02-08 13:30:50 +01:00
|
|
|
public TvdbPersonImageProvider(ILibraryManager libraryManager, IHttpClient httpClient, ILogger logger)
|
2013-11-05 17:08:44 +01:00
|
|
|
{
|
2015-06-21 05:35:22 +02:00
|
|
|
_libraryManager = libraryManager;
|
2014-01-28 19:37:01 +01:00
|
|
|
_httpClient = httpClient;
|
2019-02-08 13:30:50 +01:00
|
|
|
_logger = logger;
|
|
|
|
_tvDbClientManager = TvDbClientManager.Instance;
|
2013-11-05 17:08:44 +01:00
|
|
|
}
|
|
|
|
|
2019-01-13 21:31:14 +01:00
|
|
|
public string Name => ProviderName;
|
2013-11-05 17:08:44 +01:00
|
|
|
|
2019-01-13 21:31:14 +01:00
|
|
|
public static string ProviderName => "TheTVDB";
|
2013-11-05 17:08:44 +01:00
|
|
|
|
2018-09-12 19:26:21 +02:00
|
|
|
public bool Supports(BaseItem item)
|
2013-11-05 17:08:44 +01:00
|
|
|
{
|
|
|
|
return item is Person;
|
|
|
|
}
|
|
|
|
|
2018-09-12 19:26:21 +02:00
|
|
|
public IEnumerable<ImageType> GetSupportedImages(BaseItem item)
|
2014-01-28 19:37:01 +01:00
|
|
|
{
|
|
|
|
return new List<ImageType>
|
|
|
|
{
|
|
|
|
ImageType.Primary
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
|
|
|
IncludeItemTypes = new[] { typeof(Series).Name },
|
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
|
|
|
|
}
|
2015-07-07 04:25:23 +02:00
|
|
|
|
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-08 13:30:50 +01:00
|
|
|
var infos = await Task.WhenAll(seriesWithPerson.Select(async i => await GetImageFromSeriesData(i, item.Name, cancellationToken))
|
2013-11-05 17:08:44 +01:00
|
|
|
.Where(i => i != null)
|
2019-02-08 13:30:50 +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
|
|
|
{
|
2019-02-08 13:30:50 +01:00
|
|
|
var tvdbId = Convert.ToInt32(series.GetProviderId(MetadataProviders.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
|
|
|
{
|
2019-02-08 13:30:50 +01:00
|
|
|
var actorsResult = await _tvDbClientManager.GetActorsAsync(tvdbId, cancellationToken);
|
|
|
|
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-08 13:30:50 +01:00
|
|
|
Url = TVUtils.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-01-13 21:31:14 +01:00
|
|
|
public int Order => 1;
|
2014-01-28 19:37:01 +01:00
|
|
|
|
|
|
|
public Task<HttpResponseInfo> GetImageResponse(string url, CancellationToken cancellationToken)
|
|
|
|
{
|
|
|
|
return _httpClient.GetResponse(new HttpRequestOptions
|
|
|
|
{
|
|
|
|
CancellationToken = cancellationToken,
|
2016-10-31 19:39:41 +01:00
|
|
|
Url = url
|
2014-01-28 19:37:01 +01:00
|
|
|
});
|
2013-11-05 17:08:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|