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

91 lines
3.1 KiB
C#
Raw Normal View History

#pragma warning disable CS1591
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
2020-08-17 19:10:02 +00:00
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
2020-08-31 17:05:21 +00: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-31 06:23:09 +00:00
namespace MediaBrowser.Providers.Plugins.Tmdb.People
{
public class TmdbPersonImageProvider : IRemoteImageProvider, IHasOrder
{
2020-08-17 19:10:02 +00:00
private readonly IHttpClientFactory _httpClientFactory;
private readonly TmdbClientManager _tmdbClientManager;
public TmdbPersonImageProvider(IHttpClientFactory httpClientFactory, TmdbClientManager tmdbClientManager)
{
2020-08-17 19:10:02 +00:00
_httpClientFactory = httpClientFactory;
_tmdbClientManager = tmdbClientManager;
}
2020-08-31 20:20:19 +00:00
/// <inheritdoc />
public string Name => TmdbUtils.ProviderName;
2020-08-31 20:20:19 +00:00
/// <inheritdoc />
public int Order => 0;
2018-09-12 17:26:21 +00:00
public bool Supports(BaseItem item)
{
return item is Person;
}
2018-09-12 17:26:21 +00:00
public IEnumerable<ImageType> GetSupportedImages(BaseItem item)
{
return new List<ImageType>
{
ImageType.Primary
};
}
2018-09-12 17:26:21 +00:00
public async Task<IEnumerable<RemoteImageInfo>> GetImages(BaseItem item, CancellationToken cancellationToken)
{
2013-12-21 18:37:34 +00:00
var person = (Person)item;
var personTmdbId = Convert.ToInt32(person.GetProviderId(MetadataProvider.Tmdb), CultureInfo.InvariantCulture);
if (personTmdbId > 0)
{
var personResult = await _tmdbClientManager.GetPersonAsync(personTmdbId, cancellationToken).ConfigureAwait(false);
if (personResult?.Images?.Profiles == null)
{
return Enumerable.Empty<RemoteImageInfo>();
}
2020-06-15 21:43:52 +00:00
var remoteImages = new List<RemoteImageInfo>();
var language = item.GetPreferredMetadataLanguage();
2020-06-15 21:43:52 +00:00
for (var i = 0; i < personResult.Images.Profiles.Count; i++)
{
var image = personResult.Images.Profiles[i];
remoteImages.Add(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)
});
}
2020-06-15 21:43:52 +00:00
return remoteImages.OrderByLanguageDescending(language);
}
return Enumerable.Empty<RemoteImageInfo>();
}
2020-08-17 19:10:02 +00:00
public Task<HttpResponseMessage> GetImageResponse(string url, CancellationToken cancellationToken)
{
2020-08-31 17:05:21 +00:00
return _httpClientFactory.CreateClient(NamedClient.Default).GetAsync(url, cancellationToken);
}
}
}