AlbumDynamicInfoProvider.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. using MediaBrowser.Controller.Configuration;
  2. using MediaBrowser.Controller.Entities;
  3. using MediaBrowser.Controller.Entities.Audio;
  4. using MediaBrowser.Controller.Providers;
  5. using MediaBrowser.Model.Logging;
  6. using System;
  7. using System.Linq;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10. namespace MediaBrowser.Providers.Music
  11. {
  12. /// <summary>
  13. /// Class MusicAlbumDynamicInfoProvider
  14. /// </summary>
  15. public class AlbumDynamicInfoProvider : BaseMetadataProvider, IDynamicInfoProvider
  16. {
  17. /// <summary>
  18. /// Initializes a new instance of the <see cref="BaseMetadataProvider" /> class.
  19. /// </summary>
  20. /// <param name="logManager">The log manager.</param>
  21. /// <param name="configurationManager">The configuration manager.</param>
  22. public AlbumDynamicInfoProvider(ILogManager logManager, IServerConfigurationManager configurationManager)
  23. : base(logManager, configurationManager)
  24. {
  25. }
  26. /// <summary>
  27. /// Supportses the specified item.
  28. /// </summary>
  29. /// <param name="item">The item.</param>
  30. /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
  31. public override bool Supports(BaseItem item)
  32. {
  33. return item is MusicAlbum;
  34. }
  35. /// <summary>
  36. /// Needses the refresh internal.
  37. /// </summary>
  38. /// <param name="item">The item.</param>
  39. /// <param name="providerInfo">The provider info.</param>
  40. /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
  41. protected override bool NeedsRefreshInternal(BaseItem item, BaseProviderInfo providerInfo)
  42. {
  43. return true;
  44. }
  45. /// <summary>
  46. /// Fetches metadata and returns true or false indicating if any work that requires persistence was done
  47. /// </summary>
  48. /// <param name="item">The item.</param>
  49. /// <param name="force">if set to <c>true</c> [force].</param>
  50. /// <param name="cancellationToken">The cancellation token.</param>
  51. /// <returns>Task{System.Boolean}.</returns>
  52. public override Task<bool> FetchAsync(BaseItem item, bool force, CancellationToken cancellationToken)
  53. {
  54. var album = (MusicAlbum)item;
  55. var songs = album.RecursiveChildren
  56. .OfType<Audio>()
  57. .ToList();
  58. album.AlbumArtist = songs
  59. .Select(i => i.AlbumArtist)
  60. .FirstOrDefault(i => !string.IsNullOrEmpty(i));
  61. album.Artists = songs.SelectMany(i => i.Artists)
  62. .Distinct(StringComparer.OrdinalIgnoreCase)
  63. .ToList();
  64. var date = songs.Select(i => i.PremiereDate)
  65. .FirstOrDefault(i => i.HasValue);
  66. if (date.HasValue)
  67. {
  68. album.PremiereDate = date.Value;
  69. album.ProductionYear = date.Value.Year;
  70. }
  71. else
  72. {
  73. var year = songs.Select(i => i.ProductionYear ?? 1800).FirstOrDefault(i => i != 1800);
  74. if (year != 1800)
  75. {
  76. album.ProductionYear = year;
  77. }
  78. }
  79. // Don't save to the db
  80. return FalseTaskResult;
  81. }
  82. /// <summary>
  83. /// Gets the priority.
  84. /// </summary>
  85. /// <value>The priority.</value>
  86. public override MetadataProviderPriority Priority
  87. {
  88. get { return MetadataProviderPriority.Last; }
  89. }
  90. }
  91. }