From 8b548fff38641e0de6ea2870a5d37c6bc76a6a90 Mon Sep 17 00:00:00 2001 From: ebr11 Eric Reed spam Date: Mon, 17 Sep 2012 20:06:31 -0400 Subject: [PATCH] Provider Data on item and NeedsRefresh functionality for providers --- .../Entities/BaseEntity.cs | 21 +++++++ MediaBrowser.Controller/Kernel.cs | 6 ++ .../MediaBrowser.Controller.csproj | 1 + .../Providers/BaseMetadataProvider.cs | 55 +++++++++++++++++++ .../Providers/BaseProviderInfo.cs | 15 +++++ .../Providers/Movies/MovieProviderFromXml.cs | 7 +++ 6 files changed, 105 insertions(+) create mode 100644 MediaBrowser.Controller/Providers/BaseProviderInfo.cs diff --git a/MediaBrowser.Controller/Entities/BaseEntity.cs b/MediaBrowser.Controller/Entities/BaseEntity.cs index 7f0ea12b80..5b4a360c1f 100644 --- a/MediaBrowser.Controller/Entities/BaseEntity.cs +++ b/MediaBrowser.Controller/Entities/BaseEntity.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Linq; using MediaBrowser.Controller.Library; using MediaBrowser.Controller.IO; +using MediaBrowser.Controller.Providers; namespace MediaBrowser.Controller.Entities { @@ -29,6 +30,26 @@ namespace MediaBrowser.Controller.Entities { return Name; } + protected Dictionary _providerData; + /// + /// Holds persistent data for providers like last refresh date. + /// Providers can use this to determine if they need to refresh. + /// The BaseProviderInfo class can be extended to hold anything a provider may need. + /// + /// Keyed by a unique provider ID. + /// + public Dictionary ProviderData + { + get + { + if (_providerData == null) _providerData = new Dictionary(); + return _providerData; + } + set + { + _providerData = value; + } + } protected ItemResolveEventArgs _resolveArgs; /// diff --git a/MediaBrowser.Controller/Kernel.cs b/MediaBrowser.Controller/Kernel.cs index f26783480b..6baae88113 100644 --- a/MediaBrowser.Controller/Kernel.cs +++ b/MediaBrowser.Controller/Kernel.cs @@ -368,6 +368,12 @@ namespace MediaBrowser.Controller continue; } + // Skip if provider says we don't need to run + if (!provider.NeedsRefresh(item)) + { + continue; + } + try { await provider.FetchAsync(item, item.ResolveArgs).ConfigureAwait(false); diff --git a/MediaBrowser.Controller/MediaBrowser.Controller.csproj b/MediaBrowser.Controller/MediaBrowser.Controller.csproj index 440a3d2dfa..f5d89f87d2 100644 --- a/MediaBrowser.Controller/MediaBrowser.Controller.csproj +++ b/MediaBrowser.Controller/MediaBrowser.Controller.csproj @@ -76,6 +76,7 @@ + diff --git a/MediaBrowser.Controller/Providers/BaseMetadataProvider.cs b/MediaBrowser.Controller/Providers/BaseMetadataProvider.cs index bae1ff0f9c..80a264af27 100644 --- a/MediaBrowser.Controller/Providers/BaseMetadataProvider.cs +++ b/MediaBrowser.Controller/Providers/BaseMetadataProvider.cs @@ -1,11 +1,23 @@ using MediaBrowser.Controller.Entities; using MediaBrowser.Controller.Library; +using MediaBrowser.Common.Extensions; using System.Threading.Tasks; +using System; namespace MediaBrowser.Controller.Providers { public abstract class BaseMetadataProvider { + protected Guid _id; + public virtual Guid Id + { + get + { + if (_id == null) _id = this.GetType().FullName.GetMD5(); + return _id; + } + } + public abstract bool Supports(BaseEntity item); public virtual bool RequiresInternet @@ -16,6 +28,49 @@ namespace MediaBrowser.Controller.Providers } } + /// + /// Returns the last refresh time of this provider for this item. Providers that care should + /// call SetLastRefreshed to update this value. + /// + /// + /// + protected virtual DateTime LastRefreshed(BaseEntity item) + { + return (item.ProviderData[this.Id] ?? new BaseProviderInfo()).LastRefreshed; + } + + /// + /// Sets the persisted last refresh date on the item for this provider. + /// + /// + /// + protected virtual void SetLastRefreshed(BaseEntity item, DateTime value) + { + var data = item.ProviderData[this.Id] ?? new BaseProviderInfo(); + data.LastRefreshed = value; + item.ProviderData[this.Id] = data; + } + + /// + /// Returns whether or not this provider should be re-fetched. Default functionality can + /// compare a provided date with a last refresh time. This can be overridden for more complex + /// determinations. + /// + /// + public virtual bool NeedsRefresh(BaseEntity item) + { + return CompareDate(item) > LastRefreshed(item); + } + + /// + /// Override this to return the date that should be compared to the last refresh date + /// to determine if this provider should be re-fetched. + /// + protected virtual DateTime CompareDate(BaseEntity item) + { + return DateTime.MinValue; + } + public abstract Task FetchAsync(BaseEntity item, ItemResolveEventArgs args); public abstract MetadataProviderPriority Priority { get; } diff --git a/MediaBrowser.Controller/Providers/BaseProviderInfo.cs b/MediaBrowser.Controller/Providers/BaseProviderInfo.cs new file mode 100644 index 0000000000..1538b2262f --- /dev/null +++ b/MediaBrowser.Controller/Providers/BaseProviderInfo.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace MediaBrowser.Controller.Providers +{ + public class BaseProviderInfo + { + public Guid ProviderId { get; set; } + public DateTime LastRefreshed { get; set; } + + } +} diff --git a/MediaBrowser.Controller/Providers/Movies/MovieProviderFromXml.cs b/MediaBrowser.Controller/Providers/Movies/MovieProviderFromXml.cs index f1d625aeb8..7ef53d546a 100644 --- a/MediaBrowser.Controller/Providers/Movies/MovieProviderFromXml.cs +++ b/MediaBrowser.Controller/Providers/Movies/MovieProviderFromXml.cs @@ -4,6 +4,7 @@ using MediaBrowser.Controller.Library; using System.ComponentModel.Composition; using System.IO; using System.Threading.Tasks; +using System; namespace MediaBrowser.Controller.Providers.Movies { @@ -20,6 +21,12 @@ namespace MediaBrowser.Controller.Providers.Movies get { return MetadataProviderPriority.First; } } + protected override DateTime CompareDate(BaseEntity item) + { + var entry = item.ResolveArgs.GetFileSystemEntry(Path.Combine(item.Path, "movie.xml")); + return entry != null ? entry.Value.LastWriteTimeUtc : DateTime.MinValue; + } + public override async Task FetchAsync(BaseEntity item, ItemResolveEventArgs args) { await Task.Run(() => Fetch(item, args)).ConfigureAwait(false);