BaseMetadataProvider.cs 10 KB

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