MusicArtist.cs 7.4 KB

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