2013-09-15 17:34:48 +00:00
|
|
|
|
using MediaBrowser.Common.Net;
|
2013-05-06 20:47:37 +00:00
|
|
|
|
using MediaBrowser.Controller.Configuration;
|
|
|
|
|
using MediaBrowser.Controller.Entities;
|
|
|
|
|
using MediaBrowser.Controller.Entities.Movies;
|
2013-09-02 15:39:09 +00:00
|
|
|
|
using MediaBrowser.Controller.Entities.TV;
|
2013-06-09 16:47:28 +00:00
|
|
|
|
using MediaBrowser.Controller.Providers;
|
2013-05-06 20:47:37 +00:00
|
|
|
|
using MediaBrowser.Model.Entities;
|
|
|
|
|
using MediaBrowser.Model.Logging;
|
|
|
|
|
using MediaBrowser.Model.Serialization;
|
|
|
|
|
using System;
|
2013-05-07 04:34:27 +00:00
|
|
|
|
using System.Globalization;
|
2013-09-15 17:34:48 +00:00
|
|
|
|
using System.Linq;
|
2013-12-08 02:45:09 +00:00
|
|
|
|
using System.Net;
|
2013-05-06 20:47:37 +00:00
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
2013-06-09 16:47:28 +00:00
|
|
|
|
namespace MediaBrowser.Providers.Movies
|
2013-05-06 20:47:37 +00:00
|
|
|
|
{
|
2013-05-07 04:34:27 +00:00
|
|
|
|
public class OpenMovieDatabaseProvider : BaseMetadataProvider
|
2013-05-06 20:47:37 +00:00
|
|
|
|
{
|
2013-07-05 17:40:51 +00:00
|
|
|
|
private readonly SemaphoreSlim _resourcePool = new SemaphoreSlim(1, 1);
|
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; }
|
|
|
|
|
|
2013-05-07 04:34:27 +00:00
|
|
|
|
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
|
|
|
|
|
{
|
2013-10-14 17:35:24 +00:00
|
|
|
|
return "13";
|
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;
|
|
|
|
|
|
2013-05-07 04:34:27 +00:00
|
|
|
|
// Don't support local trailers
|
2013-05-06 20:47:37 +00:00
|
|
|
|
if (trailer != null)
|
|
|
|
|
{
|
|
|
|
|
return !trailer.IsLocalTrailer;
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-02 15:39:09 +00:00
|
|
|
|
return item is Movie || item is MusicVideo || item is Series;
|
2013-05-06 20:47:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the priority.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value>The priority.</value>
|
|
|
|
|
public override MetadataProviderPriority Priority
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
// Run after moviedb and xml providers
|
2013-08-18 01:23:37 +00:00
|
|
|
|
return MetadataProviderPriority.Fifth;
|
2013-05-06 20:47:37 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-07 04:34:27 +00:00
|
|
|
|
protected readonly CultureInfo UsCulture = new CultureInfo("en-US");
|
2013-09-14 21:58:51 +00:00
|
|
|
|
|
2013-12-06 20:07:34 +00:00
|
|
|
|
public override async Task<bool> FetchAsync(BaseItem item, bool force, BaseProviderInfo providerInfo, CancellationToken cancellationToken)
|
2013-05-06 20:47:37 +00:00
|
|
|
|
{
|
2013-05-07 04:34:27 +00:00
|
|
|
|
var imdbId = item.GetProviderId(MetadataProviders.Imdb);
|
2013-05-06 20:47:37 +00:00
|
|
|
|
|
2013-05-07 04:34:27 +00:00
|
|
|
|
if (string.IsNullOrEmpty(imdbId))
|
2013-05-06 20:47:37 +00:00
|
|
|
|
{
|
2013-12-06 20:07:34 +00:00
|
|
|
|
SetLastRefreshed(item, DateTime.UtcNow, providerInfo);
|
2013-05-06 20:47:37 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-07 04:34:27 +00:00
|
|
|
|
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
|
|
|
|
|
{
|
2013-05-07 04:34:27 +00:00
|
|
|
|
Url = url,
|
|
|
|
|
ResourcePool = _resourcePool,
|
2013-05-24 16:51:42 +00:00
|
|
|
|
CancellationToken = cancellationToken
|
2013-05-06 20:47:37 +00:00
|
|
|
|
|
|
|
|
|
}).ConfigureAwait(false))
|
|
|
|
|
{
|
2013-05-07 04:34:27 +00:00
|
|
|
|
var result = JsonSerializer.DeserializeFromStream<RootObject>(stream);
|
2013-05-06 20:47:37 +00:00
|
|
|
|
|
2013-11-06 16:06:16 +00:00
|
|
|
|
var hasCriticRating = item as IHasCriticRating;
|
|
|
|
|
if (hasCriticRating != null)
|
2013-05-06 20:47:37 +00:00
|
|
|
|
{
|
2013-11-06 16:06:16 +00:00
|
|
|
|
// Seeing some bogus RT data on omdb for series, so filter it out here
|
|
|
|
|
// RT doesn't even have tv series
|
|
|
|
|
int tomatoMeter;
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(result.tomatoMeter)
|
|
|
|
|
&& int.TryParse(result.tomatoMeter, NumberStyles.Integer, UsCulture, out tomatoMeter)
|
|
|
|
|
&& tomatoMeter >= 0)
|
|
|
|
|
{
|
|
|
|
|
hasCriticRating.CriticRating = tomatoMeter;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(result.tomatoConsensus)
|
|
|
|
|
&& !string.Equals(result.tomatoConsensus, "n/a", StringComparison.OrdinalIgnoreCase)
|
|
|
|
|
&& !string.Equals(result.tomatoConsensus, "No consensus yet.", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
2013-12-08 02:45:09 +00:00
|
|
|
|
hasCriticRating.CriticRatingSummary = WebUtility.HtmlDecode(result.tomatoConsensus);
|
2013-11-06 16:06:16 +00:00
|
|
|
|
}
|
2013-05-07 04:34:27 +00:00
|
|
|
|
}
|
2013-05-06 20:47:37 +00:00
|
|
|
|
|
2013-07-05 17:40:51 +00:00
|
|
|
|
int voteCount;
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(result.imdbVotes)
|
2013-10-13 14:37:17 +00:00
|
|
|
|
&& int.TryParse(result.imdbVotes, NumberStyles.Number, UsCulture, out voteCount)
|
2013-07-05 17:40:51 +00:00
|
|
|
|
&& voteCount >= 0)
|
|
|
|
|
{
|
|
|
|
|
item.VoteCount = voteCount;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
float imdbRating;
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(result.imdbRating)
|
2013-10-13 14:37:17 +00:00
|
|
|
|
&& float.TryParse(result.imdbRating, NumberStyles.Any, UsCulture, out imdbRating)
|
2013-07-05 17:40:51 +00:00
|
|
|
|
&& imdbRating >= 0)
|
|
|
|
|
{
|
|
|
|
|
item.CommunityRating = imdbRating;
|
|
|
|
|
}
|
2013-09-14 21:58:51 +00:00
|
|
|
|
|
|
|
|
|
ParseAdditionalMetadata(item, result);
|
2013-05-07 04:34:27 +00:00
|
|
|
|
}
|
2013-09-14 21:58:51 +00:00
|
|
|
|
|
2013-12-06 20:07:34 +00:00
|
|
|
|
SetLastRefreshed(item, DateTime.UtcNow, providerInfo);
|
2013-05-06 20:47:37 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-14 21:58:51 +00:00
|
|
|
|
private void ParseAdditionalMetadata(BaseItem item, RootObject result)
|
|
|
|
|
{
|
|
|
|
|
// Grab series genres because imdb data is better than tvdb. Leave movies alone
|
|
|
|
|
// But only do it if english is the preferred language because this data will not be localized
|
|
|
|
|
if (!item.LockedFields.Contains(MetadataFields.Genres) &&
|
2013-10-04 15:22:03 +00:00
|
|
|
|
ShouldFetchGenres(item) &&
|
2013-09-14 21:58:51 +00:00
|
|
|
|
!string.IsNullOrWhiteSpace(result.Genre) &&
|
2013-10-04 15:22:03 +00:00
|
|
|
|
!string.Equals(result.Genre, "n/a", StringComparison.OrdinalIgnoreCase))
|
2013-09-14 21:58:51 +00:00
|
|
|
|
{
|
|
|
|
|
item.Genres.Clear();
|
|
|
|
|
|
|
|
|
|
foreach (var genre in result.Genre
|
|
|
|
|
.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
|
|
|
|
|
.Select(i => i.Trim())
|
|
|
|
|
.Where(i => !string.IsNullOrWhiteSpace(i)))
|
|
|
|
|
{
|
|
|
|
|
item.AddGenre(genre);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-05-06 20:47:37 +00:00
|
|
|
|
|
2013-10-04 15:22:03 +00:00
|
|
|
|
private bool ShouldFetchGenres(BaseItem item)
|
|
|
|
|
{
|
|
|
|
|
// Only fetch if other providers didn't get anything
|
|
|
|
|
if (item is Trailer)
|
|
|
|
|
{
|
|
|
|
|
return item.Genres.Count == 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return item is Series;
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-07 04:34:27 +00:00
|
|
|
|
protected class RootObject
|
2013-05-06 20:47:37 +00:00
|
|
|
|
{
|
2013-05-07 04:34:27 +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
|
|
|
|
}
|
|
|
|
|
}
|
2013-05-07 04:34:27 +00:00
|
|
|
|
}
|