FanArtAlbumProvider.cs 3.9 KB

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