From 256bda66f28c8cd726ce6ccaed4443dc454113f6 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Tue, 7 May 2013 00:34:27 -0400 Subject: [PATCH] use open movie database for critic scores --- .../MediaBrowser.Controller.csproj | 2 +- ...ovider.cs => OpenMovieDatabaseProvider.cs} | 136 ++++++++---------- .../Movies/RottenTomatoesMovieProvider.cs | 111 ++++++++++---- 3 files changed, 147 insertions(+), 102 deletions(-) rename MediaBrowser.Controller/Providers/Movies/{RottenTomatoesMovieReviewsProvider.cs => OpenMovieDatabaseProvider.cs} (58%) diff --git a/MediaBrowser.Controller/MediaBrowser.Controller.csproj b/MediaBrowser.Controller/MediaBrowser.Controller.csproj index cb7f635ad..8c1cc5163 100644 --- a/MediaBrowser.Controller/MediaBrowser.Controller.csproj +++ b/MediaBrowser.Controller/MediaBrowser.Controller.csproj @@ -116,8 +116,8 @@ + - diff --git a/MediaBrowser.Controller/Providers/Movies/RottenTomatoesMovieReviewsProvider.cs b/MediaBrowser.Controller/Providers/Movies/OpenMovieDatabaseProvider.cs similarity index 58% rename from MediaBrowser.Controller/Providers/Movies/RottenTomatoesMovieReviewsProvider.cs rename to MediaBrowser.Controller/Providers/Movies/OpenMovieDatabaseProvider.cs index 3e2a5b558..10ddb4516 100644 --- a/MediaBrowser.Controller/Providers/Movies/RottenTomatoesMovieReviewsProvider.cs +++ b/MediaBrowser.Controller/Providers/Movies/OpenMovieDatabaseProvider.cs @@ -7,21 +7,15 @@ using MediaBrowser.Model.Entities; using MediaBrowser.Model.Logging; using MediaBrowser.Model.Serialization; using System; -using System.Collections.Generic; -using System.Linq; +using System.Globalization; using System.Threading; using System.Threading.Tasks; namespace MediaBrowser.Controller.Providers.Movies { - /// - /// Class RottenTomatoesMovieProvider - /// - public class RottenTomatoesMovieReviewsProvider : BaseMetadataProvider + public class OpenMovieDatabaseProvider : BaseMetadataProvider { - // http://developer.rottentomatoes.com/iodocs - - private const string MoviesReviews = @"movies/{1}/reviews.json?review_type=top_critic&page_limit=10&page=1&country=us&apikey={0}"; + private readonly SemaphoreSlim _resourcePool = new SemaphoreSlim(1, 1); /// /// Gets the json serializer. @@ -35,14 +29,7 @@ namespace MediaBrowser.Controller.Providers.Movies /// 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 RottenTomatoesMovieReviewsProvider(ILogManager logManager, IServerConfigurationManager configurationManager, IJsonSerializer jsonSerializer, IHttpClient httpClient) + public OpenMovieDatabaseProvider(ILogManager logManager, IServerConfigurationManager configurationManager, IJsonSerializer jsonSerializer, IHttpClient httpClient) : base(logManager, configurationManager) { JsonSerializer = jsonSerializer; @@ -57,7 +44,7 @@ namespace MediaBrowser.Controller.Providers.Movies { get { - return "5"; + return "6"; } } @@ -92,15 +79,14 @@ namespace MediaBrowser.Controller.Providers.Movies /// true if XXXX, false otherwise public override bool Supports(BaseItem item) { - return false; var trailer = item as Trailer; + // Don't support local trailers if (trailer != null) { return !trailer.IsLocalTrailer; } - // Don't support local trailers return item is Movie; } @@ -136,7 +122,7 @@ namespace MediaBrowser.Controller.Providers.Movies protected override bool NeedsRefreshInternal(BaseItem item, BaseProviderInfo providerInfo) { // Refresh if rt id has changed - if (providerInfo.Data != GetComparisonData(item.GetProviderId(MetadataProviders.RottenTomatoes))) + if (providerInfo.Data != GetComparisonData(item.GetProviderId(MetadataProviders.Imdb))) { return true; } @@ -144,13 +130,8 @@ namespace MediaBrowser.Controller.Providers.Movies 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}. + protected readonly CultureInfo UsCulture = new CultureInfo("en-US"); + public override async Task FetchAsync(BaseItem item, bool force, CancellationToken cancellationToken) { BaseProviderInfo data; @@ -161,76 +142,85 @@ namespace MediaBrowser.Controller.Providers.Movies item.ProviderData[Id] = data; } - var rottenTomatoesId = item.GetProviderId(MetadataProviders.RottenTomatoes); + var imdbId = item.GetProviderId(MetadataProviders.Imdb); - - if (string.IsNullOrEmpty(rottenTomatoesId)) + if (string.IsNullOrEmpty(imdbId)) { - data.Data = GetComparisonData(rottenTomatoesId); + data.Data = GetComparisonData(imdbId); 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); + using (var stream = await HttpClient.Get(new HttpRequestOptions { - Url = GetMovieReviewsUrl(rottenTomatoesId), - ResourcePool = RottenTomatoesMovieProvider.Current.RottenTomatoesResourcePool, + Url = url, + ResourcePool = _resourcePool, CancellationToken = cancellationToken, EnableResponseCache = true }).ConfigureAwait(false)) { + var result = JsonSerializer.DeserializeFromStream(stream); - var result = JsonSerializer.DeserializeFromStream(stream); + int tomatoMeter; - item.CriticReviews = result.reviews.Select(rtReview => new ItemReview + if (!string.IsNullOrEmpty(result.tomatoMeter) && int.TryParse(result.tomatoMeter, NumberStyles.Integer, UsCulture, out tomatoMeter)) { - ReviewerName = rtReview.critic, - Publisher = rtReview.publication, - Date = DateTime.Parse(rtReview.date).ToUniversalTime(), - Caption = rtReview.quote, - Url = rtReview.links.review, - Likes = string.Equals(rtReview.freshness, "fresh", StringComparison.OrdinalIgnoreCase) + item.CriticRating = tomatoMeter; + } - }).ToList(); + 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(rottenTomatoesId); + + data.Data = GetComparisonData(item.GetProviderId(MetadataProviders.Imdb)); data.LastRefreshStatus = ProviderRefreshStatus.Success; SetLastRefreshed(item, DateTime.UtcNow); return true; } - // Utility functions to get the URL of the API calls - private string GetMovieReviewsUrl(string rtId) + protected class RootObject { - return RottenTomatoesMovieProvider.BasicUrl + string.Format(MoviesReviews, RottenTomatoesMovieProvider.ApiKey, rtId); - } - - // Data contract classes for use with the Rotten Tomatoes API - - protected class RTReviewList - { - public int total { get; set; } - public List reviews { get; set; } - } - - protected class RTReview - { - public string critic { get; set; } - public string date { get; set; } - public string freshness { get; set; } - public string publication { get; set; } - public string quote { get; set; } - public RTReviewLink links { get; set; } - public string original_score { get; set; } - } - - protected class RTReviewLink - { - public string review { get; set; } + 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; } } } -} \ No newline at end of file +} diff --git a/MediaBrowser.Controller/Providers/Movies/RottenTomatoesMovieProvider.cs b/MediaBrowser.Controller/Providers/Movies/RottenTomatoesMovieProvider.cs index acf8c8da6..1ddc8d72c 100644 --- a/MediaBrowser.Controller/Providers/Movies/RottenTomatoesMovieProvider.cs +++ b/MediaBrowser.Controller/Providers/Movies/RottenTomatoesMovieProvider.cs @@ -17,23 +17,18 @@ namespace MediaBrowser.Controller.Providers.Movies /// /// Class RottenTomatoesMovieProvider /// - public class RottenTomatoesMovieProvider : BaseMetadataProvider + public class RottenTomatoesProvider : BaseMetadataProvider { // http://developer.rottentomatoes.com/iodocs - /// - /// The API key - /// - internal const string ApiKey = "x9wjnvv39ntjmt9zs95nm7bg"; - internal const string BasicUrl = @"http://api.rottentomatoes.com/api/public/v1.0/"; + private const string MoviesReviews = @"movies/{1}/reviews.json?review_type=top_critic&page_limit=10&page=1&country=us&apikey={0}"; + + private const string ApiKey = "x9wjnvv39ntjmt9zs95nm7bg"; + + private const string BasicUrl = @"http://api.rottentomatoes.com/api/public/v1.0/"; private const string MovieImdb = @"movie_alias.json?id={1}&type=imdb&apikey={0}"; - internal static RottenTomatoesMovieProvider Current { get; private set; } - - /// - /// The _rotten tomatoes resource pool - /// - internal readonly SemaphoreSlim RottenTomatoesResourcePool = new SemaphoreSlim(1, 1); + private readonly SemaphoreSlim _rottenTomatoesResourcePool = new SemaphoreSlim(1, 1); /// /// Gets the json serializer. @@ -54,12 +49,11 @@ namespace MediaBrowser.Controller.Providers.Movies /// The configuration manager. /// The json serializer. /// The HTTP client. - public RottenTomatoesMovieProvider(ILogManager logManager, IServerConfigurationManager configurationManager, IJsonSerializer jsonSerializer, IHttpClient httpClient) + public RottenTomatoesProvider(ILogManager logManager, IServerConfigurationManager configurationManager, IJsonSerializer jsonSerializer, IHttpClient httpClient) : base(logManager, configurationManager) { JsonSerializer = jsonSerializer; HttpClient = httpClient; - Current = this; } /// @@ -136,7 +130,7 @@ namespace MediaBrowser.Controller.Providers.Movies get { // Run after moviedb and xml providers - return MetadataProviderPriority.Third; + return MetadataProviderPriority.Last; } } @@ -148,7 +142,7 @@ namespace MediaBrowser.Controller.Providers.Movies /// true if XXXX, false otherwise protected override bool NeedsRefreshInternal(BaseItem item, BaseProviderInfo providerInfo) { - // Refresh if imdb id has changed + // Refresh if rt id has changed if (providerInfo.Data != GetComparisonData(item.GetProviderId(MetadataProviders.Imdb))) { return true; @@ -175,7 +169,7 @@ namespace MediaBrowser.Controller.Providers.Movies } var imdbId = item.GetProviderId(MetadataProviders.Imdb); - + if (string.IsNullOrEmpty(imdbId)) { data.Data = GetComparisonData(imdbId); @@ -183,11 +177,54 @@ namespace MediaBrowser.Controller.Providers.Movies return true; } + await FetchRottenTomatoesId(item, cancellationToken).ConfigureAwait(false); + + using (var stream = await HttpClient.Get(new HttpRequestOptions + { + Url = GetMovieReviewsUrl(item.GetProviderId(MetadataProviders.RottenTomatoes)), + ResourcePool = _rottenTomatoesResourcePool, + CancellationToken = cancellationToken, + EnableResponseCache = true + + }).ConfigureAwait(false)) + { + + var result = JsonSerializer.DeserializeFromStream(stream); + + item.CriticReviews = result.reviews.Select(rtReview => new ItemReview + { + ReviewerName = rtReview.critic, + Publisher = rtReview.publication, + Date = DateTime.Parse(rtReview.date).ToUniversalTime(), + Caption = rtReview.quote, + Url = rtReview.links.review, + Likes = string.Equals(rtReview.freshness, "fresh", StringComparison.OrdinalIgnoreCase) + + }).ToList(); + } + + data.Data = GetComparisonData(item.GetProviderId(MetadataProviders.Imdb)); + data.LastRefreshStatus = ProviderRefreshStatus.Success; + SetLastRefreshed(item, DateTime.UtcNow); + + return true; + } + + /// + /// Fetches the rotten tomatoes id. + /// + /// The item. + /// The cancellation token. + /// Task. + private async Task FetchRottenTomatoesId(BaseItem item, CancellationToken cancellationToken) + { + var imdbId = item.GetProviderId(MetadataProviders.Imdb); + // Have IMDB Id using (var stream = await HttpClient.Get(new HttpRequestOptions { Url = GetMovieImdbUrl(imdbId), - ResourcePool = RottenTomatoesResourcePool, + ResourcePool = _rottenTomatoesResourcePool, CancellationToken = cancellationToken, EnableResponseCache = true @@ -201,23 +238,18 @@ namespace MediaBrowser.Controller.Providers.Movies item.CriticRatingSummary = hit.critics_consensus; item.CriticRating = float.Parse(hit.ratings.critics_score); - data.Data = GetComparisonData(hit.alternate_ids.imdb); - - item.SetProviderId(MetadataProviders.Imdb, hit.alternate_ids.imdb); item.SetProviderId(MetadataProviders.RottenTomatoes, hit.id); } } - - data.Data = GetComparisonData(imdbId); - data.LastRefreshStatus = ProviderRefreshStatus.Success; - - SetLastRefreshed(item, DateTime.UtcNow); - - return true; } + // Utility functions to get the URL of the API calls + private string GetMovieReviewsUrl(string rtId) + { + return BasicUrl + string.Format(MoviesReviews, ApiKey, rtId); + } private string GetMovieImdbUrl(string imdbId) { return BasicUrl + string.Format(MovieImdb, ApiKey, imdbId.TrimStart('t')); @@ -225,6 +257,28 @@ namespace MediaBrowser.Controller.Providers.Movies // Data contract classes for use with the Rotten Tomatoes API + protected class RTReviewList + { + public int total { get; set; } + public List reviews { get; set; } + } + + protected class RTReview + { + public string critic { get; set; } + public string date { get; set; } + public string freshness { get; set; } + public string publication { get; set; } + public string quote { get; set; } + public RTReviewLink links { get; set; } + public string original_score { get; set; } + } + + protected class RTReviewLink + { + public string review { get; set; } + } + protected class RTSearchResults { public int total { get; set; } @@ -263,5 +317,6 @@ namespace MediaBrowser.Controller.Providers.Movies { public string imdb { get; set; } } + } } \ No newline at end of file