Provider Data on item and NeedsRefresh functionality for providers
This commit is contained in:
parent
9fbd88a713
commit
8b548fff38
|
@ -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<Guid, BaseProviderInfo> _providerData;
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
public Dictionary<Guid, BaseProviderInfo> ProviderData
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_providerData == null) _providerData = new Dictionary<Guid, BaseProviderInfo>();
|
||||
return _providerData;
|
||||
}
|
||||
set
|
||||
{
|
||||
_providerData = value;
|
||||
}
|
||||
}
|
||||
|
||||
protected ItemResolveEventArgs _resolveArgs;
|
||||
/// <summary>
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -76,6 +76,7 @@
|
|||
<Compile Include="Entities\Year.cs" />
|
||||
<Compile Include="IO\FileSystemHelper.cs" />
|
||||
<Compile Include="Library\ChildrenChangedEventArgs.cs" />
|
||||
<Compile Include="Providers\BaseProviderInfo.cs" />
|
||||
<Compile Include="Providers\Movies\MovieProviderFromXml.cs" />
|
||||
<Compile Include="Providers\Movies\MovieSpecialFeaturesProvider.cs" />
|
||||
<Compile Include="Providers\TV\EpisodeImageFromMediaLocationProvider.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
|
|||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the last refresh time of this provider for this item. Providers that care should
|
||||
/// call SetLastRefreshed to update this value.
|
||||
/// </summary>
|
||||
/// <param name="item"></param>
|
||||
/// <returns></returns>
|
||||
protected virtual DateTime LastRefreshed(BaseEntity item)
|
||||
{
|
||||
return (item.ProviderData[this.Id] ?? new BaseProviderInfo()).LastRefreshed;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the persisted last refresh date on the item for this provider.
|
||||
/// </summary>
|
||||
/// <param name="item"></param>
|
||||
/// <param name="value"></param>
|
||||
protected virtual void SetLastRefreshed(BaseEntity item, DateTime value)
|
||||
{
|
||||
var data = item.ProviderData[this.Id] ?? new BaseProviderInfo();
|
||||
data.LastRefreshed = value;
|
||||
item.ProviderData[this.Id] = data;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public virtual bool NeedsRefresh(BaseEntity item)
|
||||
{
|
||||
return CompareDate(item) > LastRefreshed(item);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Override this to return the date that should be compared to the last refresh date
|
||||
/// to determine if this provider should be re-fetched.
|
||||
/// </summary>
|
||||
protected virtual DateTime CompareDate(BaseEntity item)
|
||||
{
|
||||
return DateTime.MinValue;
|
||||
}
|
||||
|
||||
public abstract Task FetchAsync(BaseEntity item, ItemResolveEventArgs args);
|
||||
|
||||
public abstract MetadataProviderPriority Priority { get; }
|
||||
|
|
15
MediaBrowser.Controller/Providers/BaseProviderInfo.cs
Normal file
15
MediaBrowser.Controller/Providers/BaseProviderInfo.cs
Normal file
|
@ -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; }
|
||||
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
|
|
Loading…
Reference in New Issue
Block a user