LastfmBaseProvider.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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 "3-12-13.3";
  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. protected override bool NeedsRefreshInternal(BaseItem item, BaseProviderInfo providerInfo)
  150. {
  151. if (item.DontFetchMeta) return false;
  152. if (RefreshOnFileSystemStampChange && HasFileSystemStampChanged(item, providerInfo))
  153. {
  154. //If they deleted something from file system, chances are, this item was mis-identified the first time
  155. item.SetProviderId(MetadataProviders.Musicbrainz, null);
  156. Logger.Debug("LastfmProvider reports file system stamp change...");
  157. return true;
  158. }
  159. if (providerInfo.LastRefreshStatus != ProviderRefreshStatus.Success)
  160. {
  161. Logger.Debug("LastfmProvider for {0} - last attempt had errors. Will try again.", item.Path);
  162. return true;
  163. }
  164. if (RefreshOnVersionChange && ProviderVersion != providerInfo.ProviderVersion)
  165. {
  166. Logger.Debug("LastfmProvider version change re-running for {0}", item.Path);
  167. return true;
  168. }
  169. if (DateTime.UtcNow.Subtract(providerInfo.LastRefreshed).TotalDays > ConfigurationManager.Configuration.MetadataRefreshDays) // only refresh every n days
  170. return true;
  171. return false;
  172. }
  173. /// <summary>
  174. /// Fetches metadata and returns true or false indicating if any work that requires persistence was done
  175. /// </summary>
  176. /// <param name="item">The item.</param>
  177. /// <param name="force">if set to <c>true</c> [force].</param>
  178. /// <param name="cancellationToken">The cancellation token</param>
  179. /// <returns>Task{System.Boolean}.</returns>
  180. public override async Task<bool> FetchAsync(BaseItem item, bool force, CancellationToken cancellationToken)
  181. {
  182. cancellationToken.ThrowIfCancellationRequested();
  183. await FetchData(item, cancellationToken).ConfigureAwait(false);
  184. SetLastRefreshed(item, DateTime.UtcNow);
  185. return true;
  186. }
  187. }
  188. #region Result Objects
  189. public class LastfmStats
  190. {
  191. public string listeners { get; set; }
  192. public string playcount { get; set; }
  193. }
  194. public class LastfmTag
  195. {
  196. public string name { get; set; }
  197. public string url { get; set; }
  198. }
  199. public class LastfmTags
  200. {
  201. public List<LastfmTag> tag { get; set; }
  202. }
  203. public class LastfmFormationInfo
  204. {
  205. public string yearfrom { get; set; }
  206. public string yearto { get; set; }
  207. }
  208. public class LastFmBio
  209. {
  210. public string published { get; set; }
  211. public string summary { get; set; }
  212. public string content { get; set; }
  213. public string placeformed { get; set; }
  214. public string yearformed { get; set; }
  215. public List<LastfmFormationInfo> formationlist { get; set; }
  216. }
  217. public class LastfmArtist
  218. {
  219. public string name { get; set; }
  220. public string mbid { get; set; }
  221. public string url { get; set; }
  222. public string streamable { get; set; }
  223. public string ontour { get; set; }
  224. public LastfmStats stats { get; set; }
  225. public List<LastfmArtist> similar { get; set; }
  226. public LastfmTags tags { get; set; }
  227. public LastFmBio bio { get; set; }
  228. }
  229. public class LastfmAlbum
  230. {
  231. public string name { get; set; }
  232. public string artist { get; set; }
  233. public string id { get; set; }
  234. public string mbid { get; set; }
  235. public string releasedate { get; set; }
  236. public int listeners { get; set; }
  237. public int playcount { get; set; }
  238. public LastfmTags toptags { get; set; }
  239. public LastFmBio wiki { get; set; }
  240. }
  241. public class LastfmGetAlbumResult
  242. {
  243. public LastfmAlbum album { get; set; }
  244. }
  245. public class LastfmGetArtistResult
  246. {
  247. public LastfmArtist artist { get; set; }
  248. }
  249. public class Artistmatches
  250. {
  251. public List<LastfmArtist> artist { get; set; }
  252. }
  253. public class LastfmArtistSearchResult
  254. {
  255. public Artistmatches artistmatches { get; set; }
  256. }
  257. public class LastfmArtistSearchResults
  258. {
  259. public LastfmArtistSearchResult results { get; set; }
  260. }
  261. #endregion
  262. }