using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using MediaBrowser.Common.Net;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Providers;
namespace MediaBrowser.Providers.Plugins.Tmdb.People
{
///
/// Person image provider powered by TMDb.
///
public class TmdbPersonImageProvider : IRemoteImageProvider, IHasOrder
{
private readonly IHttpClientFactory _httpClientFactory;
private readonly TmdbClientManager _tmdbClientManager;
///
/// Initializes a new instance of the class.
///
/// The .
/// The .
public TmdbPersonImageProvider(IHttpClientFactory httpClientFactory, TmdbClientManager tmdbClientManager)
{
_httpClientFactory = httpClientFactory;
_tmdbClientManager = tmdbClientManager;
}
///
public string Name => TmdbUtils.ProviderName;
///
public int Order => 0;
///
public bool Supports(BaseItem item)
{
return item is Person;
}
///
public IEnumerable GetSupportedImages(BaseItem item)
{
return new List
{
ImageType.Primary
};
}
///
public async Task> GetImages(BaseItem item, CancellationToken cancellationToken)
{
var person = (Person)item;
if (!person.TryGetProviderId(MetadataProvider.Tmdb, out var personTmdbId))
{
return Enumerable.Empty();
}
var language = item.GetPreferredMetadataLanguage();
var personResult = await _tmdbClientManager.GetPersonAsync(int.Parse(personTmdbId, CultureInfo.InvariantCulture), language, cancellationToken).ConfigureAwait(false);
if (personResult?.Images?.Profiles is null)
{
return Enumerable.Empty();
}
return _tmdbClientManager.ConvertProfilesToRemoteImageInfo(personResult.Images.Profiles, language);
}
///
public Task GetImageResponse(string url, CancellationToken cancellationToken)
{
return _httpClientFactory.CreateClient(NamedClient.Default).GetAsync(url, cancellationToken);
}
}
}