TmdbPersonProvider.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. result.TrySetProviderId(MetadataProvider.Imdb, personResult.ExternalIds.ImdbId);
  53. return new[] { result };
  54. }
  55. }
  56. var personSearchResult = await _tmdbClientManager.SearchPersonAsync(searchInfo.Name, cancellationToken).ConfigureAwait(false);
  57. var remoteSearchResults = new RemoteSearchResult[personSearchResult.Count];
  58. for (var i = 0; i < personSearchResult.Count; i++)
  59. {
  60. var person = personSearchResult[i];
  61. var remoteSearchResult = new RemoteSearchResult
  62. {
  63. SearchProviderName = Name,
  64. Name = person.Name,
  65. ImageUrl = _tmdbClientManager.GetProfileUrl(person.ProfilePath)
  66. };
  67. remoteSearchResult.SetProviderId(MetadataProvider.Tmdb, person.Id.ToString(CultureInfo.InvariantCulture));
  68. remoteSearchResults[i] = remoteSearchResult;
  69. }
  70. return remoteSearchResults;
  71. }
  72. /// <inheritdoc />
  73. public async Task<MetadataResult<Person>> GetMetadata(PersonLookupInfo info, CancellationToken cancellationToken)
  74. {
  75. var personTmdbId = Convert.ToInt32(info.GetProviderId(MetadataProvider.Tmdb), CultureInfo.InvariantCulture);
  76. // We don't already have an Id, need to fetch it
  77. if (personTmdbId <= 0)
  78. {
  79. var personSearchResults = await _tmdbClientManager.SearchPersonAsync(info.Name, cancellationToken).ConfigureAwait(false);
  80. if (personSearchResults.Count > 0)
  81. {
  82. personTmdbId = personSearchResults[0].Id;
  83. }
  84. }
  85. var result = new MetadataResult<Person>();
  86. if (personTmdbId > 0)
  87. {
  88. var person = await _tmdbClientManager.GetPersonAsync(personTmdbId, info.MetadataLanguage, cancellationToken).ConfigureAwait(false);
  89. if (person is null)
  90. {
  91. return result;
  92. }
  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. item.TrySetProviderId(MetadataProvider.Imdb, person.ImdbId);
  110. result.HasMetadata = true;
  111. result.Item = item;
  112. }
  113. return result;
  114. }
  115. /// <inheritdoc />
  116. public Task<HttpResponseMessage> GetImageResponse(string url, CancellationToken cancellationToken)
  117. {
  118. return _httpClientFactory.CreateClient(NamedClient.Default).GetAsync(url, cancellationToken);
  119. }
  120. }
  121. }