jellyfin/MediaBrowser.Providers/People/TvdbPersonImageProvider.cs

115 lines
3.9 KiB
C#
Raw Normal View History

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