TmdbBoxSetProvider.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #nullable disable
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Globalization;
  5. using System.Linq;
  6. using System.Net.Http;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. using MediaBrowser.Common.Net;
  10. using MediaBrowser.Controller.Entities.Movies;
  11. using MediaBrowser.Controller.Library;
  12. using MediaBrowser.Controller.Providers;
  13. using MediaBrowser.Model.Entities;
  14. using MediaBrowser.Model.Providers;
  15. namespace MediaBrowser.Providers.Plugins.Tmdb.BoxSets
  16. {
  17. /// <summary>
  18. /// BoxSet provider powered by TMDb.
  19. /// </summary>
  20. public class TmdbBoxSetProvider : IRemoteMetadataProvider<BoxSet, BoxSetInfo>
  21. {
  22. private readonly IHttpClientFactory _httpClientFactory;
  23. private readonly TmdbClientManager _tmdbClientManager;
  24. private readonly ILibraryManager _libraryManager;
  25. /// <summary>
  26. /// Initializes a new instance of the <see cref="TmdbBoxSetProvider"/> class.
  27. /// </summary>
  28. /// <param name="libraryManager">The <see cref="ILibraryManager"/>.</param>
  29. /// <param name="httpClientFactory">The <see cref="IHttpClientFactory"/>.</param>
  30. /// <param name="tmdbClientManager">The <see cref="TmdbClientManager"/>.</param>
  31. public TmdbBoxSetProvider(IHttpClientFactory httpClientFactory, TmdbClientManager tmdbClientManager, ILibraryManager libraryManager)
  32. {
  33. _httpClientFactory = httpClientFactory;
  34. _tmdbClientManager = tmdbClientManager;
  35. _libraryManager = libraryManager;
  36. }
  37. /// <inheritdoc />
  38. public string Name => TmdbUtils.ProviderName;
  39. /// <inheritdoc />
  40. public async Task<IEnumerable<RemoteSearchResult>> GetSearchResults(BoxSetInfo searchInfo, CancellationToken cancellationToken)
  41. {
  42. var tmdbId = Convert.ToInt32(searchInfo.GetProviderId(MetadataProvider.Tmdb), CultureInfo.InvariantCulture);
  43. var language = searchInfo.MetadataLanguage;
  44. if (tmdbId > 0)
  45. {
  46. var collection = await _tmdbClientManager.GetCollectionAsync(tmdbId, language, TmdbUtils.GetImageLanguagesParam(language), cancellationToken).ConfigureAwait(false);
  47. if (collection == null)
  48. {
  49. return Enumerable.Empty<RemoteSearchResult>();
  50. }
  51. var result = new RemoteSearchResult
  52. {
  53. Name = collection.Name,
  54. SearchProviderName = Name
  55. };
  56. if (collection.Images != null)
  57. {
  58. result.ImageUrl = _tmdbClientManager.GetPosterUrl(collection.PosterPath);
  59. }
  60. result.SetProviderId(MetadataProvider.Tmdb, collection.Id.ToString(CultureInfo.InvariantCulture));
  61. return new[] { result };
  62. }
  63. var collectionSearchResults = await _tmdbClientManager.SearchCollectionAsync(searchInfo.Name, language, cancellationToken).ConfigureAwait(false);
  64. var collections = new List<RemoteSearchResult>();
  65. for (var i = 0; i < collectionSearchResults.Count; i++)
  66. {
  67. var collection = new RemoteSearchResult
  68. {
  69. Name = collectionSearchResults[i].Name,
  70. SearchProviderName = Name
  71. };
  72. collection.SetProviderId(MetadataProvider.Tmdb, collectionSearchResults[i].Id.ToString(CultureInfo.InvariantCulture));
  73. collections.Add(collection);
  74. }
  75. return collections;
  76. }
  77. /// <inheritdoc />
  78. public async Task<MetadataResult<BoxSet>> GetMetadata(BoxSetInfo info, CancellationToken cancellationToken)
  79. {
  80. var tmdbId = Convert.ToInt32(info.GetProviderId(MetadataProvider.Tmdb), CultureInfo.InvariantCulture);
  81. var language = info.MetadataLanguage;
  82. // We don't already have an Id, need to fetch it
  83. if (tmdbId <= 0)
  84. {
  85. // ParseName is required here.
  86. // Caller provides the filename with extension stripped and NOT the parsed filename
  87. var parsedName = _libraryManager.ParseName(info.Name);
  88. var cleanedName = TmdbUtils.CleanName(parsedName.Name);
  89. var searchResults = await _tmdbClientManager.SearchCollectionAsync(cleanedName, language, cancellationToken).ConfigureAwait(false);
  90. if (searchResults != null && searchResults.Count > 0)
  91. {
  92. tmdbId = searchResults[0].Id;
  93. }
  94. }
  95. var result = new MetadataResult<BoxSet>();
  96. if (tmdbId > 0)
  97. {
  98. var collection = await _tmdbClientManager.GetCollectionAsync(tmdbId, language, TmdbUtils.GetImageLanguagesParam(language), cancellationToken).ConfigureAwait(false);
  99. if (collection != null)
  100. {
  101. var item = new BoxSet
  102. {
  103. Name = collection.Name,
  104. Overview = collection.Overview
  105. };
  106. item.SetProviderId(MetadataProvider.Tmdb, collection.Id.ToString(CultureInfo.InvariantCulture));
  107. result.HasMetadata = true;
  108. result.Item = item;
  109. }
  110. }
  111. return result;
  112. }
  113. /// <inheritdoc />
  114. public Task<HttpResponseMessage> GetImageResponse(string url, CancellationToken cancellationToken)
  115. {
  116. return _httpClientFactory.CreateClient(NamedClient.Default).GetAsync(url, cancellationToken);
  117. }
  118. }
  119. }