MusicArtist.cs 7.3 KB

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