MusicAlbum.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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 MediaBrowser.Model.Serialization;
  9. using System.Threading;
  10. using System.Threading.Tasks;
  11. using MediaBrowser.Controller.Dto;
  12. using MediaBrowser.Controller.Library;
  13. using MediaBrowser.Model.Dto;
  14. namespace MediaBrowser.Controller.Entities.Audio
  15. {
  16. /// <summary>
  17. /// Class MusicAlbum
  18. /// </summary>
  19. public class MusicAlbum : Folder, IHasAlbumArtist, IHasArtist, IHasMusicGenres, IHasLookupInfo<AlbumInfo>, IMetadataContainer
  20. {
  21. public List<string> AlbumArtists { get; set; }
  22. public List<string> Artists { get; set; }
  23. public MusicAlbum()
  24. {
  25. Artists = new List<string>();
  26. AlbumArtists = new List<string>();
  27. }
  28. [IgnoreDataMember]
  29. public override bool SupportsAddingToPlaylist
  30. {
  31. get { return true; }
  32. }
  33. [IgnoreDataMember]
  34. public override bool SupportsInheritedParentImages
  35. {
  36. get { return true; }
  37. }
  38. [IgnoreDataMember]
  39. public MusicArtist MusicArtist
  40. {
  41. get { return GetMusicArtist(new DtoOptions(true)); }
  42. }
  43. public MusicArtist GetMusicArtist(DtoOptions options)
  44. {
  45. var artist = GetParents().OfType<MusicArtist>().FirstOrDefault();
  46. if (artist == null)
  47. {
  48. var name = AlbumArtist;
  49. if (!string.IsNullOrWhiteSpace(name))
  50. {
  51. artist = LibraryManager.GetArtist(name, options);
  52. }
  53. }
  54. return artist;
  55. }
  56. [IgnoreDataMember]
  57. public override bool SupportsPlayedStatus
  58. {
  59. get
  60. {
  61. return false;
  62. }
  63. }
  64. [IgnoreDataMember]
  65. public override bool SupportsCumulativeRunTimeTicks
  66. {
  67. get
  68. {
  69. return true;
  70. }
  71. }
  72. [IgnoreDataMember]
  73. public List<string> AllArtists
  74. {
  75. get
  76. {
  77. var list = AlbumArtists.ToList();
  78. list.AddRange(Artists);
  79. return list;
  80. }
  81. }
  82. [IgnoreDataMember]
  83. public string AlbumArtist
  84. {
  85. get { return AlbumArtists.FirstOrDefault(); }
  86. }
  87. [IgnoreDataMember]
  88. public override bool SupportsPeople
  89. {
  90. get { return false; }
  91. }
  92. /// <summary>
  93. /// Gets the tracks.
  94. /// </summary>
  95. /// <value>The tracks.</value>
  96. [IgnoreDataMember]
  97. public IEnumerable<Audio> Tracks
  98. {
  99. get
  100. {
  101. return GetRecursiveChildren(i => i is Audio).Cast<Audio>();
  102. }
  103. }
  104. protected override IEnumerable<BaseItem> GetEligibleChildrenForRecursiveChildren(User user)
  105. {
  106. return Tracks;
  107. }
  108. public override double? GetDefaultPrimaryImageAspectRatio()
  109. {
  110. return 1;
  111. }
  112. public override List<string> GetUserDataKeys()
  113. {
  114. var list = base.GetUserDataKeys();
  115. if (ConfigurationManager.Configuration.EnableStandaloneMusicKeys)
  116. {
  117. var albumArtist = AlbumArtist;
  118. if (!string.IsNullOrWhiteSpace(albumArtist))
  119. {
  120. list.Insert(0, albumArtist + "-" + Name);
  121. }
  122. }
  123. var id = this.GetProviderId(MetadataProviders.MusicBrainzAlbum);
  124. if (!string.IsNullOrWhiteSpace(id))
  125. {
  126. list.Insert(0, "MusicAlbum-Musicbrainz-" + id);
  127. }
  128. id = this.GetProviderId(MetadataProviders.MusicBrainzReleaseGroup);
  129. if (!string.IsNullOrWhiteSpace(id))
  130. {
  131. list.Insert(0, "MusicAlbum-MusicBrainzReleaseGroup-" + id);
  132. }
  133. return list;
  134. }
  135. protected override bool GetBlockUnratedValue(UserPolicy config)
  136. {
  137. return config.BlockUnratedItems.Contains(UnratedItem.Music);
  138. }
  139. public override UnratedItem GetBlockUnratedType()
  140. {
  141. return UnratedItem.Music;
  142. }
  143. public AlbumInfo GetLookupInfo()
  144. {
  145. var id = GetItemLookupInfo<AlbumInfo>();
  146. id.AlbumArtists = AlbumArtists;
  147. var artist = GetMusicArtist(new DtoOptions(false));
  148. if (artist != null)
  149. {
  150. id.ArtistProviderIds = artist.ProviderIds;
  151. }
  152. id.SongInfos = GetRecursiveChildren(i => i is Audio)
  153. .Cast<Audio>()
  154. .Select(i => i.GetLookupInfo())
  155. .ToList();
  156. var album = id.SongInfos
  157. .Select(i => i.Album)
  158. .FirstOrDefault(i => !string.IsNullOrWhiteSpace(i));
  159. if (!string.IsNullOrWhiteSpace(album))
  160. {
  161. id.Name = album;
  162. }
  163. return id;
  164. }
  165. public async Task RefreshAllMetadata(MetadataRefreshOptions refreshOptions, IProgress<double> progress, CancellationToken cancellationToken)
  166. {
  167. var items = GetRecursiveChildren().ToList();
  168. var totalItems = items.Count;
  169. var numComplete = 0;
  170. var childUpdateType = ItemUpdateType.None;
  171. // Refresh songs
  172. foreach (var item in items)
  173. {
  174. cancellationToken.ThrowIfCancellationRequested();
  175. var updateType = await item.RefreshMetadata(refreshOptions, cancellationToken).ConfigureAwait(false);
  176. childUpdateType = childUpdateType | updateType;
  177. numComplete++;
  178. double percent = numComplete;
  179. percent /= totalItems;
  180. progress.Report(percent * 95);
  181. }
  182. var parentRefreshOptions = refreshOptions;
  183. if (childUpdateType > ItemUpdateType.None)
  184. {
  185. parentRefreshOptions = new MetadataRefreshOptions(refreshOptions);
  186. parentRefreshOptions.MetadataRefreshMode = MetadataRefreshMode.FullRefresh;
  187. }
  188. // Refresh current item
  189. await RefreshMetadata(parentRefreshOptions, cancellationToken).ConfigureAwait(false);
  190. if (!refreshOptions.IsAutomated)
  191. {
  192. await RefreshArtists(refreshOptions, cancellationToken).ConfigureAwait(false);
  193. }
  194. progress.Report(100);
  195. }
  196. private async Task RefreshArtists(MetadataRefreshOptions refreshOptions, CancellationToken cancellationToken)
  197. {
  198. var artists = AllArtists.Select(i =>
  199. {
  200. // This should not be necessary but we're seeing some cases of it
  201. if (string.IsNullOrWhiteSpace(i))
  202. {
  203. return null;
  204. }
  205. var artist = LibraryManager.GetArtist(i);
  206. if (!artist.IsAccessedByName)
  207. {
  208. return null;
  209. }
  210. return artist;
  211. }).Where(i => i != null).ToList();
  212. foreach (var artist in artists)
  213. {
  214. await artist.RefreshMetadata(refreshOptions, cancellationToken).ConfigureAwait(false);
  215. }
  216. }
  217. }
  218. }