Updated RottenTomatoes provider to use 'using' blocks

This commit is contained in:
Mark van Veen 2013-05-05 18:55:53 +02:00
parent 31cc00be9e
commit 7c2aa12db8

View File

@ -108,179 +108,6 @@ namespace MediaBrowser.Controller.Providers.Movies
return item is Movie;
}
/// <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)
{
// Refresh if imdb id has changed
if (providerInfo.Data != GetComparisonData(item.GetProviderId(MetadataProviders.Imdb)))
{
return true;
}
return base.NeedsRefreshInternal(item, providerInfo);
}
/// <summary>
/// Fetches metadata and returns true or false indicating if any work that requires persistence was done
/// </summary>
/// <param name="item">The item.</param>
/// <param name="force">if set to <c>true</c> [force].</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>Task{System.Boolean}.</returns>
public override Task<bool> 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<RTSearchResults>(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<RTSearchResults>(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<RTMovieSearchResult>(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<RTReviewList>(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);
}
/// <summary>
/// Gets the comparison data.
/// </summary>
@ -304,6 +131,188 @@ namespace MediaBrowser.Controller.Providers.Movies
}
}
/// <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)
{
return true;
// Refresh if imdb id has changed
if (providerInfo.Data != GetComparisonData(item.GetProviderId(MetadataProviders.Imdb)))
{
return true;
}
return base.NeedsRefreshInternal(item, providerInfo);
}
/// <summary>
/// Fetches metadata and returns true or false indicating if any work that requires persistence was done
/// </summary>
/// <param name="item">The item.</param>
/// <param name="force">if set to <c>true</c> [force].</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>Task{System.Boolean}.</returns>
public override Task<bool> 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<RTSearchResults>(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<RTSearchResults>(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<RTMovieSearchResult>(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<RTReviewList>(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; }
}