jellyfin/MediaBrowser.Providers/Plugins/Tmdb/People/TmdbPersonImageProvider.cs

89 lines
3.0 KiB
C#
Raw Normal View History

#pragma warning disable CS1591
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
2020-08-17 12:10:02 -07:00
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
2020-08-31 10:05:21 -07:00
using MediaBrowser.Common.Net;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Extensions;
using MediaBrowser.Model.Providers;
2020-05-30 23:23:09 -07:00
namespace MediaBrowser.Providers.Plugins.Tmdb.People
{
public class TmdbPersonImageProvider : IRemoteImageProvider, IHasOrder
{
2020-08-17 12:10:02 -07:00
private readonly IHttpClientFactory _httpClientFactory;
private readonly TmdbClientManager _tmdbClientManager;
public TmdbPersonImageProvider(IHttpClientFactory httpClientFactory, TmdbClientManager tmdbClientManager)
{
2020-08-17 12:10:02 -07:00
_httpClientFactory = httpClientFactory;
_tmdbClientManager = tmdbClientManager;
}
2020-08-31 13:20:19 -07:00
/// <inheritdoc />
public string Name => TmdbUtils.ProviderName;
2020-08-31 13:20:19 -07:00
/// <inheritdoc />
public int Order => 0;
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
};
}
2018-09-12 10:26:21 -07:00
public async Task<IEnumerable<RemoteImageInfo>> GetImages(BaseItem item, CancellationToken cancellationToken)
{
2013-12-21 11:37:34 -07:00
var person = (Person)item;
2021-04-19 23:59:15 -07:00
if (!person.TryGetProviderId(MetadataProvider.Tmdb, out var personTmdbId))
{
2021-04-19 23:59:15 -07:00
return Enumerable.Empty<RemoteImageInfo>();
}
2020-06-15 14:43:52 -07:00
2021-05-18 04:44:38 -07:00
var language = item.GetPreferredMetadataLanguage();
2021-05-12 05:48:45 -07:00
var personResult = await _tmdbClientManager.GetPersonAsync(int.Parse(personTmdbId, CultureInfo.InvariantCulture), language, cancellationToken).ConfigureAwait(false);
2021-04-19 23:59:15 -07:00
if (personResult?.Images?.Profiles == null)
{
return Enumerable.Empty<RemoteImageInfo>();
}
2020-06-15 14:43:52 -07:00
2021-04-19 23:59:15 -07:00
var remoteImages = new RemoteImageInfo[personResult.Images.Profiles.Count];
2020-06-15 14:43:52 -07:00
2021-04-19 23:59:15 -07:00
for (var i = 0; i < personResult.Images.Profiles.Count; i++)
{
var image = personResult.Images.Profiles[i];
remoteImages[i] = new RemoteImageInfo
{
ProviderName = Name,
Type = ImageType.Primary,
Width = image.Width,
Height = image.Height,
Language = TmdbUtils.AdjustImageLanguage(image.Iso_639_1, language),
Url = _tmdbClientManager.GetProfileUrl(image.FilePath)
};
}
2021-04-19 23:59:15 -07:00
return remoteImages.OrderByLanguageDescending(language);
}
2020-08-17 12:10:02 -07:00
public Task<HttpResponseMessage> GetImageResponse(string url, CancellationToken cancellationToken)
{
2020-08-31 10:05:21 -07:00
return _httpClientFactory.CreateClient(NamedClient.Default).GetAsync(url, cancellationToken);
}
}
}