TmdbBoxSetImageProvider.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. #pragma warning disable CS1591
  2. using System;
  3. using System.Collections.Generic;
  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.Dto;
  13. using MediaBrowser.Model.Entities;
  14. using MediaBrowser.Model.Providers;
  15. using MediaBrowser.Providers.Plugins.Tmdb.Models.Collections;
  16. using MediaBrowser.Providers.Plugins.Tmdb.Models.General;
  17. using MediaBrowser.Providers.Plugins.Tmdb.Movies;
  18. namespace MediaBrowser.Providers.Plugins.Tmdb.BoxSets
  19. {
  20. public class TmdbBoxSetImageProvider : IRemoteImageProvider, IHasOrder
  21. {
  22. private readonly IHttpClientFactory _httpClientFactory;
  23. public TmdbBoxSetImageProvider(IHttpClientFactory httpClientFactory)
  24. {
  25. _httpClientFactory = httpClientFactory;
  26. }
  27. public string Name => ProviderName;
  28. public static string ProviderName => TmdbUtils.ProviderName;
  29. public bool Supports(BaseItem item)
  30. {
  31. return item is BoxSet;
  32. }
  33. public IEnumerable<ImageType> GetSupportedImages(BaseItem item)
  34. {
  35. return new List<ImageType>
  36. {
  37. ImageType.Primary,
  38. ImageType.Backdrop
  39. };
  40. }
  41. public async Task<IEnumerable<RemoteImageInfo>> GetImages(BaseItem item, CancellationToken cancellationToken)
  42. {
  43. var tmdbId = item.GetProviderId(MetadataProvider.Tmdb);
  44. if (!string.IsNullOrEmpty(tmdbId))
  45. {
  46. var language = item.GetPreferredMetadataLanguage();
  47. var mainResult = await TmdbBoxSetProvider.Current.GetMovieDbResult(tmdbId, null, cancellationToken).ConfigureAwait(false);
  48. if (mainResult != null)
  49. {
  50. var tmdbSettings = await TmdbMovieProvider.Current.GetTmdbSettings(cancellationToken).ConfigureAwait(false);
  51. var tmdbImageUrl = tmdbSettings.images.GetImageUrl("original");
  52. return GetImages(mainResult, language, tmdbImageUrl);
  53. }
  54. }
  55. return new List<RemoteImageInfo>();
  56. }
  57. private IEnumerable<RemoteImageInfo> GetImages(CollectionResult obj, string language, string baseUrl)
  58. {
  59. var list = new List<RemoteImageInfo>();
  60. var images = obj.Images ?? new CollectionImages();
  61. list.AddRange(GetPosters(images).Select(i => new RemoteImageInfo
  62. {
  63. Url = baseUrl + i.File_Path,
  64. CommunityRating = i.Vote_Average,
  65. VoteCount = i.Vote_Count,
  66. Width = i.Width,
  67. Height = i.Height,
  68. Language = TmdbMovieProvider.AdjustImageLanguage(i.Iso_639_1, language),
  69. ProviderName = Name,
  70. Type = ImageType.Primary,
  71. RatingType = RatingType.Score
  72. }));
  73. list.AddRange(GetBackdrops(images).Select(i => new RemoteImageInfo
  74. {
  75. Url = baseUrl + i.File_Path,
  76. CommunityRating = i.Vote_Average,
  77. VoteCount = i.Vote_Count,
  78. Width = i.Width,
  79. Height = i.Height,
  80. ProviderName = Name,
  81. Type = ImageType.Backdrop,
  82. RatingType = RatingType.Score
  83. }));
  84. var isLanguageEn = string.Equals(language, "en", StringComparison.OrdinalIgnoreCase);
  85. return list.OrderByDescending(i =>
  86. {
  87. if (string.Equals(language, i.Language, StringComparison.OrdinalIgnoreCase))
  88. {
  89. return 3;
  90. }
  91. if (!isLanguageEn)
  92. {
  93. if (string.Equals("en", i.Language, StringComparison.OrdinalIgnoreCase))
  94. {
  95. return 2;
  96. }
  97. }
  98. if (string.IsNullOrEmpty(i.Language))
  99. {
  100. return isLanguageEn ? 3 : 2;
  101. }
  102. return 0;
  103. })
  104. .ThenByDescending(i => i.CommunityRating ?? 0)
  105. .ThenByDescending(i => i.VoteCount ?? 0);
  106. }
  107. /// <summary>
  108. /// Gets the posters.
  109. /// </summary>
  110. /// <param name="images">The images.</param>
  111. /// <returns>IEnumerable{MovieDbProvider.Poster}.</returns>
  112. private IEnumerable<Poster> GetPosters(CollectionImages images)
  113. {
  114. return images.Posters ?? new List<Poster>();
  115. }
  116. /// <summary>
  117. /// Gets the backdrops.
  118. /// </summary>
  119. /// <param name="images">The images.</param>
  120. /// <returns>IEnumerable{MovieDbProvider.Backdrop}.</returns>
  121. private IEnumerable<Backdrop> GetBackdrops(CollectionImages images)
  122. {
  123. var eligibleBackdrops = images.Backdrops == null ? new List<Backdrop>() :
  124. images.Backdrops;
  125. return eligibleBackdrops.OrderByDescending(i => i.Vote_Average)
  126. .ThenByDescending(i => i.Vote_Count);
  127. }
  128. public int Order => 0;
  129. public Task<HttpResponseMessage> GetImageResponse(string url, CancellationToken cancellationToken)
  130. {
  131. return _httpClientFactory.CreateClient(NamedClient.Default).GetAsync(url, cancellationToken);
  132. }
  133. }
  134. }