using MediaBrowser.Common.Net;
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.Movies;
using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Logging;
using MediaBrowser.Model.Serialization;
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
namespace MediaBrowser.Providers.Movies
{
///
/// Class MovieProviderFromJson
///
public class MovieProviderFromJson : MovieDbProvider
{
public MovieProviderFromJson(ILogManager logManager, IServerConfigurationManager configurationManager, IJsonSerializer jsonSerializer, IHttpClient httpClient, IProviderManager providerManager)
: base(logManager, configurationManager, jsonSerializer, httpClient, providerManager)
{
}
public override bool Supports(BaseItem item)
{
if (item.LocationType != LocationType.FileSystem)
{
return false;
}
var trailer = item as Trailer;
if (trailer != null)
{
return !trailer.IsLocalTrailer;
}
return item is Movie || item is BoxSet || item is MusicVideo;
}
///
/// 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, LocalMetaFileName));
return entry != null ? entry.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(AltMetaFileName))
{
return false; // don't read our file if 3rd party data exists
}
if (!item.ResolveArgs.ContainsMetaFileByName(LocalMetaFileName))
{
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}.
public override Task FetchAsync(BaseItem item, bool force, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
var entry = item.ResolveArgs.GetMetaFileByPath(Path.Combine(item.MetaLocation, LocalMetaFileName));
if (entry != null)
{
// read in our saved meta and pass to processing function
var movieData = JsonSerializer.DeserializeFromFile(entry.FullName);
cancellationToken.ThrowIfCancellationRequested();
ProcessMainInfo(item, movieData);
SetLastRefreshed(item, DateTime.UtcNow);
return TrueTaskResult;
}
return FalseTaskResult;
}
}
}