TmdbPersonProvider.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #nullable disable
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Globalization;
  5. using System.Net.Http;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. using MediaBrowser.Common.Net;
  9. using MediaBrowser.Controller.Entities;
  10. using MediaBrowser.Controller.Providers;
  11. using MediaBrowser.Model.Entities;
  12. using MediaBrowser.Model.Providers;
  13. namespace MediaBrowser.Providers.Plugins.Tmdb.People
  14. {
  15. /// <summary>
  16. /// Person image provider powered by TMDb.
  17. /// </summary>
  18. public class TmdbPersonProvider : IRemoteMetadataProvider<Person, PersonLookupInfo>
  19. {
  20. private readonly IHttpClientFactory _httpClientFactory;
  21. private readonly TmdbClientManager _tmdbClientManager;
  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. {
  29. _httpClientFactory = httpClientFactory;
  30. _tmdbClientManager = tmdbClientManager;
  31. }
  32. /// <inheritdoc />
  33. public string Name => TmdbUtils.ProviderName;
  34. /// <inheritdoc />
  35. public async Task<IEnumerable<RemoteSearchResult>> GetSearchResults(PersonLookupInfo searchInfo, CancellationToken cancellationToken)
  36. {
  37. if (searchInfo.TryGetProviderId(MetadataProvider.Tmdb, out var personTmdbId))
  38. {
  39. var personResult = await _tmdbClientManager.GetPersonAsync(int.Parse(personTmdbId, CultureInfo.InvariantCulture), searchInfo.MetadataLanguage, cancellationToken).ConfigureAwait(false);
  40. if (personResult is not null)
  41. {
  42. var result = new RemoteSearchResult
  43. {
  44. Name = personResult.Name,
  45. SearchProviderName = Name,
  46. Overview = personResult.Biography
  47. };
  48. if (personResult.Images?.Profiles is not null && personResult.Images.Profiles.Count > 0)
  49. {
  50. result.ImageUrl = _tmdbClientManager.GetProfileUrl(personResult.Images.Profiles[0].FilePath);
  51. }
  52. result.SetProviderId(MetadataProvider.Tmdb, personResult.Id.ToString(CultureInfo.InvariantCulture));
  53. if (!string.IsNullOrEmpty(personResult.ExternalIds.ImdbId))
  54. {
  55. result.SetProviderId(MetadataProvider.Imdb, personResult.ExternalIds.ImdbId);
  56. }
  57. return new[] { result };
  58. }
  59. }
  60. var personSearchResult = await _tmdbClientManager.SearchPersonAsync(searchInfo.Name, cancellationToken).ConfigureAwait(false);
  61. var remoteSearchResults = new List<RemoteSearchResult>();
  62. for (var i = 0; i < personSearchResult.Count; i++)
  63. {
  64. var person = personSearchResult[i];
  65. var remoteSearchResult = new RemoteSearchResult
  66. {
  67. SearchProviderName = Name,
  68. Name = person.Name,
  69. ImageUrl = _tmdbClientManager.GetProfileUrl(person.ProfilePath)
  70. };
  71. remoteSearchResult.SetProviderId(MetadataProvider.Tmdb, person.Id.ToString(CultureInfo.InvariantCulture));
  72. remoteSearchResults.Add(remoteSearchResult);
  73. }
  74. return remoteSearchResults;
  75. }
  76. /// <inheritdoc />
  77. public async Task<MetadataResult<Person>> GetMetadata(PersonLookupInfo info, CancellationToken cancellationToken)
  78. {
  79. var personTmdbId = Convert.ToInt32(info.GetProviderId(MetadataProvider.Tmdb), CultureInfo.InvariantCulture);
  80. // We don't already have an Id, need to fetch it
  81. if (personTmdbId <= 0)
  82. {
  83. var personSearchResults = await _tmdbClientManager.SearchPersonAsync(info.Name, cancellationToken).ConfigureAwait(false);
  84. if (personSearchResults.Count > 0)
  85. {
  86. personTmdbId = personSearchResults[0].Id;
  87. }
  88. }
  89. var result = new MetadataResult<Person>();
  90. if (personTmdbId > 0)
  91. {
  92. var person = await _tmdbClientManager.GetPersonAsync(personTmdbId, info.MetadataLanguage, cancellationToken).ConfigureAwait(false);
  93. result.HasMetadata = true;
  94. var item = new Person
  95. {
  96. // Take name from incoming info, don't rename the person
  97. // TODO: This should go in PersonMetadataService, not each person provider
  98. Name = info.Name,
  99. HomePageUrl = person.Homepage,
  100. Overview = person.Biography,
  101. PremiereDate = person.Birthday?.ToUniversalTime(),
  102. EndDate = person.Deathday?.ToUniversalTime()
  103. };
  104. if (!string.IsNullOrWhiteSpace(person.PlaceOfBirth))
  105. {
  106. item.ProductionLocations = new[] { person.PlaceOfBirth };
  107. }
  108. item.SetProviderId(MetadataProvider.Tmdb, person.Id.ToString(CultureInfo.InvariantCulture));
  109. if (!string.IsNullOrEmpty(person.ImdbId))
  110. {
  111. item.SetProviderId(MetadataProvider.Imdb, person.ImdbId);
  112. }
  113. result.HasMetadata = true;
  114. result.Item = item;
  115. }
  116. return result;
  117. }
  118. /// <inheritdoc />
  119. public Task<HttpResponseMessage> GetImageResponse(string url, CancellationToken cancellationToken)
  120. {
  121. return _httpClientFactory.CreateClient(NamedClient.Default).GetAsync(url, cancellationToken);
  122. }
  123. }
  124. }