MusicAlbum.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. /// <summary>
  85. /// Gets the user data key.
  86. /// </summary>
  87. /// <returns>System.String.</returns>
  88. protected override string CreateUserDataKey()
  89. {
  90. var id = this.GetProviderId(MetadataProviders.MusicBrainzReleaseGroup);
  91. if (!string.IsNullOrWhiteSpace(id))
  92. {
  93. return "MusicAlbum-MusicBrainzReleaseGroup-" + id;
  94. }
  95. id = this.GetProviderId(MetadataProviders.MusicBrainzAlbum);
  96. if (!string.IsNullOrWhiteSpace(id))
  97. {
  98. return "MusicAlbum-Musicbrainz-" + id;
  99. }
  100. if (ConfigurationManager.Configuration.EnableStandaloneMusicKeys)
  101. {
  102. var albumArtist = AlbumArtist;
  103. if (!string.IsNullOrWhiteSpace(albumArtist))
  104. {
  105. return albumArtist + "-" + Name;
  106. }
  107. }
  108. return base.CreateUserDataKey();
  109. }
  110. protected override bool GetBlockUnratedValue(UserPolicy config)
  111. {
  112. return config.BlockUnratedItems.Contains(UnratedItem.Music);
  113. }
  114. public override UnratedItem GetBlockUnratedType()
  115. {
  116. return UnratedItem.Music;
  117. }
  118. public AlbumInfo GetLookupInfo()
  119. {
  120. var id = GetItemLookupInfo<AlbumInfo>();
  121. id.AlbumArtists = AlbumArtists;
  122. var artist = MusicArtist;
  123. if (artist != null)
  124. {
  125. id.ArtistProviderIds = artist.ProviderIds;
  126. }
  127. id.SongInfos = GetRecursiveChildren(i => i is Audio)
  128. .Cast<Audio>()
  129. .Select(i => i.GetLookupInfo())
  130. .ToList();
  131. var album = id.SongInfos
  132. .Select(i => i.Album)
  133. .FirstOrDefault(i => !string.IsNullOrWhiteSpace(i));
  134. if (!string.IsNullOrWhiteSpace(album))
  135. {
  136. id.Name = album;
  137. }
  138. return id;
  139. }
  140. public async Task RefreshAllMetadata(MetadataRefreshOptions refreshOptions, IProgress<double> progress, CancellationToken cancellationToken)
  141. {
  142. var items = GetRecursiveChildren().ToList();
  143. var songs = items.OfType<Audio>().ToList();
  144. var others = items.Except(songs).ToList();
  145. var totalItems = songs.Count + others.Count;
  146. var numComplete = 0;
  147. var childUpdateType = ItemUpdateType.None;
  148. // Refresh songs
  149. foreach (var item in songs)
  150. {
  151. cancellationToken.ThrowIfCancellationRequested();
  152. var updateType = await item.RefreshMetadata(refreshOptions, cancellationToken).ConfigureAwait(false);
  153. childUpdateType = childUpdateType | updateType;
  154. numComplete++;
  155. double percent = numComplete;
  156. percent /= totalItems;
  157. progress.Report(percent * 100);
  158. }
  159. var parentRefreshOptions = refreshOptions;
  160. if (childUpdateType > ItemUpdateType.None)
  161. {
  162. parentRefreshOptions = new MetadataRefreshOptions(refreshOptions);
  163. parentRefreshOptions.MetadataRefreshMode = MetadataRefreshMode.FullRefresh;
  164. }
  165. // Refresh current item
  166. await RefreshMetadata(parentRefreshOptions, cancellationToken).ConfigureAwait(false);
  167. // Refresh all non-songs
  168. foreach (var item in others)
  169. {
  170. cancellationToken.ThrowIfCancellationRequested();
  171. var updateType = await item.RefreshMetadata(parentRefreshOptions, cancellationToken).ConfigureAwait(false);
  172. numComplete++;
  173. double percent = numComplete;
  174. percent /= totalItems;
  175. progress.Report(percent * 100);
  176. }
  177. progress.Report(100);
  178. }
  179. }
  180. }