2019-01-12 20:41:08 +00:00
|
|
|
using MediaBrowser.Model.IO;
|
2016-06-04 00:23:44 +00:00
|
|
|
using MediaBrowser.Common.Net;
|
|
|
|
using MediaBrowser.Controller.Configuration;
|
2014-02-03 20:51:28 +00:00
|
|
|
using MediaBrowser.Controller.Entities;
|
2016-07-01 20:03:36 +00:00
|
|
|
using MediaBrowser.Controller.Providers;
|
2015-05-30 14:32:18 +00:00
|
|
|
using MediaBrowser.Model.Entities;
|
2014-02-03 20:51:28 +00:00
|
|
|
using MediaBrowser.Model.Serialization;
|
|
|
|
using System;
|
2016-06-23 01:27:25 +00:00
|
|
|
using System.Collections.Generic;
|
2014-02-03 20:51:28 +00:00
|
|
|
using System.Globalization;
|
2016-06-03 21:46:39 +00:00
|
|
|
using System.IO;
|
2014-02-03 20:51:28 +00:00
|
|
|
using System.Linq;
|
|
|
|
using System.Net;
|
2016-06-03 21:46:39 +00:00
|
|
|
using System.Text;
|
2014-02-03 20:51:28 +00:00
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
2018-09-12 17:26:21 +00:00
|
|
|
using MediaBrowser.Common;
|
2014-02-03 20:51:28 +00:00
|
|
|
|
|
|
|
namespace MediaBrowser.Providers.Omdb
|
|
|
|
{
|
|
|
|
public class OmdbProvider
|
|
|
|
{
|
|
|
|
private readonly IJsonSerializer _jsonSerializer;
|
2016-06-04 00:23:44 +00:00
|
|
|
private readonly IFileSystem _fileSystem;
|
|
|
|
private readonly IServerConfigurationManager _configurationManager;
|
2014-02-03 20:51:28 +00:00
|
|
|
private readonly IHttpClient _httpClient;
|
|
|
|
private readonly CultureInfo _usCulture = new CultureInfo("en-US");
|
2018-09-12 17:26:21 +00:00
|
|
|
private readonly IApplicationHost _appHost;
|
2014-02-03 20:51:28 +00:00
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
public OmdbProvider(IJsonSerializer jsonSerializer, IHttpClient httpClient, IFileSystem fileSystem, IApplicationHost appHost, IServerConfigurationManager configurationManager)
|
2014-02-03 20:51:28 +00:00
|
|
|
{
|
|
|
|
_jsonSerializer = jsonSerializer;
|
|
|
|
_httpClient = httpClient;
|
2016-06-04 00:23:44 +00:00
|
|
|
_fileSystem = fileSystem;
|
|
|
|
_configurationManager = configurationManager;
|
2018-09-12 17:26:21 +00:00
|
|
|
_appHost = appHost;
|
2014-02-03 20:51:28 +00:00
|
|
|
}
|
|
|
|
|
2016-07-01 20:03:36 +00:00
|
|
|
public async Task Fetch<T>(MetadataResult<T> itemResult, string imdbId, string language, string country, CancellationToken cancellationToken)
|
2016-11-15 19:42:43 +00:00
|
|
|
where T : BaseItem
|
2014-02-03 20:51:28 +00:00
|
|
|
{
|
2015-05-28 23:37:43 +00:00
|
|
|
if (string.IsNullOrWhiteSpace(imdbId))
|
|
|
|
{
|
2019-01-06 20:50:43 +00:00
|
|
|
throw new ArgumentNullException(nameof(imdbId));
|
2015-05-28 23:37:43 +00:00
|
|
|
}
|
|
|
|
|
2016-07-01 20:03:36 +00:00
|
|
|
T item = itemResult.Item;
|
|
|
|
|
2016-12-23 19:35:05 +00:00
|
|
|
var result = await GetRootObject(imdbId, cancellationToken).ConfigureAwait(false);
|
2014-02-03 20:51:28 +00:00
|
|
|
|
2016-11-15 19:42:43 +00:00
|
|
|
// Only take the name and rating if the user's language is set to english, since Omdb has no localization
|
2017-10-05 18:10:07 +00:00
|
|
|
if (string.Equals(language, "en", StringComparison.OrdinalIgnoreCase) || _configurationManager.Configuration.EnableNewOmdbSupport)
|
2016-11-15 19:42:43 +00:00
|
|
|
{
|
|
|
|
item.Name = result.Title;
|
2015-08-19 16:43:23 +00:00
|
|
|
|
2016-11-15 19:42:43 +00:00
|
|
|
if (string.Equals(country, "us", StringComparison.OrdinalIgnoreCase))
|
|
|
|
{
|
|
|
|
item.OfficialRating = result.Rated;
|
2015-07-06 14:20:23 +00:00
|
|
|
}
|
2016-11-15 19:42:43 +00:00
|
|
|
}
|
2015-05-30 14:32:18 +00:00
|
|
|
|
2016-11-15 19:42:43 +00:00
|
|
|
int year;
|
2015-05-30 14:32:18 +00:00
|
|
|
|
2016-11-15 19:42:43 +00:00
|
|
|
if (!string.IsNullOrEmpty(result.Year) && result.Year.Length >= 4
|
|
|
|
&& int.TryParse(result.Year.Substring(0, 4), NumberStyles.Number, _usCulture, out year)
|
|
|
|
&& year >= 0)
|
|
|
|
{
|
|
|
|
item.ProductionYear = year;
|
|
|
|
}
|
2015-05-30 14:32:18 +00:00
|
|
|
|
2017-04-12 17:09:12 +00:00
|
|
|
var tomatoScore = result.GetRottenTomatoScore();
|
2014-02-03 20:51:28 +00:00
|
|
|
|
2017-04-12 17:09:12 +00:00
|
|
|
if (tomatoScore.HasValue)
|
2016-10-17 16:35:29 +00:00
|
|
|
{
|
2017-04-12 17:09:12 +00:00
|
|
|
item.CriticRating = tomatoScore;
|
2016-10-17 16:35:29 +00:00
|
|
|
}
|
2014-02-03 20:51:28 +00:00
|
|
|
|
2016-10-17 16:35:29 +00:00
|
|
|
int voteCount;
|
2014-02-03 20:51:28 +00:00
|
|
|
|
2016-11-15 19:42:43 +00:00
|
|
|
if (!string.IsNullOrEmpty(result.imdbVotes)
|
|
|
|
&& int.TryParse(result.imdbVotes, NumberStyles.Number, _usCulture, out voteCount)
|
|
|
|
&& voteCount >= 0)
|
|
|
|
{
|
2017-06-03 07:36:32 +00:00
|
|
|
//item.VoteCount = voteCount;
|
2016-11-15 19:42:43 +00:00
|
|
|
}
|
2014-02-03 20:51:28 +00:00
|
|
|
|
2016-11-15 19:42:43 +00:00
|
|
|
float imdbRating;
|
2014-02-03 20:51:28 +00:00
|
|
|
|
2016-11-15 19:42:43 +00:00
|
|
|
if (!string.IsNullOrEmpty(result.imdbRating)
|
|
|
|
&& float.TryParse(result.imdbRating, NumberStyles.Any, _usCulture, out imdbRating)
|
|
|
|
&& imdbRating >= 0)
|
|
|
|
{
|
|
|
|
item.CommunityRating = imdbRating;
|
|
|
|
}
|
2014-02-03 20:51:28 +00:00
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
//if (!string.IsNullOrEmpty(result.Website))
|
|
|
|
//{
|
|
|
|
// item.HomePageUrl = result.Website;
|
|
|
|
//}
|
2014-02-03 20:51:28 +00:00
|
|
|
|
2016-11-15 19:42:43 +00:00
|
|
|
if (!string.IsNullOrWhiteSpace(result.imdbID))
|
|
|
|
{
|
|
|
|
item.SetProviderId(MetadataProviders.Imdb, result.imdbID);
|
|
|
|
}
|
2015-05-30 14:32:18 +00:00
|
|
|
|
2016-11-15 19:42:43 +00:00
|
|
|
ParseAdditionalMetadata(itemResult, result);
|
2016-06-04 00:23:44 +00:00
|
|
|
}
|
|
|
|
|
2017-05-03 21:53:33 +00:00
|
|
|
public async Task<bool> FetchEpisodeData<T>(MetadataResult<T> itemResult, int episodeNumber, int seasonNumber, string episodeImdbId, string seriesImdbId, string language, string country, CancellationToken cancellationToken)
|
2016-07-01 20:03:36 +00:00
|
|
|
where T : BaseItem
|
2016-06-23 01:27:25 +00:00
|
|
|
{
|
2017-05-03 21:53:33 +00:00
|
|
|
if (string.IsNullOrWhiteSpace(seriesImdbId))
|
2016-06-23 01:27:25 +00:00
|
|
|
{
|
2019-01-06 20:50:43 +00:00
|
|
|
throw new ArgumentNullException(nameof(seriesImdbId));
|
2016-06-23 01:27:25 +00:00
|
|
|
}
|
|
|
|
|
2016-07-01 20:03:36 +00:00
|
|
|
T item = itemResult.Item;
|
|
|
|
|
2017-05-03 21:53:33 +00:00
|
|
|
var seasonResult = await GetSeasonRootObject(seriesImdbId, seasonNumber, cancellationToken).ConfigureAwait(false);
|
2016-10-22 14:51:19 +00:00
|
|
|
|
|
|
|
if (seasonResult == null)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2016-06-23 01:27:25 +00:00
|
|
|
|
|
|
|
RootObject result = null;
|
|
|
|
|
2017-05-03 21:53:33 +00:00
|
|
|
if (!string.IsNullOrWhiteSpace(episodeImdbId))
|
|
|
|
{
|
|
|
|
foreach (var episode in (seasonResult.Episodes ?? new RootObject[] { }))
|
|
|
|
{
|
|
|
|
if (string.Equals(episodeImdbId, episode.imdbID, StringComparison.OrdinalIgnoreCase))
|
|
|
|
{
|
|
|
|
result = episode;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// finally, search by numbers
|
|
|
|
if (result == null)
|
2016-06-23 01:27:25 +00:00
|
|
|
{
|
2017-05-03 21:53:33 +00:00
|
|
|
foreach (var episode in (seasonResult.Episodes ?? new RootObject[] { }))
|
2016-06-23 01:27:25 +00:00
|
|
|
{
|
2017-05-03 21:53:33 +00:00
|
|
|
if (episode.Episode == episodeNumber)
|
|
|
|
{
|
|
|
|
result = episode;
|
|
|
|
break;
|
|
|
|
}
|
2016-06-23 01:27:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result == null)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only take the name and rating if the user's language is set to english, since Omdb has no localization
|
2017-10-05 18:10:07 +00:00
|
|
|
if (string.Equals(language, "en", StringComparison.OrdinalIgnoreCase) || _configurationManager.Configuration.EnableNewOmdbSupport)
|
2016-06-23 01:27:25 +00:00
|
|
|
{
|
|
|
|
item.Name = result.Title;
|
|
|
|
|
|
|
|
if (string.Equals(country, "us", StringComparison.OrdinalIgnoreCase))
|
|
|
|
{
|
|
|
|
item.OfficialRating = result.Rated;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int year;
|
|
|
|
|
2016-07-01 20:03:36 +00:00
|
|
|
if (!string.IsNullOrEmpty(result.Year) && result.Year.Length >= 4
|
|
|
|
&& int.TryParse(result.Year.Substring(0, 4), NumberStyles.Number, _usCulture, out year)
|
2016-06-23 01:27:25 +00:00
|
|
|
&& year >= 0)
|
|
|
|
{
|
|
|
|
item.ProductionYear = year;
|
|
|
|
}
|
|
|
|
|
2017-04-12 17:09:12 +00:00
|
|
|
var tomatoScore = result.GetRottenTomatoScore();
|
2016-06-23 01:27:25 +00:00
|
|
|
|
2017-04-12 17:09:12 +00:00
|
|
|
if (tomatoScore.HasValue)
|
2016-10-17 16:35:29 +00:00
|
|
|
{
|
2017-04-12 17:09:12 +00:00
|
|
|
item.CriticRating = tomatoScore;
|
2016-06-23 01:27:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int voteCount;
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(result.imdbVotes)
|
|
|
|
&& int.TryParse(result.imdbVotes, NumberStyles.Number, _usCulture, out voteCount)
|
|
|
|
&& voteCount >= 0)
|
|
|
|
{
|
2017-06-03 07:36:32 +00:00
|
|
|
//item.VoteCount = voteCount;
|
2016-06-23 01:27:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
float imdbRating;
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(result.imdbRating)
|
|
|
|
&& float.TryParse(result.imdbRating, NumberStyles.Any, _usCulture, out imdbRating)
|
|
|
|
&& imdbRating >= 0)
|
|
|
|
{
|
|
|
|
item.CommunityRating = imdbRating;
|
|
|
|
}
|
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
//if (!string.IsNullOrEmpty(result.Website))
|
|
|
|
//{
|
|
|
|
// item.HomePageUrl = result.Website;
|
|
|
|
//}
|
2016-06-23 01:27:25 +00:00
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(result.imdbID))
|
|
|
|
{
|
|
|
|
item.SetProviderId(MetadataProviders.Imdb, result.imdbID);
|
|
|
|
}
|
|
|
|
|
2016-07-01 20:03:36 +00:00
|
|
|
ParseAdditionalMetadata(itemResult, result);
|
2016-06-23 01:27:25 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-06-04 01:08:27 +00:00
|
|
|
internal async Task<RootObject> GetRootObject(string imdbId, CancellationToken cancellationToken)
|
|
|
|
{
|
2016-12-23 19:35:05 +00:00
|
|
|
var path = await EnsureItemInfo(imdbId, cancellationToken).ConfigureAwait(false);
|
2016-06-04 01:08:27 +00:00
|
|
|
|
|
|
|
string resultString;
|
|
|
|
|
2016-10-25 19:02:04 +00:00
|
|
|
using (Stream stream = _fileSystem.GetFileStream(path, FileOpenMode.Open, FileAccessMode.Read, FileShareMode.Read))
|
2016-06-04 01:08:27 +00:00
|
|
|
{
|
|
|
|
using (var reader = new StreamReader(stream, new UTF8Encoding(false)))
|
|
|
|
{
|
|
|
|
resultString = reader.ReadToEnd();
|
|
|
|
resultString = resultString.Replace("\"N/A\"", "\"\"");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var result = _jsonSerializer.DeserializeFromString<RootObject>(resultString);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-06-23 01:27:25 +00:00
|
|
|
internal async Task<SeasonRootObject> GetSeasonRootObject(string imdbId, int seasonId, CancellationToken cancellationToken)
|
|
|
|
{
|
2016-12-23 19:35:05 +00:00
|
|
|
var path = await EnsureSeasonInfo(imdbId, seasonId, cancellationToken).ConfigureAwait(false);
|
2016-06-23 01:27:25 +00:00
|
|
|
|
|
|
|
string resultString;
|
|
|
|
|
2016-10-25 19:02:04 +00:00
|
|
|
using (Stream stream = _fileSystem.GetFileStream(path, FileOpenMode.Open, FileAccessMode.Read, FileShareMode.Read))
|
2016-06-23 01:27:25 +00:00
|
|
|
{
|
|
|
|
using (var reader = new StreamReader(stream, new UTF8Encoding(false)))
|
|
|
|
{
|
|
|
|
resultString = reader.ReadToEnd();
|
|
|
|
resultString = resultString.Replace("\"N/A\"", "\"\"");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var result = _jsonSerializer.DeserializeFromString<SeasonRootObject>(resultString);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
internal static bool IsValidSeries(Dictionary<string, string> seriesProviderIds)
|
|
|
|
{
|
|
|
|
string id;
|
|
|
|
if (seriesProviderIds.TryGetValue(MetadataProviders.Imdb.ToString(), out id) && !string.IsNullOrEmpty(id))
|
|
|
|
{
|
|
|
|
// This check should ideally never be necessary but we're seeing some cases of this and haven't tracked them down yet.
|
|
|
|
if (!string.IsNullOrWhiteSpace(id))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
public static string GetOmdbUrl(string query, IApplicationHost appHost, CancellationToken cancellationToken)
|
2017-02-13 21:03:41 +00:00
|
|
|
{
|
2019-01-10 20:16:01 +00:00
|
|
|
const string url = "https://www.omdbapi.com?apikey=2c9d9507";
|
2018-09-12 17:26:21 +00:00
|
|
|
|
2019-01-03 18:51:18 +00:00
|
|
|
if (string.IsNullOrWhiteSpace(query))
|
2018-09-12 17:26:21 +00:00
|
|
|
{
|
2019-01-03 18:51:18 +00:00
|
|
|
return url;
|
2018-09-12 17:26:21 +00:00
|
|
|
}
|
2019-01-03 18:51:18 +00:00
|
|
|
return url + "&" + query;
|
2017-02-13 21:03:41 +00:00
|
|
|
}
|
|
|
|
|
2016-06-04 01:08:27 +00:00
|
|
|
private async Task<string> EnsureItemInfo(string imdbId, CancellationToken cancellationToken)
|
2016-06-04 00:23:44 +00:00
|
|
|
{
|
|
|
|
if (string.IsNullOrWhiteSpace(imdbId))
|
|
|
|
{
|
2019-01-06 20:50:43 +00:00
|
|
|
throw new ArgumentNullException(nameof(imdbId));
|
2016-06-04 00:23:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var imdbParam = imdbId.StartsWith("tt", StringComparison.OrdinalIgnoreCase) ? imdbId : "tt" + imdbId;
|
|
|
|
|
|
|
|
var path = GetDataFilePath(imdbParam);
|
|
|
|
|
|
|
|
var fileInfo = _fileSystem.GetFileSystemInfo(path);
|
|
|
|
|
|
|
|
if (fileInfo.Exists)
|
|
|
|
{
|
|
|
|
// If it's recent or automatic updates are enabled, don't re-download
|
2018-09-12 17:26:21 +00:00
|
|
|
if ((DateTime.UtcNow - _fileSystem.GetLastWriteTimeUtc(fileInfo)).TotalDays <= 1)
|
2016-06-04 00:23:44 +00:00
|
|
|
{
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
var url = GetOmdbUrl(string.Format("i={0}&plot=short&tomatoes=true&r=json", imdbParam), _appHost, cancellationToken);
|
2016-06-04 00:23:44 +00:00
|
|
|
|
2017-10-20 16:16:56 +00:00
|
|
|
using (var response = await GetOmdbResponse(_httpClient, url, cancellationToken).ConfigureAwait(false))
|
2016-06-04 00:23:44 +00:00
|
|
|
{
|
2017-10-20 16:16:56 +00:00
|
|
|
using (var stream = response.Content)
|
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
var rootObject = await _jsonSerializer.DeserializeFromStreamAsync<RootObject>(stream).ConfigureAwait(false);
|
2017-10-20 16:16:56 +00:00
|
|
|
_fileSystem.CreateDirectory(_fileSystem.GetDirectoryName(path));
|
|
|
|
_jsonSerializer.SerializeToFile(rootObject, path);
|
|
|
|
}
|
2016-06-04 00:23:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
2016-06-23 01:27:25 +00:00
|
|
|
private async Task<string> EnsureSeasonInfo(string seriesImdbId, int seasonId, CancellationToken cancellationToken)
|
|
|
|
{
|
|
|
|
if (string.IsNullOrWhiteSpace(seriesImdbId))
|
|
|
|
{
|
2019-01-12 20:41:08 +00:00
|
|
|
throw new ArgumentException("The series IMDb ID was null or whitespace.", nameof(seriesImdbId));
|
2016-06-23 01:27:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var imdbParam = seriesImdbId.StartsWith("tt", StringComparison.OrdinalIgnoreCase) ? seriesImdbId : "tt" + seriesImdbId;
|
|
|
|
|
|
|
|
var path = GetSeasonFilePath(imdbParam, seasonId);
|
|
|
|
|
|
|
|
var fileInfo = _fileSystem.GetFileSystemInfo(path);
|
|
|
|
|
|
|
|
if (fileInfo.Exists)
|
|
|
|
{
|
|
|
|
// If it's recent or automatic updates are enabled, don't re-download
|
2018-09-12 17:26:21 +00:00
|
|
|
if ((DateTime.UtcNow - _fileSystem.GetLastWriteTimeUtc(fileInfo)).TotalDays <= 1)
|
2016-06-23 01:27:25 +00:00
|
|
|
{
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
var url = GetOmdbUrl(string.Format("i={0}&season={1}&detail=full", imdbParam, seasonId), _appHost, cancellationToken);
|
2016-06-23 01:27:25 +00:00
|
|
|
|
2017-10-20 16:16:56 +00:00
|
|
|
using (var response = await GetOmdbResponse(_httpClient, url, cancellationToken).ConfigureAwait(false))
|
2016-06-23 01:27:25 +00:00
|
|
|
{
|
2017-10-20 16:16:56 +00:00
|
|
|
using (var stream = response.Content)
|
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
var rootObject = await _jsonSerializer.DeserializeFromStreamAsync<SeasonRootObject>(stream).ConfigureAwait(false);
|
2017-10-20 16:16:56 +00:00
|
|
|
_fileSystem.CreateDirectory(_fileSystem.GetDirectoryName(path));
|
|
|
|
_jsonSerializer.SerializeToFile(rootObject, path);
|
|
|
|
}
|
2016-06-23 01:27:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
2017-10-20 16:16:56 +00:00
|
|
|
public static Task<HttpResponseInfo> GetOmdbResponse(IHttpClient httpClient, string url, CancellationToken cancellationToken)
|
2017-02-04 21:22:55 +00:00
|
|
|
{
|
2017-10-20 16:16:56 +00:00
|
|
|
return httpClient.SendAsync(new HttpRequestOptions
|
2017-02-04 21:22:55 +00:00
|
|
|
{
|
|
|
|
Url = url,
|
|
|
|
CancellationToken = cancellationToken,
|
2017-02-08 18:50:50 +00:00
|
|
|
BufferContent = true,
|
|
|
|
EnableDefaultUserAgent = true
|
2017-10-20 16:16:56 +00:00
|
|
|
}, "GET");
|
2017-02-04 21:22:55 +00:00
|
|
|
}
|
|
|
|
|
2016-06-04 00:23:44 +00:00
|
|
|
internal string GetDataFilePath(string imdbId)
|
|
|
|
{
|
|
|
|
if (string.IsNullOrEmpty(imdbId))
|
|
|
|
{
|
2019-01-06 20:50:43 +00:00
|
|
|
throw new ArgumentNullException(nameof(imdbId));
|
2014-02-03 20:51:28 +00:00
|
|
|
}
|
2016-06-04 00:23:44 +00:00
|
|
|
|
|
|
|
var dataPath = Path.Combine(_configurationManager.ApplicationPaths.CachePath, "omdb");
|
|
|
|
|
|
|
|
var filename = string.Format("{0}.json", imdbId);
|
|
|
|
|
|
|
|
return Path.Combine(dataPath, filename);
|
2014-02-03 20:51:28 +00:00
|
|
|
}
|
|
|
|
|
2016-06-23 01:27:25 +00:00
|
|
|
internal string GetSeasonFilePath(string imdbId, int seasonId)
|
|
|
|
{
|
|
|
|
if (string.IsNullOrEmpty(imdbId))
|
|
|
|
{
|
2019-01-06 20:50:43 +00:00
|
|
|
throw new ArgumentNullException(nameof(imdbId));
|
2016-06-23 01:27:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var dataPath = Path.Combine(_configurationManager.ApplicationPaths.CachePath, "omdb");
|
|
|
|
|
|
|
|
var filename = string.Format("{0}_season_{1}.json", imdbId, seasonId);
|
|
|
|
|
|
|
|
return Path.Combine(dataPath, filename);
|
|
|
|
}
|
|
|
|
|
2016-07-01 20:03:36 +00:00
|
|
|
private void ParseAdditionalMetadata<T>(MetadataResult<T> itemResult, RootObject result)
|
|
|
|
where T : BaseItem
|
2014-02-03 20:51:28 +00:00
|
|
|
{
|
2016-07-01 20:03:36 +00:00
|
|
|
T item = itemResult.Item;
|
|
|
|
|
2017-10-05 18:10:07 +00:00
|
|
|
var isConfiguredForEnglish = IsConfiguredForEnglish(item) || _configurationManager.Configuration.EnableNewOmdbSupport;
|
2017-02-09 23:25:10 +00:00
|
|
|
|
2014-02-03 20:51:28 +00:00
|
|
|
// 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
|
2017-02-09 23:25:10 +00:00
|
|
|
if (isConfiguredForEnglish && !string.IsNullOrWhiteSpace(result.Genre))
|
2014-02-03 20:51:28 +00:00
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
item.Genres = Array.Empty<string>();
|
2014-02-03 20:51:28 +00:00
|
|
|
|
|
|
|
foreach (var genre in result.Genre
|
|
|
|
.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
|
|
|
|
.Select(i => i.Trim())
|
|
|
|
.Where(i => !string.IsNullOrWhiteSpace(i)))
|
|
|
|
{
|
|
|
|
item.AddGenre(genre);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-09 23:25:10 +00:00
|
|
|
if (isConfiguredForEnglish)
|
|
|
|
{
|
|
|
|
// Omdb is currently english only, so for other languages skip this and let secondary providers fill it in
|
|
|
|
item.Overview = result.Plot;
|
|
|
|
}
|
2016-07-01 20:03:36 +00:00
|
|
|
|
2016-07-02 02:15:56 +00:00
|
|
|
//if (!string.IsNullOrWhiteSpace(result.Director))
|
|
|
|
//{
|
|
|
|
// var person = new PersonInfo
|
|
|
|
// {
|
|
|
|
// Name = result.Director.Trim(),
|
|
|
|
// Type = PersonType.Director
|
|
|
|
// };
|
|
|
|
|
|
|
|
// itemResult.AddPerson(person);
|
|
|
|
//}
|
|
|
|
|
|
|
|
//if (!string.IsNullOrWhiteSpace(result.Writer))
|
|
|
|
//{
|
|
|
|
// var person = new PersonInfo
|
|
|
|
// {
|
|
|
|
// Name = result.Director.Trim(),
|
|
|
|
// Type = PersonType.Writer
|
|
|
|
// };
|
|
|
|
|
|
|
|
// itemResult.AddPerson(person);
|
|
|
|
//}
|
|
|
|
|
|
|
|
//if (!string.IsNullOrWhiteSpace(result.Actors))
|
|
|
|
//{
|
|
|
|
// var actorList = result.Actors.Split(',');
|
|
|
|
// foreach (var actor in actorList)
|
|
|
|
// {
|
|
|
|
// if (!string.IsNullOrWhiteSpace(actor))
|
|
|
|
// {
|
|
|
|
// var person = new PersonInfo
|
|
|
|
// {
|
|
|
|
// Name = actor.Trim(),
|
|
|
|
// Type = PersonType.Actor
|
|
|
|
// };
|
|
|
|
|
|
|
|
// itemResult.AddPerson(person);
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
//}
|
2014-02-03 20:51:28 +00:00
|
|
|
}
|
|
|
|
|
2017-02-09 23:25:10 +00:00
|
|
|
private bool IsConfiguredForEnglish(BaseItem item)
|
2014-02-03 20:51:28 +00:00
|
|
|
{
|
|
|
|
var lang = item.GetPreferredMetadataLanguage();
|
|
|
|
|
|
|
|
// The data isn't localized and so can only be used for english users
|
2014-02-15 16:36:09 +00:00
|
|
|
return string.Equals(lang, "en", StringComparison.OrdinalIgnoreCase);
|
2014-02-03 20:51:28 +00:00
|
|
|
}
|
|
|
|
|
2016-06-23 01:27:25 +00:00
|
|
|
internal class SeasonRootObject
|
|
|
|
{
|
|
|
|
public string Title { get; set; }
|
|
|
|
public string seriesID { get; set; }
|
|
|
|
public int Season { get; set; }
|
|
|
|
public int? totalSeasons { get; set; }
|
|
|
|
public RootObject[] Episodes { get; set; }
|
|
|
|
public string Response { get; set; }
|
|
|
|
}
|
|
|
|
|
2016-06-04 01:08:27 +00:00
|
|
|
internal class RootObject
|
2014-02-03 20:51:28 +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; }
|
2017-04-12 17:09:12 +00:00
|
|
|
public string Language { get; set; }
|
|
|
|
public string Country { get; set; }
|
|
|
|
public string Awards { get; set; }
|
2014-02-03 20:51:28 +00:00
|
|
|
public string Poster { get; set; }
|
2017-04-12 17:09:12 +00:00
|
|
|
public List<OmdbRating> Ratings { get; set; }
|
|
|
|
public string Metascore { get; set; }
|
2014-02-03 20:51:28 +00:00
|
|
|
public string imdbRating { get; set; }
|
|
|
|
public string imdbVotes { get; set; }
|
|
|
|
public string imdbID { get; set; }
|
|
|
|
public string Type { 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; }
|
2017-04-12 17:09:12 +00:00
|
|
|
public int Episode { get; set; }
|
2014-02-03 20:51:28 +00:00
|
|
|
|
2017-04-12 17:09:12 +00:00
|
|
|
public float? GetRottenTomatoScore()
|
|
|
|
{
|
|
|
|
if (Ratings != null)
|
|
|
|
{
|
|
|
|
var rating = Ratings.FirstOrDefault(i => string.Equals(i.Source, "Rotten Tomatoes", StringComparison.OrdinalIgnoreCase));
|
|
|
|
if (rating != null && rating.Value != null)
|
|
|
|
{
|
|
|
|
var value = rating.Value.TrimEnd('%');
|
|
|
|
float score;
|
|
|
|
if (float.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out score))
|
|
|
|
{
|
|
|
|
return score;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
public class OmdbRating
|
|
|
|
{
|
|
|
|
public string Source { get; set; }
|
|
|
|
public string Value { get; set; }
|
2014-02-03 20:51:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|