LastfmBaseProvider.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. using System.Collections.Generic;
  2. using System.Net;
  3. using MediaBrowser.Common.Net;
  4. using MediaBrowser.Controller.Configuration;
  5. using MediaBrowser.Controller.Entities;
  6. using MediaBrowser.Model.Entities;
  7. using MediaBrowser.Model.Logging;
  8. using System;
  9. using System.Threading;
  10. using System.Threading.Tasks;
  11. using MediaBrowser.Model.Serialization;
  12. namespace MediaBrowser.Controller.Providers.Music
  13. {
  14. class LastfmProviderException : ApplicationException
  15. {
  16. public LastfmProviderException(string msg)
  17. : base(msg)
  18. {
  19. }
  20. }
  21. /// <summary>
  22. /// Class MovieDbProvider
  23. /// </summary>
  24. public abstract class LastfmBaseProvider : BaseMetadataProvider
  25. {
  26. protected static readonly SemaphoreSlim LastfmResourcePool = new SemaphoreSlim(5, 5);
  27. /// <summary>
  28. /// Initializes a new instance of the <see cref="LastfmBaseProvider" /> class.
  29. /// </summary>
  30. /// <param name="jsonSerializer">The json serializer.</param>
  31. /// <param name="httpClient">The HTTP client.</param>
  32. /// <param name="logManager">The log manager.</param>
  33. /// <param name="configurationManager">The configuration manager.</param>
  34. /// <exception cref="System.ArgumentNullException">jsonSerializer</exception>
  35. protected LastfmBaseProvider(IJsonSerializer jsonSerializer, IHttpClient httpClient, ILogManager logManager, IServerConfigurationManager configurationManager)
  36. : base(logManager, configurationManager)
  37. {
  38. if (jsonSerializer == null)
  39. {
  40. throw new ArgumentNullException("jsonSerializer");
  41. }
  42. if (httpClient == null)
  43. {
  44. throw new ArgumentNullException("httpClient");
  45. }
  46. JsonSerializer = jsonSerializer;
  47. HttpClient = httpClient;
  48. }
  49. protected override string ProviderVersion
  50. {
  51. get
  52. {
  53. return "3-12-13";
  54. }
  55. }
  56. protected override bool RefreshOnVersionChange
  57. {
  58. get
  59. {
  60. return true;
  61. }
  62. }
  63. /// <summary>
  64. /// Gets the json serializer.
  65. /// </summary>
  66. /// <value>The json serializer.</value>
  67. protected IJsonSerializer JsonSerializer { get; private set; }
  68. /// <summary>
  69. /// Gets the HTTP client.
  70. /// </summary>
  71. /// <value>The HTTP client.</value>
  72. protected IHttpClient HttpClient { get; private set; }
  73. /// <summary>
  74. /// The name of the local json meta file for this item type
  75. /// </summary>
  76. protected string LocalMetaFileName { get; set; }
  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 ConfigurationManager.Configuration.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 (id != null)
  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 (ConfigurationManager.Configuration.SaveLocalMeta && 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.CompletedWithErrors)
  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. var downloadDate = providerInfo.LastRefreshed;
  170. if (ConfigurationManager.Configuration.MetadataRefreshDays == -1 && downloadDate != DateTime.MinValue)
  171. {
  172. return false;
  173. }
  174. if (DateTime.Today.Subtract(item.DateCreated).TotalDays > 180 && downloadDate != DateTime.MinValue)
  175. return false; // don't trigger a refresh data for item that are more than 6 months old and have been refreshed before
  176. if (DateTime.Today.Subtract(downloadDate).TotalDays < ConfigurationManager.Configuration.MetadataRefreshDays) // only refresh every n days
  177. return false;
  178. Logger.Debug("LastfmProvider - " + item.Name + " needs refresh. Download date: " + downloadDate + " item created date: " + item.DateCreated + " Check for Update age: " + ConfigurationManager.Configuration.MetadataRefreshDays);
  179. return true;
  180. }
  181. /// <summary>
  182. /// Fetches metadata and returns true or false indicating if any work that requires persistence was done
  183. /// </summary>
  184. /// <param name="item">The item.</param>
  185. /// <param name="force">if set to <c>true</c> [force].</param>
  186. /// <param name="cancellationToken">The cancellation token</param>
  187. /// <returns>Task{System.Boolean}.</returns>
  188. public override async Task<bool> FetchAsync(BaseItem item, bool force, CancellationToken cancellationToken)
  189. {
  190. if (item.DontFetchMeta)
  191. {
  192. Logger.Info("LastfmProvider - Not fetching because requested to ignore " + item.Name);
  193. return false;
  194. }
  195. cancellationToken.ThrowIfCancellationRequested();
  196. if (!ConfigurationManager.Configuration.SaveLocalMeta || !HasLocalMeta(item) || (force && !HasLocalMeta(item)) || (RefreshOnVersionChange && item.ProviderData[Id].ProviderVersion != ProviderVersion))
  197. {
  198. try
  199. {
  200. await FetchData(item, cancellationToken).ConfigureAwait(false);
  201. SetLastRefreshed(item, DateTime.UtcNow);
  202. }
  203. catch (LastfmProviderException)
  204. {
  205. SetLastRefreshed(item, DateTime.UtcNow, ProviderRefreshStatus.CompletedWithErrors);
  206. }
  207. return true;
  208. }
  209. Logger.Debug("LastfmProvider not fetching because local meta exists for " + item.Name);
  210. SetLastRefreshed(item, DateTime.UtcNow);
  211. return true;
  212. }
  213. }
  214. #region Result Objects
  215. public class LastfmStats
  216. {
  217. public string listeners { get; set; }
  218. public string playcount { get; set; }
  219. }
  220. public class LastfmTag
  221. {
  222. public string name { get; set; }
  223. public string url { get; set; }
  224. }
  225. public class LastfmTags
  226. {
  227. public List<LastfmTag> tag { get; set; }
  228. }
  229. public class LastfmFormationInfo
  230. {
  231. public string yearfrom { get; set; }
  232. public string yearto { get; set; }
  233. }
  234. public class LastFmBio
  235. {
  236. public string published { get; set; }
  237. public string summary { get; set; }
  238. public string content { get; set; }
  239. public string placeformed { get; set; }
  240. public string yearformed { get; set; }
  241. public List<LastfmFormationInfo> formationlist { get; set; }
  242. }
  243. public class LastfmArtist
  244. {
  245. public string name { get; set; }
  246. public string mbid { get; set; }
  247. public string url { get; set; }
  248. public string streamable { get; set; }
  249. public string ontour { get; set; }
  250. public LastfmStats stats { get; set; }
  251. public List<LastfmArtist> similar { get; set; }
  252. public LastfmTags tags { get; set; }
  253. public LastFmBio bio { get; set; }
  254. }
  255. public class LastfmAlbum
  256. {
  257. public string name { get; set; }
  258. public string artist { get; set; }
  259. public string id { get; set; }
  260. public string mbid { get; set; }
  261. public string releasedate { get; set; }
  262. public int listeners { get; set; }
  263. public int playcount { get; set; }
  264. public LastfmTags toptags { get; set; }
  265. public LastFmBio wiki { get; set; }
  266. }
  267. public class LastfmGetAlbumResult
  268. {
  269. public LastfmAlbum album { get; set; }
  270. }
  271. public class LastfmGetArtistResult
  272. {
  273. public LastfmArtist artist { get; set; }
  274. }
  275. public class Artistmatches
  276. {
  277. public List<LastfmArtist> artist { get; set; }
  278. }
  279. public class LastfmArtistSearchResult
  280. {
  281. public Artistmatches artistmatches { get; set; }
  282. }
  283. public class LastfmArtistSearchResults
  284. {
  285. public LastfmArtistSearchResult results { get; set; }
  286. }
  287. #endregion
  288. }