MusicArtist.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. public override string GetUserDataKey()
  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 = RecursiveChildren.ToList();
  110. var songs = items.OfType<Audio>().ToList();
  111. var others = items.Except(songs).ToList();
  112. var totalItems = songs.Count + others.Count;
  113. var percentages = new Dictionary<Guid, double>(totalItems);
  114. var tasks = new List<Task>();
  115. // Refresh songs
  116. foreach (var item in songs)
  117. {
  118. if (tasks.Count >= 2)
  119. {
  120. await Task.WhenAll(tasks).ConfigureAwait(false);
  121. tasks.Clear();
  122. }
  123. cancellationToken.ThrowIfCancellationRequested();
  124. var innerProgress = new ActionableProgress<double>();
  125. // Avoid implicitly captured closure
  126. var currentChild = item;
  127. innerProgress.RegisterAction(p =>
  128. {
  129. lock (percentages)
  130. {
  131. percentages[currentChild.Id] = p / 100;
  132. var percent = percentages.Values.Sum();
  133. percent /= totalItems;
  134. percent *= 100;
  135. progress.Report(percent);
  136. }
  137. });
  138. var taskChild = item;
  139. tasks.Add(Task.Run(async () => await RefreshItem(taskChild, refreshOptions, innerProgress, cancellationToken).ConfigureAwait(false), cancellationToken));
  140. }
  141. await Task.WhenAll(tasks).ConfigureAwait(false);
  142. tasks.Clear();
  143. // Refresh current item
  144. await RefreshMetadata(refreshOptions, cancellationToken).ConfigureAwait(false);
  145. // Refresh all non-songs
  146. foreach (var item in others)
  147. {
  148. cancellationToken.ThrowIfCancellationRequested();
  149. // Avoid implicitly captured closure
  150. var currentChild = item;
  151. await item.RefreshMetadata(refreshOptions, cancellationToken).ConfigureAwait(false);
  152. lock (percentages)
  153. {
  154. percentages[currentChild.Id] = 1;
  155. var percent = percentages.Values.Sum();
  156. percent /= totalItems;
  157. percent *= 100;
  158. progress.Report(percent);
  159. }
  160. }
  161. progress.Report(100);
  162. }
  163. private async Task RefreshItem(BaseItem item, MetadataRefreshOptions refreshOptions, IProgress<double> progress, CancellationToken cancellationToken)
  164. {
  165. await item.RefreshMetadata(refreshOptions, cancellationToken).ConfigureAwait(false);
  166. progress.Report(100);
  167. }
  168. public ArtistInfo GetLookupInfo()
  169. {
  170. var info = GetItemLookupInfo<ArtistInfo>();
  171. info.SongInfos = RecursiveChildren.OfType<Audio>()
  172. .Select(i => i.GetLookupInfo())
  173. .ToList();
  174. return info;
  175. }
  176. public IEnumerable<BaseItem> GetTaggedItems(IEnumerable<BaseItem> inputItems)
  177. {
  178. return inputItems.OfType<IHasArtist>()
  179. .Where(i => i.HasArtist(Name))
  180. .Cast<BaseItem>();
  181. }
  182. }
  183. }