using MediaBrowser.Common.Extensions;
using MediaBrowser.Common.Net;
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.Movies;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Logging;
using MediaBrowser.Model.Serialization;
using System;
using System.Threading;
using System.Threading.Tasks;
namespace MediaBrowser.Controller.Providers.Movies
{
///
/// Class RottenTomatoesMovieProvider
///
public class RottenTomatoesMovieProvider : BaseMetadataProvider
{
// http://developer.rottentomatoes.com/iodocs
///
/// The API key
///
private const string ApiKey = "x9wjnvv39ntjmt9zs95nm7bg";
///
/// The _rotten tomatoes resource pool
///
private SemaphoreSlim _rottenTomatoesResourcePool = new SemaphoreSlim(3, 3);
///
/// Gets the json serializer.
///
/// The json serializer.
protected IJsonSerializer JsonSerializer { get; private set; }
///
/// Gets the HTTP client.
///
/// The HTTP client.
protected IHttpClient HttpClient { get; private set; }
///
/// Initializes a new instance of the class.
///
/// The log manager.
/// The configuration manager.
/// The json serializer.
/// The HTTP client.
public RottenTomatoesMovieProvider(ILogManager logManager, IServerConfigurationManager configurationManager, IJsonSerializer jsonSerializer, IHttpClient httpClient)
: base(logManager, configurationManager)
{
JsonSerializer = jsonSerializer;
HttpClient = httpClient;
}
///
/// Gets the provider version.
///
/// The provider version.
protected override string ProviderVersion
{
get
{
return "1";
}
}
///
/// Gets a value indicating whether [requires internet].
///
/// true if [requires internet]; otherwise, false.
public override bool RequiresInternet
{
get
{
return true;
}
}
///
/// Gets a value indicating whether [refresh on version change].
///
/// true if [refresh on version change]; otherwise, false.
protected override bool RefreshOnVersionChange
{
get
{
return true;
}
}
///
/// Supportses the specified item.
///
/// The item.
/// true if XXXX, false otherwise
public override bool Supports(BaseItem item)
{
return item is Movie;
}
///
/// Needses the refresh internal.
///
/// The item.
/// The provider info.
/// true if XXXX, false otherwise
protected override bool NeedsRefreshInternal(BaseItem item, BaseProviderInfo providerInfo)
{
if (providerInfo.LastRefreshStatus != ProviderRefreshStatus.Success)
{
Logger.Debug("RottenTomatoesMovieProvider for {0} - last attempt had errors. Will try again.", item.Path);
return true;
}
// Refresh if imdb id has changed
if (providerInfo.Data != GetComparisonData(item.GetProviderId(MetadataProviders.Imdb)))
{
return true;
}
if (DateTime.Today.Subtract(providerInfo.LastRefreshed).TotalDays < ConfigurationManager.Configuration.MetadataRefreshDays)
{
return false;
}
return base.NeedsRefreshInternal(item, providerInfo);
}
///
/// 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)
{
// Do work here
// Lastly, record the Imdb id here
BaseProviderInfo data;
if (item.ProviderData.TryGetValue(Id, out data))
{
data.Data = GetComparisonData(item.GetProviderId(MetadataProviders.Imdb));
}
SetLastRefreshed(item, DateTime.UtcNow);
return Task.FromResult(true);
}
///
/// Gets the comparison data.
///
/// The imdb id.
/// Guid.
private Guid GetComparisonData(string imdbId)
{
return string.IsNullOrEmpty(imdbId) ? Guid.Empty : imdbId.GetMD5();
}
///
/// Gets the priority.
///
/// The priority.
public override MetadataProviderPriority Priority
{
get
{
// Run after moviedb and xml providers
return MetadataProviderPriority.Last;
}
}
}
}