| | | 1 | | using System.Collections.Generic; |
| | | 2 | | using System.Globalization; |
| | | 3 | | using System.Net.Http; |
| | | 4 | | using System.Threading; |
| | | 5 | | using System.Threading.Tasks; |
| | | 6 | | using MediaBrowser.Common.Net; |
| | | 7 | | using MediaBrowser.Controller.Entities; |
| | | 8 | | using MediaBrowser.Controller.Providers; |
| | | 9 | | using MediaBrowser.Model.Entities; |
| | | 10 | | using MediaBrowser.Model.Providers; |
| | | 11 | | |
| | | 12 | | namespace MediaBrowser.Providers.Plugins.Tmdb.People |
| | | 13 | | { |
| | | 14 | | /// <summary> |
| | | 15 | | /// Person image provider powered by TMDb. |
| | | 16 | | /// </summary> |
| | | 17 | | public class TmdbPersonProvider : IRemoteMetadataProvider<Person, PersonLookupInfo> |
| | | 18 | | { |
| | | 19 | | private readonly IHttpClientFactory _httpClientFactory; |
| | | 20 | | private readonly TmdbClientManager _tmdbClientManager; |
| | | 21 | | |
| | | 22 | | /// <summary> |
| | | 23 | | /// Initializes a new instance of the <see cref="TmdbPersonProvider"/> class. |
| | | 24 | | /// </summary> |
| | | 25 | | /// <param name="httpClientFactory">The <see cref="IHttpClientFactory"/>.</param> |
| | | 26 | | /// <param name="tmdbClientManager">The <see cref="TmdbClientManager"/>.</param> |
| | | 27 | | public TmdbPersonProvider(IHttpClientFactory httpClientFactory, TmdbClientManager tmdbClientManager) |
| | | 28 | | { |
| | 27 | 29 | | _httpClientFactory = httpClientFactory; |
| | 27 | 30 | | _tmdbClientManager = tmdbClientManager; |
| | 27 | 31 | | } |
| | | 32 | | |
| | | 33 | | /// <inheritdoc /> |
| | 0 | 34 | | public string Name => TmdbUtils.ProviderName; |
| | | 35 | | |
| | | 36 | | /// <inheritdoc /> |
| | | 37 | | public async Task<IEnumerable<RemoteSearchResult>> GetSearchResults(PersonLookupInfo searchInfo, CancellationTok |
| | | 38 | | { |
| | 0 | 39 | | if (searchInfo.TryGetTmdbId(out var personTmdbId)) |
| | | 40 | | { |
| | 0 | 41 | | var personResult = await _tmdbClientManager.GetPersonAsync(personTmdbId, searchInfo.MetadataLanguage, se |
| | | 42 | | |
| | 0 | 43 | | if (personResult is not null) |
| | | 44 | | { |
| | 0 | 45 | | var result = new RemoteSearchResult |
| | 0 | 46 | | { |
| | 0 | 47 | | Name = personResult.Name, |
| | 0 | 48 | | SearchProviderName = Name, |
| | 0 | 49 | | Overview = personResult.Biography |
| | 0 | 50 | | }; |
| | | 51 | | |
| | 0 | 52 | | if (personResult.Images?.Profiles is not null && personResult.Images.Profiles.Count > 0) |
| | | 53 | | { |
| | 0 | 54 | | result.ImageUrl = _tmdbClientManager.GetProfileUrl(personResult.Images.Profiles[0].FilePath); |
| | | 55 | | } |
| | | 56 | | |
| | 0 | 57 | | result.SetProviderId(MetadataProvider.Tmdb, personResult.Id.ToString(CultureInfo.InvariantCulture)); |
| | 0 | 58 | | result.TrySetProviderId(MetadataProvider.Imdb, personResult.ExternalIds?.ImdbId); |
| | | 59 | | |
| | 0 | 60 | | return [result]; |
| | | 61 | | } |
| | | 62 | | } |
| | | 63 | | |
| | 0 | 64 | | var personSearchResult = await _tmdbClientManager.SearchPersonAsync(searchInfo.Name, cancellationToken).Conf |
| | 0 | 65 | | if (personSearchResult is null) |
| | | 66 | | { |
| | 0 | 67 | | return []; |
| | | 68 | | } |
| | | 69 | | |
| | 0 | 70 | | var remoteSearchResults = new RemoteSearchResult[personSearchResult.Count]; |
| | 0 | 71 | | for (var i = 0; i < personSearchResult.Count; i++) |
| | | 72 | | { |
| | 0 | 73 | | var person = personSearchResult[i]; |
| | 0 | 74 | | var remoteSearchResult = new RemoteSearchResult |
| | 0 | 75 | | { |
| | 0 | 76 | | SearchProviderName = Name, |
| | 0 | 77 | | Name = person.Name, |
| | 0 | 78 | | ImageUrl = _tmdbClientManager.GetProfileUrl(person.ProfilePath) |
| | 0 | 79 | | }; |
| | | 80 | | |
| | 0 | 81 | | remoteSearchResult.SetProviderId(MetadataProvider.Tmdb, person.Id.ToString(CultureInfo.InvariantCulture) |
| | 0 | 82 | | remoteSearchResults[i] = remoteSearchResult; |
| | | 83 | | } |
| | | 84 | | |
| | 0 | 85 | | return remoteSearchResults; |
| | 0 | 86 | | } |
| | | 87 | | |
| | | 88 | | /// <inheritdoc /> |
| | | 89 | | public async Task<MetadataResult<Person>> GetMetadata(PersonLookupInfo info, CancellationToken cancellationToken |
| | | 90 | | { |
| | | 91 | | // A person can carry another provider's id under the TMDb key, which is no more usable here |
| | | 92 | | // than no id at all, so both take the search path and get the stored id repaired. |
| | 0 | 93 | | info.TryGetTmdbId(out var personTmdbId); |
| | | 94 | | |
| | | 95 | | // We don't already have an Id, need to fetch it |
| | 0 | 96 | | if (personTmdbId <= 0) |
| | | 97 | | { |
| | 0 | 98 | | var personSearchResults = await _tmdbClientManager.SearchPersonAsync(info.Name, cancellationToken).Confi |
| | 0 | 99 | | if (personSearchResults?.Count > 0) |
| | | 100 | | { |
| | 0 | 101 | | personTmdbId = personSearchResults[0].Id; |
| | | 102 | | } |
| | | 103 | | } |
| | | 104 | | |
| | 0 | 105 | | var result = new MetadataResult<Person> |
| | 0 | 106 | | { |
| | 0 | 107 | | ResultLanguage = info.MetadataLanguage |
| | 0 | 108 | | }; |
| | | 109 | | |
| | 0 | 110 | | if (personTmdbId > 0) |
| | | 111 | | { |
| | 0 | 112 | | var person = await _tmdbClientManager.GetPersonAsync(personTmdbId, info.MetadataLanguage, info.MetadataC |
| | 0 | 113 | | if (person is null) |
| | | 114 | | { |
| | 0 | 115 | | return result; |
| | | 116 | | } |
| | | 117 | | |
| | 0 | 118 | | result.HasMetadata = true; |
| | | 119 | | |
| | 0 | 120 | | var item = new Person |
| | 0 | 121 | | { |
| | 0 | 122 | | // Take name from incoming info, don't rename the person |
| | 0 | 123 | | // TODO: This should go in PersonMetadataService, not each person provider |
| | 0 | 124 | | Name = info.Name, |
| | 0 | 125 | | HomePageUrl = person.Homepage, |
| | 0 | 126 | | Overview = person.Biography, |
| | 0 | 127 | | PremiereDate = person.Birthday?.ToUniversalTime(), |
| | 0 | 128 | | EndDate = person.Deathday?.ToUniversalTime() |
| | 0 | 129 | | }; |
| | | 130 | | |
| | 0 | 131 | | if (!string.IsNullOrWhiteSpace(person.PlaceOfBirth)) |
| | | 132 | | { |
| | 0 | 133 | | item.ProductionLocations = new[] { person.PlaceOfBirth }; |
| | | 134 | | } |
| | | 135 | | |
| | 0 | 136 | | item.SetProviderId(MetadataProvider.Tmdb, person.Id.ToString(CultureInfo.InvariantCulture)); |
| | 0 | 137 | | item.TrySetProviderId(MetadataProvider.Imdb, person.ImdbId); |
| | | 138 | | |
| | 0 | 139 | | result.HasMetadata = true; |
| | 0 | 140 | | result.Item = item; |
| | | 141 | | } |
| | | 142 | | |
| | 0 | 143 | | return result; |
| | 0 | 144 | | } |
| | | 145 | | |
| | | 146 | | /// <inheritdoc /> |
| | | 147 | | public Task<HttpResponseMessage> GetImageResponse(string url, CancellationToken cancellationToken) |
| | | 148 | | { |
| | 0 | 149 | | return _httpClientFactory.CreateClient(NamedClient.Default).GetAsync(url, cancellationToken); |
| | | 150 | | } |
| | | 151 | | } |
| | | 152 | | } |