TmdbPersonProvider.cs 5.7 KB

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