2021-10-26 15:49:01 +02:00
|
|
|
#nullable disable
|
|
|
|
|
2021-07-23 02:33:19 +02:00
|
|
|
#pragma warning disable CS159, SA1300
|
2020-06-19 20:24:13 +02:00
|
|
|
|
2014-02-03 21:51:28 +01:00
|
|
|
using System;
|
2016-06-23 03:27:25 +02:00
|
|
|
using System.Collections.Generic;
|
2014-02-03 21:51:28 +01:00
|
|
|
using System.Globalization;
|
2016-06-03 23:46:39 +02:00
|
|
|
using System.IO;
|
2014-02-03 21:51:28 +01:00
|
|
|
using System.Linq;
|
2019-06-14 18:38:14 +02:00
|
|
|
using System.Net.Http;
|
2020-12-23 13:06:29 +01:00
|
|
|
using System.Text.Json;
|
2014-02-03 21:51:28 +01:00
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
2021-06-19 18:02:33 +02:00
|
|
|
using Jellyfin.Extensions.Json;
|
2021-06-27 02:00:17 +02:00
|
|
|
using MediaBrowser.Common;
|
2020-08-31 19:05:21 +02:00
|
|
|
using MediaBrowser.Common.Net;
|
2019-01-13 20:26:31 +01:00
|
|
|
using MediaBrowser.Controller.Configuration;
|
|
|
|
using MediaBrowser.Controller.Entities;
|
|
|
|
using MediaBrowser.Controller.Providers;
|
|
|
|
using MediaBrowser.Model.Entities;
|
|
|
|
using MediaBrowser.Model.IO;
|
2014-02-03 21:51:28 +01:00
|
|
|
|
2020-03-09 15:36:02 +01:00
|
|
|
namespace MediaBrowser.Providers.Plugins.Omdb
|
2014-02-03 21:51:28 +01:00
|
|
|
{
|
2021-07-23 02:33:19 +02:00
|
|
|
/// <summary>Provider for OMDB service.</summary>
|
2014-02-03 21:51:28 +01:00
|
|
|
public class OmdbProvider
|
|
|
|
{
|
2016-06-04 02:23:44 +02:00
|
|
|
private readonly IFileSystem _fileSystem;
|
|
|
|
private readonly IServerConfigurationManager _configurationManager;
|
2020-08-17 21:10:02 +02:00
|
|
|
private readonly IHttpClientFactory _httpClientFactory;
|
2018-09-12 19:26:21 +02:00
|
|
|
private readonly IApplicationHost _appHost;
|
2020-12-25 19:37:38 +01:00
|
|
|
private readonly JsonSerializerOptions _jsonOptions;
|
2014-02-03 21:51:28 +01:00
|
|
|
|
2021-07-23 02:33:19 +02:00
|
|
|
/// <summary>Initializes a new instance of the <see cref="OmdbProvider"/> class.</summary>
|
|
|
|
/// <param name="httpClientFactory">HttpClientFactory to use for calls to OMDB service.</param>
|
|
|
|
/// <param name="fileSystem">IFileSystem to use for store OMDB data.</param>
|
|
|
|
/// <param name="appHost">IApplicationHost to use.</param>
|
|
|
|
/// <param name="configurationManager">IServerConfigurationManager to use.</param>
|
2020-12-23 13:06:29 +01:00
|
|
|
public OmdbProvider(IHttpClientFactory httpClientFactory, IFileSystem fileSystem, IApplicationHost appHost, IServerConfigurationManager configurationManager)
|
2014-02-03 21:51:28 +01:00
|
|
|
{
|
2020-08-17 21:10:02 +02:00
|
|
|
_httpClientFactory = httpClientFactory;
|
2016-06-04 02:23:44 +02:00
|
|
|
_fileSystem = fileSystem;
|
|
|
|
_configurationManager = configurationManager;
|
2018-09-12 19:26:21 +02:00
|
|
|
_appHost = appHost;
|
2020-12-25 19:37:38 +01:00
|
|
|
|
2021-03-09 05:57:38 +01:00
|
|
|
_jsonOptions = new JsonSerializerOptions(JsonDefaults.Options);
|
2020-12-25 19:37:38 +01:00
|
|
|
_jsonOptions.Converters.Add(new JsonOmdbNotAvailableStringConverter());
|
2021-01-04 15:52:44 +01:00
|
|
|
_jsonOptions.Converters.Add(new JsonOmdbNotAvailableInt32Converter());
|
2014-02-03 21:51:28 +01:00
|
|
|
}
|
|
|
|
|
2021-07-23 02:33:19 +02:00
|
|
|
/// <summary>Fetches data from OMDB service.</summary>
|
|
|
|
/// <param name="itemResult">Metadata about media item.</param>
|
|
|
|
/// <param name="imdbId">IMDB ID for media.</param>
|
|
|
|
/// <param name="language">Media language.</param>
|
|
|
|
/// <param name="country">Country of origin.</param>
|
|
|
|
/// <param name="cancellationToken">CancellationToken to use for operation.</param>
|
|
|
|
/// <typeparam name="T">The first generic type parameter.</typeparam>
|
|
|
|
/// <returns>Returns a Task object that can be awaited.</returns>
|
2016-07-01 22:03:36 +02:00
|
|
|
public async Task Fetch<T>(MetadataResult<T> itemResult, string imdbId, string language, string country, CancellationToken cancellationToken)
|
2016-11-15 20:42:43 +01:00
|
|
|
where T : BaseItem
|
2014-02-03 21:51:28 +01:00
|
|
|
{
|
2015-05-29 01:37:43 +02:00
|
|
|
if (string.IsNullOrWhiteSpace(imdbId))
|
|
|
|
{
|
2019-01-06 21:50:43 +01:00
|
|
|
throw new ArgumentNullException(nameof(imdbId));
|
2015-05-29 01:37:43 +02:00
|
|
|
}
|
|
|
|
|
2019-01-13 21:37:13 +01:00
|
|
|
var item = itemResult.Item;
|
2016-07-01 22:03:36 +02:00
|
|
|
|
2016-12-23 20:35:05 +01:00
|
|
|
var result = await GetRootObject(imdbId, cancellationToken).ConfigureAwait(false);
|
2014-02-03 21:51:28 +01:00
|
|
|
|
2020-11-18 14:46:14 +01:00
|
|
|
// Only take the name and rating if the user's language is set to English, since Omdb has no localization
|
2017-10-05 20:10:07 +02:00
|
|
|
if (string.Equals(language, "en", StringComparison.OrdinalIgnoreCase) || _configurationManager.Configuration.EnableNewOmdbSupport)
|
2016-11-15 20:42:43 +01:00
|
|
|
{
|
|
|
|
item.Name = result.Title;
|
2015-08-19 18:43:23 +02:00
|
|
|
|
2016-11-15 20:42:43 +01:00
|
|
|
if (string.Equals(country, "us", StringComparison.OrdinalIgnoreCase))
|
|
|
|
{
|
|
|
|
item.OfficialRating = result.Rated;
|
2015-07-06 16:20:23 +02:00
|
|
|
}
|
2016-11-15 20:42:43 +01:00
|
|
|
}
|
2015-05-30 16:32:18 +02:00
|
|
|
|
2016-11-15 20:42:43 +01:00
|
|
|
if (!string.IsNullOrEmpty(result.Year) && result.Year.Length >= 4
|
2021-09-26 16:14:36 +02:00
|
|
|
&& int.TryParse(result.Year.AsSpan().Slice(0, 4), NumberStyles.Number, CultureInfo.InvariantCulture, out var year)
|
2016-11-15 20:42:43 +01:00
|
|
|
&& year >= 0)
|
|
|
|
{
|
|
|
|
item.ProductionYear = year;
|
|
|
|
}
|
2015-05-30 16:32:18 +02:00
|
|
|
|
2017-04-12 19:09:12 +02:00
|
|
|
var tomatoScore = result.GetRottenTomatoScore();
|
2014-02-03 21:51:28 +01:00
|
|
|
|
2017-04-12 19:09:12 +02:00
|
|
|
if (tomatoScore.HasValue)
|
2016-10-17 18:35:29 +02:00
|
|
|
{
|
2017-04-12 19:09:12 +02:00
|
|
|
item.CriticRating = tomatoScore;
|
2016-10-17 18:35:29 +02:00
|
|
|
}
|
2014-02-03 21:51:28 +01:00
|
|
|
|
2016-11-15 20:42:43 +01:00
|
|
|
if (!string.IsNullOrEmpty(result.imdbVotes)
|
2021-09-26 16:14:36 +02:00
|
|
|
&& int.TryParse(result.imdbVotes, NumberStyles.Number, CultureInfo.InvariantCulture, out var voteCount)
|
2016-11-15 20:42:43 +01:00
|
|
|
&& voteCount >= 0)
|
|
|
|
{
|
2020-06-14 11:11:11 +02:00
|
|
|
// item.VoteCount = voteCount;
|
2016-11-15 20:42:43 +01:00
|
|
|
}
|
2014-02-03 21:51:28 +01:00
|
|
|
|
2016-11-15 20:42:43 +01:00
|
|
|
if (!string.IsNullOrEmpty(result.imdbRating)
|
2021-09-26 16:14:36 +02:00
|
|
|
&& float.TryParse(result.imdbRating, NumberStyles.Any, CultureInfo.InvariantCulture, out var imdbRating)
|
2016-11-15 20:42:43 +01:00
|
|
|
&& imdbRating >= 0)
|
|
|
|
{
|
|
|
|
item.CommunityRating = imdbRating;
|
|
|
|
}
|
2014-02-03 21:51:28 +01:00
|
|
|
|
2020-05-31 08:15:34 +02:00
|
|
|
if (!string.IsNullOrEmpty(result.Website))
|
|
|
|
{
|
|
|
|
item.HomePageUrl = result.Website;
|
|
|
|
}
|
2014-02-03 21:51:28 +01:00
|
|
|
|
2016-11-15 20:42:43 +01:00
|
|
|
if (!string.IsNullOrWhiteSpace(result.imdbID))
|
|
|
|
{
|
2020-06-06 21:17:49 +02:00
|
|
|
item.SetProviderId(MetadataProvider.Imdb, result.imdbID);
|
2016-11-15 20:42:43 +01:00
|
|
|
}
|
2015-05-30 16:32:18 +02:00
|
|
|
|
2016-11-15 20:42:43 +01:00
|
|
|
ParseAdditionalMetadata(itemResult, result);
|
2016-06-04 02:23:44 +02:00
|
|
|
}
|
|
|
|
|
2021-07-23 02:33:19 +02:00
|
|
|
/// <summary>Gets data about an episode.</summary>
|
|
|
|
/// <param name="itemResult">Metadata about episode.</param>
|
|
|
|
/// <param name="episodeNumber">Episode number.</param>
|
|
|
|
/// <param name="seasonNumber">Season number.</param>
|
|
|
|
/// <param name="episodeImdbId">Episode ID.</param>
|
|
|
|
/// <param name="seriesImdbId">Season ID.</param>
|
|
|
|
/// <param name="language">Episode language.</param>
|
|
|
|
/// <param name="country">Country of origin.</param>
|
|
|
|
/// <param name="cancellationToken">CancellationToken to use for operation.</param>
|
|
|
|
/// <typeparam name="T">The first generic type parameter.</typeparam>
|
|
|
|
/// <returns>Whether operation was successful.</returns>
|
2017-05-03 23:53:33 +02: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 22:03:36 +02:00
|
|
|
where T : BaseItem
|
2016-06-23 03:27:25 +02:00
|
|
|
{
|
2017-05-03 23:53:33 +02:00
|
|
|
if (string.IsNullOrWhiteSpace(seriesImdbId))
|
2016-06-23 03:27:25 +02:00
|
|
|
{
|
2019-01-06 21:50:43 +01:00
|
|
|
throw new ArgumentNullException(nameof(seriesImdbId));
|
2016-06-23 03:27:25 +02:00
|
|
|
}
|
|
|
|
|
2019-01-13 21:37:13 +01:00
|
|
|
var item = itemResult.Item;
|
2016-07-01 22:03:36 +02:00
|
|
|
|
2017-05-03 23:53:33 +02:00
|
|
|
var seasonResult = await GetSeasonRootObject(seriesImdbId, seasonNumber, cancellationToken).ConfigureAwait(false);
|
2016-10-22 16:51:19 +02:00
|
|
|
|
2020-11-21 14:22:23 +01:00
|
|
|
if (seasonResult?.Episodes == null)
|
2016-10-22 16:51:19 +02:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2016-06-23 03:27:25 +02:00
|
|
|
|
|
|
|
RootObject result = null;
|
|
|
|
|
2017-05-03 23:53:33 +02:00
|
|
|
if (!string.IsNullOrWhiteSpace(episodeImdbId))
|
|
|
|
{
|
2020-05-31 08:15:34 +02:00
|
|
|
foreach (var episode in seasonResult.Episodes)
|
2017-05-03 23:53:33 +02:00
|
|
|
{
|
|
|
|
if (string.Equals(episodeImdbId, episode.imdbID, StringComparison.OrdinalIgnoreCase))
|
|
|
|
{
|
|
|
|
result = episode;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// finally, search by numbers
|
|
|
|
if (result == null)
|
2016-06-23 03:27:25 +02:00
|
|
|
{
|
2020-05-31 08:15:34 +02:00
|
|
|
foreach (var episode in seasonResult.Episodes)
|
2016-06-23 03:27:25 +02:00
|
|
|
{
|
2017-05-03 23:53:33 +02:00
|
|
|
if (episode.Episode == episodeNumber)
|
|
|
|
{
|
|
|
|
result = episode;
|
|
|
|
break;
|
|
|
|
}
|
2016-06-23 03:27:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result == null)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-11-18 14:46:14 +01:00
|
|
|
// Only take the name and rating if the user's language is set to English, since Omdb has no localization
|
2017-10-05 20:10:07 +02:00
|
|
|
if (string.Equals(language, "en", StringComparison.OrdinalIgnoreCase) || _configurationManager.Configuration.EnableNewOmdbSupport)
|
2016-06-23 03:27:25 +02:00
|
|
|
{
|
|
|
|
item.Name = result.Title;
|
|
|
|
|
|
|
|
if (string.Equals(country, "us", StringComparison.OrdinalIgnoreCase))
|
|
|
|
{
|
|
|
|
item.OfficialRating = result.Rated;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-01 22:03:36 +02:00
|
|
|
if (!string.IsNullOrEmpty(result.Year) && result.Year.Length >= 4
|
2021-09-26 16:14:36 +02:00
|
|
|
&& int.TryParse(result.Year.AsSpan().Slice(0, 4), NumberStyles.Number, CultureInfo.InvariantCulture, out var year)
|
2016-06-23 03:27:25 +02:00
|
|
|
&& year >= 0)
|
|
|
|
{
|
|
|
|
item.ProductionYear = year;
|
|
|
|
}
|
|
|
|
|
2017-04-12 19:09:12 +02:00
|
|
|
var tomatoScore = result.GetRottenTomatoScore();
|
2016-06-23 03:27:25 +02:00
|
|
|
|
2017-04-12 19:09:12 +02:00
|
|
|
if (tomatoScore.HasValue)
|
2016-10-17 18:35:29 +02:00
|
|
|
{
|
2017-04-12 19:09:12 +02:00
|
|
|
item.CriticRating = tomatoScore;
|
2016-06-23 03:27:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(result.imdbVotes)
|
2021-09-26 16:14:36 +02:00
|
|
|
&& int.TryParse(result.imdbVotes, NumberStyles.Number, CultureInfo.InvariantCulture, out var voteCount)
|
2016-06-23 03:27:25 +02:00
|
|
|
&& voteCount >= 0)
|
|
|
|
{
|
2020-06-14 11:11:11 +02:00
|
|
|
// item.VoteCount = voteCount;
|
2016-06-23 03:27:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(result.imdbRating)
|
2021-09-26 16:14:36 +02:00
|
|
|
&& float.TryParse(result.imdbRating, NumberStyles.Any, CultureInfo.InvariantCulture, out var imdbRating)
|
2016-06-23 03:27:25 +02:00
|
|
|
&& imdbRating >= 0)
|
|
|
|
{
|
|
|
|
item.CommunityRating = imdbRating;
|
|
|
|
}
|
|
|
|
|
2020-05-31 08:15:34 +02:00
|
|
|
if (!string.IsNullOrEmpty(result.Website))
|
|
|
|
{
|
|
|
|
item.HomePageUrl = result.Website;
|
|
|
|
}
|
2016-06-23 03:27:25 +02:00
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(result.imdbID))
|
|
|
|
{
|
2020-06-06 21:17:49 +02:00
|
|
|
item.SetProviderId(MetadataProvider.Imdb, result.imdbID);
|
2016-06-23 03:27:25 +02:00
|
|
|
}
|
|
|
|
|
2016-07-01 22:03:36 +02:00
|
|
|
ParseAdditionalMetadata(itemResult, result);
|
2016-06-23 03:27:25 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-06-04 03:08:27 +02:00
|
|
|
internal async Task<RootObject> GetRootObject(string imdbId, CancellationToken cancellationToken)
|
|
|
|
{
|
2016-12-23 20:35:05 +01:00
|
|
|
var path = await EnsureItemInfo(imdbId, cancellationToken).ConfigureAwait(false);
|
2021-06-12 22:20:35 +02:00
|
|
|
await using var stream = AsyncFile.OpenRead(path);
|
2021-06-27 02:00:17 +02:00
|
|
|
return await JsonSerializer.DeserializeAsync<RootObject>(stream, _jsonOptions, cancellationToken).ConfigureAwait(false);
|
2016-06-04 03:08:27 +02:00
|
|
|
}
|
|
|
|
|
2016-06-23 03:27:25 +02:00
|
|
|
internal async Task<SeasonRootObject> GetSeasonRootObject(string imdbId, int seasonId, CancellationToken cancellationToken)
|
|
|
|
{
|
2016-12-23 20:35:05 +01:00
|
|
|
var path = await EnsureSeasonInfo(imdbId, seasonId, cancellationToken).ConfigureAwait(false);
|
2021-06-12 22:20:35 +02:00
|
|
|
await using var stream = AsyncFile.OpenRead(path);
|
2021-06-27 02:00:17 +02:00
|
|
|
return await JsonSerializer.DeserializeAsync<SeasonRootObject>(stream, _jsonOptions, cancellationToken).ConfigureAwait(false);
|
2016-06-23 03:27:25 +02:00
|
|
|
}
|
|
|
|
|
2021-07-23 02:33:19 +02:00
|
|
|
/// <summary>Gets OMDB URL.</summary>
|
|
|
|
/// <param name="query">Appends query string to URL.</param>
|
|
|
|
/// <returns>OMDB URL with optional query string.</returns>
|
2020-08-07 19:26:28 +02:00
|
|
|
public static string GetOmdbUrl(string query)
|
2017-02-13 22:03:41 +01:00
|
|
|
{
|
2020-08-07 19:26:28 +02:00
|
|
|
const string Url = "https://www.omdbapi.com?apikey=2c9d9507";
|
2018-09-12 19:26:21 +02:00
|
|
|
|
2019-01-03 19:51:18 +01:00
|
|
|
if (string.IsNullOrWhiteSpace(query))
|
2018-09-12 19:26:21 +02:00
|
|
|
{
|
2020-08-07 19:26:28 +02:00
|
|
|
return Url;
|
2018-09-12 19:26:21 +02:00
|
|
|
}
|
2020-05-31 08:15:34 +02:00
|
|
|
|
2020-08-07 19:26:28 +02:00
|
|
|
return Url + "&" + query;
|
2017-02-13 22:03:41 +01:00
|
|
|
}
|
|
|
|
|
2016-06-04 03:08:27 +02:00
|
|
|
private async Task<string> EnsureItemInfo(string imdbId, CancellationToken cancellationToken)
|
2016-06-04 02:23:44 +02:00
|
|
|
{
|
|
|
|
if (string.IsNullOrWhiteSpace(imdbId))
|
|
|
|
{
|
2019-01-06 21:50:43 +01:00
|
|
|
throw new ArgumentNullException(nameof(imdbId));
|
2016-06-04 02:23:44 +02: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 19:26:21 +02:00
|
|
|
if ((DateTime.UtcNow - _fileSystem.GetLastWriteTimeUtc(fileInfo)).TotalDays <= 1)
|
2016-06-04 02:23:44 +02:00
|
|
|
{
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
}
|
2021-01-12 16:27:38 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
Directory.CreateDirectory(Path.GetDirectoryName(path));
|
|
|
|
}
|
2016-06-04 02:23:44 +02:00
|
|
|
|
2020-08-07 19:26:28 +02:00
|
|
|
var url = GetOmdbUrl(
|
|
|
|
string.Format(
|
|
|
|
CultureInfo.InvariantCulture,
|
|
|
|
"i={0}&plot=short&tomatoes=true&r=json",
|
|
|
|
imdbParam));
|
2016-06-04 02:23:44 +02:00
|
|
|
|
2020-12-24 11:15:12 +01:00
|
|
|
var rootObject = await GetDeserializedOmdbResponse<RootObject>(_httpClientFactory.CreateClient(NamedClient.Default), url, cancellationToken).ConfigureAwait(false);
|
2021-09-25 19:44:40 +02:00
|
|
|
await using FileStream jsonFileStream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None, IODefaults.FileStreamBufferSize, FileOptions.Asynchronous);
|
2020-12-24 11:15:12 +01:00
|
|
|
await JsonSerializer.SerializeAsync(jsonFileStream, rootObject, _jsonOptions, cancellationToken).ConfigureAwait(false);
|
2016-06-04 02:23:44 +02:00
|
|
|
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
2016-06-23 03:27:25 +02:00
|
|
|
private async Task<string> EnsureSeasonInfo(string seriesImdbId, int seasonId, CancellationToken cancellationToken)
|
|
|
|
{
|
|
|
|
if (string.IsNullOrWhiteSpace(seriesImdbId))
|
|
|
|
{
|
2019-01-12 21:41:08 +01:00
|
|
|
throw new ArgumentException("The series IMDb ID was null or whitespace.", nameof(seriesImdbId));
|
2016-06-23 03:27:25 +02: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 19:26:21 +02:00
|
|
|
if ((DateTime.UtcNow - _fileSystem.GetLastWriteTimeUtc(fileInfo)).TotalDays <= 1)
|
2016-06-23 03:27:25 +02:00
|
|
|
{
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
}
|
2021-01-12 16:27:38 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
Directory.CreateDirectory(Path.GetDirectoryName(path));
|
|
|
|
}
|
2016-06-23 03:27:25 +02:00
|
|
|
|
2020-08-07 19:26:28 +02:00
|
|
|
var url = GetOmdbUrl(
|
|
|
|
string.Format(
|
|
|
|
CultureInfo.InvariantCulture,
|
|
|
|
"i={0}&season={1}&detail=full",
|
|
|
|
imdbParam,
|
|
|
|
seasonId));
|
2016-06-23 03:27:25 +02:00
|
|
|
|
2020-12-24 11:15:12 +01:00
|
|
|
var rootObject = await GetDeserializedOmdbResponse<SeasonRootObject>(_httpClientFactory.CreateClient(NamedClient.Default), url, cancellationToken).ConfigureAwait(false);
|
2021-09-25 19:44:40 +02:00
|
|
|
await using FileStream jsonFileStream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None, IODefaults.FileStreamBufferSize, FileOptions.Asynchronous);
|
2020-12-24 11:15:12 +01:00
|
|
|
await JsonSerializer.SerializeAsync(jsonFileStream, rootObject, _jsonOptions, cancellationToken).ConfigureAwait(false);
|
2016-06-23 03:27:25 +02:00
|
|
|
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
2021-07-23 02:33:19 +02:00
|
|
|
/// <summary>Gets response from OMDB service as type T.</summary>
|
|
|
|
/// <param name="httpClient">HttpClient instance to use for service call.</param>
|
|
|
|
/// <param name="url">Http URL to use for service call.</param>
|
|
|
|
/// <param name="cancellationToken">CancellationToken to use for service call.</param>
|
|
|
|
/// <typeparam name="T">The first generic type parameter.</typeparam>
|
|
|
|
/// <returns>OMDB service response as type T.</returns>
|
2020-12-25 19:37:38 +01:00
|
|
|
public async Task<T> GetDeserializedOmdbResponse<T>(HttpClient httpClient, string url, CancellationToken cancellationToken)
|
2020-12-23 14:45:05 +01:00
|
|
|
{
|
|
|
|
using var response = await GetOmdbResponse(httpClient, url, cancellationToken).ConfigureAwait(false);
|
2020-12-27 11:15:46 +01:00
|
|
|
await using Stream content = await response.Content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false);
|
2020-12-23 14:45:05 +01:00
|
|
|
|
2020-12-27 11:15:46 +01:00
|
|
|
return await JsonSerializer.DeserializeAsync<T>(content, _jsonOptions, cancellationToken).ConfigureAwait(false);
|
2020-12-23 14:45:05 +01:00
|
|
|
}
|
|
|
|
|
2021-07-23 02:33:19 +02:00
|
|
|
/// <summary>Gets response from OMDB service.</summary>
|
|
|
|
/// <param name="httpClient">HttpClient instance to use for service call.</param>
|
|
|
|
/// <param name="url">Http URL to use for service call.</param>
|
|
|
|
/// <param name="cancellationToken">CancellationToken to use for service call.</param>
|
|
|
|
/// <returns>OMDB service response as HttpResponseMessage.</returns>
|
2020-08-17 21:10:02 +02:00
|
|
|
public static Task<HttpResponseMessage> GetOmdbResponse(HttpClient httpClient, string url, CancellationToken cancellationToken)
|
2017-02-04 22:22:55 +01:00
|
|
|
{
|
2020-08-17 21:10:02 +02:00
|
|
|
return httpClient.GetAsync(url, cancellationToken);
|
2017-02-04 22:22:55 +01:00
|
|
|
}
|
|
|
|
|
2016-06-04 02:23:44 +02:00
|
|
|
internal string GetDataFilePath(string imdbId)
|
|
|
|
{
|
|
|
|
if (string.IsNullOrEmpty(imdbId))
|
|
|
|
{
|
2019-01-06 21:50:43 +01:00
|
|
|
throw new ArgumentNullException(nameof(imdbId));
|
2014-02-03 21:51:28 +01:00
|
|
|
}
|
2016-06-04 02:23:44 +02:00
|
|
|
|
|
|
|
var dataPath = Path.Combine(_configurationManager.ApplicationPaths.CachePath, "omdb");
|
|
|
|
|
2020-08-07 19:26:28 +02:00
|
|
|
var filename = string.Format(CultureInfo.InvariantCulture, "{0}.json", imdbId);
|
2016-06-04 02:23:44 +02:00
|
|
|
|
|
|
|
return Path.Combine(dataPath, filename);
|
2014-02-03 21:51:28 +01:00
|
|
|
}
|
|
|
|
|
2016-06-23 03:27:25 +02:00
|
|
|
internal string GetSeasonFilePath(string imdbId, int seasonId)
|
|
|
|
{
|
|
|
|
if (string.IsNullOrEmpty(imdbId))
|
|
|
|
{
|
2019-01-06 21:50:43 +01:00
|
|
|
throw new ArgumentNullException(nameof(imdbId));
|
2016-06-23 03:27:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var dataPath = Path.Combine(_configurationManager.ApplicationPaths.CachePath, "omdb");
|
|
|
|
|
2020-08-07 19:26:28 +02:00
|
|
|
var filename = string.Format(CultureInfo.InvariantCulture, "{0}_season_{1}.json", imdbId, seasonId);
|
2016-06-23 03:27:25 +02:00
|
|
|
|
|
|
|
return Path.Combine(dataPath, filename);
|
|
|
|
}
|
|
|
|
|
2016-07-01 22:03:36 +02:00
|
|
|
private void ParseAdditionalMetadata<T>(MetadataResult<T> itemResult, RootObject result)
|
|
|
|
where T : BaseItem
|
2014-02-03 21:51:28 +01:00
|
|
|
{
|
2019-01-13 21:37:13 +01:00
|
|
|
var item = itemResult.Item;
|
2016-07-01 22:03:36 +02:00
|
|
|
|
2017-10-05 20:10:07 +02:00
|
|
|
var isConfiguredForEnglish = IsConfiguredForEnglish(item) || _configurationManager.Configuration.EnableNewOmdbSupport;
|
2017-02-10 00:25:10 +01:00
|
|
|
|
2020-05-31 08:15:34 +02:00
|
|
|
// Grab series genres because IMDb data is better than TVDB. Leave movies alone
|
2020-11-18 14:46:14 +01:00
|
|
|
// But only do it if English is the preferred language because this data will not be localized
|
2017-02-10 00:25:10 +01:00
|
|
|
if (isConfiguredForEnglish && !string.IsNullOrWhiteSpace(result.Genre))
|
2014-02-03 21:51:28 +01:00
|
|
|
{
|
2018-09-12 19:26:21 +02:00
|
|
|
item.Genres = Array.Empty<string>();
|
2014-02-03 21:51:28 +01:00
|
|
|
|
|
|
|
foreach (var genre in result.Genre
|
2020-11-14 16:28:49 +01:00
|
|
|
.Split(',', StringSplitOptions.RemoveEmptyEntries)
|
2014-02-03 21:51:28 +01:00
|
|
|
.Select(i => i.Trim())
|
|
|
|
.Where(i => !string.IsNullOrWhiteSpace(i)))
|
|
|
|
{
|
|
|
|
item.AddGenre(genre);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-10 00:25:10 +01:00
|
|
|
if (isConfiguredForEnglish)
|
|
|
|
{
|
2020-11-18 14:46:14 +01:00
|
|
|
// Omdb is currently English only, so for other languages skip this and let secondary providers fill it in
|
2017-02-10 00:25:10 +01:00
|
|
|
item.Overview = result.Plot;
|
|
|
|
}
|
2016-07-01 22:03:36 +02:00
|
|
|
|
2020-05-31 08:15:34 +02:00
|
|
|
if (!Plugin.Instance.Configuration.CastAndCrew)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
{
|
2020-12-08 03:10:35 +01:00
|
|
|
Name = result.Writer.Trim(),
|
2020-05-31 08:15:34 +02:00
|
|
|
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 21:51:28 +01:00
|
|
|
}
|
|
|
|
|
2017-02-10 00:25:10 +01:00
|
|
|
private bool IsConfiguredForEnglish(BaseItem item)
|
2014-02-03 21:51:28 +01:00
|
|
|
{
|
|
|
|
var lang = item.GetPreferredMetadataLanguage();
|
|
|
|
|
2020-11-18 14:46:14 +01:00
|
|
|
// The data isn't localized and so can only be used for English users
|
2014-02-15 17:36:09 +01:00
|
|
|
return string.Equals(lang, "en", StringComparison.OrdinalIgnoreCase);
|
2014-02-03 21:51:28 +01:00
|
|
|
}
|
|
|
|
|
2020-12-28 23:41:46 +01:00
|
|
|
internal class SeasonRootObject
|
2016-06-23 03:27:25 +02:00
|
|
|
{
|
|
|
|
public string Title { get; set; }
|
2020-05-31 08:15:34 +02:00
|
|
|
|
2016-06-23 03:27:25 +02:00
|
|
|
public string seriesID { get; set; }
|
2020-05-31 08:15:34 +02:00
|
|
|
|
2020-12-24 16:10:30 +01:00
|
|
|
public int? Season { get; set; }
|
2020-05-31 08:15:34 +02:00
|
|
|
|
2016-06-23 03:27:25 +02:00
|
|
|
public int? totalSeasons { get; set; }
|
2020-05-31 08:15:34 +02:00
|
|
|
|
2016-06-23 03:27:25 +02:00
|
|
|
public RootObject[] Episodes { get; set; }
|
2020-05-31 08:15:34 +02:00
|
|
|
|
2016-06-23 03:27:25 +02:00
|
|
|
public string Response { get; set; }
|
|
|
|
}
|
|
|
|
|
2020-12-28 23:41:46 +01:00
|
|
|
internal class RootObject
|
2014-02-03 21:51:28 +01:00
|
|
|
{
|
|
|
|
public string Title { get; set; }
|
2020-05-31 08:15:34 +02:00
|
|
|
|
2014-02-03 21:51:28 +01:00
|
|
|
public string Year { get; set; }
|
2020-05-31 08:15:34 +02:00
|
|
|
|
2014-02-03 21:51:28 +01:00
|
|
|
public string Rated { get; set; }
|
2020-05-31 08:15:34 +02:00
|
|
|
|
2014-02-03 21:51:28 +01:00
|
|
|
public string Released { get; set; }
|
2020-05-31 08:15:34 +02:00
|
|
|
|
2014-02-03 21:51:28 +01:00
|
|
|
public string Runtime { get; set; }
|
2020-05-31 08:15:34 +02:00
|
|
|
|
2014-02-03 21:51:28 +01:00
|
|
|
public string Genre { get; set; }
|
2020-05-31 08:15:34 +02:00
|
|
|
|
2014-02-03 21:51:28 +01:00
|
|
|
public string Director { get; set; }
|
2020-05-31 08:15:34 +02:00
|
|
|
|
2014-02-03 21:51:28 +01:00
|
|
|
public string Writer { get; set; }
|
2020-05-31 08:15:34 +02:00
|
|
|
|
2014-02-03 21:51:28 +01:00
|
|
|
public string Actors { get; set; }
|
2020-05-31 08:15:34 +02:00
|
|
|
|
2014-02-03 21:51:28 +01:00
|
|
|
public string Plot { get; set; }
|
2020-05-31 08:15:34 +02:00
|
|
|
|
2017-04-12 19:09:12 +02:00
|
|
|
public string Language { get; set; }
|
2020-05-31 08:15:34 +02:00
|
|
|
|
2017-04-12 19:09:12 +02:00
|
|
|
public string Country { get; set; }
|
2020-05-31 08:15:34 +02:00
|
|
|
|
2017-04-12 19:09:12 +02:00
|
|
|
public string Awards { get; set; }
|
2020-05-31 08:15:34 +02:00
|
|
|
|
2014-02-03 21:51:28 +01:00
|
|
|
public string Poster { get; set; }
|
2020-05-31 08:15:34 +02:00
|
|
|
|
2017-04-12 19:09:12 +02:00
|
|
|
public List<OmdbRating> Ratings { get; set; }
|
2020-05-31 08:15:34 +02:00
|
|
|
|
2017-04-12 19:09:12 +02:00
|
|
|
public string Metascore { get; set; }
|
2020-05-31 08:15:34 +02:00
|
|
|
|
2014-02-03 21:51:28 +01:00
|
|
|
public string imdbRating { get; set; }
|
2020-05-31 08:15:34 +02:00
|
|
|
|
2014-02-03 21:51:28 +01:00
|
|
|
public string imdbVotes { get; set; }
|
2020-05-31 08:15:34 +02:00
|
|
|
|
2014-02-03 21:51:28 +01:00
|
|
|
public string imdbID { get; set; }
|
2020-05-31 08:15:34 +02:00
|
|
|
|
2014-02-03 21:51:28 +01:00
|
|
|
public string Type { get; set; }
|
2020-05-31 08:15:34 +02:00
|
|
|
|
2014-02-03 21:51:28 +01:00
|
|
|
public string DVD { get; set; }
|
2020-05-31 08:15:34 +02:00
|
|
|
|
2014-02-03 21:51:28 +01:00
|
|
|
public string BoxOffice { get; set; }
|
2020-05-31 08:15:34 +02:00
|
|
|
|
2014-02-03 21:51:28 +01:00
|
|
|
public string Production { get; set; }
|
2020-05-31 08:15:34 +02:00
|
|
|
|
2014-02-03 21:51:28 +01:00
|
|
|
public string Website { get; set; }
|
2020-05-31 08:15:34 +02:00
|
|
|
|
2014-02-03 21:51:28 +01:00
|
|
|
public string Response { get; set; }
|
2020-05-31 08:15:34 +02:00
|
|
|
|
2020-12-24 16:10:30 +01:00
|
|
|
public int? Episode { get; set; }
|
2014-02-03 21:51:28 +01:00
|
|
|
|
2017-04-12 19:09:12 +02: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('%');
|
2019-01-13 21:46:33 +01:00
|
|
|
if (float.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out var score))
|
2017-04-12 19:09:12 +02:00
|
|
|
{
|
|
|
|
return score;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-05-31 08:15:34 +02:00
|
|
|
|
2017-04-12 19:09:12 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2020-05-31 08:15:34 +02:00
|
|
|
|
2021-07-23 05:16:38 +02:00
|
|
|
#pragma warning disable CA1034
|
2021-07-23 02:33:19 +02:00
|
|
|
/// <summary>Describes OMDB rating.</summary>
|
2017-04-12 19:09:12 +02:00
|
|
|
public class OmdbRating
|
|
|
|
{
|
2021-07-23 02:33:19 +02:00
|
|
|
/// <summary>Gets or sets rating source.</summary>
|
2017-04-12 19:09:12 +02:00
|
|
|
public string Source { get; set; }
|
2020-05-31 08:15:34 +02:00
|
|
|
|
2021-07-23 02:33:19 +02:00
|
|
|
/// <summary>Gets or sets rating value.</summary>
|
2017-04-12 19:09:12 +02:00
|
|
|
public string Value { get; set; }
|
2014-02-03 21:51:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|