ManualFanartAlbumProvider.cs 12 KB

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