jellyfin-server/MediaBrowser.Controller/Providers/Movies/OpenMovieDatabaseProvider.cs

226 lines
7.8 KiB
C#
Raw Normal View History

2013-05-06 20:47:37 +00:00
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.Globalization;
2013-05-06 20:47:37 +00:00
using System.Threading;
using System.Threading.Tasks;
namespace MediaBrowser.Controller.Providers.Movies
{
public class OpenMovieDatabaseProvider : BaseMetadataProvider
2013-05-06 20:47:37 +00:00
{
2013-05-23 15:40:20 +00:00
private readonly SemaphoreSlim _resourcePool = new SemaphoreSlim(2, 2);
2013-05-06 20:47:37 +00:00
/// <summary>
/// Gets the json serializer.
/// </summary>
/// <value>The json serializer.</value>
protected IJsonSerializer JsonSerializer { get; private set; }
/// <summary>
/// Gets the HTTP client.
/// </summary>
/// <value>The HTTP client.</value>
protected IHttpClient HttpClient { get; private set; }
public OpenMovieDatabaseProvider(ILogManager logManager, IServerConfigurationManager configurationManager, IJsonSerializer jsonSerializer, IHttpClient httpClient)
2013-05-06 20:47:37 +00:00
: base(logManager, configurationManager)
{
JsonSerializer = jsonSerializer;
HttpClient = httpClient;
}
/// <summary>
/// Gets the provider version.
/// </summary>
/// <value>The provider version.</value>
protected override string ProviderVersion
{
get
{
return "6";
2013-05-06 20:47:37 +00:00
}
}
/// <summary>
/// Gets a value indicating whether [requires internet].
/// </summary>
/// <value><c>true</c> if [requires internet]; otherwise, <c>false</c>.</value>
public override bool RequiresInternet
{
get
{
return true;
}
}
/// <summary>
/// Gets a value indicating whether [refresh on version change].
/// </summary>
/// <value><c>true</c> if [refresh on version change]; otherwise, <c>false</c>.</value>
protected override bool RefreshOnVersionChange
{
get
{
return true;
}
}
/// <summary>
/// Supports the specified item.
/// </summary>
/// <param name="item">The item.</param>
/// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
public override bool Supports(BaseItem item)
{
var trailer = item as Trailer;
// Don't support local trailers
2013-05-06 20:47:37 +00:00
if (trailer != null)
{
return !trailer.IsLocalTrailer;
}
return item is Movie;
}
/// <summary>
/// Gets the comparison data.
/// </summary>
/// <param name="imdbId">The imdb id.</param>
/// <returns>Guid.</returns>
private Guid GetComparisonData(string imdbId)
{
return string.IsNullOrEmpty(imdbId) ? Guid.Empty : imdbId.GetMD5();
}
/// <summary>
/// Gets the priority.
/// </summary>
/// <value>The priority.</value>
public override MetadataProviderPriority Priority
{
get
{
// Run after moviedb and xml providers
return MetadataProviderPriority.Last;
}
}
/// <summary>
/// Needses the refresh internal.
/// </summary>
/// <param name="item">The item.</param>
/// <param name="providerInfo">The provider info.</param>
/// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
protected override bool NeedsRefreshInternal(BaseItem item, BaseProviderInfo providerInfo)
{
2013-05-12 22:57:51 +00:00
// Refresh if imdb id has changed
if (providerInfo.Data != GetComparisonData(item.GetProviderId(MetadataProviders.Imdb)))
2013-05-06 20:47:37 +00:00
{
return true;
}
return base.NeedsRefreshInternal(item, providerInfo);
}
protected readonly CultureInfo UsCulture = new CultureInfo("en-US");
2013-05-06 20:47:37 +00:00
public override async Task<bool> FetchAsync(BaseItem item, bool force, CancellationToken cancellationToken)
{
BaseProviderInfo data;
if (!item.ProviderData.TryGetValue(Id, out data))
{
data = new BaseProviderInfo();
item.ProviderData[Id] = data;
}
var imdbId = item.GetProviderId(MetadataProviders.Imdb);
2013-05-06 20:47:37 +00:00
if (string.IsNullOrEmpty(imdbId))
2013-05-06 20:47:37 +00:00
{
data.Data = GetComparisonData(imdbId);
2013-05-06 20:47:37 +00:00
data.LastRefreshStatus = ProviderRefreshStatus.Success;
return true;
}
var imdbParam = imdbId.StartsWith("tt", StringComparison.OrdinalIgnoreCase) ? imdbId : "tt" + imdbId;
var url = string.Format("http://www.omdbapi.com/?i={0}&tomatoes=true", imdbParam);
2013-05-06 20:47:37 +00:00
using (var stream = await HttpClient.Get(new HttpRequestOptions
{
Url = url,
ResourcePool = _resourcePool,
CancellationToken = cancellationToken
2013-05-06 20:47:37 +00:00
}).ConfigureAwait(false))
{
var result = JsonSerializer.DeserializeFromStream<RootObject>(stream);
2013-05-06 20:47:37 +00:00
int tomatoMeter;
2013-05-06 20:47:37 +00:00
if (!string.IsNullOrEmpty(result.tomatoMeter) && int.TryParse(result.tomatoMeter, NumberStyles.Integer, UsCulture, out tomatoMeter))
2013-05-06 20:47:37 +00:00
{
item.CriticRating = tomatoMeter;
}
2013-05-06 20:47:37 +00:00
if (!string.IsNullOrEmpty(result.tomatoConsensus)
&& !string.Equals(result.tomatoConsensus, "n/a", StringComparison.OrdinalIgnoreCase)
&& !string.Equals(result.tomatoConsensus, "No consensus yet.", StringComparison.OrdinalIgnoreCase))
{
item.CriticRatingSummary = result.tomatoConsensus;
}
}
data.Data = GetComparisonData(item.GetProviderId(MetadataProviders.Imdb));
2013-05-06 20:47:37 +00:00
data.LastRefreshStatus = ProviderRefreshStatus.Success;
SetLastRefreshed(item, DateTime.UtcNow);
return true;
}
protected class RootObject
2013-05-06 20:47:37 +00:00
{
public string Title { get; set; }
public string Year { get; set; }
public string Rated { get; set; }
public string Released { get; set; }
public string Runtime { get; set; }
public string Genre { get; set; }
public string Director { get; set; }
public string Writer { get; set; }
public string Actors { get; set; }
public string Plot { get; set; }
public string Poster { get; set; }
public string imdbRating { get; set; }
public string imdbVotes { get; set; }
public string imdbID { get; set; }
public string Type { get; set; }
public string tomatoMeter { get; set; }
public string tomatoImage { get; set; }
public string tomatoRating { get; set; }
public string tomatoReviews { get; set; }
public string tomatoFresh { get; set; }
public string tomatoRotten { get; set; }
public string tomatoConsensus { get; set; }
public string tomatoUserMeter { get; set; }
public string tomatoUserRating { get; set; }
public string tomatoUserReviews { get; set; }
public string DVD { get; set; }
public string BoxOffice { get; set; }
public string Production { get; set; }
public string Website { get; set; }
public string Response { get; set; }
2013-05-06 20:47:37 +00:00
}
}
}