FanArtAlbumProvider.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. using MediaBrowser.Common.IO;
  2. using MediaBrowser.Common.Net;
  3. using MediaBrowser.Controller.Configuration;
  4. using MediaBrowser.Controller.Entities;
  5. using MediaBrowser.Controller.Entities.Audio;
  6. using MediaBrowser.Controller.Providers;
  7. using MediaBrowser.Model.Dto;
  8. using MediaBrowser.Model.Entities;
  9. using MediaBrowser.Model.Providers;
  10. using MediaBrowser.Providers.TV;
  11. using System;
  12. using System.Collections.Generic;
  13. using System.Globalization;
  14. using System.IO;
  15. using System.Linq;
  16. using System.Text;
  17. using System.Threading;
  18. using System.Threading.Tasks;
  19. using System.Xml;
  20. namespace MediaBrowser.Providers.Music
  21. {
  22. public class FanartAlbumProvider : IRemoteImageProvider, IHasChangeMonitor, IHasOrder
  23. {
  24. private readonly CultureInfo _usCulture = new CultureInfo("en-US");
  25. private readonly IServerConfigurationManager _config;
  26. private readonly IHttpClient _httpClient;
  27. private readonly IFileSystem _fileSystem;
  28. public FanartAlbumProvider(IServerConfigurationManager config, IHttpClient httpClient, IFileSystem fileSystem)
  29. {
  30. _config = config;
  31. _httpClient = httpClient;
  32. _fileSystem = fileSystem;
  33. }
  34. public string Name
  35. {
  36. get { return ProviderName; }
  37. }
  38. public static string ProviderName
  39. {
  40. get { return "FanArt"; }
  41. }
  42. public bool Supports(IHasImages item)
  43. {
  44. return item is MusicAlbum;
  45. }
  46. public IEnumerable<ImageType> GetSupportedImages(IHasImages item)
  47. {
  48. return new List<ImageType>
  49. {
  50. ImageType.Primary,
  51. ImageType.Disc
  52. };
  53. }
  54. public async Task<IEnumerable<RemoteImageInfo>> GetImages(IHasImages item, CancellationToken cancellationToken)
  55. {
  56. var album = (MusicAlbum)item;
  57. var list = new List<RemoteImageInfo>();
  58. var artistMusicBrainzId = album.MusicArtist.GetProviderId(MetadataProviders.MusicBrainzArtist);
  59. if (!string.IsNullOrEmpty(artistMusicBrainzId))
  60. {
  61. await FanartArtistProvider.Current.EnsureArtistXml(artistMusicBrainzId, cancellationToken).ConfigureAwait(false);
  62. var artistXmlPath = FanartArtistProvider.GetArtistXmlPath(_config.CommonApplicationPaths, artistMusicBrainzId);
  63. var musicBrainzReleaseGroupId = album.GetProviderId(MetadataProviders.MusicBrainzReleaseGroup);
  64. var musicBrainzId = album.GetProviderId(MetadataProviders.MusicBrainzAlbum);
  65. try
  66. {
  67. AddImages(list, artistXmlPath, musicBrainzId, musicBrainzReleaseGroupId, cancellationToken);
  68. }
  69. catch (FileNotFoundException)
  70. {
  71. }
  72. catch (DirectoryNotFoundException)
  73. {
  74. }
  75. }
  76. var language = item.GetPreferredMetadataLanguage();
  77. var isLanguageEn = string.Equals(language, "en", StringComparison.OrdinalIgnoreCase);
  78. // Sort first by width to prioritize HD versions
  79. return list.OrderByDescending(i => i.Width ?? 0)
  80. .ThenByDescending(i =>
  81. {
  82. if (string.Equals(language, i.Language, StringComparison.OrdinalIgnoreCase))
  83. {
  84. return 3;
  85. }
  86. if (!isLanguageEn)
  87. {
  88. if (string.Equals("en", i.Language, StringComparison.OrdinalIgnoreCase))
  89. {
  90. return 2;
  91. }
  92. }
  93. if (string.IsNullOrEmpty(i.Language))
  94. {
  95. return isLanguageEn ? 3 : 2;
  96. }
  97. return 0;
  98. })
  99. .ThenByDescending(i => i.CommunityRating ?? 0)
  100. .ThenByDescending(i => i.VoteCount ?? 0);
  101. }
  102. /// <summary>
  103. /// Adds the images.
  104. /// </summary>
  105. /// <param name="list">The list.</param>
  106. /// <param name="xmlPath">The XML path.</param>
  107. /// <param name="releaseId">The release identifier.</param>
  108. /// <param name="releaseGroupId">The release group identifier.</param>
  109. /// <param name="cancellationToken">The cancellation token.</param>
  110. private void AddImages(List<RemoteImageInfo> list, string xmlPath, string releaseId, string releaseGroupId, CancellationToken cancellationToken)
  111. {
  112. using (var streamReader = new StreamReader(xmlPath, Encoding.UTF8))
  113. {
  114. // Use XmlReader for best performance
  115. using (var reader = XmlReader.Create(streamReader, new XmlReaderSettings
  116. {
  117. CheckCharacters = false,
  118. IgnoreProcessingInstructions = true,
  119. IgnoreComments = true,
  120. ValidationType = ValidationType.None
  121. }))
  122. {
  123. reader.MoveToContent();
  124. // Loop through each element
  125. while (reader.Read())
  126. {
  127. cancellationToken.ThrowIfCancellationRequested();
  128. if (reader.NodeType == XmlNodeType.Element)
  129. {
  130. switch (reader.Name)
  131. {
  132. case "music":
  133. {
  134. using (var subReader = reader.ReadSubtree())
  135. {
  136. AddImagesFromMusicNode(list, releaseId, releaseGroupId, subReader, cancellationToken);
  137. }
  138. break;
  139. }
  140. default:
  141. reader.Skip();
  142. break;
  143. }
  144. }
  145. }
  146. }
  147. }
  148. }
  149. /// <summary>
  150. /// Adds the images from music node.
  151. /// </summary>
  152. /// <param name="list">The list.</param>
  153. /// <param name="releaseId">The release identifier.</param>
  154. /// <param name="releaseGroupId">The release group identifier.</param>
  155. /// <param name="reader">The reader.</param>
  156. /// <param name="cancellationToken">The cancellation token.</param>
  157. private void AddImagesFromMusicNode(List<RemoteImageInfo> list, string releaseId, string releaseGroupId, XmlReader reader, CancellationToken cancellationToken)
  158. {
  159. reader.MoveToContent();
  160. while (reader.Read())
  161. {
  162. if (reader.NodeType == XmlNodeType.Element)
  163. {
  164. switch (reader.Name)
  165. {
  166. case "albums":
  167. {
  168. using (var subReader = reader.ReadSubtree())
  169. {
  170. AddImagesFromAlbumsNode(list, releaseId, releaseGroupId, subReader, cancellationToken);
  171. }
  172. break;
  173. }
  174. default:
  175. {
  176. using (reader.ReadSubtree())
  177. {
  178. }
  179. break;
  180. }
  181. }
  182. }
  183. }
  184. }
  185. /// <summary>
  186. /// Adds the images from albums node.
  187. /// </summary>
  188. /// <param name="list">The list.</param>
  189. /// <param name="releaseId">The release identifier.</param>
  190. /// <param name="releaseGroupId">The release group identifier.</param>
  191. /// <param name="reader">The reader.</param>
  192. /// <param name="cancellationToken">The cancellation token.</param>
  193. private void AddImagesFromAlbumsNode(List<RemoteImageInfo> list, string releaseId, string releaseGroupId, XmlReader reader, CancellationToken cancellationToken)
  194. {
  195. reader.MoveToContent();
  196. while (reader.Read())
  197. {
  198. if (reader.NodeType == XmlNodeType.Element)
  199. {
  200. switch (reader.Name)
  201. {
  202. case "album":
  203. {
  204. var id = reader.GetAttribute("id");
  205. using (var subReader = reader.ReadSubtree())
  206. {
  207. if (string.Equals(id, releaseId, StringComparison.OrdinalIgnoreCase) ||
  208. string.Equals(id, releaseGroupId, StringComparison.OrdinalIgnoreCase))
  209. {
  210. AddImages(list, subReader, cancellationToken);
  211. }
  212. }
  213. break;
  214. }
  215. default:
  216. {
  217. using (reader.ReadSubtree())
  218. {
  219. }
  220. break;
  221. }
  222. }
  223. }
  224. }
  225. }
  226. /// <summary>
  227. /// Adds the images.
  228. /// </summary>
  229. /// <param name="list">The list.</param>
  230. /// <param name="reader">The reader.</param>
  231. /// <param name="cancellationToken">The cancellation token.</param>
  232. private void AddImages(List<RemoteImageInfo> list, XmlReader reader, CancellationToken cancellationToken)
  233. {
  234. reader.MoveToContent();
  235. while (reader.Read())
  236. {
  237. if (reader.NodeType == XmlNodeType.Element)
  238. {
  239. switch (reader.Name)
  240. {
  241. case "cdart":
  242. {
  243. AddImage(list, reader, ImageType.Disc, 1000, 1000);
  244. break;
  245. }
  246. case "albumcover":
  247. {
  248. AddImage(list, reader, ImageType.Primary, 1000, 1000);
  249. break;
  250. }
  251. default:
  252. {
  253. using (reader.ReadSubtree())
  254. {
  255. }
  256. break;
  257. }
  258. }
  259. }
  260. }
  261. }
  262. /// <summary>
  263. /// Adds the image.
  264. /// </summary>
  265. /// <param name="list">The list.</param>
  266. /// <param name="reader">The reader.</param>
  267. /// <param name="type">The type.</param>
  268. /// <param name="width">The width.</param>
  269. /// <param name="height">The height.</param>
  270. private void AddImage(List<RemoteImageInfo> list, XmlReader reader, ImageType type, int width, int height)
  271. {
  272. var url = reader.GetAttribute("url");
  273. var size = reader.GetAttribute("size");
  274. if (!string.IsNullOrEmpty(size))
  275. {
  276. int sizeNum;
  277. if (int.TryParse(size, NumberStyles.Any, _usCulture, out sizeNum))
  278. {
  279. width = sizeNum;
  280. height = sizeNum;
  281. }
  282. }
  283. var likesString = reader.GetAttribute("likes");
  284. int likes;
  285. var info = new RemoteImageInfo
  286. {
  287. RatingType = RatingType.Likes,
  288. Type = type,
  289. Width = width,
  290. Height = height,
  291. ProviderName = Name,
  292. Url = url,
  293. Language = reader.GetAttribute("lang")
  294. };
  295. if (!string.IsNullOrEmpty(likesString) && int.TryParse(likesString, NumberStyles.Any, _usCulture, out likes))
  296. {
  297. info.CommunityRating = likes;
  298. }
  299. list.Add(info);
  300. }
  301. public int Order
  302. {
  303. get
  304. {
  305. // After embedded provider
  306. return 1;
  307. }
  308. }
  309. public Task<HttpResponseInfo> GetImageResponse(string url, CancellationToken cancellationToken)
  310. {
  311. return _httpClient.GetResponse(new HttpRequestOptions
  312. {
  313. CancellationToken = cancellationToken,
  314. Url = url,
  315. ResourcePool = FanartArtistProvider.Current.FanArtResourcePool
  316. });
  317. }
  318. public bool HasChanged(IHasMetadata item, IDirectoryService directoryService, DateTime date)
  319. {
  320. var options = FanartSeriesProvider.Current.GetFanartOptions();
  321. if (!options.EnableAutomaticUpdates)
  322. {
  323. return false;
  324. }
  325. var album = (MusicAlbum)item;
  326. var artist = album.MusicArtist;
  327. if (artist != null)
  328. {
  329. var artistMusicBrainzId = artist.GetProviderId(MetadataProviders.MusicBrainzArtist);
  330. if (!String.IsNullOrEmpty(artistMusicBrainzId))
  331. {
  332. // Process images
  333. var artistXmlPath = FanartArtistProvider.GetArtistXmlPath(_config.CommonApplicationPaths, artistMusicBrainzId);
  334. var fileInfo = new FileInfo(artistXmlPath);
  335. return !fileInfo.Exists || _fileSystem.GetLastWriteTimeUtc(fileInfo) > date;
  336. }
  337. }
  338. return false;
  339. }
  340. }
  341. }