MusicArtist.cs 6.6 KB

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