MusicArtist.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. using System.Runtime.Serialization;
  2. using MediaBrowser.Common.Progress;
  3. using MediaBrowser.Controller.Providers;
  4. using MediaBrowser.Model.Configuration;
  5. using MediaBrowser.Model.Entities;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Threading;
  10. using System.Threading.Tasks;
  11. using MediaBrowser.Model.Users;
  12. namespace MediaBrowser.Controller.Entities.Audio
  13. {
  14. /// <summary>
  15. /// Class MusicArtist
  16. /// </summary>
  17. public class MusicArtist : Folder, IMetadataContainer, IItemByName, IHasMusicGenres, IHasDualAccess, IHasProductionLocations, IHasLookupInfo<ArtistInfo>
  18. {
  19. public bool IsAccessedByName { get; set; }
  20. public List<string> ProductionLocations { get; set; }
  21. public override bool IsFolder
  22. {
  23. get
  24. {
  25. return !IsAccessedByName;
  26. }
  27. }
  28. [IgnoreDataMember]
  29. public override bool SupportsAddingToPlaylist
  30. {
  31. get { return true; }
  32. }
  33. protected override IEnumerable<BaseItem> ActualChildren
  34. {
  35. get
  36. {
  37. if (IsAccessedByName)
  38. {
  39. return new List<BaseItem>();
  40. }
  41. return base.ActualChildren;
  42. }
  43. }
  44. private readonly Task _cachedTask = Task.FromResult(true);
  45. protected override Task ValidateChildrenInternal(IProgress<double> progress, CancellationToken cancellationToken, bool recursive, bool refreshChildMetadata, MetadataRefreshOptions refreshOptions, IDirectoryService directoryService)
  46. {
  47. if (IsAccessedByName)
  48. {
  49. // Should never get in here anyway
  50. return _cachedTask;
  51. }
  52. return base.ValidateChildrenInternal(progress, cancellationToken, recursive, refreshChildMetadata, refreshOptions, directoryService);
  53. }
  54. public MusicArtist()
  55. {
  56. ProductionLocations = new List<string>();
  57. }
  58. /// <summary>
  59. /// Gets the user data key.
  60. /// </summary>
  61. /// <returns>System.String.</returns>
  62. protected override string CreateUserDataKey()
  63. {
  64. return GetUserDataKey(this);
  65. }
  66. /// <summary>
  67. /// Returns the folder containing the item.
  68. /// If the item is a folder, it returns the folder itself
  69. /// </summary>
  70. /// <value>The containing folder path.</value>
  71. public override string ContainingFolderPath
  72. {
  73. get
  74. {
  75. return Path;
  76. }
  77. }
  78. /// <summary>
  79. /// Gets a value indicating whether this instance is owned item.
  80. /// </summary>
  81. /// <value><c>true</c> if this instance is owned item; otherwise, <c>false</c>.</value>
  82. public override bool IsOwnedItem
  83. {
  84. get
  85. {
  86. return false;
  87. }
  88. }
  89. /// <summary>
  90. /// Gets the user data key.
  91. /// </summary>
  92. /// <param name="item">The item.</param>
  93. /// <returns>System.String.</returns>
  94. private static string GetUserDataKey(MusicArtist item)
  95. {
  96. var id = item.GetProviderId(MetadataProviders.MusicBrainzArtist);
  97. if (!string.IsNullOrEmpty(id))
  98. {
  99. return "Artist-Musicbrainz-" + id;
  100. }
  101. return "Artist-" + item.Name;
  102. }
  103. protected override bool GetBlockUnratedValue(UserPolicy config)
  104. {
  105. return config.BlockUnratedItems.Contains(UnratedItem.Music);
  106. }
  107. public async Task RefreshAllMetadata(MetadataRefreshOptions refreshOptions, IProgress<double> progress, CancellationToken cancellationToken)
  108. {
  109. var items = GetRecursiveChildren().ToList();
  110. var songs = items.OfType<Audio>().ToList();
  111. var others = items.Except(songs).ToList();
  112. var totalItems = songs.Count + others.Count;
  113. var numComplete = 0;
  114. // Refresh songs
  115. foreach (var item in songs)
  116. {
  117. cancellationToken.ThrowIfCancellationRequested();
  118. await item.RefreshMetadata(refreshOptions, cancellationToken).ConfigureAwait(false);
  119. numComplete++;
  120. double percent = numComplete;
  121. percent /= totalItems;
  122. progress.Report(percent * 100);
  123. }
  124. // Refresh current item
  125. await RefreshMetadata(refreshOptions, cancellationToken).ConfigureAwait(false);
  126. // Refresh all non-songs
  127. foreach (var item in others)
  128. {
  129. cancellationToken.ThrowIfCancellationRequested();
  130. await item.RefreshMetadata(refreshOptions, cancellationToken).ConfigureAwait(false);
  131. numComplete++;
  132. double percent = numComplete;
  133. percent /= totalItems;
  134. progress.Report(percent * 100);
  135. }
  136. progress.Report(100);
  137. }
  138. public ArtistInfo GetLookupInfo()
  139. {
  140. var info = GetItemLookupInfo<ArtistInfo>();
  141. info.SongInfos = GetRecursiveChildren(i => i is Audio)
  142. .Cast<Audio>()
  143. .Select(i => i.GetLookupInfo())
  144. .ToList();
  145. return info;
  146. }
  147. public IEnumerable<BaseItem> GetTaggedItems(IEnumerable<BaseItem> inputItems)
  148. {
  149. return inputItems.Where(ItemFilter);
  150. }
  151. public Func<BaseItem, bool> ItemFilter
  152. {
  153. get
  154. {
  155. return i =>
  156. {
  157. var hasArtist = i as IHasArtist;
  158. return hasArtist != null && hasArtist.HasArtist(Name);
  159. };
  160. }
  161. }
  162. }
  163. }