LastfmArtistProvider.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Net;
  6. using System.Text;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. using MediaBrowser.Common.Net;
  10. using MediaBrowser.Controller.Configuration;
  11. using MediaBrowser.Model.Entities;
  12. using MediaBrowser.Controller.Entities;
  13. using MediaBrowser.Controller.Entities.Audio;
  14. using MediaBrowser.Model.Logging;
  15. using MediaBrowser.Model.Net;
  16. using MediaBrowser.Model.Serialization;
  17. namespace MediaBrowser.Controller.Providers.Music
  18. {
  19. public class LastfmArtistProvider : LastfmBaseProvider
  20. {
  21. public LastfmArtistProvider(IJsonSerializer jsonSerializer, IHttpClient httpClient, ILogManager logManager, IServerConfigurationManager configurationManager)
  22. : base(jsonSerializer, httpClient, logManager, configurationManager)
  23. {
  24. LocalMetaFileName = LastfmHelper.LocalArtistMetaFileName;
  25. }
  26. protected override async Task<string> FindId(Entities.BaseItem item, System.Threading.CancellationToken cancellationToken)
  27. {
  28. //Execute the Artist search against our name and assume first one is the one we want
  29. var url = RootUrl + string.Format("method=artist.search&artist={0}&api_key={1}&format=json", UrlEncode(item.Name), ApiKey);
  30. LastfmArtistSearchResults searchResult = null;
  31. try
  32. {
  33. using (var json = await HttpClient.Get(url, LastfmResourcePool, cancellationToken).ConfigureAwait(false))
  34. {
  35. searchResult = JsonSerializer.DeserializeFromStream<LastfmArtistSearchResults>(json);
  36. }
  37. }
  38. catch (HttpException e)
  39. {
  40. if (e.StatusCode == HttpStatusCode.NotFound)
  41. {
  42. return null;
  43. }
  44. throw;
  45. }
  46. if (searchResult != null && searchResult.results != null && searchResult.results.artistmatches != null && searchResult.results.artistmatches.artist.Any())
  47. {
  48. return searchResult.results.artistmatches.artist.First().mbid;
  49. }
  50. return null;
  51. }
  52. protected override async Task FetchLastfmData(BaseItem item, string id, CancellationToken cancellationToken)
  53. {
  54. // Get artist info with provided id
  55. var url = RootUrl + string.Format("method=artist.getInfo&mbid={0}&api_key={1}&format=json", UrlEncode(id), ApiKey);
  56. LastfmGetArtistResult result = null;
  57. try
  58. {
  59. using (var json = await HttpClient.Get(url, LastfmResourcePool, cancellationToken).ConfigureAwait(false))
  60. {
  61. result = JsonSerializer.DeserializeFromStream<LastfmGetArtistResult>(json);
  62. }
  63. }
  64. catch (HttpException e)
  65. {
  66. if (e.StatusCode == HttpStatusCode.NotFound)
  67. {
  68. throw new LastfmProviderException(string.Format("Unable to retrieve artist info for {0} with id {1}", item.Name, id));
  69. }
  70. throw;
  71. }
  72. if (result != null && result.artist != null)
  73. {
  74. LastfmHelper.ProcessArtistData(item, result.artist);
  75. //And save locally if indicated
  76. if (ConfigurationManager.Configuration.SaveLocalMeta)
  77. {
  78. var ms = new MemoryStream();
  79. JsonSerializer.SerializeToStream(result.artist, ms);
  80. cancellationToken.ThrowIfCancellationRequested();
  81. await Kernel.Instance.FileSystemManager.SaveToLibraryFilesystem(item, Path.Combine(item.MetaLocation, LocalMetaFileName), ms, cancellationToken).ConfigureAwait(false);
  82. }
  83. }
  84. }
  85. public override bool Supports(Entities.BaseItem item)
  86. {
  87. return item is MusicArtist;
  88. }
  89. }
  90. }