BaseMetadataProvider.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. using MediaBrowser.Common.Extensions;
  2. using MediaBrowser.Controller.Configuration;
  3. using MediaBrowser.Controller.Entities;
  4. using MediaBrowser.Model.Entities;
  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. protected 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(5, 5);
  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. /// <summary>
  66. /// Gets a value indicating whether [refresh on version change].
  67. /// </summary>
  68. /// <value><c>true</c> if [refresh on version change]; otherwise, <c>false</c>.</value>
  69. protected virtual bool RefreshOnVersionChange
  70. {
  71. get
  72. {
  73. return false;
  74. }
  75. }
  76. /// <summary>
  77. /// Determines if this provider is relatively slow and, therefore, should be skipped
  78. /// in certain instances. Default is whether or not it requires internet. Can be overridden
  79. /// for explicit designation.
  80. /// </summary>
  81. /// <value><c>true</c> if this instance is slow; otherwise, <c>false</c>.</value>
  82. public virtual bool IsSlow
  83. {
  84. get { return RequiresInternet; }
  85. }
  86. /// <summary>
  87. /// Initializes a new instance of the <see cref="BaseMetadataProvider" /> class.
  88. /// </summary>
  89. protected BaseMetadataProvider(ILogManager logManager, IServerConfigurationManager configurationManager)
  90. {
  91. Logger = logManager.GetLogger(GetType().Name);
  92. LogManager = logManager;
  93. ConfigurationManager = configurationManager;
  94. Id = GetType().FullName.GetMD5();
  95. Initialize();
  96. }
  97. /// <summary>
  98. /// Initializes this instance.
  99. /// </summary>
  100. protected virtual void Initialize()
  101. {
  102. }
  103. /// <summary>
  104. /// Sets the persisted last refresh date on the item for this provider.
  105. /// </summary>
  106. /// <param name="item">The item.</param>
  107. /// <param name="value">The value.</param>
  108. /// <param name="providerVersion">The provider version.</param>
  109. /// <param name="status">The status.</param>
  110. /// <exception cref="System.ArgumentNullException">item</exception>
  111. public virtual void SetLastRefreshed(BaseItem item, DateTime value, string providerVersion, ProviderRefreshStatus status = ProviderRefreshStatus.Success)
  112. {
  113. if (item == null)
  114. {
  115. throw new ArgumentNullException("item");
  116. }
  117. BaseProviderInfo data;
  118. if (!item.ProviderData.TryGetValue(Id, out data))
  119. {
  120. data = new BaseProviderInfo();
  121. }
  122. data.LastRefreshed = value;
  123. data.LastRefreshStatus = status;
  124. data.ProviderVersion = providerVersion;
  125. // Save the file system stamp for future comparisons
  126. if (RefreshOnFileSystemStampChange && item.LocationType == LocationType.FileSystem)
  127. {
  128. data.FileStamp = 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. BaseProviderInfo data;
  157. if (!item.ProviderData.TryGetValue(Id, out data))
  158. {
  159. data = new BaseProviderInfo();
  160. }
  161. return NeedsRefreshInternal(item, data);
  162. }
  163. /// <summary>
  164. /// Needses the refresh internal.
  165. /// </summary>
  166. /// <param name="item">The item.</param>
  167. /// <param name="providerInfo">The provider info.</param>
  168. /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
  169. /// <exception cref="System.ArgumentNullException"></exception>
  170. protected virtual bool NeedsRefreshInternal(BaseItem item, BaseProviderInfo providerInfo)
  171. {
  172. if (item == null)
  173. {
  174. throw new ArgumentNullException("item");
  175. }
  176. if (providerInfo == null)
  177. {
  178. throw new ArgumentNullException("providerInfo");
  179. }
  180. if (item.DontFetchMeta && RequiresInternet) return false;
  181. if (CompareDate(item) > providerInfo.LastRefreshed)
  182. {
  183. return true;
  184. }
  185. if (RefreshOnFileSystemStampChange && item.LocationType == LocationType.FileSystem && HasFileSystemStampChanged(item, providerInfo))
  186. {
  187. return true;
  188. }
  189. if (RefreshOnVersionChange && !String.Equals(ProviderVersion, providerInfo.ProviderVersion))
  190. {
  191. return true;
  192. }
  193. if (RequiresInternet && DateTime.UtcNow > (providerInfo.LastRefreshed.AddDays(ConfigurationManager.Configuration.MetadataRefreshDays)))
  194. {
  195. return true;
  196. }
  197. if (providerInfo.LastRefreshStatus != ProviderRefreshStatus.Success)
  198. {
  199. return true;
  200. }
  201. return false;
  202. }
  203. /// <summary>
  204. /// Determines if the item's file system stamp has changed from the last time the provider refreshed
  205. /// </summary>
  206. /// <param name="item">The item.</param>
  207. /// <param name="providerInfo">The provider info.</param>
  208. /// <returns><c>true</c> if [has file system stamp changed] [the specified item]; otherwise, <c>false</c>.</returns>
  209. protected bool HasFileSystemStampChanged(BaseItem item, BaseProviderInfo providerInfo)
  210. {
  211. return GetCurrentFileSystemStamp(item) != providerInfo.FileStamp;
  212. }
  213. /// <summary>
  214. /// Override this to return the date that should be compared to the last refresh date
  215. /// to determine if this provider should be re-fetched.
  216. /// </summary>
  217. /// <param name="item">The item.</param>
  218. /// <returns>DateTime.</returns>
  219. protected virtual DateTime CompareDate(BaseItem item)
  220. {
  221. return DateTime.MinValue.AddMinutes(1); // want this to be greater than mindate so new items will refresh
  222. }
  223. /// <summary>
  224. /// Fetches metadata and returns true or false indicating if any work that requires persistence was done
  225. /// </summary>
  226. /// <param name="item">The item.</param>
  227. /// <param name="force">if set to <c>true</c> [force].</param>
  228. /// <param name="cancellationToken">The cancellation token.</param>
  229. /// <returns>Task{System.Boolean}.</returns>
  230. /// <exception cref="System.ArgumentNullException"></exception>
  231. public abstract Task<bool> FetchAsync(BaseItem item, bool force, CancellationToken cancellationToken);
  232. /// <summary>
  233. /// Gets the priority.
  234. /// </summary>
  235. /// <value>The priority.</value>
  236. public abstract MetadataProviderPriority Priority { get; }
  237. /// <summary>
  238. /// Returns true or false indicating if the provider should refresh when the contents of it's directory changes
  239. /// </summary>
  240. /// <value><c>true</c> if [refresh on file system stamp change]; otherwise, <c>false</c>.</value>
  241. protected virtual bool RefreshOnFileSystemStampChange
  242. {
  243. get
  244. {
  245. return false;
  246. }
  247. }
  248. /// <summary>
  249. /// Determines if the parent's file system stamp should be used for comparison
  250. /// </summary>
  251. /// <param name="item">The item.</param>
  252. /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
  253. protected virtual bool UseParentFileSystemStamp(BaseItem item)
  254. {
  255. // True when the current item is just a file
  256. return !item.ResolveArgs.IsDirectory;
  257. }
  258. /// <summary>
  259. /// Gets the item's current file system stamp
  260. /// </summary>
  261. /// <param name="item">The item.</param>
  262. /// <returns>Guid.</returns>
  263. private Guid GetCurrentFileSystemStamp(BaseItem item)
  264. {
  265. if (UseParentFileSystemStamp(item) && item.Parent != null)
  266. {
  267. return item.Parent.FileSystemStamp;
  268. }
  269. return item.FileSystemStamp;
  270. }
  271. }
  272. }