FanArtAlbumProvider.cs 3.3 KB

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