MusicArtist.cs 7.5 KB

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