LastfmAlbumProvider.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. using MediaBrowser.Common.Net;
  2. using MediaBrowser.Controller.Configuration;
  3. using MediaBrowser.Controller.Entities;
  4. using MediaBrowser.Controller.Entities.Audio;
  5. using MediaBrowser.Model.Logging;
  6. using MediaBrowser.Model.Serialization;
  7. using MoreLinq;
  8. using System;
  9. using System.IO;
  10. using System.Linq;
  11. using System.Threading;
  12. using System.Threading.Tasks;
  13. namespace MediaBrowser.Controller.Providers.Music
  14. {
  15. public class LastfmAlbumProvider : LastfmBaseProvider
  16. {
  17. private static readonly Task<string> BlankId = Task.FromResult("");
  18. private readonly IProviderManager _providerManager;
  19. public LastfmAlbumProvider(IJsonSerializer jsonSerializer, IHttpClient httpClient, ILogManager logManager, IServerConfigurationManager configurationManager, IProviderManager providerManager)
  20. : base(jsonSerializer, httpClient, logManager, configurationManager)
  21. {
  22. _providerManager = providerManager;
  23. LocalMetaFileName = LastfmHelper.LocalAlbumMetaFileName;
  24. }
  25. protected override Task<string> FindId(BaseItem item, CancellationToken cancellationToken)
  26. {
  27. // We don't fetch by id
  28. return BlankId;
  29. }
  30. protected override async Task FetchLastfmData(BaseItem item, string id, CancellationToken cancellationToken)
  31. {
  32. var result = await GetAlbumResult(item, cancellationToken).ConfigureAwait(false);
  33. if (result != null && result.album != null)
  34. {
  35. LastfmHelper.ProcessAlbumData(item, result.album);
  36. //And save locally if indicated
  37. if (ConfigurationManager.Configuration.SaveLocalMeta)
  38. {
  39. var ms = new MemoryStream();
  40. JsonSerializer.SerializeToStream(result.album, ms);
  41. cancellationToken.ThrowIfCancellationRequested();
  42. await _providerManager.SaveToLibraryFilesystem(item, Path.Combine(item.MetaLocation, LocalMetaFileName), ms, cancellationToken).ConfigureAwait(false);
  43. }
  44. }
  45. }
  46. private async Task<LastfmGetAlbumResult> GetAlbumResult(BaseItem item, CancellationToken cancellationToken)
  47. {
  48. var folder = (Folder)item;
  49. // Get each song, distinct by the combination of AlbumArtist and Album
  50. var songs = folder.RecursiveChildren.OfType<Audio>().DistinctBy(i => (i.AlbumArtist ?? string.Empty) + (i.Album ?? string.Empty), StringComparer.OrdinalIgnoreCase).ToList();
  51. foreach (var song in songs.Where(song => !string.IsNullOrEmpty(song.Album) && !string.IsNullOrEmpty(song.AlbumArtist)))
  52. {
  53. var result = await GetAlbumResult(song.AlbumArtist, song.Album, cancellationToken).ConfigureAwait(false);
  54. if (result != null && result.album != null)
  55. {
  56. return result;
  57. }
  58. }
  59. // Try the folder name
  60. return await GetAlbumResult(item.Parent.Name, item.Name, cancellationToken);
  61. }
  62. private async Task<LastfmGetAlbumResult> GetAlbumResult(string artist, string album, CancellationToken cancellationToken)
  63. {
  64. // Get albu info using artist and album name
  65. var url = RootUrl + string.Format("method=album.getInfo&artist={0}&album={1}&api_key={2}&format=json", UrlEncode(artist), UrlEncode(album), ApiKey);
  66. using (var json = await HttpClient.Get(url, LastfmResourcePool, cancellationToken).ConfigureAwait(false))
  67. {
  68. return JsonSerializer.DeserializeFromStream<LastfmGetAlbumResult>(json);
  69. }
  70. }
  71. protected override Task FetchData(BaseItem item, CancellationToken cancellationToken)
  72. {
  73. return FetchLastfmData(item, string.Empty, cancellationToken);
  74. }
  75. public override bool Supports(BaseItem item)
  76. {
  77. return item is MusicAlbum;
  78. }
  79. protected override bool RefreshOnFileSystemStampChange
  80. {
  81. get
  82. {
  83. return true;
  84. }
  85. }
  86. }
  87. }