MusicArtist.cs 7.6 KB

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