2019-01-13 20:03:10 +00:00
|
|
|
using System;
|
2019-01-13 19:26:31 +00:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using MediaBrowser.Common.Net;
|
2014-01-28 18:37:01 +00:00
|
|
|
using MediaBrowser.Controller.Configuration;
|
2013-11-05 16:52:43 +00:00
|
|
|
using MediaBrowser.Controller.Entities;
|
|
|
|
using MediaBrowser.Controller.Providers;
|
|
|
|
using MediaBrowser.Model.Entities;
|
|
|
|
using MediaBrowser.Model.Providers;
|
|
|
|
using MediaBrowser.Model.Serialization;
|
2014-01-28 18:37:01 +00:00
|
|
|
using MediaBrowser.Providers.Movies;
|
2019-08-18 11:20:52 +00:00
|
|
|
using MediaBrowser.Providers.Tmdb.Models.General;
|
|
|
|
using MediaBrowser.Providers.Tmdb.Models.People;
|
|
|
|
using MediaBrowser.Providers.Tmdb.Movies;
|
2013-11-05 16:52:43 +00:00
|
|
|
|
2019-08-18 11:20:52 +00:00
|
|
|
namespace MediaBrowser.Providers.Tmdb.People
|
2013-11-05 16:52:43 +00:00
|
|
|
{
|
2019-08-18 11:20:52 +00:00
|
|
|
public class TmdbPersonImageProvider : IRemoteImageProvider, IHasOrder
|
2013-11-05 16:52:43 +00:00
|
|
|
{
|
|
|
|
private readonly IServerConfigurationManager _config;
|
|
|
|
private readonly IJsonSerializer _jsonSerializer;
|
2014-01-28 18:37:01 +00:00
|
|
|
private readonly IHttpClient _httpClient;
|
2013-11-05 16:52:43 +00:00
|
|
|
|
2019-08-18 11:20:52 +00:00
|
|
|
public TmdbPersonImageProvider(IServerConfigurationManager config, IJsonSerializer jsonSerializer, IHttpClient httpClient)
|
2013-11-05 16:52:43 +00:00
|
|
|
{
|
|
|
|
_config = config;
|
|
|
|
_jsonSerializer = jsonSerializer;
|
2014-01-28 18:37:01 +00:00
|
|
|
_httpClient = httpClient;
|
2013-11-05 16:52:43 +00:00
|
|
|
}
|
|
|
|
|
2019-01-13 20:31:14 +00:00
|
|
|
public string Name => ProviderName;
|
2013-11-05 16:52:43 +00:00
|
|
|
|
2019-08-18 11:20:52 +00:00
|
|
|
public static string ProviderName => TmdbUtils.ProviderName;
|
2013-11-05 16:52:43 +00:00
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
public bool Supports(BaseItem item)
|
2013-11-05 16:52:43 +00:00
|
|
|
{
|
|
|
|
return item is Person;
|
|
|
|
}
|
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
public IEnumerable<ImageType> GetSupportedImages(BaseItem item)
|
2014-01-28 18:37:01 +00:00
|
|
|
{
|
|
|
|
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-11-05 16:52:43 +00:00
|
|
|
{
|
2013-12-21 18:37:34 +00:00
|
|
|
var person = (Person)item;
|
|
|
|
var id = person.GetProviderId(MetadataProviders.Tmdb);
|
2013-11-05 16:52:43 +00:00
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(id))
|
|
|
|
{
|
2019-08-18 11:20:52 +00:00
|
|
|
await TmdbPersonProvider.Current.EnsurePersonInfo(id, cancellationToken).ConfigureAwait(false);
|
2013-11-05 16:52:43 +00:00
|
|
|
|
2019-08-18 11:20:52 +00:00
|
|
|
var dataFilePath = TmdbPersonProvider.GetPersonDataFilePath(_config.ApplicationPaths, id);
|
2013-11-05 16:52:43 +00:00
|
|
|
|
2019-08-18 11:20:52 +00:00
|
|
|
var result = _jsonSerializer.DeserializeFromFile<PersonResult>(dataFilePath);
|
2013-11-05 16:52:43 +00:00
|
|
|
|
2019-08-18 12:44:13 +00:00
|
|
|
var images = result.Images ?? new PersonImages();
|
2013-11-05 16:52:43 +00:00
|
|
|
|
2019-08-18 11:20:52 +00:00
|
|
|
var tmdbSettings = await TmdbMovieProvider.Current.GetTmdbSettings(cancellationToken).ConfigureAwait(false);
|
2013-11-05 16:52:43 +00:00
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
var tmdbImageUrl = tmdbSettings.images.GetImageUrl("original");
|
2013-12-02 15:47:24 +00:00
|
|
|
|
2013-12-27 00:23:58 +00:00
|
|
|
return GetImages(images, item.GetPreferredMetadataLanguage(), tmdbImageUrl);
|
2013-11-05 16:52:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return new List<RemoteImageInfo>();
|
|
|
|
}
|
2013-12-21 18:37:34 +00:00
|
|
|
|
2019-08-18 11:20:52 +00:00
|
|
|
private IEnumerable<RemoteImageInfo> GetImages(PersonImages images, string preferredLanguage, string baseImageUrl)
|
2013-11-05 16:52:43 +00:00
|
|
|
{
|
|
|
|
var list = new List<RemoteImageInfo>();
|
|
|
|
|
2019-08-18 12:44:13 +00:00
|
|
|
if (images.Profiles != null)
|
2013-11-05 16:52:43 +00:00
|
|
|
{
|
2019-08-18 12:44:13 +00:00
|
|
|
list.AddRange(images.Profiles.Select(i => new RemoteImageInfo
|
2013-11-05 16:52:43 +00:00
|
|
|
{
|
|
|
|
ProviderName = Name,
|
|
|
|
Type = ImageType.Primary,
|
2019-08-18 12:44:13 +00:00
|
|
|
Width = i.Width,
|
|
|
|
Height = i.Height,
|
2013-11-05 16:52:43 +00:00
|
|
|
Language = GetLanguage(i),
|
2019-08-18 12:44:13 +00:00
|
|
|
Url = baseImageUrl + i.File_Path
|
2013-11-05 16:52:43 +00:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2013-12-27 00:23:58 +00:00
|
|
|
var language = preferredLanguage;
|
2013-11-05 16:52:43 +00:00
|
|
|
|
|
|
|
var isLanguageEn = string.Equals(language, "en", StringComparison.OrdinalIgnoreCase);
|
|
|
|
|
|
|
|
return list.OrderByDescending(i =>
|
|
|
|
{
|
|
|
|
if (string.Equals(language, i.Language, StringComparison.OrdinalIgnoreCase))
|
|
|
|
{
|
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
if (!isLanguageEn)
|
|
|
|
{
|
|
|
|
if (string.Equals("en", i.Language, StringComparison.OrdinalIgnoreCase))
|
|
|
|
{
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (string.IsNullOrEmpty(i.Language))
|
|
|
|
{
|
|
|
|
return isLanguageEn ? 3 : 2;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
})
|
|
|
|
.ThenByDescending(i => i.CommunityRating ?? 0)
|
2017-08-09 19:56:38 +00:00
|
|
|
.ThenByDescending(i => i.VoteCount ?? 0);
|
2013-11-05 16:52:43 +00:00
|
|
|
}
|
|
|
|
|
2019-08-18 11:20:52 +00:00
|
|
|
private string GetLanguage(Profile profile)
|
2013-11-05 16:52:43 +00:00
|
|
|
{
|
2019-08-18 12:44:13 +00:00
|
|
|
return profile.Iso_639_1?.ToString();
|
2013-11-05 16:52:43 +00:00
|
|
|
}
|
|
|
|
|
2019-01-13 20:31:14 +00:00
|
|
|
public int Order => 0;
|
2014-01-28 18:37:01 +00:00
|
|
|
|
|
|
|
public Task<HttpResponseInfo> GetImageResponse(string url, CancellationToken cancellationToken)
|
|
|
|
{
|
|
|
|
return _httpClient.GetResponse(new HttpRequestOptions
|
|
|
|
{
|
|
|
|
CancellationToken = cancellationToken,
|
2016-10-31 18:39:41 +00:00
|
|
|
Url = url
|
2014-01-28 18:37:01 +00:00
|
|
|
});
|
|
|
|
}
|
2013-11-05 16:52:43 +00:00
|
|
|
}
|
|
|
|
}
|