BaseMetadataProvider.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. using MediaBrowser.Common.Extensions;
  2. using MediaBrowser.Controller.Configuration;
  3. using MediaBrowser.Controller.Entities;
  4. using MediaBrowser.Controller.Library;
  5. using MediaBrowser.Model.Logging;
  6. using System;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. namespace MediaBrowser.Controller.Providers
  10. {
  11. /// <summary>
  12. /// Class BaseMetadataProvider
  13. /// </summary>
  14. public abstract class BaseMetadataProvider
  15. {
  16. /// <summary>
  17. /// Gets the logger.
  18. /// </summary>
  19. /// <value>The logger.</value>
  20. protected ILogger Logger { get; set; }
  21. protected ILogManager LogManager { get; set; }
  22. /// <summary>
  23. /// Gets the configuration manager.
  24. /// </summary>
  25. /// <value>The configuration manager.</value>
  26. protected IServerConfigurationManager ConfigurationManager { get; private set; }
  27. /// <summary>
  28. /// The _id
  29. /// </summary>
  30. public readonly Guid Id;
  31. /// <summary>
  32. /// The true task result
  33. /// </summary>
  34. protected static readonly Task<bool> TrueTaskResult = Task.FromResult(true);
  35. protected static readonly Task<bool> FalseTaskResult = Task.FromResult(false);
  36. protected static readonly SemaphoreSlim XmlParsingResourcePool = new SemaphoreSlim(4, 4);
  37. /// <summary>
  38. /// Supportses the specified item.
  39. /// </summary>
  40. /// <param name="item">The item.</param>
  41. /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
  42. public abstract bool Supports(BaseItem item);
  43. /// <summary>
  44. /// Gets a value indicating whether [requires internet].
  45. /// </summary>
  46. /// <value><c>true</c> if [requires internet]; otherwise, <c>false</c>.</value>
  47. public virtual bool RequiresInternet
  48. {
  49. get
  50. {
  51. return false;
  52. }
  53. }
  54. /// <summary>
  55. /// Gets the provider version.
  56. /// </summary>
  57. /// <value>The provider version.</value>
  58. protected virtual string ProviderVersion
  59. {
  60. get
  61. {
  62. return null;
  63. }
  64. }
  65. public virtual ItemUpdateType ItemUpdateType
  66. {
  67. get { return RequiresInternet ? ItemUpdateType.MetadataDownload : ItemUpdateType.MetadataImport; }
  68. }
  69. /// <summary>
  70. /// Gets a value indicating whether [refresh on version change].
  71. /// </summary>
  72. /// <value><c>true</c> if [refresh on version change]; otherwise, <c>false</c>.</value>
  73. protected virtual bool RefreshOnVersionChange
  74. {
  75. get
  76. {
  77. return false;
  78. }
  79. }
  80. /// <summary>
  81. /// Determines if this provider is relatively slow and, therefore, should be skipped
  82. /// in certain instances. Default is whether or not it requires internet. Can be overridden
  83. /// for explicit designation.
  84. /// </summary>
  85. /// <value><c>true</c> if this instance is slow; otherwise, <c>false</c>.</value>
  86. public virtual bool IsSlow
  87. {
  88. get { return RequiresInternet; }
  89. }
  90. /// <summary>
  91. /// Initializes a new instance of the <see cref="BaseMetadataProvider" /> class.
  92. /// </summary>
  93. protected BaseMetadataProvider(ILogManager logManager, IServerConfigurationManager configurationManager)
  94. {
  95. Logger = logManager.GetLogger(GetType().Name);
  96. LogManager = logManager;
  97. ConfigurationManager = configurationManager;
  98. Id = GetType().FullName.GetMD5();
  99. Initialize();
  100. }
  101. /// <summary>
  102. /// Initializes this instance.
  103. /// </summary>
  104. protected virtual void Initialize()
  105. {
  106. }
  107. /// <summary>
  108. /// Sets the persisted last refresh date on the item for this provider.
  109. /// </summary>
  110. /// <param name="item">The item.</param>
  111. /// <param name="value">The value.</param>
  112. /// <param name="providerVersion">The provider version.</param>
  113. /// <param name="providerInfo">The provider information.</param>
  114. /// <param name="status">The status.</param>
  115. /// <exception cref="System.ArgumentNullException">item</exception>
  116. public virtual void SetLastRefreshed(BaseItem item, DateTime value, string providerVersion,
  117. BaseProviderInfo providerInfo, ProviderRefreshStatus status = ProviderRefreshStatus.Success)
  118. {
  119. if (item == null)
  120. {
  121. throw new ArgumentNullException("item");
  122. }
  123. providerInfo.LastRefreshed = value;
  124. providerInfo.LastRefreshStatus = status;
  125. providerInfo.ProviderVersion = providerVersion;
  126. }
  127. /// <summary>
  128. /// Sets the last refreshed.
  129. /// </summary>
  130. /// <param name="item">The item.</param>
  131. /// <param name="value">The value.</param>
  132. /// <param name="providerInfo">The provider information.</param>
  133. /// <param name="status">The status.</param>
  134. public void SetLastRefreshed(BaseItem item, DateTime value,
  135. BaseProviderInfo providerInfo, ProviderRefreshStatus status = ProviderRefreshStatus.Success)
  136. {
  137. SetLastRefreshed(item, value, ProviderVersion, providerInfo, status);
  138. }
  139. /// <summary>
  140. /// Returns whether or not this provider should be re-fetched. Default functionality can
  141. /// compare a provided date with a last refresh time. This can be overridden for more complex
  142. /// determinations.
  143. /// </summary>
  144. /// <param name="item">The item.</param>
  145. /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
  146. /// <exception cref="System.ArgumentNullException"></exception>
  147. public bool NeedsRefresh(BaseItem item, BaseProviderInfo data)
  148. {
  149. if (item == null)
  150. {
  151. throw new ArgumentNullException();
  152. }
  153. return NeedsRefreshInternal(item, data);
  154. }
  155. /// <summary>
  156. /// Gets a value indicating whether [enforce dont fetch metadata].
  157. /// </summary>
  158. /// <value><c>true</c> if [enforce dont fetch metadata]; otherwise, <c>false</c>.</value>
  159. public virtual bool EnforceDontFetchMetadata
  160. {
  161. get
  162. {
  163. return true;
  164. }
  165. }
  166. /// <summary>
  167. /// Needses the refresh internal.
  168. /// </summary>
  169. /// <param name="item">The item.</param>
  170. /// <param name="providerInfo">The provider info.</param>
  171. /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
  172. /// <exception cref="System.ArgumentNullException"></exception>
  173. protected virtual bool NeedsRefreshInternal(BaseItem item, BaseProviderInfo providerInfo)
  174. {
  175. if (item == null)
  176. {
  177. throw new ArgumentNullException("item");
  178. }
  179. if (providerInfo == null)
  180. {
  181. throw new ArgumentNullException("providerInfo");
  182. }
  183. if (providerInfo.LastRefreshed == default(DateTime))
  184. {
  185. return true;
  186. }
  187. if (NeedsRefreshBasedOnCompareDate(item, providerInfo))
  188. {
  189. return true;
  190. }
  191. if (RefreshOnVersionChange && !String.Equals(ProviderVersion, providerInfo.ProviderVersion))
  192. {
  193. return true;
  194. }
  195. if (providerInfo.LastRefreshStatus != ProviderRefreshStatus.Success)
  196. {
  197. return true;
  198. }
  199. return false;
  200. }
  201. /// <summary>
  202. /// Needses the refresh based on compare date.
  203. /// </summary>
  204. /// <param name="item">The item.</param>
  205. /// <param name="providerInfo">The provider info.</param>
  206. /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
  207. protected virtual bool NeedsRefreshBasedOnCompareDate(BaseItem item, BaseProviderInfo providerInfo)
  208. {
  209. return CompareDate(item) > providerInfo.LastRefreshed;
  210. }
  211. /// <summary>
  212. /// Override this to return the date that should be compared to the last refresh date
  213. /// to determine if this provider should be re-fetched.
  214. /// </summary>
  215. /// <param name="item">The item.</param>
  216. /// <returns>DateTime.</returns>
  217. protected virtual DateTime CompareDate(BaseItem item)
  218. {
  219. return DateTime.MinValue.AddMinutes(1); // want this to be greater than mindate so new items will refresh
  220. }
  221. /// <summary>
  222. /// Fetches metadata and returns true or false indicating if any work that requires persistence was done
  223. /// </summary>
  224. /// <param name="item">The item.</param>
  225. /// <param name="force">if set to <c>true</c> [force].</param>
  226. /// <param name="providerInfo">The provider information.</param>
  227. /// <param name="cancellationToken">The cancellation token.</param>
  228. /// <returns>Task{System.Boolean}.</returns>
  229. /// <exception cref="System.ArgumentNullException"></exception>
  230. public abstract Task<bool> FetchAsync(BaseItem item, bool force, BaseProviderInfo providerInfo, CancellationToken cancellationToken);
  231. /// <summary>
  232. /// Gets the priority.
  233. /// </summary>
  234. /// <value>The priority.</value>
  235. public abstract MetadataProviderPriority Priority { get; }
  236. }
  237. }