FanArtArtistProvider.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. using MediaBrowser.Common.Configuration;
  2. using MediaBrowser.Common.Extensions;
  3. using MediaBrowser.Common.IO;
  4. using MediaBrowser.Common.Net;
  5. using MediaBrowser.Controller.Configuration;
  6. using MediaBrowser.Controller.Entities;
  7. using MediaBrowser.Controller.Entities.Audio;
  8. using MediaBrowser.Controller.Library;
  9. using MediaBrowser.Controller.Providers;
  10. using MediaBrowser.Model.Entities;
  11. using MediaBrowser.Model.Logging;
  12. using System;
  13. using System.Collections.Generic;
  14. using System.Globalization;
  15. using System.IO;
  16. using System.Linq;
  17. using System.Threading;
  18. using System.Threading.Tasks;
  19. using System.Xml;
  20. namespace MediaBrowser.Providers.Music
  21. {
  22. /// <summary>
  23. /// Class FanArtArtistProvider
  24. /// </summary>
  25. public class FanArtArtistProvider : FanartBaseProvider
  26. {
  27. /// <summary>
  28. /// Gets the HTTP client.
  29. /// </summary>
  30. /// <value>The HTTP client.</value>
  31. protected IHttpClient HttpClient { get; private set; }
  32. /// <summary>
  33. /// The _provider manager
  34. /// </summary>
  35. private readonly IProviderManager _providerManager;
  36. internal static FanArtArtistProvider Current;
  37. /// <summary>
  38. /// Initializes a new instance of the <see cref="FanArtArtistProvider"/> class.
  39. /// </summary>
  40. /// <param name="httpClient">The HTTP client.</param>
  41. /// <param name="logManager">The log manager.</param>
  42. /// <param name="configurationManager">The configuration manager.</param>
  43. /// <param name="providerManager">The provider manager.</param>
  44. /// <exception cref="System.ArgumentNullException">httpClient</exception>
  45. public FanArtArtistProvider(IHttpClient httpClient, ILogManager logManager, IServerConfigurationManager configurationManager, IProviderManager providerManager)
  46. : base(logManager, configurationManager)
  47. {
  48. if (httpClient == null)
  49. {
  50. throw new ArgumentNullException("httpClient");
  51. }
  52. HttpClient = httpClient;
  53. _providerManager = providerManager;
  54. Current = this;
  55. }
  56. /// <summary>
  57. /// The fan art base URL
  58. /// </summary>
  59. protected string FanArtBaseUrl = "http://api.fanart.tv/webservice/artist/{0}/{1}/xml/all/1/1";
  60. public override ItemUpdateType ItemUpdateType
  61. {
  62. get
  63. {
  64. return ItemUpdateType.ImageUpdate;
  65. }
  66. }
  67. /// <summary>
  68. /// Supportses the specified item.
  69. /// </summary>
  70. /// <param name="item">The item.</param>
  71. /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
  72. public override bool Supports(BaseItem item)
  73. {
  74. return item is MusicArtist;
  75. }
  76. /// <summary>
  77. /// Gets a value indicating whether [save local meta].
  78. /// </summary>
  79. /// <value><c>true</c> if [save local meta]; otherwise, <c>false</c>.</value>
  80. protected virtual bool SaveLocalMeta
  81. {
  82. get { return ConfigurationManager.Configuration.SaveLocalMeta; }
  83. }
  84. /// <summary>
  85. /// Gets a value indicating whether [refresh on version change].
  86. /// </summary>
  87. /// <value><c>true</c> if [refresh on version change]; otherwise, <c>false</c>.</value>
  88. protected override bool RefreshOnVersionChange
  89. {
  90. get
  91. {
  92. return true;
  93. }
  94. }
  95. /// <summary>
  96. /// Gets the provider version.
  97. /// </summary>
  98. /// <value>The provider version.</value>
  99. protected override string ProviderVersion
  100. {
  101. get
  102. {
  103. return "7";
  104. }
  105. }
  106. public override MetadataProviderPriority Priority
  107. {
  108. get
  109. {
  110. return MetadataProviderPriority.Fourth;
  111. }
  112. }
  113. /// <summary>
  114. /// Needses the refresh internal.
  115. /// </summary>
  116. /// <param name="item">The item.</param>
  117. /// <param name="providerInfo">The provider info.</param>
  118. /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
  119. protected override bool NeedsRefreshInternal(BaseItem item, BaseProviderInfo providerInfo)
  120. {
  121. if (string.IsNullOrEmpty(item.GetProviderId(MetadataProviders.Musicbrainz)))
  122. {
  123. return false;
  124. }
  125. if (!ConfigurationManager.Configuration.DownloadMusicArtistImages.Art &&
  126. !ConfigurationManager.Configuration.DownloadMusicArtistImages.Backdrops &&
  127. !ConfigurationManager.Configuration.DownloadMusicArtistImages.Banner &&
  128. !ConfigurationManager.Configuration.DownloadMusicArtistImages.Logo &&
  129. !ConfigurationManager.Configuration.DownloadMusicArtistImages.Primary &&
  130. // The fanart album provider depends on xml downloaded here, so honor it's settings too
  131. !ConfigurationManager.Configuration.DownloadMusicAlbumImages.Disc &&
  132. !ConfigurationManager.Configuration.DownloadMusicAlbumImages.Primary)
  133. {
  134. return false;
  135. }
  136. return base.NeedsRefreshInternal(item, providerInfo);
  137. }
  138. protected override DateTime CompareDate(BaseItem item)
  139. {
  140. var musicBrainzId = item.GetProviderId(MetadataProviders.Musicbrainz);
  141. if (!string.IsNullOrEmpty(musicBrainzId))
  142. {
  143. // Process images
  144. var path = GetArtistDataPath(ConfigurationManager.ApplicationPaths, musicBrainzId);
  145. var files = new DirectoryInfo(path)
  146. .EnumerateFiles("*.xml", SearchOption.TopDirectoryOnly)
  147. .Select(i => i.LastWriteTimeUtc)
  148. .ToArray();
  149. if (files.Length > 0)
  150. {
  151. return files.Max();
  152. }
  153. }
  154. return base.CompareDate(item);
  155. }
  156. /// <summary>
  157. /// The us culture
  158. /// </summary>
  159. protected readonly CultureInfo UsCulture = new CultureInfo("en-US");
  160. /// <summary>
  161. /// Gets the series data path.
  162. /// </summary>
  163. /// <param name="appPaths">The app paths.</param>
  164. /// <param name="musicBrainzArtistId">The music brainz artist id.</param>
  165. /// <returns>System.String.</returns>
  166. internal static string GetArtistDataPath(IApplicationPaths appPaths, string musicBrainzArtistId)
  167. {
  168. var seriesDataPath = Path.Combine(GetArtistDataPath(appPaths), musicBrainzArtistId);
  169. if (!Directory.Exists(seriesDataPath))
  170. {
  171. Directory.CreateDirectory(seriesDataPath);
  172. }
  173. return seriesDataPath;
  174. }
  175. /// <summary>
  176. /// Gets the series data path.
  177. /// </summary>
  178. /// <param name="appPaths">The app paths.</param>
  179. /// <returns>System.String.</returns>
  180. internal static string GetArtistDataPath(IApplicationPaths appPaths)
  181. {
  182. var dataPath = Path.Combine(appPaths.DataPath, "fanart-music");
  183. if (!Directory.Exists(dataPath))
  184. {
  185. Directory.CreateDirectory(dataPath);
  186. }
  187. return dataPath;
  188. }
  189. /// <summary>
  190. /// Fetches metadata and returns true or false indicating if any work that requires persistence was done
  191. /// </summary>
  192. /// <param name="item">The item.</param>
  193. /// <param name="force">if set to <c>true</c> [force].</param>
  194. /// <param name="cancellationToken">The cancellation token.</param>
  195. /// <returns>Task{System.Boolean}.</returns>
  196. public override async Task<bool> FetchAsync(BaseItem item, bool force, CancellationToken cancellationToken)
  197. {
  198. cancellationToken.ThrowIfCancellationRequested();
  199. var musicBrainzId = item.GetProviderId(MetadataProviders.Musicbrainz);
  200. var artistDataPath = GetArtistDataPath(ConfigurationManager.ApplicationPaths, musicBrainzId);
  201. var xmlPath = Path.Combine(artistDataPath, "fanart.xml");
  202. // Only download the xml if it doesn't already exist. The prescan task will take care of getting updates
  203. if (!File.Exists(xmlPath))
  204. {
  205. await DownloadArtistXml(artistDataPath, musicBrainzId, cancellationToken).ConfigureAwait(false);
  206. }
  207. if (ConfigurationManager.Configuration.DownloadMusicArtistImages.Art ||
  208. ConfigurationManager.Configuration.DownloadMusicArtistImages.Backdrops ||
  209. ConfigurationManager.Configuration.DownloadMusicArtistImages.Banner ||
  210. ConfigurationManager.Configuration.DownloadMusicArtistImages.Logo ||
  211. ConfigurationManager.Configuration.DownloadMusicArtistImages.Primary)
  212. {
  213. if (File.Exists(xmlPath))
  214. {
  215. await FetchFromXml(item, xmlPath, cancellationToken).ConfigureAwait(false);
  216. }
  217. }
  218. BaseProviderInfo data;
  219. if (!item.ProviderData.TryGetValue(Id, out data))
  220. {
  221. data = new BaseProviderInfo();
  222. item.ProviderData[Id] = data;
  223. }
  224. SetLastRefreshed(item, DateTime.UtcNow);
  225. return true;
  226. }
  227. /// <summary>
  228. /// Downloads the artist XML.
  229. /// </summary>
  230. /// <param name="artistPath">The artist path.</param>
  231. /// <param name="musicBrainzId">The music brainz id.</param>
  232. /// <param name="cancellationToken">The cancellation token.</param>
  233. /// <returns>Task{System.Boolean}.</returns>
  234. internal async Task DownloadArtistXml(string artistPath, string musicBrainzId, CancellationToken cancellationToken)
  235. {
  236. cancellationToken.ThrowIfCancellationRequested();
  237. var url = string.Format(FanArtBaseUrl, ApiKey, musicBrainzId);
  238. var xmlPath = Path.Combine(artistPath, "fanart.xml");
  239. using (var response = await HttpClient.Get(new HttpRequestOptions
  240. {
  241. Url = url,
  242. ResourcePool = FanArtResourcePool,
  243. CancellationToken = cancellationToken
  244. }).ConfigureAwait(false))
  245. {
  246. using (var xmlFileStream = new FileStream(xmlPath, FileMode.Create, FileAccess.Write, FileShare.Read, StreamDefaults.DefaultFileStreamBufferSize, FileOptions.Asynchronous))
  247. {
  248. await response.CopyToAsync(xmlFileStream).ConfigureAwait(false);
  249. }
  250. }
  251. }
  252. /// <summary>
  253. /// Fetches from XML.
  254. /// </summary>
  255. /// <param name="item">The item.</param>
  256. /// <param name="xmlFilePath">The XML file path.</param>
  257. /// <param name="cancellationToken">The cancellation token.</param>
  258. /// <returns>Task.</returns>
  259. private async Task FetchFromXml(BaseItem item, string xmlFilePath, CancellationToken cancellationToken)
  260. {
  261. var doc = new XmlDocument();
  262. doc.Load(xmlFilePath);
  263. cancellationToken.ThrowIfCancellationRequested();
  264. string path;
  265. var hd = ConfigurationManager.Configuration.DownloadHDFanArt ? "hd" : "";
  266. if (ConfigurationManager.Configuration.DownloadMusicArtistImages.Logo && !item.HasImage(ImageType.Logo))
  267. {
  268. var node =
  269. doc.SelectSingleNode("//fanart/music/musiclogos/" + hd + "musiclogo/@url") ??
  270. doc.SelectSingleNode("//fanart/music/musiclogos/musiclogo/@url");
  271. path = node != null ? node.Value : null;
  272. if (!string.IsNullOrEmpty(path))
  273. {
  274. await _providerManager.SaveImage(item, path, FanArtResourcePool, ImageType.Logo, null, cancellationToken)
  275. .ConfigureAwait(false);
  276. }
  277. }
  278. cancellationToken.ThrowIfCancellationRequested();
  279. if (ConfigurationManager.Configuration.DownloadMusicArtistImages.Backdrops && item.BackdropImagePaths.Count == 0)
  280. {
  281. var nodes = doc.SelectNodes("//fanart/music/artistbackgrounds//@url");
  282. if (nodes != null)
  283. {
  284. var numBackdrops = 0;
  285. item.BackdropImagePaths = new List<string>();
  286. foreach (XmlNode node in nodes)
  287. {
  288. path = node.Value;
  289. if (!string.IsNullOrEmpty(path))
  290. {
  291. await _providerManager.SaveImage(item, path, FanArtResourcePool, ImageType.Backdrop, numBackdrops, cancellationToken)
  292. .ConfigureAwait(false);
  293. numBackdrops++;
  294. if (numBackdrops >= ConfigurationManager.Configuration.MaxBackdrops) break;
  295. }
  296. }
  297. }
  298. }
  299. cancellationToken.ThrowIfCancellationRequested();
  300. if (ConfigurationManager.Configuration.DownloadMusicArtistImages.Art && !item.HasImage(ImageType.Art))
  301. {
  302. var node =
  303. doc.SelectSingleNode("//fanart/music/musicarts/" + hd + "musicart/@url") ??
  304. doc.SelectSingleNode("//fanart/music/musicarts/musicart/@url");
  305. path = node != null ? node.Value : null;
  306. if (!string.IsNullOrEmpty(path))
  307. {
  308. await _providerManager.SaveImage(item, path, FanArtResourcePool, ImageType.Art, null, cancellationToken)
  309. .ConfigureAwait(false);
  310. }
  311. }
  312. cancellationToken.ThrowIfCancellationRequested();
  313. if (ConfigurationManager.Configuration.DownloadMusicArtistImages.Banner && !item.HasImage(ImageType.Banner))
  314. {
  315. var node = doc.SelectSingleNode("//fanart/music/musicbanners/" + hd + "musicbanner/@url") ??
  316. doc.SelectSingleNode("//fanart/music/musicbanners/musicbanner/@url");
  317. path = node != null ? node.Value : null;
  318. if (!string.IsNullOrEmpty(path))
  319. {
  320. await _providerManager.SaveImage(item, path, FanArtResourcePool, ImageType.Banner, null, cancellationToken)
  321. .ConfigureAwait(false);
  322. }
  323. }
  324. cancellationToken.ThrowIfCancellationRequested();
  325. // Artist thumbs are actually primary images (they are square/portrait)
  326. if (ConfigurationManager.Configuration.DownloadMusicArtistImages.Primary && !item.HasImage(ImageType.Primary))
  327. {
  328. var node = doc.SelectSingleNode("//fanart/music/artistthumbs/artistthumb/@url");
  329. path = node != null ? node.Value : null;
  330. if (!string.IsNullOrEmpty(path))
  331. {
  332. await _providerManager.SaveImage(item, path, FanArtResourcePool, ImageType.Primary, null, cancellationToken)
  333. .ConfigureAwait(false);
  334. }
  335. }
  336. }
  337. }
  338. }