using MediaBrowser.Common.Serialization;
using MediaBrowser.Controller.Entities;
using System;
using System.ComponentModel.Composition;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
namespace MediaBrowser.Controller.Providers.Movies
{
///
/// Class MovieProviderFromJson
///
[Export(typeof(BaseMetadataProvider))]
public class MovieProviderFromJson : MovieDbProvider
{
///
/// Gets the priority.
///
/// The priority.
public override MetadataProviderPriority Priority
{
get { return MetadataProviderPriority.First; }
}
///
/// Gets a value indicating whether [requires internet].
///
/// true if [requires internet]; otherwise, false.
public override bool RequiresInternet
{
get { return false; }
}
///
/// Override this to return the date that should be compared to the last refresh date
/// to determine if this provider should be re-fetched.
///
/// The item.
/// DateTime.
protected override DateTime CompareDate(BaseItem item)
{
var entry = item.ResolveArgs.GetMetaFileByPath(Path.Combine(item.MetaLocation, LOCAL_META_FILE_NAME));
return entry != null ? entry.Value.LastWriteTimeUtc : DateTime.MinValue;
}
///
/// Needses the refresh internal.
///
/// The item.
/// The provider info.
/// true if XXXX, false otherwise
protected override bool NeedsRefreshInternal(BaseItem item, BaseProviderInfo providerInfo)
{
if (item.ResolveArgs.ContainsMetaFileByName(ALT_META_FILE_NAME))
{
return false; // don't read our file if 3rd party data exists
}
if (!item.ResolveArgs.ContainsMetaFileByName(LOCAL_META_FILE_NAME))
{
return false; // nothing to read
}
// Need to re-override to jump over intermediate implementation
return CompareDate(item) > providerInfo.LastRefreshed;
}
///
/// Fetches the async.
///
/// The item.
/// if set to true [force].
/// The cancellation token.
/// Task{System.Boolean}.
protected override Task FetchAsyncInternal(BaseItem item, bool force, CancellationToken cancellationToken)
{
// Since we don't have anything truly async, and since deserializing can be expensive, create a task to force parallelism
return Task.Run(() =>
{
cancellationToken.ThrowIfCancellationRequested();
var entry = item.ResolveArgs.GetMetaFileByPath(Path.Combine(item.MetaLocation, LOCAL_META_FILE_NAME));
if (entry.HasValue)
{
// read in our saved meta and pass to processing function
var movieData = JsonSerializer.DeserializeFromFile(entry.Value.Path);
cancellationToken.ThrowIfCancellationRequested();
ProcessMainInfo(item, movieData);
SetLastRefreshed(item, DateTime.UtcNow);
return true;
}
return false;
});
}
}
}