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 PersonProviderFromJson
///
[Export(typeof(BaseMetadataProvider))]
class PersonProviderFromJson : TmdbPersonProvider
{
///
/// Supportses the specified item.
///
/// The item.
/// true if XXXX, false otherwise
public override bool Supports(BaseItem item)
{
return item is Person;
}
///
/// Gets a value indicating whether [requires internet].
///
/// true if [requires internet]; otherwise, false.
public override bool RequiresInternet
{
get
{
return false;
}
}
// Need to re-override to jump over intermediate implementation
///
/// 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(MetaFileName))
{
return false;
}
return CompareDate(item) > providerInfo.LastRefreshed;
}
///
/// 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,MetaFileName));
return entry != null ? entry.Value.LastWriteTimeUtc : DateTime.MinValue;
}
///
/// Gets the priority.
///
/// The priority.
public override MetadataProviderPriority Priority
{
get
{
return MetadataProviderPriority.Third;
}
}
///
/// Fetches metadata and returns true or false indicating if any work that requires persistence was done
///
/// The item.
/// if set to true [force].
/// The cancellation token.
/// Task{System.Boolean}.
protected override Task FetchAsyncInternal(BaseItem item, bool force, CancellationToken cancellationToken)
{
return Task.Run(() =>
{
cancellationToken.ThrowIfCancellationRequested();
try
{
var personInfo = JsonSerializer.DeserializeFromFile(Path.Combine(item.MetaLocation, MetaFileName));
cancellationToken.ThrowIfCancellationRequested();
ProcessInfo((Person)item, personInfo);
SetLastRefreshed(item, DateTime.UtcNow);
return true;
}
catch (FileNotFoundException)
{
// This is okay - just means we force refreshed and there isn't a json file
return false;
}
});
}
}
}