using MediaBrowser.Common.Net;
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Model.Logging;
using MediaBrowser.Model.Serialization;
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
namespace MediaBrowser.Controller.Providers.Movies
{
///
/// Class PersonProviderFromJson
///
class PersonProviderFromJson : TmdbPersonProvider
{
public PersonProviderFromJson(IHttpClient httpClient, IJsonSerializer jsonSerializer, ILogManager logManager, IServerConfigurationManager configurationManager, IProviderManager providerManager)
: base(httpClient, jsonSerializer, logManager, configurationManager, providerManager)
{
}
///
/// 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.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}.
public override Task FetchAsync(BaseItem item, bool force, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
try
{
var personInfo = JsonSerializer.DeserializeFromFile(Path.Combine(item.MetaLocation, MetaFileName));
cancellationToken.ThrowIfCancellationRequested();
ProcessInfo((Person)item, personInfo);
SetLastRefreshed(item, DateTime.UtcNow);
return TrueTaskResult;
}
catch (FileNotFoundException)
{
// This is okay - just means we force refreshed and there isn't a json file
return FalseTaskResult;
}
}
}
}