LastfmArtistProvider.cs 5.4 KB

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