MusicArtist.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. private readonly Task _cachedTask = Task.FromResult(true);
  49. protected override Task ValidateChildrenInternal(IProgress<double> progress, CancellationToken cancellationToken, bool recursive, bool refreshChildMetadata, MetadataRefreshOptions refreshOptions, IDirectoryService directoryService)
  50. {
  51. if (IsAccessedByName)
  52. {
  53. // Should never get in here anyway
  54. return _cachedTask;
  55. }
  56. return base.ValidateChildrenInternal(progress, cancellationToken, recursive, refreshChildMetadata, refreshOptions, directoryService);
  57. }
  58. public MusicArtist()
  59. {
  60. ProductionLocations = new List<string>();
  61. }
  62. /// <summary>
  63. /// Gets the user data key.
  64. /// </summary>
  65. /// <returns>System.String.</returns>
  66. protected override string CreateUserDataKey()
  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. [IgnoreDataMember]
  76. public override string ContainingFolderPath
  77. {
  78. get
  79. {
  80. return Path;
  81. }
  82. }
  83. /// <summary>
  84. /// Gets a value indicating whether this instance is owned item.
  85. /// </summary>
  86. /// <value><c>true</c> if this instance is owned item; otherwise, <c>false</c>.</value>
  87. [IgnoreDataMember]
  88. public override bool IsOwnedItem
  89. {
  90. get
  91. {
  92. return false;
  93. }
  94. }
  95. /// <summary>
  96. /// Gets the user data key.
  97. /// </summary>
  98. /// <param name="item">The item.</param>
  99. /// <returns>System.String.</returns>
  100. private static string GetUserDataKey(MusicArtist item)
  101. {
  102. var id = item.GetProviderId(MetadataProviders.MusicBrainzArtist);
  103. if (!string.IsNullOrEmpty(id))
  104. {
  105. return "Artist-Musicbrainz-" + id;
  106. }
  107. return "Artist-" + item.Name;
  108. }
  109. protected override bool GetBlockUnratedValue(UserPolicy config)
  110. {
  111. return config.BlockUnratedItems.Contains(UnratedItem.Music);
  112. }
  113. public async Task RefreshAllMetadata(MetadataRefreshOptions refreshOptions, IProgress<double> progress, CancellationToken cancellationToken)
  114. {
  115. var items = GetRecursiveChildren().ToList();
  116. var songs = items.OfType<Audio>().ToList();
  117. var others = items.Except(songs).ToList();
  118. var totalItems = songs.Count + others.Count;
  119. var numComplete = 0;
  120. // Refresh songs
  121. foreach (var item in songs)
  122. {
  123. cancellationToken.ThrowIfCancellationRequested();
  124. await item.RefreshMetadata(refreshOptions, cancellationToken).ConfigureAwait(false);
  125. numComplete++;
  126. double percent = numComplete;
  127. percent /= totalItems;
  128. progress.Report(percent * 100);
  129. }
  130. // Refresh current item
  131. await RefreshMetadata(refreshOptions, cancellationToken).ConfigureAwait(false);
  132. // Refresh all non-songs
  133. foreach (var item in others)
  134. {
  135. cancellationToken.ThrowIfCancellationRequested();
  136. await item.RefreshMetadata(refreshOptions, cancellationToken).ConfigureAwait(false);
  137. numComplete++;
  138. double percent = numComplete;
  139. percent /= totalItems;
  140. progress.Report(percent * 100);
  141. }
  142. progress.Report(100);
  143. }
  144. public ArtistInfo GetLookupInfo()
  145. {
  146. var info = GetItemLookupInfo<ArtistInfo>();
  147. info.SongInfos = GetRecursiveChildren(i => i is Audio)
  148. .Cast<Audio>()
  149. .Select(i => i.GetLookupInfo())
  150. .ToList();
  151. return info;
  152. }
  153. public IEnumerable<BaseItem> GetTaggedItems(IEnumerable<BaseItem> inputItems)
  154. {
  155. return inputItems.Where(GetItemFilter());
  156. }
  157. public Func<BaseItem, bool> GetItemFilter()
  158. {
  159. return i =>
  160. {
  161. var hasArtist = i as IHasArtist;
  162. return hasArtist != null && hasArtist.HasArtist(Name);
  163. };
  164. }
  165. }
  166. }