FanArtArtistProvider.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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. /// <summary>
  107. /// Needses the refresh internal.
  108. /// </summary>
  109. /// <param name="item">The item.</param>
  110. /// <param name="providerInfo">The provider info.</param>
  111. /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
  112. protected override bool NeedsRefreshInternal(BaseItem item, BaseProviderInfo providerInfo)
  113. {
  114. if (string.IsNullOrEmpty(item.GetProviderId(MetadataProviders.Musicbrainz)))
  115. {
  116. return false;
  117. }
  118. if (!ConfigurationManager.Configuration.DownloadMusicArtistImages.Art &&
  119. !ConfigurationManager.Configuration.DownloadMusicArtistImages.Backdrops &&
  120. !ConfigurationManager.Configuration.DownloadMusicArtistImages.Banner &&
  121. !ConfigurationManager.Configuration.DownloadMusicArtistImages.Logo &&
  122. !ConfigurationManager.Configuration.DownloadMusicArtistImages.Primary &&
  123. // The fanart album provider depends on xml downloaded here, so honor it's settings too
  124. !ConfigurationManager.Configuration.DownloadMusicAlbumImages.Disc &&
  125. !ConfigurationManager.Configuration.DownloadMusicAlbumImages.Primary)
  126. {
  127. return false;
  128. }
  129. return base.NeedsRefreshInternal(item, providerInfo);
  130. }
  131. protected override DateTime CompareDate(BaseItem item)
  132. {
  133. var musicBrainzId = item.GetProviderId(MetadataProviders.Musicbrainz);
  134. if (!string.IsNullOrEmpty(musicBrainzId))
  135. {
  136. // Process images
  137. var path = GetArtistDataPath(ConfigurationManager.ApplicationPaths, musicBrainzId);
  138. var files = new DirectoryInfo(path)
  139. .EnumerateFiles("*.xml", SearchOption.TopDirectoryOnly)
  140. .Select(i => i.LastWriteTimeUtc)
  141. .ToArray();
  142. if (files.Length > 0)
  143. {
  144. return files.Max();
  145. }
  146. }
  147. return base.CompareDate(item);
  148. }
  149. /// <summary>
  150. /// The us culture
  151. /// </summary>
  152. protected readonly CultureInfo UsCulture = new CultureInfo("en-US");
  153. /// <summary>
  154. /// Gets the series data path.
  155. /// </summary>
  156. /// <param name="appPaths">The app paths.</param>
  157. /// <param name="musicBrainzArtistId">The music brainz artist id.</param>
  158. /// <returns>System.String.</returns>
  159. internal static string GetArtistDataPath(IApplicationPaths appPaths, string musicBrainzArtistId)
  160. {
  161. var seriesDataPath = Path.Combine(GetArtistDataPath(appPaths), musicBrainzArtistId);
  162. if (!Directory.Exists(seriesDataPath))
  163. {
  164. Directory.CreateDirectory(seriesDataPath);
  165. }
  166. return seriesDataPath;
  167. }
  168. /// <summary>
  169. /// Gets the series data path.
  170. /// </summary>
  171. /// <param name="appPaths">The app paths.</param>
  172. /// <returns>System.String.</returns>
  173. internal static string GetArtistDataPath(IApplicationPaths appPaths)
  174. {
  175. var dataPath = Path.Combine(appPaths.DataPath, "fanart-music");
  176. if (!Directory.Exists(dataPath))
  177. {
  178. Directory.CreateDirectory(dataPath);
  179. }
  180. return dataPath;
  181. }
  182. /// <summary>
  183. /// Fetches metadata and returns true or false indicating if any work that requires persistence was done
  184. /// </summary>
  185. /// <param name="item">The item.</param>
  186. /// <param name="force">if set to <c>true</c> [force].</param>
  187. /// <param name="cancellationToken">The cancellation token.</param>
  188. /// <returns>Task{System.Boolean}.</returns>
  189. public override async Task<bool> FetchAsync(BaseItem item, bool force, CancellationToken cancellationToken)
  190. {
  191. cancellationToken.ThrowIfCancellationRequested();
  192. var musicBrainzId = item.GetProviderId(MetadataProviders.Musicbrainz);
  193. var artistDataPath = GetArtistDataPath(ConfigurationManager.ApplicationPaths, musicBrainzId);
  194. var xmlPath = Path.Combine(artistDataPath, "fanart.xml");
  195. // Only download the xml if it doesn't already exist. The prescan task will take care of getting updates
  196. if (!File.Exists(xmlPath))
  197. {
  198. await DownloadArtistXml(artistDataPath, musicBrainzId, cancellationToken).ConfigureAwait(false);
  199. }
  200. if (ConfigurationManager.Configuration.DownloadMusicArtistImages.Art ||
  201. ConfigurationManager.Configuration.DownloadMusicArtistImages.Backdrops ||
  202. ConfigurationManager.Configuration.DownloadMusicArtistImages.Banner ||
  203. ConfigurationManager.Configuration.DownloadMusicArtistImages.Logo ||
  204. ConfigurationManager.Configuration.DownloadMusicArtistImages.Primary)
  205. {
  206. if (File.Exists(xmlPath))
  207. {
  208. await FetchFromXml(item, xmlPath, cancellationToken).ConfigureAwait(false);
  209. }
  210. }
  211. BaseProviderInfo data;
  212. if (!item.ProviderData.TryGetValue(Id, out data))
  213. {
  214. data = new BaseProviderInfo();
  215. item.ProviderData[Id] = data;
  216. }
  217. SetLastRefreshed(item, DateTime.UtcNow);
  218. return true;
  219. }
  220. /// <summary>
  221. /// Downloads the artist XML.
  222. /// </summary>
  223. /// <param name="artistPath">The artist path.</param>
  224. /// <param name="musicBrainzId">The music brainz id.</param>
  225. /// <param name="cancellationToken">The cancellation token.</param>
  226. /// <returns>Task{System.Boolean}.</returns>
  227. internal async Task DownloadArtistXml(string artistPath, string musicBrainzId, CancellationToken cancellationToken)
  228. {
  229. cancellationToken.ThrowIfCancellationRequested();
  230. var url = string.Format(FanArtBaseUrl, ApiKey, musicBrainzId);
  231. var xmlPath = Path.Combine(artistPath, "fanart.xml");
  232. using (var response = await HttpClient.Get(new HttpRequestOptions
  233. {
  234. Url = url,
  235. ResourcePool = FanArtResourcePool,
  236. CancellationToken = cancellationToken
  237. }).ConfigureAwait(false))
  238. {
  239. using (var xmlFileStream = new FileStream(xmlPath, FileMode.Create, FileAccess.Write, FileShare.Read, StreamDefaults.DefaultFileStreamBufferSize, FileOptions.Asynchronous))
  240. {
  241. await response.CopyToAsync(xmlFileStream).ConfigureAwait(false);
  242. }
  243. }
  244. }
  245. /// <summary>
  246. /// Fetches from XML.
  247. /// </summary>
  248. /// <param name="item">The item.</param>
  249. /// <param name="xmlFilePath">The XML file path.</param>
  250. /// <param name="cancellationToken">The cancellation token.</param>
  251. /// <returns>Task.</returns>
  252. private async Task FetchFromXml(BaseItem item, string xmlFilePath, CancellationToken cancellationToken)
  253. {
  254. var doc = new XmlDocument();
  255. doc.Load(xmlFilePath);
  256. cancellationToken.ThrowIfCancellationRequested();
  257. string path;
  258. var hd = ConfigurationManager.Configuration.DownloadHDFanArt ? "hd" : "";
  259. if (ConfigurationManager.Configuration.DownloadMusicArtistImages.Logo && !item.HasImage(ImageType.Logo))
  260. {
  261. var node =
  262. doc.SelectSingleNode("//fanart/music/musiclogos/" + hd + "musiclogo/@url") ??
  263. doc.SelectSingleNode("//fanart/music/musiclogos/musiclogo/@url");
  264. path = node != null ? node.Value : null;
  265. if (!string.IsNullOrEmpty(path))
  266. {
  267. await _providerManager.SaveImage(item, path, FanArtResourcePool, ImageType.Logo, null, cancellationToken)
  268. .ConfigureAwait(false);
  269. }
  270. }
  271. cancellationToken.ThrowIfCancellationRequested();
  272. if (ConfigurationManager.Configuration.DownloadMusicArtistImages.Backdrops && item.BackdropImagePaths.Count == 0)
  273. {
  274. var nodes = doc.SelectNodes("//fanart/music/artistbackgrounds//@url");
  275. if (nodes != null)
  276. {
  277. var numBackdrops = 0;
  278. item.BackdropImagePaths = new List<string>();
  279. foreach (XmlNode node in nodes)
  280. {
  281. path = node.Value;
  282. if (!string.IsNullOrEmpty(path))
  283. {
  284. await _providerManager.SaveImage(item, path, FanArtResourcePool, ImageType.Backdrop, numBackdrops, cancellationToken)
  285. .ConfigureAwait(false);
  286. numBackdrops++;
  287. if (numBackdrops >= ConfigurationManager.Configuration.MaxBackdrops) break;
  288. }
  289. }
  290. }
  291. }
  292. cancellationToken.ThrowIfCancellationRequested();
  293. if (ConfigurationManager.Configuration.DownloadMusicArtistImages.Art && !item.HasImage(ImageType.Art))
  294. {
  295. var node =
  296. doc.SelectSingleNode("//fanart/music/musicarts/" + hd + "musicart/@url") ??
  297. doc.SelectSingleNode("//fanart/music/musicarts/musicart/@url");
  298. path = node != null ? node.Value : null;
  299. if (!string.IsNullOrEmpty(path))
  300. {
  301. await _providerManager.SaveImage(item, path, FanArtResourcePool, ImageType.Art, null, cancellationToken)
  302. .ConfigureAwait(false);
  303. }
  304. }
  305. cancellationToken.ThrowIfCancellationRequested();
  306. if (ConfigurationManager.Configuration.DownloadMusicArtistImages.Banner && !item.HasImage(ImageType.Banner))
  307. {
  308. var node = doc.SelectSingleNode("//fanart/music/musicbanners/" + hd + "musicbanner/@url") ??
  309. doc.SelectSingleNode("//fanart/music/musicbanners/musicbanner/@url");
  310. path = node != null ? node.Value : null;
  311. if (!string.IsNullOrEmpty(path))
  312. {
  313. await _providerManager.SaveImage(item, path, FanArtResourcePool, ImageType.Banner, null, cancellationToken)
  314. .ConfigureAwait(false);
  315. }
  316. }
  317. cancellationToken.ThrowIfCancellationRequested();
  318. // Artist thumbs are actually primary images (they are square/portrait)
  319. if (ConfigurationManager.Configuration.DownloadMusicArtistImages.Primary && !item.HasImage(ImageType.Primary))
  320. {
  321. var node = doc.SelectSingleNode("//fanart/music/artistthumbs/artistthumb/@url");
  322. path = node != null ? node.Value : null;
  323. if (!string.IsNullOrEmpty(path))
  324. {
  325. await _providerManager.SaveImage(item, path, FanArtResourcePool, ImageType.Primary, null, cancellationToken)
  326. .ConfigureAwait(false);
  327. }
  328. }
  329. }
  330. }
  331. }