ManualFanartAlbumProvider.cs 12 KB

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