LastfmAlbumProvider.cs 6.3 KB

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