LastfmBaseProvider.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. using MediaBrowser.Common.Net;
  2. using MediaBrowser.Controller.Configuration;
  3. using MediaBrowser.Controller.Entities;
  4. using MediaBrowser.Controller.Providers;
  5. using MediaBrowser.Model.Entities;
  6. using MediaBrowser.Model.Logging;
  7. using MediaBrowser.Model.Serialization;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Net;
  11. using System.Threading;
  12. using System.Threading.Tasks;
  13. namespace MediaBrowser.Providers.Music
  14. {
  15. /// <summary>
  16. /// Class MovieDbProvider
  17. /// </summary>
  18. public abstract class LastfmBaseProvider : BaseMetadataProvider
  19. {
  20. internal static readonly SemaphoreSlim LastfmResourcePool = new SemaphoreSlim(4, 4);
  21. /// <summary>
  22. /// Initializes a new instance of the <see cref="LastfmBaseProvider" /> class.
  23. /// </summary>
  24. /// <param name="jsonSerializer">The json serializer.</param>
  25. /// <param name="httpClient">The HTTP client.</param>
  26. /// <param name="logManager">The log manager.</param>
  27. /// <param name="configurationManager">The configuration manager.</param>
  28. /// <exception cref="System.ArgumentNullException">jsonSerializer</exception>
  29. protected LastfmBaseProvider(IJsonSerializer jsonSerializer, IHttpClient httpClient, ILogManager logManager, IServerConfigurationManager configurationManager)
  30. : base(logManager, configurationManager)
  31. {
  32. if (jsonSerializer == null)
  33. {
  34. throw new ArgumentNullException("jsonSerializer");
  35. }
  36. if (httpClient == null)
  37. {
  38. throw new ArgumentNullException("httpClient");
  39. }
  40. JsonSerializer = jsonSerializer;
  41. HttpClient = httpClient;
  42. }
  43. protected override string ProviderVersion
  44. {
  45. get
  46. {
  47. return "5";
  48. }
  49. }
  50. protected override bool RefreshOnVersionChange
  51. {
  52. get
  53. {
  54. return true;
  55. }
  56. }
  57. /// <summary>
  58. /// Gets the json serializer.
  59. /// </summary>
  60. /// <value>The json serializer.</value>
  61. protected IJsonSerializer JsonSerializer { get; private set; }
  62. /// <summary>
  63. /// Gets the HTTP client.
  64. /// </summary>
  65. /// <value>The HTTP client.</value>
  66. protected IHttpClient HttpClient { get; private set; }
  67. protected virtual bool SaveLocalMeta
  68. {
  69. get
  70. {
  71. return ConfigurationManager.Configuration.SaveLocalMeta;
  72. }
  73. }
  74. /// <summary>
  75. /// Gets a value indicating whether [requires internet].
  76. /// </summary>
  77. /// <value><c>true</c> if [requires internet]; otherwise, <c>false</c>.</value>
  78. public override bool RequiresInternet
  79. {
  80. get
  81. {
  82. return true;
  83. }
  84. }
  85. protected const string RootUrl = @"http://ws.audioscrobbler.com/2.0/?";
  86. protected static string ApiKey = "7b76553c3eb1d341d642755aecc40a33";
  87. /// <summary>
  88. /// Fetches the items data.
  89. /// </summary>
  90. /// <param name="item">The item.</param>
  91. /// <param name="cancellationToken"></param>
  92. /// <returns>Task.</returns>
  93. protected virtual async Task FetchData(BaseItem item, bool force, CancellationToken cancellationToken)
  94. {
  95. var id = item.GetProviderId(MetadataProviders.Musicbrainz) ?? await FindId(item, cancellationToken).ConfigureAwait(false);
  96. if (!string.IsNullOrWhiteSpace(id))
  97. {
  98. Logger.Debug("LastfmProvider - getting info for {0}", item.Name);
  99. cancellationToken.ThrowIfCancellationRequested();
  100. item.SetProviderId(MetadataProviders.Musicbrainz, id);
  101. await FetchLastfmData(item, id, force, cancellationToken).ConfigureAwait(false);
  102. }
  103. else
  104. {
  105. Logger.Info("LastfmProvider could not find " + item.Name + ". Check name on Last.fm.");
  106. }
  107. }
  108. protected abstract Task<string> FindId(BaseItem item, CancellationToken cancellationToken);
  109. protected abstract Task FetchLastfmData(BaseItem item, string id, bool force, CancellationToken cancellationToken);
  110. /// <summary>
  111. /// Encodes an URL.
  112. /// </summary>
  113. /// <param name="name">The name.</param>
  114. /// <returns>System.String.</returns>
  115. protected static string UrlEncode(string name)
  116. {
  117. return WebUtility.UrlEncode(name);
  118. }
  119. /// <summary>
  120. /// Fetches metadata and returns true or false indicating if any work that requires persistence was done
  121. /// </summary>
  122. /// <param name="item">The item.</param>
  123. /// <param name="force">if set to <c>true</c> [force].</param>
  124. /// <param name="cancellationToken">The cancellation token</param>
  125. /// <returns>Task{System.Boolean}.</returns>
  126. public override async Task<bool> FetchAsync(BaseItem item, bool force, CancellationToken cancellationToken)
  127. {
  128. cancellationToken.ThrowIfCancellationRequested();
  129. await FetchData(item, force, cancellationToken).ConfigureAwait(false);
  130. SetLastRefreshed(item, DateTime.UtcNow);
  131. return true;
  132. }
  133. }
  134. #region Result Objects
  135. public class LastfmStats
  136. {
  137. public string listeners { get; set; }
  138. public string playcount { get; set; }
  139. }
  140. public class LastfmTag
  141. {
  142. public string name { get; set; }
  143. public string url { get; set; }
  144. }
  145. public class LastfmTags
  146. {
  147. public List<LastfmTag> tag { get; set; }
  148. }
  149. public class LastfmFormationInfo
  150. {
  151. public string yearfrom { get; set; }
  152. public string yearto { get; set; }
  153. }
  154. public class LastFmBio
  155. {
  156. public string published { get; set; }
  157. public string summary { get; set; }
  158. public string content { get; set; }
  159. public string placeformed { get; set; }
  160. public string yearformed { get; set; }
  161. public List<LastfmFormationInfo> formationlist { get; set; }
  162. }
  163. public class LastFmImage
  164. {
  165. public string url { get; set; }
  166. public string size { get; set; }
  167. }
  168. public class LastfmArtist : IHasLastFmImages
  169. {
  170. public string name { get; set; }
  171. public string mbid { get; set; }
  172. public string url { get; set; }
  173. public string streamable { get; set; }
  174. public string ontour { get; set; }
  175. public LastfmStats stats { get; set; }
  176. public List<LastfmArtist> similar { get; set; }
  177. public LastfmTags tags { get; set; }
  178. public LastFmBio bio { get; set; }
  179. public List<LastFmImage> image { get; set; }
  180. }
  181. public class LastfmAlbum : IHasLastFmImages
  182. {
  183. public string name { get; set; }
  184. public string artist { get; set; }
  185. public string id { get; set; }
  186. public string mbid { get; set; }
  187. public string releasedate { get; set; }
  188. public int listeners { get; set; }
  189. public int playcount { get; set; }
  190. public LastfmTags toptags { get; set; }
  191. public LastFmBio wiki { get; set; }
  192. public List<LastFmImage> image { get; set; }
  193. }
  194. public interface IHasLastFmImages
  195. {
  196. List<LastFmImage> image { get; set; }
  197. }
  198. public class LastfmGetAlbumResult
  199. {
  200. public LastfmAlbum album { get; set; }
  201. }
  202. public class LastfmGetArtistResult
  203. {
  204. public LastfmArtist artist { get; set; }
  205. }
  206. public class Artistmatches
  207. {
  208. public List<LastfmArtist> artist { get; set; }
  209. }
  210. public class LastfmArtistSearchResult
  211. {
  212. public Artistmatches artistmatches { get; set; }
  213. }
  214. public class LastfmArtistSearchResults
  215. {
  216. public LastfmArtistSearchResult results { get; set; }
  217. }
  218. #endregion
  219. }