LastfmAlbumProvider.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. using MediaBrowser.Common.Extensions;
  2. using MediaBrowser.Common.Net;
  3. using MediaBrowser.Controller.Configuration;
  4. using MediaBrowser.Controller.Entities;
  5. using MediaBrowser.Controller.Entities.Audio;
  6. using MediaBrowser.Model.Entities;
  7. using MediaBrowser.Model.Logging;
  8. using MediaBrowser.Model.Serialization;
  9. using MoreLinq;
  10. using System;
  11. using System.IO;
  12. using System.Linq;
  13. using System.Threading;
  14. using System.Threading.Tasks;
  15. namespace MediaBrowser.Controller.Providers.Music
  16. {
  17. public class LastfmAlbumProvider : LastfmBaseProvider
  18. {
  19. private static readonly Task<string> BlankId = Task.FromResult("");
  20. private readonly IProviderManager _providerManager;
  21. public LastfmAlbumProvider(IJsonSerializer jsonSerializer, IHttpClient httpClient, ILogManager logManager, IServerConfigurationManager configurationManager, IProviderManager providerManager)
  22. : base(jsonSerializer, httpClient, logManager, configurationManager)
  23. {
  24. _providerManager = providerManager;
  25. LocalMetaFileName = LastfmHelper.LocalAlbumMetaFileName;
  26. }
  27. protected override Task<string> FindId(BaseItem item, CancellationToken cancellationToken)
  28. {
  29. // We don't fetch by id
  30. return BlankId;
  31. }
  32. /// <summary>
  33. /// Needses the refresh internal.
  34. /// </summary>
  35. /// <param name="item">The item.</param>
  36. /// <param name="providerInfo">The provider info.</param>
  37. /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
  38. protected override bool NeedsRefreshInternal(BaseItem item, BaseProviderInfo providerInfo)
  39. {
  40. // If song metadata has changed and we don't have an mbid, refresh
  41. if (string.IsNullOrEmpty(item.GetProviderId(MetadataProviders.Musicbrainz)) &&
  42. GetComparisonData(item as MusicAlbum) != providerInfo.Data)
  43. {
  44. return true;
  45. }
  46. return base.NeedsRefreshInternal(item, providerInfo);
  47. }
  48. protected override async Task FetchLastfmData(BaseItem item, string id, CancellationToken cancellationToken)
  49. {
  50. var result = await GetAlbumResult(item, cancellationToken).ConfigureAwait(false);
  51. if (result != null && result.album != null)
  52. {
  53. LastfmHelper.ProcessAlbumData(item, result.album);
  54. //And save locally if indicated
  55. if (ConfigurationManager.Configuration.SaveLocalMeta)
  56. {
  57. var ms = new MemoryStream();
  58. JsonSerializer.SerializeToStream(result.album, ms);
  59. cancellationToken.ThrowIfCancellationRequested();
  60. await _providerManager.SaveToLibraryFilesystem(item, Path.Combine(item.MetaLocation, LocalMetaFileName), ms, cancellationToken).ConfigureAwait(false);
  61. }
  62. }
  63. BaseProviderInfo data;
  64. if (!item.ProviderData.TryGetValue(Id, out data))
  65. {
  66. data = new BaseProviderInfo();
  67. item.ProviderData[Id] = data;
  68. }
  69. data.Data = GetComparisonData(item as MusicAlbum);
  70. }
  71. private async Task<LastfmGetAlbumResult> GetAlbumResult(BaseItem item, CancellationToken cancellationToken)
  72. {
  73. var folder = (Folder)item;
  74. // Get each song, distinct by the combination of AlbumArtist and Album
  75. var songs = folder.RecursiveChildren.OfType<Audio>().DistinctBy(i => (i.AlbumArtist ?? string.Empty) + (i.Album ?? string.Empty), StringComparer.OrdinalIgnoreCase).ToList();
  76. foreach (var song in songs.Where(song => !string.IsNullOrEmpty(song.Album) && !string.IsNullOrEmpty(song.AlbumArtist)))
  77. {
  78. var result = await GetAlbumResult(song.AlbumArtist, song.Album, cancellationToken).ConfigureAwait(false);
  79. if (result != null && result.album != null)
  80. {
  81. return result;
  82. }
  83. }
  84. // Try the folder name
  85. return await GetAlbumResult(item.Parent.Name, item.Name, cancellationToken);
  86. }
  87. private async Task<LastfmGetAlbumResult> GetAlbumResult(string artist, string album, CancellationToken cancellationToken)
  88. {
  89. // Get albu info using artist and album name
  90. var url = RootUrl + string.Format("method=album.getInfo&artist={0}&album={1}&api_key={2}&format=json", UrlEncode(artist), UrlEncode(album), ApiKey);
  91. using (var json = await HttpClient.Get(new HttpRequestOptions
  92. {
  93. Url = url,
  94. ResourcePool = LastfmResourcePool,
  95. CancellationToken = cancellationToken,
  96. EnableResponseCache = true,
  97. EnableHttpCompression = false
  98. }).ConfigureAwait(false))
  99. {
  100. return JsonSerializer.DeserializeFromStream<LastfmGetAlbumResult>(json);
  101. }
  102. }
  103. protected override Task FetchData(BaseItem item, CancellationToken cancellationToken)
  104. {
  105. return FetchLastfmData(item, string.Empty, cancellationToken);
  106. }
  107. public override bool Supports(BaseItem item)
  108. {
  109. return item is MusicAlbum;
  110. }
  111. protected override bool RefreshOnFileSystemStampChange
  112. {
  113. get
  114. {
  115. return true;
  116. }
  117. }
  118. /// <summary>
  119. /// Gets the data.
  120. /// </summary>
  121. /// <param name="album">The album.</param>
  122. /// <returns>Guid.</returns>
  123. private Guid GetComparisonData(MusicAlbum album)
  124. {
  125. var songs = album.RecursiveChildren.OfType<Audio>().ToList();
  126. var albumArtists = songs.Select(i => i.AlbumArtist)
  127. .Where(i => !string.IsNullOrEmpty(i))
  128. .Distinct(StringComparer.OrdinalIgnoreCase)
  129. .ToList();
  130. var albumNames = songs.Select(i => i.AlbumArtist)
  131. .Where(i => !string.IsNullOrEmpty(i))
  132. .Distinct(StringComparer.OrdinalIgnoreCase)
  133. .ToList();
  134. albumArtists.AddRange(albumNames);
  135. return string.Join(string.Empty, albumArtists.OrderBy(i => i).ToArray()).GetMD5();
  136. }
  137. }
  138. }