FanArtArtistProvider.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. using MediaBrowser.Common.Configuration;
  2. using MediaBrowser.Common.IO;
  3. using MediaBrowser.Common.Net;
  4. using MediaBrowser.Controller.Configuration;
  5. using MediaBrowser.Controller.Entities;
  6. using MediaBrowser.Controller.Entities.Audio;
  7. using MediaBrowser.Controller.Library;
  8. using MediaBrowser.Controller.Providers;
  9. using MediaBrowser.Model.Entities;
  10. using MediaBrowser.Model.Logging;
  11. using MediaBrowser.Model.Providers;
  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. 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. private readonly IFileSystem _fileSystem;
  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, IFileSystem fileSystem)
  46. : base(logManager, configurationManager)
  47. {
  48. if (httpClient == null)
  49. {
  50. throw new ArgumentNullException("httpClient");
  51. }
  52. HttpClient = httpClient;
  53. _providerManager = providerManager;
  54. _fileSystem = fileSystem;
  55. Current = this;
  56. }
  57. /// <summary>
  58. /// The fan art base URL
  59. /// </summary>
  60. protected string FanArtBaseUrl = "http://api.fanart.tv/webservice/artist/{0}/{1}/xml/all/1/1";
  61. public override ItemUpdateType ItemUpdateType
  62. {
  63. get
  64. {
  65. return ItemUpdateType.ImageUpdate;
  66. }
  67. }
  68. /// <summary>
  69. /// Supportses the specified item.
  70. /// </summary>
  71. /// <param name="item">The item.</param>
  72. /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
  73. public override bool Supports(BaseItem item)
  74. {
  75. return item is MusicArtist;
  76. }
  77. /// <summary>
  78. /// Gets a value indicating whether [save local meta].
  79. /// </summary>
  80. /// <value><c>true</c> if [save local meta]; otherwise, <c>false</c>.</value>
  81. protected virtual bool SaveLocalMeta
  82. {
  83. get { return ConfigurationManager.Configuration.SaveLocalMeta; }
  84. }
  85. /// <summary>
  86. /// Gets a value indicating whether [refresh on version change].
  87. /// </summary>
  88. /// <value><c>true</c> if [refresh on version change]; otherwise, <c>false</c>.</value>
  89. protected override bool RefreshOnVersionChange
  90. {
  91. get
  92. {
  93. return true;
  94. }
  95. }
  96. /// <summary>
  97. /// Gets the provider version.
  98. /// </summary>
  99. /// <value>The provider version.</value>
  100. protected override string ProviderVersion
  101. {
  102. get
  103. {
  104. return "7";
  105. }
  106. }
  107. public override MetadataProviderPriority Priority
  108. {
  109. get
  110. {
  111. return MetadataProviderPriority.Fourth;
  112. }
  113. }
  114. /// <summary>
  115. /// Needses the refresh internal.
  116. /// </summary>
  117. /// <param name="item">The item.</param>
  118. /// <param name="providerInfo">The provider info.</param>
  119. /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
  120. protected override bool NeedsRefreshInternal(BaseItem item, BaseProviderInfo providerInfo)
  121. {
  122. if (string.IsNullOrEmpty(item.GetProviderId(MetadataProviders.Musicbrainz)))
  123. {
  124. return false;
  125. }
  126. return base.NeedsRefreshInternal(item, providerInfo);
  127. }
  128. protected override bool NeedsRefreshBasedOnCompareDate(BaseItem item, BaseProviderInfo providerInfo)
  129. {
  130. var musicBrainzId = item.GetProviderId(MetadataProviders.Musicbrainz);
  131. if (!string.IsNullOrEmpty(musicBrainzId))
  132. {
  133. // Process images
  134. var artistXmlPath = GetArtistDataPath(ConfigurationManager.CommonApplicationPaths, musicBrainzId);
  135. artistXmlPath = Path.Combine(artistXmlPath, "fanart.xml");
  136. var file = new FileInfo(artistXmlPath);
  137. return !file.Exists || _fileSystem.GetLastWriteTimeUtc(file) > providerInfo.LastRefreshed;
  138. }
  139. return base.NeedsRefreshBasedOnCompareDate(item, providerInfo);
  140. }
  141. /// <summary>
  142. /// The us culture
  143. /// </summary>
  144. protected readonly CultureInfo UsCulture = new CultureInfo("en-US");
  145. /// <summary>
  146. /// Gets the artist data path.
  147. /// </summary>
  148. /// <param name="appPaths">The application paths.</param>
  149. /// <param name="musicBrainzArtistId">The music brainz artist identifier.</param>
  150. /// <returns>System.String.</returns>
  151. internal static string GetArtistDataPath(IApplicationPaths appPaths, string musicBrainzArtistId)
  152. {
  153. var dataPath = Path.Combine(GetArtistDataPath(appPaths), musicBrainzArtistId);
  154. return dataPath;
  155. }
  156. /// <summary>
  157. /// Gets the artist data path.
  158. /// </summary>
  159. /// <param name="appPaths">The application paths.</param>
  160. /// <returns>System.String.</returns>
  161. internal static string GetArtistDataPath(IApplicationPaths appPaths)
  162. {
  163. var dataPath = Path.Combine(appPaths.DataPath, "fanart-music");
  164. return dataPath;
  165. }
  166. /// <summary>
  167. /// Fetches metadata and returns true or false indicating if any work that requires persistence was done
  168. /// </summary>
  169. /// <param name="item">The item.</param>
  170. /// <param name="force">if set to <c>true</c> [force].</param>
  171. /// <param name="cancellationToken">The cancellation token.</param>
  172. /// <returns>Task{System.Boolean}.</returns>
  173. public override async Task<bool> FetchAsync(BaseItem item, bool force, CancellationToken cancellationToken)
  174. {
  175. cancellationToken.ThrowIfCancellationRequested();
  176. var musicBrainzId = item.GetProviderId(MetadataProviders.Musicbrainz);
  177. var artistDataPath = GetArtistDataPath(ConfigurationManager.ApplicationPaths, musicBrainzId);
  178. var xmlPath = Path.Combine(artistDataPath, "fanart.xml");
  179. // Only download the xml if it doesn't already exist. The prescan task will take care of getting updates
  180. if (!File.Exists(xmlPath))
  181. {
  182. await DownloadArtistXml(artistDataPath, musicBrainzId, cancellationToken).ConfigureAwait(false);
  183. }
  184. if (ConfigurationManager.Configuration.DownloadMusicArtistImages.Art ||
  185. ConfigurationManager.Configuration.DownloadMusicArtistImages.Backdrops ||
  186. ConfigurationManager.Configuration.DownloadMusicArtistImages.Banner ||
  187. ConfigurationManager.Configuration.DownloadMusicArtistImages.Logo ||
  188. ConfigurationManager.Configuration.DownloadMusicArtistImages.Primary)
  189. {
  190. var images = await _providerManager.GetAvailableRemoteImages(item, cancellationToken, ManualFanartArtistProvider.ProviderName).ConfigureAwait(false);
  191. await FetchFromXml(item, images.ToList(), cancellationToken).ConfigureAwait(false);
  192. }
  193. SetLastRefreshed(item, DateTime.UtcNow);
  194. return true;
  195. }
  196. /// <summary>
  197. /// Downloads the artist XML.
  198. /// </summary>
  199. /// <param name="artistPath">The artist path.</param>
  200. /// <param name="musicBrainzId">The music brainz id.</param>
  201. /// <param name="cancellationToken">The cancellation token.</param>
  202. /// <returns>Task{System.Boolean}.</returns>
  203. internal async Task DownloadArtistXml(string artistPath, string musicBrainzId, CancellationToken cancellationToken)
  204. {
  205. cancellationToken.ThrowIfCancellationRequested();
  206. var url = string.Format(FanArtBaseUrl, ApiKey, musicBrainzId);
  207. var xmlPath = Path.Combine(artistPath, "fanart.xml");
  208. Directory.CreateDirectory(artistPath);
  209. using (var response = await HttpClient.Get(new HttpRequestOptions
  210. {
  211. Url = url,
  212. ResourcePool = FanArtResourcePool,
  213. CancellationToken = cancellationToken
  214. }).ConfigureAwait(false))
  215. {
  216. using (var xmlFileStream = _fileSystem.GetFileStream(xmlPath, FileMode.Create, FileAccess.Write, FileShare.Read, true))
  217. {
  218. await response.CopyToAsync(xmlFileStream).ConfigureAwait(false);
  219. }
  220. }
  221. }
  222. /// <summary>
  223. /// Fetches from XML.
  224. /// </summary>
  225. /// <param name="item">The item.</param>
  226. /// <param name="images">The images.</param>
  227. /// <param name="cancellationToken">The cancellation token.</param>
  228. /// <returns>Task.</returns>
  229. private async Task FetchFromXml(BaseItem item, List<RemoteImageInfo> images , CancellationToken cancellationToken)
  230. {
  231. cancellationToken.ThrowIfCancellationRequested();
  232. if (ConfigurationManager.Configuration.DownloadMusicArtistImages.Primary && !item.HasImage(ImageType.Primary))
  233. {
  234. var image = images.FirstOrDefault(i => i.Type == ImageType.Primary);
  235. if (image != null)
  236. {
  237. await _providerManager.SaveImage(item, image.Url, FanArtResourcePool, ImageType.Primary, null, cancellationToken).ConfigureAwait(false);
  238. }
  239. }
  240. cancellationToken.ThrowIfCancellationRequested();
  241. if (ConfigurationManager.Configuration.DownloadMusicArtistImages.Logo && !item.HasImage(ImageType.Logo))
  242. {
  243. var image = images.FirstOrDefault(i => i.Type == ImageType.Logo);
  244. if (image != null)
  245. {
  246. await _providerManager.SaveImage(item, image.Url, FanArtResourcePool, ImageType.Logo, null, cancellationToken).ConfigureAwait(false);
  247. }
  248. }
  249. cancellationToken.ThrowIfCancellationRequested();
  250. if (ConfigurationManager.Configuration.DownloadMusicArtistImages.Art && !item.HasImage(ImageType.Art))
  251. {
  252. var image = images.FirstOrDefault(i => i.Type == ImageType.Art);
  253. if (image != null)
  254. {
  255. await _providerManager.SaveImage(item, image.Url, FanArtResourcePool, ImageType.Art, null, cancellationToken).ConfigureAwait(false);
  256. }
  257. }
  258. cancellationToken.ThrowIfCancellationRequested();
  259. if (ConfigurationManager.Configuration.DownloadMusicArtistImages.Banner && !item.HasImage(ImageType.Banner))
  260. {
  261. var image = images.FirstOrDefault(i => i.Type == ImageType.Banner);
  262. if (image != null)
  263. {
  264. await _providerManager.SaveImage(item, image.Url, FanArtResourcePool, ImageType.Banner, null, cancellationToken).ConfigureAwait(false);
  265. }
  266. }
  267. cancellationToken.ThrowIfCancellationRequested();
  268. var backdropLimit = ConfigurationManager.Configuration.MaxBackdrops;
  269. if (ConfigurationManager.Configuration.DownloadMusicArtistImages.Backdrops &&
  270. item.BackdropImagePaths.Count < backdropLimit)
  271. {
  272. foreach (var image in images.Where(i => i.Type == ImageType.Backdrop))
  273. {
  274. await _providerManager.SaveImage(item, image.Url, FanArtResourcePool, ImageType.Backdrop, null, cancellationToken)
  275. .ConfigureAwait(false);
  276. if (item.BackdropImagePaths.Count >= backdropLimit) break;
  277. }
  278. }
  279. }
  280. }
  281. }