LastfmAlbumProvider.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 result = await GetAlbumResult(item.Parent.Name, item.Name, cancellationToken);
  49. if (result != null && result.album != null)
  50. {
  51. return result;
  52. }
  53. var folder = (Folder)item;
  54. // Get each song, distinct by the combination of AlbumArtist and Album
  55. var songs = folder.Children.OfType<Audio>().DistinctBy(i => (i.AlbumArtist ?? string.Empty) + (i.Album ?? string.Empty), StringComparer.OrdinalIgnoreCase).ToList();
  56. foreach (var song in songs.Where(song => !string.IsNullOrEmpty(song.Album) && !string.IsNullOrEmpty(song.AlbumArtist)))
  57. {
  58. result = await GetAlbumResult(song.AlbumArtist, song.Album, cancellationToken).ConfigureAwait(false);
  59. if (result != null && result.album != null)
  60. {
  61. return result;
  62. }
  63. }
  64. return null;
  65. }
  66. private async Task<LastfmGetAlbumResult> GetAlbumResult(string artist, string album, CancellationToken cancellationToken)
  67. {
  68. // Get albu info using artist and album name
  69. var url = RootUrl + string.Format("method=album.getInfo&artist={0}&album={1}&api_key={2}&format=json", UrlEncode(artist), UrlEncode(album), ApiKey);
  70. using (var json = await HttpClient.Get(url, LastfmResourcePool, cancellationToken).ConfigureAwait(false))
  71. {
  72. return JsonSerializer.DeserializeFromStream<LastfmGetAlbumResult>(json);
  73. }
  74. }
  75. protected override Task FetchData(BaseItem item, CancellationToken cancellationToken)
  76. {
  77. return FetchLastfmData(item, string.Empty, cancellationToken);
  78. }
  79. public override bool Supports(BaseItem item)
  80. {
  81. return item is MusicAlbum;
  82. }
  83. protected override bool RefreshOnFileSystemStampChange
  84. {
  85. get
  86. {
  87. return true;
  88. }
  89. }
  90. }
  91. }