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

136 lines
4.5 KiB
C#
Raw Normal View History

#pragma warning disable CS1591
using System;
using System.Collections.Generic;
using System.Linq;
2020-08-17 19:10:02 +00:00
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Providers;
using MediaBrowser.Model.Serialization;
2020-05-31 06:23:09 +00:00
using MediaBrowser.Providers.Plugins.Tmdb.Models.General;
using MediaBrowser.Providers.Plugins.Tmdb.Models.People;
using MediaBrowser.Providers.Plugins.Tmdb.Movies;
2020-05-31 06:23:09 +00:00
namespace MediaBrowser.Providers.Plugins.Tmdb.People
{
public class TmdbPersonImageProvider : IRemoteImageProvider, IHasOrder
{
private readonly IServerConfigurationManager _config;
private readonly IJsonSerializer _jsonSerializer;
2020-08-17 19:10:02 +00:00
private readonly IHttpClientFactory _httpClientFactory;
2020-08-17 19:10:02 +00:00
public TmdbPersonImageProvider(IServerConfigurationManager config, IJsonSerializer jsonSerializer, IHttpClientFactory httpClientFactory)
{
_config = config;
_jsonSerializer = jsonSerializer;
2020-08-17 19:10:02 +00:00
_httpClientFactory = httpClientFactory;
}
public string Name => ProviderName;
public static string ProviderName => TmdbUtils.ProviderName;
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;
2020-06-06 19:17:49 +00:00
var id = person.GetProviderId(MetadataProvider.Tmdb);
if (!string.IsNullOrEmpty(id))
{
await TmdbPersonProvider.Current.EnsurePersonInfo(id, cancellationToken).ConfigureAwait(false);
var dataFilePath = TmdbPersonProvider.GetPersonDataFilePath(_config.ApplicationPaths, id);
var result = _jsonSerializer.DeserializeFromFile<PersonResult>(dataFilePath);
2019-08-18 12:44:13 +00:00
var images = result.Images ?? new PersonImages();
var tmdbSettings = await TmdbMovieProvider.Current.GetTmdbSettings(cancellationToken).ConfigureAwait(false);
2018-09-12 17:26:21 +00:00
var tmdbImageUrl = tmdbSettings.images.GetImageUrl("original");
return GetImages(images, item.GetPreferredMetadataLanguage(), tmdbImageUrl);
}
return new List<RemoteImageInfo>();
}
2013-12-21 18:37:34 +00:00
private IEnumerable<RemoteImageInfo> GetImages(PersonImages images, string preferredLanguage, string baseImageUrl)
{
var list = new List<RemoteImageInfo>();
2019-08-18 12:44:13 +00:00
if (images.Profiles != null)
{
2019-08-18 12:44:13 +00:00
list.AddRange(images.Profiles.Select(i => new RemoteImageInfo
{
ProviderName = Name,
Type = ImageType.Primary,
2019-08-18 12:44:13 +00:00
Width = i.Width,
Height = i.Height,
Language = GetLanguage(i),
2019-08-18 12:44:13 +00:00
Url = baseImageUrl + i.File_Path
}));
}
var language = preferredLanguage;
var isLanguageEn = string.Equals(language, "en", StringComparison.OrdinalIgnoreCase);
return list.OrderByDescending(i =>
{
if (string.Equals(language, i.Language, StringComparison.OrdinalIgnoreCase))
{
return 3;
}
2020-06-15 21:43:52 +00:00
if (!isLanguageEn)
{
if (string.Equals("en", i.Language, StringComparison.OrdinalIgnoreCase))
{
return 2;
}
}
2020-06-15 21:43:52 +00:00
if (string.IsNullOrEmpty(i.Language))
{
return isLanguageEn ? 3 : 2;
}
2020-06-15 21:43:52 +00:00
return 0;
})
.ThenByDescending(i => i.CommunityRating ?? 0)
.ThenByDescending(i => i.VoteCount ?? 0);
}
private string GetLanguage(Profile profile)
{
2019-08-18 12:44:13 +00:00
return profile.Iso_639_1?.ToString();
}
public int Order => 0;
2020-08-17 19:10:02 +00:00
public Task<HttpResponseMessage> GetImageResponse(string url, CancellationToken cancellationToken)
{
2020-08-17 19:10:02 +00:00
return _httpClientFactory.CreateClient().GetAsync(url, cancellationToken);
}
}
}