LastfmBaseProvider.cs 8.6 KB

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