LastfmBaseProvider.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. /// <summary>
  50. /// Gets the json serializer.
  51. /// </summary>
  52. /// <value>The json serializer.</value>
  53. protected IJsonSerializer JsonSerializer { get; private set; }
  54. /// <summary>
  55. /// Gets the HTTP client.
  56. /// </summary>
  57. /// <value>The HTTP client.</value>
  58. protected IHttpClient HttpClient { get; private set; }
  59. /// <summary>
  60. /// The name of the local json meta file for this item type
  61. /// </summary>
  62. protected string LocalMetaFileName { get; set; }
  63. /// <summary>
  64. /// If we save locally, refresh if they delete something
  65. /// </summary>
  66. protected override bool RefreshOnFileSystemStampChange
  67. {
  68. get
  69. {
  70. return ConfigurationManager.Configuration.SaveLocalMeta;
  71. }
  72. }
  73. /// <summary>
  74. /// Gets the priority.
  75. /// </summary>
  76. /// <value>The priority.</value>
  77. public override MetadataProviderPriority Priority
  78. {
  79. get { return MetadataProviderPriority.Second; }
  80. }
  81. /// <summary>
  82. /// Gets a value indicating whether [requires internet].
  83. /// </summary>
  84. /// <value><c>true</c> if [requires internet]; otherwise, <c>false</c>.</value>
  85. public override bool RequiresInternet
  86. {
  87. get
  88. {
  89. return true;
  90. }
  91. }
  92. protected const string RootUrl = @"http://ws.audioscrobbler.com/2.0/?";
  93. protected static string ApiKey = "7b76553c3eb1d341d642755aecc40a33";
  94. /// <summary>
  95. /// Determines whether [has local meta] [the specified item].
  96. /// </summary>
  97. /// <param name="item">The item.</param>
  98. /// <returns><c>true</c> if [has local meta] [the specified item]; otherwise, <c>false</c>.</returns>
  99. protected bool HasLocalMeta(BaseItem item)
  100. {
  101. return item.ResolveArgs.ContainsMetaFileByName(LocalMetaFileName);
  102. }
  103. /// <summary>
  104. /// Fetches the items data.
  105. /// </summary>
  106. /// <param name="item">The item.</param>
  107. /// <param name="cancellationToken"></param>
  108. /// <returns>Task.</returns>
  109. protected virtual async Task FetchData(BaseItem item, CancellationToken cancellationToken)
  110. {
  111. var id = item.GetProviderId(MetadataProviders.Musicbrainz) ?? await FindId(item, cancellationToken).ConfigureAwait(false);
  112. if (id != null)
  113. {
  114. Logger.Debug("LastfmProvider - getting info for {0}", item.Name);
  115. cancellationToken.ThrowIfCancellationRequested();
  116. item.SetProviderId(MetadataProviders.Musicbrainz, id);
  117. await FetchLastfmData(item, id, cancellationToken).ConfigureAwait(false);
  118. }
  119. else
  120. {
  121. Logger.Info("LastfmProvider could not find " + item.Name + ". Check name on Last.fm.");
  122. }
  123. }
  124. protected abstract Task<string> FindId(BaseItem item, CancellationToken cancellationToken);
  125. protected abstract Task FetchLastfmData(BaseItem item, string id, CancellationToken cancellationToken);
  126. /// <summary>
  127. /// Encodes an URL.
  128. /// </summary>
  129. /// <param name="name">The name.</param>
  130. /// <returns>System.String.</returns>
  131. protected static string UrlEncode(string name)
  132. {
  133. return WebUtility.UrlEncode(name);
  134. }
  135. protected override bool NeedsRefreshInternal(BaseItem item, BaseProviderInfo providerInfo)
  136. {
  137. if (item.DontFetchMeta) return false;
  138. if (ConfigurationManager.Configuration.SaveLocalMeta && HasFileSystemStampChanged(item, providerInfo))
  139. {
  140. //If they deleted something from file system, chances are, this item was mis-identified the first time
  141. item.SetProviderId(MetadataProviders.Musicbrainz, null);
  142. Logger.Debug("LastfmProvider reports file system stamp change...");
  143. return true;
  144. }
  145. if (providerInfo.LastRefreshStatus == ProviderRefreshStatus.CompletedWithErrors)
  146. {
  147. Logger.Debug("LastfmProvider for {0} - last attempt had errors. Will try again.", item.Path);
  148. return true;
  149. }
  150. var downloadDate = providerInfo.LastRefreshed;
  151. if (ConfigurationManager.Configuration.MetadataRefreshDays == -1 && downloadDate != DateTime.MinValue)
  152. {
  153. return false;
  154. }
  155. if (DateTime.Today.Subtract(item.DateCreated).TotalDays > 180 && downloadDate != DateTime.MinValue)
  156. return false; // don't trigger a refresh data for item that are more than 6 months old and have been refreshed before
  157. if (DateTime.Today.Subtract(downloadDate).TotalDays < ConfigurationManager.Configuration.MetadataRefreshDays) // only refresh every n days
  158. return false;
  159. Logger.Debug("LastfmProvider - " + item.Name + " needs refresh. Download date: " + downloadDate + " item created date: " + item.DateCreated + " Check for Update age: " + ConfigurationManager.Configuration.MetadataRefreshDays);
  160. return true;
  161. }
  162. /// <summary>
  163. /// Fetches metadata and returns true or false indicating if any work that requires persistence was done
  164. /// </summary>
  165. /// <param name="item">The item.</param>
  166. /// <param name="force">if set to <c>true</c> [force].</param>
  167. /// <param name="cancellationToken">The cancellation token</param>
  168. /// <returns>Task{System.Boolean}.</returns>
  169. protected override async Task<bool> FetchAsyncInternal(BaseItem item, bool force, CancellationToken cancellationToken)
  170. {
  171. if (item.DontFetchMeta)
  172. {
  173. Logger.Info("LastfmProvider - Not fetching because requested to ignore " + item.Name);
  174. return false;
  175. }
  176. cancellationToken.ThrowIfCancellationRequested();
  177. if (!ConfigurationManager.Configuration.SaveLocalMeta || !HasLocalMeta(item) || (force && !HasLocalMeta(item)))
  178. {
  179. try
  180. {
  181. await FetchData(item, cancellationToken).ConfigureAwait(false);
  182. SetLastRefreshed(item, DateTime.UtcNow);
  183. }
  184. catch (LastfmProviderException)
  185. {
  186. SetLastRefreshed(item, DateTime.UtcNow, ProviderRefreshStatus.CompletedWithErrors);
  187. }
  188. return true;
  189. }
  190. Logger.Debug("LastfmProvider not fetching because local meta exists for " + item.Name);
  191. SetLastRefreshed(item, DateTime.UtcNow);
  192. return true;
  193. }
  194. }
  195. #region Result Objects
  196. public class LastfmStats
  197. {
  198. public string listeners { get; set; }
  199. public string playcount { get; set; }
  200. }
  201. public class LastfmTag
  202. {
  203. public string name { get; set; }
  204. public string url { get; set; }
  205. }
  206. public class LastfmTags
  207. {
  208. public List<LastfmTag> tag { get; set; }
  209. }
  210. public class LastfmFormationInfo
  211. {
  212. public string yearfrom { get; set; }
  213. public string yearto { get; set; }
  214. }
  215. public class LastFmBio
  216. {
  217. public string published { get; set; }
  218. public string summary { get; set; }
  219. public string content { get; set; }
  220. public string placeformed { get; set; }
  221. public string yearformed { get; set; }
  222. public List<LastfmFormationInfo> formationlist { get; set; }
  223. }
  224. public class LastfmArtist
  225. {
  226. public string name { get; set; }
  227. public string mbid { get; set; }
  228. public string url { get; set; }
  229. public string streamable { get; set; }
  230. public string ontour { get; set; }
  231. public LastfmStats stats { get; set; }
  232. public List<LastfmArtist> similar { get; set; }
  233. public LastfmTags tags { get; set; }
  234. public LastFmBio bio { get; set; }
  235. }
  236. public class LastfmAlbum
  237. {
  238. public string name { get; set; }
  239. public string artist { get; set; }
  240. public string id { get; set; }
  241. public string mbid { get; set; }
  242. public string releasedate { get; set; }
  243. public int listeners { get; set; }
  244. public int playcount { get; set; }
  245. public LastfmTags toptags { get; set; }
  246. public LastFmBio wiki { get; set; }
  247. }
  248. public class LastfmGetAlbumResult
  249. {
  250. public LastfmAlbum album { get; set; }
  251. }
  252. public class LastfmGetArtistResult
  253. {
  254. public LastfmArtist artist { get; set; }
  255. }
  256. public class Artistmatches
  257. {
  258. public List<LastfmArtist> artist { get; set; }
  259. }
  260. public class LastfmArtistSearchResult
  261. {
  262. public Artistmatches artistmatches { get; set; }
  263. }
  264. public class LastfmArtistSearchResults
  265. {
  266. public LastfmArtistSearchResult results { get; set; }
  267. }
  268. #endregion
  269. }