FanArtArtistProvider.cs 13 KB

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