diff --git a/MediaBrowser.Controller/Providers/Movies/RottenTomatoesMovieProvider.cs b/MediaBrowser.Controller/Providers/Movies/RottenTomatoesMovieProvider.cs index 79aa0a7c0..0e99ea9df 100644 --- a/MediaBrowser.Controller/Providers/Movies/RottenTomatoesMovieProvider.cs +++ b/MediaBrowser.Controller/Providers/Movies/RottenTomatoesMovieProvider.cs @@ -108,179 +108,6 @@ namespace MediaBrowser.Controller.Providers.Movies 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) - { - // Refresh if imdb id has changed - if (providerInfo.Data != GetComparisonData(item.GetProviderId(MetadataProviders.Imdb))) - { - return true; - } - - 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) - { - // Lastly, record the Imdb id here - BaseProviderInfo data; - - string imdbId = null; - - if (item.ProviderData.TryGetValue(Id, out data)) - { - imdbId = item.GetProviderId(MetadataProviders.Imdb); - } - else - { - // Wat - } - - - - RTMovieSearchResult hit = null; - Stream stream = null; - - if (string.IsNullOrEmpty(imdbId)) - { - // No IMDB Id, search RT for an ID - - int page = 1; - stream = HttpClient.Get(MovieSearchUrl(item.Name, page), _rottenTomatoesResourcePool, cancellationToken).Result; - RTSearchResults result = JsonSerializer.DeserializeFromStream(stream); - - if (result.total == 1) - { - hit = result.movies[0]; - } - else if (result.total > 1) - { - while (hit == null) - { - // See if there's an absolute hit somewhere - foreach (var searchHit in result.movies) - { - if (string.Equals(searchHit.title, item.Name, StringComparison.InvariantCultureIgnoreCase)) - { - hit = searchHit; - break; - } - } - - if (hit == null) - { - Stream newPageStream = HttpClient.Get(MovieSearchUrl(item.Name, page++), _rottenTomatoesResourcePool, cancellationToken).Result; - result = JsonSerializer.DeserializeFromStream(stream); - - if (result.total == 0) - { - // No results found on RottenTomatoes - break; - } - } - } - } - } - else - { - // Have IMDB Id - stream = HttpClient.Get(MovieImdbUrl(imdbId), _rottenTomatoesResourcePool, cancellationToken).Result; - RTMovieSearchResult result = JsonSerializer.DeserializeFromStream(stream); - - if (!string.IsNullOrEmpty(result.id)) - { - // got a result - hit = result; - } - } - stream.Dispose(); - stream = null; - - if (hit != null) - { - item.CriticRatingSummary = hit.critics_concensus; - item.CriticRating = float.Parse(hit.ratings.critics_score); - - stream = HttpClient.Get(MovieReviewsUrl(hit.id), _rottenTomatoesResourcePool, cancellationToken).Result; - - RTReviewList result = JsonSerializer.DeserializeFromStream(stream); - - item.CriticReviews.Clear(); - foreach (var rtReview in result.reviews) - { - ItemReview review = new ItemReview(); - - review.ReviewerName = rtReview.critic; - review.Publisher = rtReview.publication; - review.Date = DateTime.Parse(rtReview.date).ToUniversalTime(); - review.Caption = rtReview.quote; - review.Url = rtReview.links.review; - item.CriticReviews.Add(review); - } - - if (data == null) - { - data = new BaseProviderInfo(); - } - - data.Data = GetComparisonData(hit.alternate_ids.imdb); - - item.SetProviderId(MetadataProviders.Imdb, hit.alternate_ids.imdb); - item.SetProviderId(MetadataProviders.RottenTomatoes, hit.id); - - stream.Dispose(); - stream = null; - } - else - { - // Nothing found on RT - Logger.Info("Nothing found on RottenTomatoes for Movie \"{0}\"", item.Name); - - // TODO: When alternative names are implemented search for those instead - } - - if (data != null) - { - data.Data = GetComparisonData(imdbId); - } - - SetLastRefreshed(item, DateTime.UtcNow); - - return Task.FromResult(true); - } - - private string MovieUrl(string rtId) - { - return BasicUrl + string.Format(Movie, ApiKey, rtId); - } - - private string MovieImdbUrl(string imdbId) - { - return BasicUrl + string.Format(MovieImdb, ApiKey, imdbId.TrimStart('t')); - } - - private string MovieSearchUrl(string query, int page = 1) - { - return BasicUrl + string.Format(MovieSearch, ApiKey, Uri.EscapeDataString(query), page); - } - - private string MovieReviewsUrl(string rtId) - { - return BasicUrl + string.Format(MoviesReviews, ApiKey, rtId); - } - /// /// Gets the comparison data. /// @@ -304,6 +131,188 @@ namespace MediaBrowser.Controller.Providers.Movies } } + /// + /// Needses the refresh internal. + /// + /// The item. + /// The provider info. + /// true if XXXX, false otherwise + protected override bool NeedsRefreshInternal(BaseItem item, BaseProviderInfo providerInfo) + { + return true; + + // Refresh if imdb id has changed + if (providerInfo.Data != GetComparisonData(item.GetProviderId(MetadataProviders.Imdb))) + { + return true; + } + + 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) + { + BaseProviderInfo data; + + // See whether there's an IMDB Id available + string imdbId = null; + if (item.ProviderData.TryGetValue(Id, out data)) + { + imdbId = item.GetProviderId(MetadataProviders.Imdb); + } + + RTMovieSearchResult hit = null; + + if (string.IsNullOrEmpty(imdbId)) + { + // No IMDB Id, search RT for an ID + + int page = 1; + using (Stream stream = HttpClient.Get(MovieSearchUrl(item.Name, page), _rottenTomatoesResourcePool, cancellationToken).Result) + { + RTSearchResults result = JsonSerializer.DeserializeFromStream(stream); + + if (result.total == 1) + { + // With only one result we'll have to assume that this is the movie we're searching for + hit = result.movies[0]; + } + else if (result.total > 1) + { + // If there are more results than one + // Step 1: Loop through all current results, see if there's an exact match (not case sensitive) somewhere, if so, accept that as the searched item, else go to step 2 + // Step 2: Retrieve the next page and go to step 1 if there are results, else, stop checking + + while (hit == null) + { + foreach (var searchHit in result.movies) + { + if (string.Equals(searchHit.title, item.Name, StringComparison.InvariantCultureIgnoreCase)) + { + hit = searchHit; + break; + } + } + + if (hit == null) + { + using (Stream newPageStream = HttpClient.Get(MovieSearchUrl(item.Name, page++), _rottenTomatoesResourcePool, cancellationToken).Result) + { + result = JsonSerializer.DeserializeFromStream(stream); + + if (result.total == 0) + { + // No results found on RottenTomatoes + break; + } + } + } + // while end + } + } + } + // if null end + } + else + { + // Have IMDB Id + using (Stream stream = HttpClient.Get(MovieImdbUrl(imdbId), _rottenTomatoesResourcePool, cancellationToken).Result) + { + RTMovieSearchResult result = JsonSerializer.DeserializeFromStream(stream); + + if (!string.IsNullOrEmpty(result.id)) + { + // Got a result + hit = result; + } + } + } + + // If we found any results, that's great! + if (hit != null) + { + item.CriticRatingSummary = hit.critics_concensus; + item.CriticRating = float.Parse(hit.ratings.critics_score); + + using (Stream stream = HttpClient.Get(MovieReviewsUrl(hit.id), _rottenTomatoesResourcePool, cancellationToken).Result) + { + + RTReviewList result = JsonSerializer.DeserializeFromStream(stream); + + item.CriticReviews.Clear(); + foreach (var rtReview in result.reviews) + { + ItemReview review = new ItemReview(); + + review.ReviewerName = rtReview.critic; + review.Publisher = rtReview.publication; + review.Date = DateTime.Parse(rtReview.date).ToUniversalTime(); + review.Caption = rtReview.quote; + review.Url = rtReview.links.review; + item.CriticReviews.Add(review); + } + + if (data == null) + { + data = new BaseProviderInfo(); + } + + data.Data = GetComparisonData(hit.alternate_ids.imdb); + + item.SetProviderId(MetadataProviders.Imdb, hit.alternate_ids.imdb); + item.SetProviderId(MetadataProviders.RottenTomatoes, hit.id); + } + } + else + { + // Nothing found on RT + Logger.Info("Nothing found on RottenTomatoes for Movie \"{0}\"", item.Name); + + // TODO: When alternative names are implemented search for those instead + } + + if (data != null) + { + data.Data = GetComparisonData(imdbId); + data.LastRefreshStatus = ProviderRefreshStatus.Success; + } + + SetLastRefreshed(item, DateTime.UtcNow); + + + return Task.FromResult(true); + } + + // Utility functions to get the URL of the API calls + + private string MovieUrl(string rtId) + { + return BasicUrl + string.Format(Movie, ApiKey, rtId); + } + + private string MovieImdbUrl(string imdbId) + { + return BasicUrl + string.Format(MovieImdb, ApiKey, imdbId.TrimStart('t')); + } + + private string MovieSearchUrl(string query, int page = 1) + { + return BasicUrl + string.Format(MovieSearch, ApiKey, Uri.EscapeDataString(query), page); + } + + private string MovieReviewsUrl(string rtId) + { + return BasicUrl + string.Format(MoviesReviews, ApiKey, rtId); + } + + // Data contract classes for use with the Rotten Tomatoes API protected class RTSearchResults @@ -361,7 +370,7 @@ namespace MediaBrowser.Controller.Providers.Movies public RTReviewLink links { get; set; } } - protected class RTReviewLink + protected class RTReviewLink { public string review { get; set; } }