2
0

TmdbBoxSetProvider.cs 5.6 KB

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