| | | 1 | | using System.Collections.Generic; |
| | | 2 | | using System.Globalization; |
| | | 3 | | using System.Linq; |
| | | 4 | | using System.Net.Http; |
| | | 5 | | using System.Threading; |
| | | 6 | | using System.Threading.Tasks; |
| | | 7 | | using MediaBrowser.Common.Net; |
| | | 8 | | using MediaBrowser.Controller.Entities; |
| | | 9 | | using MediaBrowser.Controller.Providers; |
| | | 10 | | using MediaBrowser.Model.Entities; |
| | | 11 | | using MediaBrowser.Model.Providers; |
| | | 12 | | |
| | | 13 | | namespace MediaBrowser.Providers.Plugins.Tmdb.People |
| | | 14 | | { |
| | | 15 | | /// <summary> |
| | | 16 | | /// Person image provider powered by TMDb. |
| | | 17 | | /// </summary> |
| | | 18 | | public class TmdbPersonImageProvider : IRemoteImageProvider, IHasOrder |
| | | 19 | | { |
| | | 20 | | private readonly IHttpClientFactory _httpClientFactory; |
| | | 21 | | private readonly TmdbClientManager _tmdbClientManager; |
| | | 22 | | |
| | | 23 | | /// <summary> |
| | | 24 | | /// Initializes a new instance of the <see cref="TmdbPersonImageProvider"/> class. |
| | | 25 | | /// </summary> |
| | | 26 | | /// <param name="httpClientFactory">The <see cref="IHttpClientFactory"/>.</param> |
| | | 27 | | /// <param name="tmdbClientManager">The <see cref="TmdbClientManager"/>.</param> |
| | | 28 | | public TmdbPersonImageProvider(IHttpClientFactory httpClientFactory, TmdbClientManager tmdbClientManager) |
| | | 29 | | { |
| | 21 | 30 | | _httpClientFactory = httpClientFactory; |
| | 21 | 31 | | _tmdbClientManager = tmdbClientManager; |
| | 21 | 32 | | } |
| | | 33 | | |
| | | 34 | | /// <inheritdoc /> |
| | 0 | 35 | | public string Name => TmdbUtils.ProviderName; |
| | | 36 | | |
| | | 37 | | /// <inheritdoc /> |
| | 0 | 38 | | public int Order => 0; |
| | | 39 | | |
| | | 40 | | /// <inheritdoc /> |
| | | 41 | | public bool Supports(BaseItem item) |
| | | 42 | | { |
| | 53 | 43 | | return item is Person; |
| | | 44 | | } |
| | | 45 | | |
| | | 46 | | /// <inheritdoc /> |
| | | 47 | | public IEnumerable<ImageType> GetSupportedImages(BaseItem item) |
| | | 48 | | { |
| | | 49 | | yield return ImageType.Primary; |
| | | 50 | | } |
| | | 51 | | |
| | | 52 | | /// <inheritdoc /> |
| | | 53 | | public async Task<IEnumerable<RemoteImageInfo>> GetImages(BaseItem item, CancellationToken cancellationToken) |
| | | 54 | | { |
| | | 55 | | var person = (Person)item; |
| | | 56 | | |
| | | 57 | | if (!person.TryGetProviderId(MetadataProvider.Tmdb, out var personTmdbId)) |
| | | 58 | | { |
| | | 59 | | return Enumerable.Empty<RemoteImageInfo>(); |
| | | 60 | | } |
| | | 61 | | |
| | | 62 | | var language = item.GetPreferredMetadataLanguage(); |
| | | 63 | | var countryCode = item.GetPreferredMetadataCountryCode(); |
| | | 64 | | var personResult = await _tmdbClientManager.GetPersonAsync(int.Parse(personTmdbId, CultureInfo.InvariantCult |
| | | 65 | | if (personResult?.Images?.Profiles is null) |
| | | 66 | | { |
| | | 67 | | return Enumerable.Empty<RemoteImageInfo>(); |
| | | 68 | | } |
| | | 69 | | |
| | | 70 | | return _tmdbClientManager.ConvertProfilesToRemoteImageInfo(personResult.Images.Profiles, language); |
| | | 71 | | } |
| | | 72 | | |
| | | 73 | | /// <inheritdoc /> |
| | | 74 | | public Task<HttpResponseMessage> GetImageResponse(string url, CancellationToken cancellationToken) |
| | | 75 | | { |
| | 0 | 76 | | return _httpClientFactory.CreateClient(NamedClient.Default).GetAsync(url, cancellationToken); |
| | | 77 | | } |
| | | 78 | | } |
| | | 79 | | } |