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