LastfmArtistProvider.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. using MediaBrowser.Common.Net;
  2. using MediaBrowser.Controller.Configuration;
  3. using MediaBrowser.Controller.Entities;
  4. using MediaBrowser.Controller.Entities.Audio;
  5. using MediaBrowser.Controller.Providers;
  6. using MediaBrowser.Model.Entities;
  7. using MediaBrowser.Model.Logging;
  8. using MediaBrowser.Model.Providers;
  9. using MediaBrowser.Model.Serialization;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.IO;
  13. using System.Net;
  14. using System.Threading;
  15. using System.Threading.Tasks;
  16. namespace MediaBrowser.Providers.Music
  17. {
  18. public class LastfmArtistProvider : IRemoteMetadataProvider<MusicArtist, ArtistInfo>, IHasOrder
  19. {
  20. private readonly IJsonSerializer _json;
  21. private readonly IHttpClient _httpClient;
  22. internal static readonly SemaphoreSlim LastfmResourcePool = new SemaphoreSlim(4, 4);
  23. internal const string RootUrl = @"http://ws.audioscrobbler.com/2.0/?";
  24. internal static string ApiKey = "7b76553c3eb1d341d642755aecc40a33";
  25. private readonly IServerConfigurationManager _config;
  26. private readonly ILogger _logger;
  27. public LastfmArtistProvider(IHttpClient httpClient, IJsonSerializer json, IServerConfigurationManager config, ILogger logger)
  28. {
  29. _httpClient = httpClient;
  30. _json = json;
  31. _config = config;
  32. _logger = logger;
  33. }
  34. public async Task<IEnumerable<RemoteSearchResult>> GetSearchResults(ArtistInfo searchInfo, CancellationToken cancellationToken)
  35. {
  36. return new List<RemoteSearchResult>();
  37. }
  38. public async Task<MetadataResult<MusicArtist>> GetMetadata(ArtistInfo id, CancellationToken cancellationToken)
  39. {
  40. var result = new MetadataResult<MusicArtist>();
  41. var musicBrainzId = id.GetMusicBrainzArtistId();
  42. if (!String.IsNullOrWhiteSpace(musicBrainzId))
  43. {
  44. cancellationToken.ThrowIfCancellationRequested();
  45. result.Item = new MusicArtist();
  46. result.HasMetadata = true;
  47. await FetchLastfmData(result.Item, musicBrainzId, cancellationToken).ConfigureAwait(false);
  48. }
  49. return result;
  50. }
  51. protected async Task FetchLastfmData(MusicArtist item, string musicBrainzId, CancellationToken cancellationToken)
  52. {
  53. // Get artist info with provided id
  54. var url = RootUrl + String.Format("method=artist.getInfo&mbid={0}&api_key={1}&format=json", UrlEncode(musicBrainzId), ApiKey);
  55. LastfmGetArtistResult result;
  56. using (var json = await _httpClient.Get(new HttpRequestOptions
  57. {
  58. Url = url,
  59. ResourcePool = LastfmResourcePool,
  60. CancellationToken = cancellationToken,
  61. EnableHttpCompression = false
  62. }).ConfigureAwait(false))
  63. {
  64. using (var reader = new StreamReader(json))
  65. {
  66. var jsonText = await reader.ReadToEndAsync().ConfigureAwait(false);
  67. // Fix their bad json
  68. jsonText = jsonText.Replace("\"#text\"", "\"url\"");
  69. result = _json.DeserializeFromString<LastfmGetArtistResult>(jsonText);
  70. }
  71. }
  72. if (result != null && result.artist != null)
  73. {
  74. ProcessArtistData(item, result.artist, musicBrainzId);
  75. }
  76. }
  77. private void ProcessArtistData(MusicArtist artist, LastfmArtist data, string musicBrainzId)
  78. {
  79. var yearFormed = 0;
  80. if (data.bio != null)
  81. {
  82. Int32.TryParse(data.bio.yearformed, out yearFormed);
  83. if (!artist.LockedFields.Contains(MetadataFields.Overview))
  84. {
  85. artist.Overview = data.bio.content;
  86. }
  87. if (!string.IsNullOrEmpty(data.bio.placeformed) && !artist.LockedFields.Contains(MetadataFields.ProductionLocations))
  88. {
  89. artist.AddProductionLocation(data.bio.placeformed);
  90. }
  91. }
  92. if (yearFormed > 0)
  93. {
  94. artist.PremiereDate = new DateTime(yearFormed, 1, 1, 0, 0, 0, DateTimeKind.Utc);
  95. artist.ProductionYear = yearFormed;
  96. }
  97. string imageSize;
  98. var url = LastfmHelper.GetImageUrl(data, out imageSize);
  99. if (!string.IsNullOrEmpty(musicBrainzId) && !string.IsNullOrEmpty(url))
  100. {
  101. LastfmHelper.SaveImageInfo(_config.ApplicationPaths, _logger, musicBrainzId, url, imageSize);
  102. }
  103. }
  104. /// <summary>
  105. /// Encodes an URL.
  106. /// </summary>
  107. /// <param name="name">The name.</param>
  108. /// <returns>System.String.</returns>
  109. private string UrlEncode(string name)
  110. {
  111. return WebUtility.UrlEncode(name);
  112. }
  113. public string Name
  114. {
  115. get { return "last.fm"; }
  116. }
  117. public int Order
  118. {
  119. get
  120. {
  121. // After fanart & audiodb
  122. return 2;
  123. }
  124. }
  125. public Task<HttpResponseInfo> GetImageResponse(string url, CancellationToken cancellationToken)
  126. {
  127. throw new NotImplementedException();
  128. }
  129. }
  130. }