LastfmBaseProvider.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. protected 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 the priority.
  76. /// </summary>
  77. /// <value>The priority.</value>
  78. public override MetadataProviderPriority Priority
  79. {
  80. get { return MetadataProviderPriority.Second; }
  81. }
  82. /// <summary>
  83. /// Gets a value indicating whether [requires internet].
  84. /// </summary>
  85. /// <value><c>true</c> if [requires internet]; otherwise, <c>false</c>.</value>
  86. public override bool RequiresInternet
  87. {
  88. get
  89. {
  90. return true;
  91. }
  92. }
  93. protected const string RootUrl = @"http://ws.audioscrobbler.com/2.0/?";
  94. protected static string ApiKey = "7b76553c3eb1d341d642755aecc40a33";
  95. /// <summary>
  96. /// Fetches the items data.
  97. /// </summary>
  98. /// <param name="item">The item.</param>
  99. /// <param name="cancellationToken"></param>
  100. /// <returns>Task.</returns>
  101. protected virtual async Task FetchData(BaseItem item, CancellationToken cancellationToken)
  102. {
  103. var id = item.GetProviderId(MetadataProviders.Musicbrainz) ?? await FindId(item, cancellationToken).ConfigureAwait(false);
  104. if (!string.IsNullOrWhiteSpace(id))
  105. {
  106. Logger.Debug("LastfmProvider - getting info for {0}", item.Name);
  107. cancellationToken.ThrowIfCancellationRequested();
  108. item.SetProviderId(MetadataProviders.Musicbrainz, id);
  109. await FetchLastfmData(item, id, cancellationToken).ConfigureAwait(false);
  110. }
  111. else
  112. {
  113. Logger.Info("LastfmProvider could not find " + item.Name + ". Check name on Last.fm.");
  114. }
  115. }
  116. protected abstract Task<string> FindId(BaseItem item, CancellationToken cancellationToken);
  117. protected abstract Task FetchLastfmData(BaseItem item, string id, CancellationToken cancellationToken);
  118. /// <summary>
  119. /// Encodes an URL.
  120. /// </summary>
  121. /// <param name="name">The name.</param>
  122. /// <returns>System.String.</returns>
  123. protected static string UrlEncode(string name)
  124. {
  125. return WebUtility.UrlEncode(name);
  126. }
  127. /// <summary>
  128. /// Fetches metadata and returns true or false indicating if any work that requires persistence was done
  129. /// </summary>
  130. /// <param name="item">The item.</param>
  131. /// <param name="force">if set to <c>true</c> [force].</param>
  132. /// <param name="cancellationToken">The cancellation token</param>
  133. /// <returns>Task{System.Boolean}.</returns>
  134. public override async Task<bool> FetchAsync(BaseItem item, bool force, CancellationToken cancellationToken)
  135. {
  136. cancellationToken.ThrowIfCancellationRequested();
  137. await FetchData(item, cancellationToken).ConfigureAwait(false);
  138. SetLastRefreshed(item, DateTime.UtcNow);
  139. return true;
  140. }
  141. }
  142. #region Result Objects
  143. public class LastfmStats
  144. {
  145. public string listeners { get; set; }
  146. public string playcount { get; set; }
  147. }
  148. public class LastfmTag
  149. {
  150. public string name { get; set; }
  151. public string url { get; set; }
  152. }
  153. public class LastfmTags
  154. {
  155. public List<LastfmTag> tag { get; set; }
  156. }
  157. public class LastfmFormationInfo
  158. {
  159. public string yearfrom { get; set; }
  160. public string yearto { get; set; }
  161. }
  162. public class LastFmBio
  163. {
  164. public string published { get; set; }
  165. public string summary { get; set; }
  166. public string content { get; set; }
  167. public string placeformed { get; set; }
  168. public string yearformed { get; set; }
  169. public List<LastfmFormationInfo> formationlist { get; set; }
  170. }
  171. public class LastfmArtist
  172. {
  173. public string name { get; set; }
  174. public string mbid { get; set; }
  175. public string url { get; set; }
  176. public string streamable { get; set; }
  177. public string ontour { get; set; }
  178. public LastfmStats stats { get; set; }
  179. public List<LastfmArtist> similar { get; set; }
  180. public LastfmTags tags { get; set; }
  181. public LastFmBio bio { get; set; }
  182. }
  183. public class LastfmAlbum
  184. {
  185. public string name { get; set; }
  186. public string artist { get; set; }
  187. public string id { get; set; }
  188. public string mbid { get; set; }
  189. public string releasedate { get; set; }
  190. public int listeners { get; set; }
  191. public int playcount { get; set; }
  192. public LastfmTags toptags { get; set; }
  193. public LastFmBio wiki { get; set; }
  194. }
  195. public class LastfmGetAlbumResult
  196. {
  197. public LastfmAlbum album { get; set; }
  198. }
  199. public class LastfmGetArtistResult
  200. {
  201. public LastfmArtist artist { get; set; }
  202. }
  203. public class Artistmatches
  204. {
  205. public List<LastfmArtist> artist { get; set; }
  206. }
  207. public class LastfmArtistSearchResult
  208. {
  209. public Artistmatches artistmatches { get; set; }
  210. }
  211. public class LastfmArtistSearchResults
  212. {
  213. public LastfmArtistSearchResult results { get; set; }
  214. }
  215. #endregion
  216. }