MusicArtist.cs 6.1 KB

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