FanArtAlbumProvider.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. using MediaBrowser.Common.Extensions;
  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.Threading;
  9. using System.Threading.Tasks;
  10. namespace MediaBrowser.Controller.Providers.Music
  11. {
  12. public class FanArtAlbumProvider : FanartBaseProvider
  13. {
  14. private readonly IProviderManager _providerManager;
  15. public FanArtAlbumProvider(ILogManager logManager, IServerConfigurationManager configurationManager, IProviderManager providerManager)
  16. : base(logManager, configurationManager)
  17. {
  18. _providerManager = providerManager;
  19. }
  20. public override bool Supports(BaseItem item)
  21. {
  22. return item is MusicAlbum && item.Parent is MusicArtist;
  23. }
  24. /// <summary>
  25. /// Needses the refresh internal.
  26. /// </summary>
  27. /// <param name="item">The item.</param>
  28. /// <param name="providerInfo">The provider info.</param>
  29. /// <returns><c>true</c> if we need refreshing, <c>false</c> otherwise</returns>
  30. protected override bool NeedsRefreshInternal(BaseItem item, BaseProviderInfo providerInfo)
  31. {
  32. //we fetch if image needed and haven't already tried recently
  33. return string.IsNullOrEmpty(item.PrimaryImagePath) &&
  34. DateTime.Today.Subtract(providerInfo.LastRefreshed).TotalDays > ConfigurationManager.Configuration.MetadataRefreshDays;
  35. }
  36. public override async Task<bool> FetchAsync(BaseItem item, bool force, CancellationToken cancellationToken)
  37. {
  38. var mbid = item.GetProviderId(MetadataProviders.Musicbrainz);
  39. if (mbid == null)
  40. {
  41. Logger.Warn("No Musicbrainz id associated with album {0}", item.Name);
  42. SetLastRefreshed(item, DateTime.UtcNow, ProviderRefreshStatus.CompletedWithErrors);
  43. return false;
  44. }
  45. cancellationToken.ThrowIfCancellationRequested();
  46. //Look at our parent for our album cover
  47. var artist = (MusicArtist)item.Parent;
  48. var cover = artist.AlbumCovers != null ? artist.AlbumCovers.GetValueOrDefault(mbid, null) : null;
  49. if (cover == null)
  50. {
  51. // Not there - maybe it is new since artist last refreshed so refresh it and try again
  52. await artist.RefreshMetadata(cancellationToken).ConfigureAwait(false);
  53. cancellationToken.ThrowIfCancellationRequested();
  54. cover = artist.AlbumCovers != null ? artist.AlbumCovers.GetValueOrDefault(mbid, null) : null;
  55. }
  56. if (cover == null)
  57. {
  58. Logger.Warn("Unable to find cover art for {0}", item.Name);
  59. SetLastRefreshed(item, DateTime.UtcNow, ProviderRefreshStatus.CompletedWithErrors);
  60. return false;
  61. }
  62. item.SetImage(ImageType.Primary, await _providerManager.DownloadAndSaveImage(item, cover, "folder.jpg", FanArtResourcePool, cancellationToken).ConfigureAwait(false));
  63. return true;
  64. }
  65. }
  66. }