FanArtArtistProvider.cs 15 KB

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