TmdbPersonProvider.cs 6.1 KB

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