FanArtArtistProvider.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. using MediaBrowser.Common.Net;
  2. using MediaBrowser.Controller.Configuration;
  3. using MediaBrowser.Controller.Entities;
  4. using MediaBrowser.Controller.Entities.Audio;
  5. using MediaBrowser.Model.Entities;
  6. using MediaBrowser.Model.Logging;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Globalization;
  10. using System.Threading;
  11. using System.Threading.Tasks;
  12. using System.Xml;
  13. namespace MediaBrowser.Controller.Providers.Music
  14. {
  15. /// <summary>
  16. /// Class FanArtArtistProvider
  17. /// </summary>
  18. public class FanArtArtistProvider : FanartBaseProvider
  19. {
  20. /// <summary>
  21. /// Gets the HTTP client.
  22. /// </summary>
  23. /// <value>The HTTP client.</value>
  24. protected IHttpClient HttpClient { get; private set; }
  25. private readonly IProviderManager _providerManager;
  26. public FanArtArtistProvider(IHttpClient httpClient, ILogManager logManager, IServerConfigurationManager configurationManager, IProviderManager providerManager)
  27. : base(logManager, configurationManager)
  28. {
  29. if (httpClient == null)
  30. {
  31. throw new ArgumentNullException("httpClient");
  32. }
  33. HttpClient = httpClient;
  34. _providerManager = providerManager;
  35. }
  36. /// <summary>
  37. /// The fan art base URL
  38. /// </summary>
  39. protected string FanArtBaseUrl = "http://api.fanart.tv/webservice/artist/{0}/{1}/xml/all/1/1";
  40. /// <summary>
  41. /// Supportses the specified item.
  42. /// </summary>
  43. /// <param name="item">The item.</param>
  44. /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
  45. public override bool Supports(BaseItem item)
  46. {
  47. return item is MusicArtist;
  48. }
  49. protected virtual bool SaveLocalMeta
  50. {
  51. get { return ConfigurationManager.Configuration.SaveLocalMeta; }
  52. }
  53. protected override bool RefreshOnVersionChange
  54. {
  55. get
  56. {
  57. return true;
  58. }
  59. }
  60. protected override string ProviderVersion
  61. {
  62. get
  63. {
  64. return "4";
  65. }
  66. }
  67. /// <summary>
  68. /// Needses the refresh internal.
  69. /// </summary>
  70. /// <param name="item">The item.</param>
  71. /// <param name="providerInfo">The provider info.</param>
  72. /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
  73. protected override bool NeedsRefreshInternal(BaseItem item, BaseProviderInfo providerInfo)
  74. {
  75. if (string.IsNullOrEmpty(item.GetProviderId(MetadataProviders.Musicbrainz)))
  76. {
  77. return false;
  78. }
  79. if (!ConfigurationManager.Configuration.DownloadMusicArtistImages.Art &&
  80. !ConfigurationManager.Configuration.DownloadMusicArtistImages.Backdrops &&
  81. !ConfigurationManager.Configuration.DownloadMusicArtistImages.Banner &&
  82. !ConfigurationManager.Configuration.DownloadMusicArtistImages.Logo &&
  83. !ConfigurationManager.Configuration.DownloadMusicArtistImages.Primary)
  84. {
  85. return false;
  86. }
  87. return base.NeedsRefreshInternal(item, providerInfo);
  88. }
  89. protected readonly CultureInfo UsCulture = new CultureInfo("en-US");
  90. /// <summary>
  91. /// Fetches metadata and returns true or false indicating if any work that requires persistence was done
  92. /// </summary>
  93. /// <param name="item">The item.</param>
  94. /// <param name="force">if set to <c>true</c> [force].</param>
  95. /// <param name="cancellationToken">The cancellation token.</param>
  96. /// <returns>Task{System.Boolean}.</returns>
  97. public override async Task<bool> FetchAsync(BaseItem item, bool force, CancellationToken cancellationToken)
  98. {
  99. cancellationToken.ThrowIfCancellationRequested();
  100. //var artist = item;
  101. var url = string.Format(FanArtBaseUrl, ApiKey, item.GetProviderId(MetadataProviders.Musicbrainz));
  102. var doc = new XmlDocument();
  103. var status = ProviderRefreshStatus.Success;
  104. using (var xml = await HttpClient.Get(new HttpRequestOptions
  105. {
  106. Url = url,
  107. ResourcePool = FanArtResourcePool,
  108. CancellationToken = cancellationToken
  109. }).ConfigureAwait(false))
  110. {
  111. doc.Load(xml);
  112. }
  113. cancellationToken.ThrowIfCancellationRequested();
  114. if (doc.HasChildNodes)
  115. {
  116. string path;
  117. var hd = ConfigurationManager.Configuration.DownloadHDFanArt ? "hd" : "";
  118. if (ConfigurationManager.Configuration.DownloadMusicArtistImages.Logo && !item.ResolveArgs.ContainsMetaFileByName(LogoFile))
  119. {
  120. var node =
  121. doc.SelectSingleNode("//fanart/music/musiclogos/" + hd + "musiclogo/@url") ??
  122. doc.SelectSingleNode("//fanart/music/musiclogos/musiclogo/@url");
  123. path = node != null ? node.Value : null;
  124. if (!string.IsNullOrEmpty(path))
  125. {
  126. Logger.Debug("FanArtProvider getting ClearLogo for " + item.Name);
  127. item.SetImage(ImageType.Logo, await _providerManager.DownloadAndSaveImage(item, path, LogoFile, SaveLocalMeta, FanArtResourcePool, cancellationToken).ConfigureAwait(false));
  128. }
  129. }
  130. cancellationToken.ThrowIfCancellationRequested();
  131. if (ConfigurationManager.Configuration.DownloadMusicArtistImages.Backdrops && !item.ResolveArgs.ContainsMetaFileByName(BackdropFile))
  132. {
  133. var nodes = doc.SelectNodes("//fanart/music/artistbackgrounds//@url");
  134. if (nodes != null)
  135. {
  136. var numBackdrops = 0;
  137. item.BackdropImagePaths = new List<string>();
  138. foreach (XmlNode node in nodes)
  139. {
  140. path = node.Value;
  141. if (!string.IsNullOrEmpty(path))
  142. {
  143. Logger.Debug("FanArtProvider getting Backdrop for " + item.Name);
  144. item.BackdropImagePaths.Add(await _providerManager.DownloadAndSaveImage(item, path, ("Backdrop" + (numBackdrops > 0 ? numBackdrops.ToString(UsCulture) : "") + ".jpg"), SaveLocalMeta, FanArtResourcePool, cancellationToken).ConfigureAwait(false));
  145. numBackdrops++;
  146. if (numBackdrops >= ConfigurationManager.Configuration.MaxBackdrops) break;
  147. }
  148. }
  149. }
  150. }
  151. cancellationToken.ThrowIfCancellationRequested();
  152. if (ConfigurationManager.Configuration.DownloadMusicArtistImages.Art && !item.HasImage(ImageType.Art))
  153. {
  154. var node =
  155. doc.SelectSingleNode("//fanart/music/musicarts/" + hd + "musicart/@url") ??
  156. doc.SelectSingleNode("//fanart/music/musicarts/musicart/@url");
  157. path = node != null ? node.Value : null;
  158. if (!string.IsNullOrEmpty(path))
  159. {
  160. Logger.Debug("FanArtProvider getting ClearArt for " + item.Name);
  161. item.SetImage(ImageType.Art, await _providerManager.DownloadAndSaveImage(item, path, ArtFile, SaveLocalMeta, FanArtResourcePool, cancellationToken).ConfigureAwait(false));
  162. }
  163. }
  164. cancellationToken.ThrowIfCancellationRequested();
  165. if (ConfigurationManager.Configuration.DownloadMusicArtistImages.Banner && !item.HasImage(ImageType.Banner))
  166. {
  167. var node = doc.SelectSingleNode("//fanart/music/musicbanners/" + hd + "musicbanner/@url") ??
  168. doc.SelectSingleNode("//fanart/music/musicbanners/musicbanner/@url");
  169. path = node != null ? node.Value : null;
  170. if (!string.IsNullOrEmpty(path))
  171. {
  172. Logger.Debug("FanArtProvider getting Banner for " + item.Name);
  173. item.SetImage(ImageType.Banner, await _providerManager.DownloadAndSaveImage(item, path, BannerFile, SaveLocalMeta, FanArtResourcePool, cancellationToken).ConfigureAwait(false));
  174. }
  175. }
  176. cancellationToken.ThrowIfCancellationRequested();
  177. // Artist thumbs are actually primary images (they are square/portrait)
  178. if (ConfigurationManager.Configuration.DownloadMusicArtistImages.Primary && !item.HasImage(ImageType.Primary))
  179. {
  180. var node = doc.SelectSingleNode("//fanart/music/artistthumbs/artistthumb/@url");
  181. path = node != null ? node.Value : null;
  182. if (!string.IsNullOrEmpty(path))
  183. {
  184. Logger.Debug("FanArtProvider getting Primary image for " + item.Name);
  185. item.SetImage(ImageType.Primary, await _providerManager.DownloadAndSaveImage(item, path, PrimaryFile, SaveLocalMeta, FanArtResourcePool, cancellationToken).ConfigureAwait(false));
  186. }
  187. }
  188. }
  189. SetLastRefreshed(item, DateTime.UtcNow, status);
  190. return true;
  191. }
  192. }
  193. }