FanArtAlbumProvider.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. using MediaBrowser.Common.Net;
  2. using MediaBrowser.Controller.Configuration;
  3. using MediaBrowser.Controller.Entities;
  4. using MediaBrowser.Controller.Entities.Audio;
  5. using MediaBrowser.Controller.Providers;
  6. using MediaBrowser.Model.Dto;
  7. using MediaBrowser.Model.Entities;
  8. using MediaBrowser.Model.Providers;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Globalization;
  12. using System.IO;
  13. using System.Linq;
  14. using System.Text.RegularExpressions;
  15. using System.Threading;
  16. using System.Threading.Tasks;
  17. using CommonIO;
  18. using MediaBrowser.Model.Serialization;
  19. namespace MediaBrowser.Providers.Music
  20. {
  21. public class FanartAlbumProvider : IRemoteImageProvider, IHasOrder
  22. {
  23. private readonly CultureInfo _usCulture = new CultureInfo("en-US");
  24. private readonly IServerConfigurationManager _config;
  25. private readonly IHttpClient _httpClient;
  26. private readonly IFileSystem _fileSystem;
  27. private readonly IJsonSerializer _jsonSerializer;
  28. public FanartAlbumProvider(IServerConfigurationManager config, IHttpClient httpClient, IFileSystem fileSystem, IJsonSerializer jsonSerializer)
  29. {
  30. _config = config;
  31. _httpClient = httpClient;
  32. _fileSystem = fileSystem;
  33. _jsonSerializer = jsonSerializer;
  34. }
  35. public string Name
  36. {
  37. get { return ProviderName; }
  38. }
  39. public static string ProviderName
  40. {
  41. get { return "FanArt"; }
  42. }
  43. public bool Supports(IHasImages item)
  44. {
  45. return item is MusicAlbum;
  46. }
  47. public IEnumerable<ImageType> GetSupportedImages(IHasImages item)
  48. {
  49. return new List<ImageType>
  50. {
  51. ImageType.Primary,
  52. ImageType.Disc
  53. };
  54. }
  55. public async Task<IEnumerable<RemoteImageInfo>> GetImages(IHasImages item, CancellationToken cancellationToken)
  56. {
  57. var album = (MusicAlbum)item;
  58. var list = new List<RemoteImageInfo>();
  59. var musicArtist = album.MusicArtist;
  60. if (musicArtist == null)
  61. {
  62. return list;
  63. }
  64. var artistMusicBrainzId = musicArtist.GetProviderId(MetadataProviders.MusicBrainzArtist);
  65. if (!string.IsNullOrEmpty(artistMusicBrainzId))
  66. {
  67. await FanartArtistProvider.Current.EnsureArtistJson(artistMusicBrainzId, cancellationToken).ConfigureAwait(false);
  68. var artistJsonPath = FanartArtistProvider.GetArtistJsonPath(_config.CommonApplicationPaths, artistMusicBrainzId);
  69. var musicBrainzReleaseGroupId = album.GetProviderId(MetadataProviders.MusicBrainzReleaseGroup);
  70. var musicBrainzId = album.GetProviderId(MetadataProviders.MusicBrainzAlbum);
  71. try
  72. {
  73. AddImages(list, artistJsonPath, musicBrainzId, musicBrainzReleaseGroupId, cancellationToken);
  74. }
  75. catch (FileNotFoundException)
  76. {
  77. }
  78. catch (DirectoryNotFoundException)
  79. {
  80. }
  81. }
  82. var language = item.GetPreferredMetadataLanguage();
  83. var isLanguageEn = string.Equals(language, "en", StringComparison.OrdinalIgnoreCase);
  84. // Sort first by width to prioritize HD versions
  85. return list.OrderByDescending(i => i.Width ?? 0)
  86. .ThenByDescending(i =>
  87. {
  88. if (string.Equals(language, i.Language, StringComparison.OrdinalIgnoreCase))
  89. {
  90. return 3;
  91. }
  92. if (!isLanguageEn)
  93. {
  94. if (string.Equals("en", i.Language, StringComparison.OrdinalIgnoreCase))
  95. {
  96. return 2;
  97. }
  98. }
  99. if (string.IsNullOrEmpty(i.Language))
  100. {
  101. return isLanguageEn ? 3 : 2;
  102. }
  103. return 0;
  104. })
  105. .ThenByDescending(i => i.CommunityRating ?? 0)
  106. .ThenByDescending(i => i.VoteCount ?? 0);
  107. }
  108. /// <summary>
  109. /// Adds the images.
  110. /// </summary>
  111. /// <param name="list">The list.</param>
  112. /// <param name="path">The path.</param>
  113. /// <param name="releaseId">The release identifier.</param>
  114. /// <param name="releaseGroupId">The release group identifier.</param>
  115. /// <param name="cancellationToken">The cancellation token.</param>
  116. private void AddImages(List<RemoteImageInfo> list, string path, string releaseId, string releaseGroupId, CancellationToken cancellationToken)
  117. {
  118. var obj = _jsonSerializer.DeserializeFromFile<FanartArtistProvider.FanartArtistResponse>(path);
  119. if (obj.albums != null)
  120. {
  121. var album = obj.albums.FirstOrDefault(i => string.Equals(i.release_group_id, releaseGroupId, StringComparison.OrdinalIgnoreCase));
  122. if (album != null)
  123. {
  124. PopulateImages(list, album.albumcover, ImageType.Primary, 1000, 1000);
  125. PopulateImages(list, album.cdart, ImageType.Disc, 1000, 1000);
  126. }
  127. }
  128. }
  129. private Regex _regex_http = new Regex("^http://");
  130. private void PopulateImages(List<RemoteImageInfo> list,
  131. List<FanartArtistProvider.FanartArtistImage> images,
  132. ImageType type,
  133. int width,
  134. int height)
  135. {
  136. if (images == null)
  137. {
  138. return;
  139. }
  140. list.AddRange(images.Select(i =>
  141. {
  142. var url = i.url;
  143. if (!string.IsNullOrEmpty(url))
  144. {
  145. var likesString = i.likes;
  146. int likes;
  147. var info = new RemoteImageInfo
  148. {
  149. RatingType = RatingType.Likes,
  150. Type = type,
  151. Width = width,
  152. Height = height,
  153. ProviderName = Name,
  154. Url = _regex_http.Replace(url, "https://", 1),
  155. Language = i.lang
  156. };
  157. if (!string.IsNullOrEmpty(likesString) && int.TryParse(likesString, NumberStyles.Any, _usCulture, out likes))
  158. {
  159. info.CommunityRating = likes;
  160. }
  161. return info;
  162. }
  163. return null;
  164. }).Where(i => i != null));
  165. }
  166. public int Order
  167. {
  168. get
  169. {
  170. // After embedded provider
  171. return 1;
  172. }
  173. }
  174. public Task<HttpResponseInfo> GetImageResponse(string url, CancellationToken cancellationToken)
  175. {
  176. return _httpClient.GetResponse(new HttpRequestOptions
  177. {
  178. CancellationToken = cancellationToken,
  179. Url = url,
  180. ResourcePool = FanartArtistProvider.Current.FanArtResourcePool
  181. });
  182. }
  183. }
  184. }