MusicAlbum.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. return GetParents().OfType<MusicArtist>().FirstOrDefault();
  35. }
  36. }
  37. [IgnoreDataMember]
  38. public List<string> AllArtists
  39. {
  40. get
  41. {
  42. var list = AlbumArtists.ToList();
  43. list.AddRange(Artists);
  44. return list;
  45. }
  46. }
  47. [IgnoreDataMember]
  48. public string AlbumArtist
  49. {
  50. get { return AlbumArtists.FirstOrDefault(); }
  51. }
  52. [IgnoreDataMember]
  53. public override bool SupportsPeople
  54. {
  55. get { return false; }
  56. }
  57. public List<string> AlbumArtists { get; set; }
  58. /// <summary>
  59. /// Gets the tracks.
  60. /// </summary>
  61. /// <value>The tracks.</value>
  62. [IgnoreDataMember]
  63. public IEnumerable<Audio> Tracks
  64. {
  65. get
  66. {
  67. return GetRecursiveChildren(i => i is Audio).Cast<Audio>();
  68. }
  69. }
  70. protected override IEnumerable<BaseItem> GetEligibleChildrenForRecursiveChildren(User user)
  71. {
  72. return Tracks;
  73. }
  74. public List<string> Artists { get; set; }
  75. /// <summary>
  76. /// Gets the user data key.
  77. /// </summary>
  78. /// <returns>System.String.</returns>
  79. protected override string CreateUserDataKey()
  80. {
  81. var id = this.GetProviderId(MetadataProviders.MusicBrainzReleaseGroup);
  82. if (!string.IsNullOrWhiteSpace(id))
  83. {
  84. return "MusicAlbum-MusicBrainzReleaseGroup-" + id;
  85. }
  86. id = this.GetProviderId(MetadataProviders.MusicBrainzAlbum);
  87. if (!string.IsNullOrWhiteSpace(id))
  88. {
  89. return "MusicAlbum-Musicbrainz-" + id;
  90. }
  91. return base.CreateUserDataKey();
  92. }
  93. protected override bool GetBlockUnratedValue(UserPolicy config)
  94. {
  95. return config.BlockUnratedItems.Contains(UnratedItem.Music);
  96. }
  97. public override UnratedItem GetBlockUnratedType()
  98. {
  99. return UnratedItem.Music;
  100. }
  101. public AlbumInfo GetLookupInfo()
  102. {
  103. var id = GetItemLookupInfo<AlbumInfo>();
  104. id.AlbumArtists = AlbumArtists;
  105. var artist = GetParents().OfType<MusicArtist>().FirstOrDefault();
  106. if (artist != null)
  107. {
  108. id.ArtistProviderIds = artist.ProviderIds;
  109. }
  110. id.SongInfos = GetRecursiveChildren(i => i is Audio)
  111. .Cast<Audio>()
  112. .Select(i => i.GetLookupInfo())
  113. .ToList();
  114. var album = id.SongInfos
  115. .Select(i => i.Album)
  116. .FirstOrDefault(i => !string.IsNullOrWhiteSpace(i));
  117. if (!string.IsNullOrWhiteSpace(album))
  118. {
  119. id.Name = album;
  120. }
  121. return id;
  122. }
  123. public async Task RefreshAllMetadata(MetadataRefreshOptions refreshOptions, IProgress<double> progress, CancellationToken cancellationToken)
  124. {
  125. var items = GetRecursiveChildren().ToList();
  126. var songs = items.OfType<Audio>().ToList();
  127. var others = items.Except(songs).ToList();
  128. var totalItems = songs.Count + others.Count;
  129. var numComplete = 0;
  130. var childUpdateType = ItemUpdateType.None;
  131. // Refresh songs
  132. foreach (var item in songs)
  133. {
  134. cancellationToken.ThrowIfCancellationRequested();
  135. var updateType = await item.RefreshMetadata(refreshOptions, cancellationToken).ConfigureAwait(false);
  136. childUpdateType = childUpdateType | updateType;
  137. numComplete++;
  138. double percent = numComplete;
  139. percent /= totalItems;
  140. progress.Report(percent * 100);
  141. }
  142. var parentRefreshOptions = refreshOptions;
  143. if (childUpdateType > ItemUpdateType.None)
  144. {
  145. parentRefreshOptions = new MetadataRefreshOptions(refreshOptions);
  146. parentRefreshOptions.MetadataRefreshMode = MetadataRefreshMode.FullRefresh;
  147. }
  148. // Refresh current item
  149. await RefreshMetadata(parentRefreshOptions, cancellationToken).ConfigureAwait(false);
  150. // Refresh all non-songs
  151. foreach (var item in others)
  152. {
  153. cancellationToken.ThrowIfCancellationRequested();
  154. var updateType = await item.RefreshMetadata(parentRefreshOptions, cancellationToken).ConfigureAwait(false);
  155. numComplete++;
  156. double percent = numComplete;
  157. percent /= totalItems;
  158. progress.Report(percent * 100);
  159. }
  160. progress.Report(100);
  161. }
  162. }
  163. }