MusicAlbum.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. using System;
  2. using MediaBrowser.Controller.Providers;
  3. using MediaBrowser.Model.Configuration;
  4. using MediaBrowser.Model.Entities;
  5. using MediaBrowser.Model.Users;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Runtime.Serialization;
  9. using System.Threading;
  10. using System.Threading.Tasks;
  11. using MediaBrowser.Controller.Library;
  12. namespace MediaBrowser.Controller.Entities.Audio
  13. {
  14. /// <summary>
  15. /// Class MusicAlbum
  16. /// </summary>
  17. public class MusicAlbum : Folder, IHasAlbumArtist, IHasArtist, IHasMusicGenres, IHasLookupInfo<AlbumInfo>, IMetadataContainer
  18. {
  19. public MusicAlbum()
  20. {
  21. Artists = new List<string>();
  22. AlbumArtists = new List<string>();
  23. }
  24. [IgnoreDataMember]
  25. public override bool SupportsAddingToPlaylist
  26. {
  27. get { return true; }
  28. }
  29. [IgnoreDataMember]
  30. public MusicArtist MusicArtist
  31. {
  32. get
  33. {
  34. var artist = GetParents().OfType<MusicArtist>().FirstOrDefault();
  35. if (artist == null)
  36. {
  37. var name = AlbumArtist;
  38. if (!string.IsNullOrWhiteSpace(name))
  39. {
  40. artist = LibraryManager.GetArtist(name);
  41. }
  42. }
  43. return artist;
  44. }
  45. }
  46. [IgnoreDataMember]
  47. public List<string> AllArtists
  48. {
  49. get
  50. {
  51. var list = AlbumArtists.ToList();
  52. list.AddRange(Artists);
  53. return list;
  54. }
  55. }
  56. [IgnoreDataMember]
  57. public string AlbumArtist
  58. {
  59. get { return AlbumArtists.FirstOrDefault(); }
  60. }
  61. [IgnoreDataMember]
  62. public override bool SupportsPeople
  63. {
  64. get { return false; }
  65. }
  66. public List<string> AlbumArtists { get; set; }
  67. /// <summary>
  68. /// Gets the tracks.
  69. /// </summary>
  70. /// <value>The tracks.</value>
  71. [IgnoreDataMember]
  72. public IEnumerable<Audio> Tracks
  73. {
  74. get
  75. {
  76. return GetRecursiveChildren(i => i is Audio).Cast<Audio>();
  77. }
  78. }
  79. protected override IEnumerable<BaseItem> GetEligibleChildrenForRecursiveChildren(User user)
  80. {
  81. return Tracks;
  82. }
  83. public List<string> Artists { get; set; }
  84. public override List<string> GetUserDataKeys()
  85. {
  86. var list = base.GetUserDataKeys();
  87. if (ConfigurationManager.Configuration.EnableStandaloneMusicKeys)
  88. {
  89. var albumArtist = AlbumArtist;
  90. if (!string.IsNullOrWhiteSpace(albumArtist))
  91. {
  92. list.Insert(0, albumArtist + "-" + Name);
  93. }
  94. }
  95. var id = this.GetProviderId(MetadataProviders.MusicBrainzAlbum);
  96. if (!string.IsNullOrWhiteSpace(id))
  97. {
  98. list.Insert(0, "MusicAlbum-Musicbrainz-" + id);
  99. }
  100. id = this.GetProviderId(MetadataProviders.MusicBrainzReleaseGroup);
  101. if (!string.IsNullOrWhiteSpace(id))
  102. {
  103. list.Insert(0, "MusicAlbum-MusicBrainzReleaseGroup-" + id);
  104. }
  105. return list;
  106. }
  107. protected override bool GetBlockUnratedValue(UserPolicy config)
  108. {
  109. return config.BlockUnratedItems.Contains(UnratedItem.Music);
  110. }
  111. public override UnratedItem GetBlockUnratedType()
  112. {
  113. return UnratedItem.Music;
  114. }
  115. public AlbumInfo GetLookupInfo()
  116. {
  117. var id = GetItemLookupInfo<AlbumInfo>();
  118. id.AlbumArtists = AlbumArtists;
  119. var artist = MusicArtist;
  120. if (artist != null)
  121. {
  122. id.ArtistProviderIds = artist.ProviderIds;
  123. }
  124. id.SongInfos = GetRecursiveChildren(i => i is Audio)
  125. .Cast<Audio>()
  126. .Select(i => i.GetLookupInfo())
  127. .ToList();
  128. var album = id.SongInfos
  129. .Select(i => i.Album)
  130. .FirstOrDefault(i => !string.IsNullOrWhiteSpace(i));
  131. if (!string.IsNullOrWhiteSpace(album))
  132. {
  133. id.Name = album;
  134. }
  135. return id;
  136. }
  137. public async Task RefreshAllMetadata(MetadataRefreshOptions refreshOptions, IProgress<double> progress, CancellationToken cancellationToken)
  138. {
  139. var items = GetRecursiveChildren().ToList();
  140. var songs = items.OfType<Audio>().ToList();
  141. var others = items.Except(songs).ToList();
  142. var totalItems = songs.Count + others.Count;
  143. var numComplete = 0;
  144. var childUpdateType = ItemUpdateType.None;
  145. // Refresh songs
  146. foreach (var item in songs)
  147. {
  148. cancellationToken.ThrowIfCancellationRequested();
  149. var updateType = await item.RefreshMetadata(refreshOptions, cancellationToken).ConfigureAwait(false);
  150. childUpdateType = childUpdateType | updateType;
  151. numComplete++;
  152. double percent = numComplete;
  153. percent /= totalItems;
  154. progress.Report(percent * 100);
  155. }
  156. var parentRefreshOptions = refreshOptions;
  157. if (childUpdateType > ItemUpdateType.None)
  158. {
  159. parentRefreshOptions = new MetadataRefreshOptions(refreshOptions);
  160. parentRefreshOptions.MetadataRefreshMode = MetadataRefreshMode.FullRefresh;
  161. }
  162. // Refresh current item
  163. await RefreshMetadata(parentRefreshOptions, cancellationToken).ConfigureAwait(false);
  164. // Refresh all non-songs
  165. foreach (var item in others)
  166. {
  167. cancellationToken.ThrowIfCancellationRequested();
  168. var updateType = await item.RefreshMetadata(parentRefreshOptions, cancellationToken).ConfigureAwait(false);
  169. numComplete++;
  170. double percent = numComplete;
  171. percent /= totalItems;
  172. progress.Report(percent * 100);
  173. }
  174. progress.Report(100);
  175. }
  176. }
  177. }