MusicArtist.cs 5.8 KB

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