2
0

BaseMetadataProvider.cs 10 KB

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