BaseMetadataProvider.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. using MediaBrowser.Controller.Entities;
  2. using MediaBrowser.Controller.Library;
  3. using MediaBrowser.Common.Extensions;
  4. using System.Threading.Tasks;
  5. using System;
  6. namespace MediaBrowser.Controller.Providers
  7. {
  8. public abstract class BaseMetadataProvider
  9. {
  10. protected Guid _id;
  11. public virtual Guid Id
  12. {
  13. get
  14. {
  15. if (_id == null) _id = this.GetType().FullName.GetMD5();
  16. return _id;
  17. }
  18. }
  19. public abstract bool Supports(BaseEntity item);
  20. public virtual bool RequiresInternet
  21. {
  22. get
  23. {
  24. return false;
  25. }
  26. }
  27. /// <summary>
  28. /// Returns the last refresh time of this provider for this item. Providers that care should
  29. /// call SetLastRefreshed to update this value.
  30. /// </summary>
  31. /// <param name="item"></param>
  32. /// <returns></returns>
  33. protected virtual DateTime LastRefreshed(BaseEntity item)
  34. {
  35. return (item.ProviderData.GetValueOrDefault(this.Id, new BaseProviderInfo())).LastRefreshed;
  36. }
  37. /// <summary>
  38. /// Sets the persisted last refresh date on the item for this provider.
  39. /// </summary>
  40. /// <param name="item"></param>
  41. /// <param name="value"></param>
  42. protected virtual void SetLastRefreshed(BaseEntity item, DateTime value)
  43. {
  44. var data = item.ProviderData.GetValueOrDefault(this.Id, new BaseProviderInfo());
  45. data.LastRefreshed = value;
  46. item.ProviderData[this.Id] = data;
  47. }
  48. /// <summary>
  49. /// Returns whether or not this provider should be re-fetched. Default functionality can
  50. /// compare a provided date with a last refresh time. This can be overridden for more complex
  51. /// determinations.
  52. /// </summary>
  53. /// <returns></returns>
  54. public virtual bool NeedsRefresh(BaseEntity item)
  55. {
  56. return CompareDate(item) > LastRefreshed(item);
  57. }
  58. /// <summary>
  59. /// Override this to return the date that should be compared to the last refresh date
  60. /// to determine if this provider should be re-fetched.
  61. /// </summary>
  62. protected virtual DateTime CompareDate(BaseEntity item)
  63. {
  64. return DateTime.MinValue.AddMinutes(1); // want this to be greater than mindate so new items will refresh
  65. }
  66. public virtual Task FetchIfNeededAsync(BaseEntity item)
  67. {
  68. if (this.NeedsRefresh(item))
  69. return FetchAsync(item, item.ResolveArgs);
  70. else
  71. return new Task(() => { });
  72. }
  73. public abstract Task FetchAsync(BaseEntity item, ItemResolveEventArgs args);
  74. public abstract MetadataProviderPriority Priority { get; }
  75. }
  76. /// <summary>
  77. /// Determines when a provider should execute, relative to others
  78. /// </summary>
  79. public enum MetadataProviderPriority
  80. {
  81. // Run this provider at the beginning
  82. First = 1,
  83. // Run this provider after all first priority providers
  84. Second = 2,
  85. // Run this provider after all second priority providers
  86. Third = 3,
  87. // Run this provider last
  88. Last = 4
  89. }
  90. }