TmdbMovieImageProvider.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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;
  10. using MediaBrowser.Controller.Entities.Movies;
  11. using MediaBrowser.Controller.Providers;
  12. using MediaBrowser.Model.Entities;
  13. using MediaBrowser.Model.Providers;
  14. using TMDbLib.Objects.Find;
  15. namespace MediaBrowser.Providers.Plugins.Tmdb.Movies
  16. {
  17. /// <summary>
  18. /// Movie image provider powered by TMDb.
  19. /// </summary>
  20. public class TmdbMovieImageProvider : IRemoteImageProvider, IHasOrder
  21. {
  22. private readonly IHttpClientFactory _httpClientFactory;
  23. private readonly TmdbClientManager _tmdbClientManager;
  24. /// <summary>
  25. /// Initializes a new instance of the <see cref="TmdbMovieImageProvider"/> class.
  26. /// </summary>
  27. /// <param name="httpClientFactory">The <see cref="IHttpClientFactory"/>.</param>
  28. /// <param name="tmdbClientManager">The <see cref="TmdbClientManager"/>.</param>
  29. public TmdbMovieImageProvider(IHttpClientFactory httpClientFactory, TmdbClientManager tmdbClientManager)
  30. {
  31. _httpClientFactory = httpClientFactory;
  32. _tmdbClientManager = tmdbClientManager;
  33. }
  34. /// <inheritdoc />
  35. public int Order => 0;
  36. /// <inheritdoc />
  37. public string Name => TmdbUtils.ProviderName;
  38. /// <inheritdoc />
  39. public bool Supports(BaseItem item)
  40. {
  41. return item is Movie || item is Trailer;
  42. }
  43. /// <inheritdoc />
  44. public IEnumerable<ImageType> GetSupportedImages(BaseItem item)
  45. {
  46. return new ImageType[]
  47. {
  48. ImageType.Primary,
  49. ImageType.Backdrop,
  50. ImageType.Logo
  51. };
  52. }
  53. /// <inheritdoc />
  54. public async Task<IEnumerable<RemoteImageInfo>> GetImages(BaseItem item, CancellationToken cancellationToken)
  55. {
  56. var language = item.GetPreferredMetadataLanguage();
  57. var movieTmdbId = Convert.ToInt32(item.GetProviderId(MetadataProvider.Tmdb), CultureInfo.InvariantCulture);
  58. if (movieTmdbId <= 0)
  59. {
  60. var movieImdbId = item.GetProviderId(MetadataProvider.Imdb);
  61. if (string.IsNullOrEmpty(movieImdbId))
  62. {
  63. return Enumerable.Empty<RemoteImageInfo>();
  64. }
  65. var movieResult = await _tmdbClientManager.FindByExternalIdAsync(movieImdbId, FindExternalSource.Imdb, language, cancellationToken).ConfigureAwait(false);
  66. if (movieResult?.MovieResults is not null && movieResult.MovieResults.Count > 0)
  67. {
  68. movieTmdbId = movieResult.MovieResults[0].Id;
  69. }
  70. }
  71. if (movieTmdbId <= 0)
  72. {
  73. return Enumerable.Empty<RemoteImageInfo>();
  74. }
  75. // TODO use image languages if All Languages isn't toggled, but there's currently no way to get that value in here
  76. var movie = await _tmdbClientManager
  77. .GetMovieAsync(movieTmdbId, null, null, cancellationToken)
  78. .ConfigureAwait(false);
  79. if (movie?.Images is null)
  80. {
  81. return Enumerable.Empty<RemoteImageInfo>();
  82. }
  83. var posters = movie.Images.Posters;
  84. var backdrops = movie.Images.Backdrops;
  85. var logos = movie.Images.Logos;
  86. var remoteImages = new List<RemoteImageInfo>(posters.Count + backdrops.Count + logos.Count);
  87. remoteImages.AddRange(_tmdbClientManager.ConvertPostersToRemoteImageInfo(posters, language));
  88. remoteImages.AddRange(_tmdbClientManager.ConvertBackdropsToRemoteImageInfo(backdrops, language));
  89. remoteImages.AddRange(_tmdbClientManager.ConvertLogosToRemoteImageInfo(logos, language));
  90. return remoteImages;
  91. }
  92. /// <inheritdoc />
  93. public Task<HttpResponseMessage> GetImageResponse(string url, CancellationToken cancellationToken)
  94. {
  95. return _httpClientFactory.CreateClient(NamedClient.Default).GetAsync(url, cancellationToken);
  96. }
  97. }
  98. }