2020-06-19 20:24:13 +02:00
|
|
|
#pragma warning disable CS1591
|
|
|
|
|
2019-01-13 21:03:10 +01:00
|
|
|
using System;
|
2013-02-21 02:33:05 +01:00
|
|
|
using System.Collections.Generic;
|
2014-03-02 18:09:35 +01:00
|
|
|
using System.Globalization;
|
2013-02-21 02:33:05 +01:00
|
|
|
using System.IO;
|
2015-10-26 06:29:32 +01:00
|
|
|
using System.Net;
|
2020-02-23 10:53:51 +01:00
|
|
|
using System.Net.Http;
|
2013-02-21 02:33:05 +01:00
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
2016-01-23 02:57:35 +01:00
|
|
|
using MediaBrowser.Common;
|
2019-01-13 20:26:31 +01:00
|
|
|
using MediaBrowser.Common.Configuration;
|
|
|
|
using MediaBrowser.Common.Net;
|
|
|
|
using MediaBrowser.Controller.Configuration;
|
|
|
|
using MediaBrowser.Controller.Entities;
|
|
|
|
using MediaBrowser.Controller.Entities.Movies;
|
|
|
|
using MediaBrowser.Controller.Library;
|
|
|
|
using MediaBrowser.Controller.Providers;
|
|
|
|
using MediaBrowser.Model.Entities;
|
|
|
|
using MediaBrowser.Model.IO;
|
2015-10-26 06:29:32 +01:00
|
|
|
using MediaBrowser.Model.Net;
|
2019-01-13 20:26:31 +01:00
|
|
|
using MediaBrowser.Model.Providers;
|
|
|
|
using MediaBrowser.Model.Serialization;
|
2020-05-31 08:23:09 +02:00
|
|
|
using MediaBrowser.Providers.Plugins.Tmdb.Models.Movies;
|
2019-01-13 20:26:31 +01:00
|
|
|
using Microsoft.Extensions.Logging;
|
2013-02-21 02:33:05 +01:00
|
|
|
|
2020-05-31 08:23:09 +02:00
|
|
|
namespace MediaBrowser.Providers.Plugins.Tmdb.Movies
|
2013-02-21 02:33:05 +01:00
|
|
|
{
|
|
|
|
/// <summary>
|
2020-06-16 00:37:52 +02:00
|
|
|
/// Class MovieDbProvider.
|
2013-02-21 02:33:05 +01:00
|
|
|
/// </summary>
|
2019-08-18 13:20:52 +02:00
|
|
|
public class TmdbMovieProvider : IRemoteMetadataProvider<Movie, MovieInfo>, IHasOrder
|
2013-02-21 02:33:05 +01:00
|
|
|
{
|
2019-08-18 13:20:52 +02:00
|
|
|
internal static TmdbMovieProvider Current { get; private set; }
|
2013-03-04 06:43:06 +01:00
|
|
|
|
2014-02-06 05:39:16 +01:00
|
|
|
private readonly IJsonSerializer _jsonSerializer;
|
|
|
|
private readonly IHttpClient _httpClient;
|
2013-10-31 15:03:23 +01:00
|
|
|
private readonly IFileSystem _fileSystem;
|
2014-02-06 05:39:16 +01:00
|
|
|
private readonly IServerConfigurationManager _configurationManager;
|
2020-06-06 02:15:56 +02:00
|
|
|
private readonly ILogger<TmdbMovieProvider> _logger;
|
2014-11-18 03:48:22 +01:00
|
|
|
private readonly ILibraryManager _libraryManager;
|
2016-01-23 02:57:35 +01:00
|
|
|
private readonly IApplicationHost _appHost;
|
2013-02-25 01:13:45 +01:00
|
|
|
|
2014-03-02 18:09:35 +01:00
|
|
|
private readonly CultureInfo _usCulture = new CultureInfo("en-US");
|
2014-07-05 05:17:24 +02:00
|
|
|
|
2020-02-23 10:53:51 +01:00
|
|
|
public TmdbMovieProvider(
|
|
|
|
IJsonSerializer jsonSerializer,
|
|
|
|
IHttpClient httpClient,
|
|
|
|
IFileSystem fileSystem,
|
|
|
|
IServerConfigurationManager configurationManager,
|
|
|
|
ILogger<TmdbMovieProvider> logger,
|
|
|
|
ILibraryManager libraryManager,
|
|
|
|
IApplicationHost appHost)
|
2014-02-06 05:39:16 +01:00
|
|
|
{
|
|
|
|
_jsonSerializer = jsonSerializer;
|
|
|
|
_httpClient = httpClient;
|
2013-10-31 15:03:23 +01:00
|
|
|
_fileSystem = fileSystem;
|
2014-02-06 05:39:16 +01:00
|
|
|
_configurationManager = configurationManager;
|
|
|
|
_logger = logger;
|
2014-11-18 03:48:22 +01:00
|
|
|
_libraryManager = libraryManager;
|
2016-01-23 02:57:35 +01:00
|
|
|
_appHost = appHost;
|
2013-03-04 06:43:06 +01:00
|
|
|
Current = this;
|
|
|
|
}
|
|
|
|
|
2014-03-02 18:09:35 +01:00
|
|
|
public Task<IEnumerable<RemoteSearchResult>> GetSearchResults(MovieInfo searchInfo, CancellationToken cancellationToken)
|
2014-02-20 05:53:15 +01:00
|
|
|
{
|
2014-03-02 18:09:35 +01:00
|
|
|
return GetMovieSearchResults(searchInfo, cancellationToken);
|
|
|
|
}
|
|
|
|
|
|
|
|
public async Task<IEnumerable<RemoteSearchResult>> GetMovieSearchResults(ItemLookupInfo searchInfo, CancellationToken cancellationToken)
|
|
|
|
{
|
2020-06-06 21:17:49 +02:00
|
|
|
var tmdbId = searchInfo.GetProviderId(MetadataProvider.Tmdb);
|
2014-03-02 18:09:35 +01:00
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(tmdbId))
|
|
|
|
{
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
|
|
|
|
|
|
await EnsureMovieInfo(tmdbId, searchInfo.MetadataLanguage, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
|
|
|
var dataFilePath = GetDataFilePath(tmdbId, searchInfo.MetadataLanguage);
|
|
|
|
|
2019-08-18 13:20:52 +02:00
|
|
|
var obj = _jsonSerializer.DeserializeFromFile<MovieResult>(dataFilePath);
|
2014-03-02 18:09:35 +01:00
|
|
|
|
2014-09-22 23:56:54 +02:00
|
|
|
var tmdbSettings = await GetTmdbSettings(cancellationToken).ConfigureAwait(false);
|
|
|
|
|
2018-09-12 19:26:21 +02:00
|
|
|
var tmdbImageUrl = tmdbSettings.images.GetImageUrl("original");
|
2014-09-22 23:56:54 +02:00
|
|
|
|
2014-03-02 18:09:35 +01:00
|
|
|
var remoteResult = new RemoteSearchResult
|
|
|
|
{
|
2015-03-10 02:30:20 +01:00
|
|
|
Name = obj.GetTitle(),
|
2014-03-02 18:09:35 +01:00
|
|
|
SearchProviderName = Name,
|
2019-08-18 14:44:13 +02:00
|
|
|
ImageUrl = string.IsNullOrWhiteSpace(obj.Poster_Path) ? null : tmdbImageUrl + obj.Poster_Path
|
2014-03-02 18:09:35 +01:00
|
|
|
};
|
|
|
|
|
2019-08-18 14:44:13 +02:00
|
|
|
if (!string.IsNullOrWhiteSpace(obj.Release_Date))
|
2014-03-02 19:01:46 +01:00
|
|
|
{
|
|
|
|
// These dates are always in this exact format
|
2019-08-18 14:44:13 +02:00
|
|
|
if (DateTime.TryParse(obj.Release_Date, _usCulture, DateTimeStyles.None, out var r))
|
2014-03-02 19:01:46 +01:00
|
|
|
{
|
|
|
|
remoteResult.PremiereDate = r.ToUniversalTime();
|
|
|
|
remoteResult.ProductionYear = remoteResult.PremiereDate.Value.Year;
|
|
|
|
}
|
|
|
|
}
|
2014-07-05 05:17:24 +02:00
|
|
|
|
2020-06-06 21:17:49 +02:00
|
|
|
remoteResult.SetProviderId(MetadataProvider.Tmdb, obj.Id.ToString(_usCulture));
|
2014-03-02 18:09:35 +01:00
|
|
|
|
2019-08-18 14:44:13 +02:00
|
|
|
if (!string.IsNullOrWhiteSpace(obj.Imdb_Id))
|
2014-03-02 18:09:35 +01:00
|
|
|
{
|
2020-06-06 21:17:49 +02:00
|
|
|
remoteResult.SetProviderId(MetadataProvider.Imdb, obj.Imdb_Id);
|
2014-03-02 18:09:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return new[] { remoteResult };
|
|
|
|
}
|
|
|
|
|
2019-08-18 13:20:52 +02:00
|
|
|
return await new TmdbSearch(_logger, _jsonSerializer, _libraryManager).GetMovieSearchResults(searchInfo, cancellationToken).ConfigureAwait(false);
|
2014-02-20 05:53:15 +01:00
|
|
|
}
|
|
|
|
|
2014-02-07 04:10:13 +01:00
|
|
|
public Task<MetadataResult<Movie>> GetMetadata(MovieInfo info, CancellationToken cancellationToken)
|
2013-03-04 06:43:06 +01:00
|
|
|
{
|
2014-02-07 04:10:13 +01:00
|
|
|
return GetItemMetadata<Movie>(info, cancellationToken);
|
2013-02-24 22:53:54 +01:00
|
|
|
}
|
|
|
|
|
2014-02-07 04:10:13 +01:00
|
|
|
public Task<MetadataResult<T>> GetItemMetadata<T>(ItemLookupInfo id, CancellationToken cancellationToken)
|
2015-03-14 21:00:32 +01:00
|
|
|
where T : BaseItem, new()
|
2013-02-21 02:33:05 +01:00
|
|
|
{
|
2019-08-18 13:20:52 +02:00
|
|
|
var movieDb = new GenericTmdbMovieInfo<T>(_logger, _jsonSerializer, _libraryManager, _fileSystem);
|
2014-02-06 05:39:16 +01:00
|
|
|
|
|
|
|
return movieDb.GetMetadata(id, cancellationToken);
|
2013-02-21 02:33:05 +01:00
|
|
|
}
|
|
|
|
|
2019-08-18 13:20:52 +02:00
|
|
|
public string Name => TmdbUtils.ProviderName;
|
2013-02-21 02:33:05 +01:00
|
|
|
|
|
|
|
/// <summary>
|
2020-06-16 00:37:52 +02:00
|
|
|
/// The _TMDB settings task.
|
2013-02-21 02:33:05 +01:00
|
|
|
/// </summary>
|
2013-05-19 21:37:52 +02:00
|
|
|
private TmdbSettingsResult _tmdbSettings;
|
|
|
|
|
2013-02-21 02:33:05 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Gets the TMDB settings.
|
|
|
|
/// </summary>
|
2013-05-19 21:37:52 +02:00
|
|
|
/// <returns>Task{TmdbSettingsResult}.</returns>
|
|
|
|
internal async Task<TmdbSettingsResult> GetTmdbSettings(CancellationToken cancellationToken)
|
2013-02-21 02:33:05 +01:00
|
|
|
{
|
2013-05-19 21:37:52 +02:00
|
|
|
if (_tmdbSettings != null)
|
2013-02-21 02:33:05 +01:00
|
|
|
{
|
2013-05-19 21:37:52 +02:00
|
|
|
return _tmdbSettings;
|
2013-02-21 02:33:05 +01:00
|
|
|
}
|
|
|
|
|
2019-01-17 20:24:39 +01:00
|
|
|
using (HttpResponseInfo response = await GetMovieDbResponse(new HttpRequestOptions
|
2013-02-21 02:33:05 +01:00
|
|
|
{
|
2020-07-04 22:06:27 +02:00
|
|
|
Url = string.Format(CultureInfo.InvariantCulture, TmdbConfigUrl, TmdbUtils.ApiKey),
|
2014-04-13 19:27:13 +02:00
|
|
|
CancellationToken = cancellationToken,
|
2019-08-18 13:20:52 +02:00
|
|
|
AcceptHeader = TmdbUtils.AcceptHeader
|
2014-04-13 19:27:13 +02:00
|
|
|
}).ConfigureAwait(false))
|
2013-02-21 02:33:05 +01:00
|
|
|
{
|
2019-01-17 20:24:39 +01:00
|
|
|
using (Stream json = response.Content)
|
2017-10-20 18:16:56 +02:00
|
|
|
{
|
2018-09-12 19:26:21 +02:00
|
|
|
_tmdbSettings = await _jsonSerializer.DeserializeFromStreamAsync<TmdbSettingsResult>(json).ConfigureAwait(false);
|
2014-04-13 19:27:13 +02:00
|
|
|
|
2017-10-20 18:16:56 +02:00
|
|
|
return _tmdbSettings;
|
|
|
|
}
|
2013-02-21 02:33:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-18 13:34:44 +02:00
|
|
|
private const string TmdbConfigUrl = TmdbUtils.BaseTmdbApiUrl + "3/configuration?api_key={0}";
|
|
|
|
private const string GetMovieInfo3 = TmdbUtils.BaseTmdbApiUrl + @"3/movie/{0}?api_key={1}&append_to_response=casts,releases,images,keywords,trailers";
|
2013-02-21 02:33:05 +01:00
|
|
|
|
2013-10-14 19:34:54 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Gets the movie data path.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="appPaths">The app paths.</param>
|
|
|
|
/// <param name="tmdbId">The TMDB id.</param>
|
|
|
|
/// <returns>System.String.</returns>
|
2014-01-30 22:23:54 +01:00
|
|
|
internal static string GetMovieDataPath(IApplicationPaths appPaths, string tmdbId)
|
2013-10-14 19:34:54 +02:00
|
|
|
{
|
2014-01-30 22:23:54 +01:00
|
|
|
var dataPath = GetMoviesDataPath(appPaths);
|
2013-10-14 19:34:54 +02:00
|
|
|
|
|
|
|
return Path.Combine(dataPath, tmdbId);
|
|
|
|
}
|
|
|
|
|
|
|
|
internal static string GetMoviesDataPath(IApplicationPaths appPaths)
|
|
|
|
{
|
2014-10-07 05:03:38 +02:00
|
|
|
var dataPath = Path.Combine(appPaths.CachePath, "tmdb-movies2");
|
2013-10-14 19:34:54 +02:00
|
|
|
|
|
|
|
return dataPath;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Downloads the movie info.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="id">The id.</param>
|
2013-12-27 01:23:58 +01:00
|
|
|
/// <param name="preferredMetadataLanguage">The preferred metadata language.</param>
|
2013-10-14 19:34:54 +02:00
|
|
|
/// <param name="cancellationToken">The cancellation token.</param>
|
|
|
|
/// <returns>Task.</returns>
|
2014-01-30 22:23:54 +01:00
|
|
|
internal async Task DownloadMovieInfo(string id, string preferredMetadataLanguage, CancellationToken cancellationToken)
|
2013-10-14 19:34:54 +02:00
|
|
|
{
|
2014-09-28 22:49:43 +02:00
|
|
|
var mainResult = await FetchMainResult(id, true, preferredMetadataLanguage, cancellationToken).ConfigureAwait(false);
|
2013-02-21 02:33:05 +01:00
|
|
|
|
2020-06-20 10:35:29 +02:00
|
|
|
if (mainResult == null)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2013-02-21 02:33:05 +01:00
|
|
|
|
2014-01-30 22:23:54 +01:00
|
|
|
var dataFilePath = GetDataFilePath(id, preferredMetadataLanguage);
|
2013-10-14 19:34:54 +02:00
|
|
|
|
2019-01-26 22:08:04 +01:00
|
|
|
Directory.CreateDirectory(Path.GetDirectoryName(dataFilePath));
|
2013-10-14 19:34:54 +02:00
|
|
|
|
2014-02-06 05:39:16 +01:00
|
|
|
_jsonSerializer.SerializeToFile(mainResult, dataFilePath);
|
2013-02-21 02:33:05 +01:00
|
|
|
}
|
|
|
|
|
2014-02-06 05:39:16 +01:00
|
|
|
internal Task EnsureMovieInfo(string tmdbId, string language, CancellationToken cancellationToken)
|
2014-01-10 14:52:01 +01:00
|
|
|
{
|
2014-02-06 05:39:16 +01:00
|
|
|
if (string.IsNullOrEmpty(tmdbId))
|
|
|
|
{
|
2019-01-06 21:50:43 +01:00
|
|
|
throw new ArgumentNullException(nameof(tmdbId));
|
2014-02-06 05:39:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var path = GetDataFilePath(tmdbId, language);
|
|
|
|
|
2014-01-10 14:52:01 +01:00
|
|
|
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 <= 2)
|
2014-01-10 14:52:01 +01:00
|
|
|
{
|
2018-09-12 19:26:21 +02:00
|
|
|
return Task.CompletedTask;
|
2014-01-10 14:52:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-06 05:39:16 +01:00
|
|
|
return DownloadMovieInfo(tmdbId, language, cancellationToken);
|
2014-01-10 14:52:01 +01:00
|
|
|
}
|
|
|
|
|
2014-02-06 05:39:16 +01:00
|
|
|
internal string GetDataFilePath(string tmdbId, string preferredLanguage)
|
2013-10-14 19:34:54 +02:00
|
|
|
{
|
2014-02-06 05:39:16 +01:00
|
|
|
if (string.IsNullOrEmpty(tmdbId))
|
2013-10-14 19:34:54 +02:00
|
|
|
{
|
2019-01-06 21:50:43 +01:00
|
|
|
throw new ArgumentNullException(nameof(tmdbId));
|
2014-02-06 05:39:16 +01:00
|
|
|
}
|
2013-10-14 19:34:54 +02:00
|
|
|
|
2014-02-06 05:39:16 +01:00
|
|
|
var path = GetMovieDataPath(_configurationManager.ApplicationPaths, tmdbId);
|
2013-10-14 19:34:54 +02:00
|
|
|
|
2016-03-04 18:38:51 +01:00
|
|
|
if (string.IsNullOrWhiteSpace(preferredLanguage))
|
|
|
|
{
|
|
|
|
preferredLanguage = "alllang";
|
|
|
|
}
|
|
|
|
|
2020-07-04 22:06:27 +02:00
|
|
|
var filename = string.Format(CultureInfo.InvariantCulture, "all-{0}.json", preferredLanguage);
|
2013-12-28 17:58:13 +01:00
|
|
|
|
|
|
|
return Path.Combine(path, filename);
|
2013-11-01 16:55:25 +01:00
|
|
|
}
|
|
|
|
|
2015-01-31 04:19:41 +01:00
|
|
|
public static string GetImageLanguagesParam(string preferredLanguage)
|
2014-10-07 01:30:05 +02:00
|
|
|
{
|
|
|
|
var languages = new List<string>();
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(preferredLanguage))
|
|
|
|
{
|
2016-06-25 01:53:38 +02:00
|
|
|
preferredLanguage = NormalizeLanguage(preferredLanguage);
|
|
|
|
|
2014-10-07 01:30:05 +02:00
|
|
|
languages.Add(preferredLanguage);
|
2016-06-25 01:53:38 +02:00
|
|
|
|
|
|
|
if (preferredLanguage.Length == 5) // like en-US
|
|
|
|
{
|
|
|
|
// Currenty, TMDB supports 2-letter language codes only
|
|
|
|
// They are planning to change this in the future, thus we're
|
|
|
|
// supplying both codes if we're having a 5-letter code.
|
|
|
|
languages.Add(preferredLanguage.Substring(0, 2));
|
|
|
|
}
|
2014-10-07 01:30:05 +02:00
|
|
|
}
|
2016-06-25 01:53:38 +02:00
|
|
|
|
2014-10-07 01:30:05 +02:00
|
|
|
languages.Add("null");
|
2016-06-25 01:53:38 +02:00
|
|
|
|
2014-10-07 01:30:05 +02:00
|
|
|
if (!string.Equals(preferredLanguage, "en", StringComparison.OrdinalIgnoreCase))
|
|
|
|
{
|
|
|
|
languages.Add("en");
|
|
|
|
}
|
|
|
|
|
2020-07-04 22:06:27 +02:00
|
|
|
return string.Join(",", languages);
|
2014-10-07 01:30:05 +02:00
|
|
|
}
|
|
|
|
|
2016-03-12 16:14:17 +01:00
|
|
|
public static string NormalizeLanguage(string language)
|
|
|
|
{
|
2016-06-25 01:53:38 +02:00
|
|
|
if (!string.IsNullOrEmpty(language))
|
2016-03-12 16:14:17 +01:00
|
|
|
{
|
2016-06-25 01:53:38 +02:00
|
|
|
// They require this to be uppercase
|
2018-12-30 22:08:54 +01:00
|
|
|
// Everything after the hyphen must be written in uppercase due to a way TMDB wrote their api.
|
|
|
|
// See here: https://www.themoviedb.org/talk/5119221d760ee36c642af4ad?page=3#56e372a0c3a3685a9e0019ab
|
2016-06-25 01:53:38 +02:00
|
|
|
var parts = language.Split('-');
|
|
|
|
|
|
|
|
if (parts.Length == 2)
|
|
|
|
{
|
2019-01-27 12:03:43 +01:00
|
|
|
language = parts[0] + "-" + parts[1].ToUpperInvariant();
|
2016-06-25 01:53:38 +02:00
|
|
|
}
|
2016-03-12 16:14:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return language;
|
|
|
|
}
|
|
|
|
|
2016-06-25 01:53:38 +02:00
|
|
|
public static string AdjustImageLanguage(string imageLanguage, string requestLanguage)
|
|
|
|
{
|
2018-12-11 07:31:08 +01:00
|
|
|
if (!string.IsNullOrEmpty(imageLanguage)
|
|
|
|
&& !string.IsNullOrEmpty(requestLanguage)
|
|
|
|
&& requestLanguage.Length > 2
|
2016-06-25 01:53:38 +02:00
|
|
|
&& imageLanguage.Length == 2
|
|
|
|
&& requestLanguage.StartsWith(imageLanguage, StringComparison.OrdinalIgnoreCase))
|
|
|
|
{
|
|
|
|
return requestLanguage;
|
|
|
|
}
|
|
|
|
|
|
|
|
return imageLanguage;
|
|
|
|
}
|
|
|
|
|
2013-10-14 19:34:54 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Fetches the main result.
|
|
|
|
/// </summary>
|
2013-02-21 02:33:05 +01:00
|
|
|
/// <param name="id">The id.</param>
|
2014-09-28 22:49:43 +02:00
|
|
|
/// <param name="isTmdbId">if set to <c>true</c> [is TMDB identifier].</param>
|
2013-10-22 21:03:21 +02:00
|
|
|
/// <param name="language">The language.</param>
|
2020-06-19 12:21:49 +02:00
|
|
|
/// <param name="cancellationToken">The cancellation token.</param>
|
2013-02-21 02:33:05 +01:00
|
|
|
/// <returns>Task{CompleteMovieData}.</returns>
|
2019-08-18 13:20:52 +02:00
|
|
|
internal async Task<MovieResult> FetchMainResult(string id, bool isTmdbId, string language, CancellationToken cancellationToken)
|
2013-02-21 02:33:05 +01:00
|
|
|
{
|
2020-07-04 22:06:27 +02:00
|
|
|
var url = string.Format(CultureInfo.InvariantCulture, GetMovieInfo3, id, TmdbUtils.ApiKey);
|
2013-10-22 21:52:33 +02:00
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(language))
|
|
|
|
{
|
2020-07-04 22:06:27 +02:00
|
|
|
url += string.Format(CultureInfo.InvariantCulture, "&language={0}", NormalizeLanguage(language));
|
2013-10-22 21:52:33 +02:00
|
|
|
|
2016-03-04 18:38:51 +01:00
|
|
|
// Get images in english and with no language
|
|
|
|
url += "&include_image_language=" + GetImageLanguagesParam(language);
|
|
|
|
}
|
2014-02-17 22:35:08 +01:00
|
|
|
|
2019-08-18 13:20:52 +02:00
|
|
|
MovieResult mainResult;
|
2013-02-21 02:33:05 +01:00
|
|
|
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
|
|
|
2014-09-28 22:49:43 +02:00
|
|
|
// Cache if not using a tmdbId because we won't have the tmdb cache directory structure. So use the lower level cache.
|
2014-10-02 02:28:16 +02:00
|
|
|
var cacheMode = isTmdbId ? CacheMode.None : CacheMode.Unconditional;
|
2014-09-28 22:49:43 +02:00
|
|
|
var cacheLength = TimeSpan.FromDays(3);
|
|
|
|
|
2015-10-26 06:29:32 +01:00
|
|
|
try
|
2013-02-21 02:33:05 +01:00
|
|
|
{
|
2017-10-20 18:16:56 +02:00
|
|
|
using (var response = await GetMovieDbResponse(new HttpRequestOptions
|
2015-10-26 06:29:32 +01:00
|
|
|
{
|
|
|
|
Url = url,
|
|
|
|
CancellationToken = cancellationToken,
|
2019-08-18 13:20:52 +02:00
|
|
|
AcceptHeader = TmdbUtils.AcceptHeader,
|
2015-10-26 06:29:32 +01:00
|
|
|
CacheMode = cacheMode,
|
|
|
|
CacheLength = cacheLength
|
|
|
|
}).ConfigureAwait(false))
|
|
|
|
{
|
2017-10-20 18:16:56 +02:00
|
|
|
using (var json = response.Content)
|
|
|
|
{
|
2019-08-18 13:20:52 +02:00
|
|
|
mainResult = await _jsonSerializer.DeserializeFromStreamAsync<MovieResult>(json).ConfigureAwait(false);
|
2017-10-20 18:16:56 +02:00
|
|
|
}
|
2015-10-26 06:29:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (HttpException ex)
|
2013-02-21 02:33:05 +01:00
|
|
|
{
|
2015-10-26 06:29:32 +01:00
|
|
|
// Return null so that callers know there is no metadata for this id
|
|
|
|
if (ex.StatusCode.HasValue && ex.StatusCode.Value == HttpStatusCode.NotFound)
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw;
|
2013-02-21 02:33:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
|
|
|
2015-01-31 04:19:41 +01:00
|
|
|
// If the language preference isn't english, then have the overview fallback to english if it's blank
|
|
|
|
if (mainResult != null &&
|
2019-08-18 14:44:13 +02:00
|
|
|
string.IsNullOrEmpty(mainResult.Overview) &&
|
2015-10-26 06:29:32 +01:00
|
|
|
!string.IsNullOrEmpty(language) &&
|
2015-01-31 04:19:41 +01:00
|
|
|
!string.Equals(language, "en", StringComparison.OrdinalIgnoreCase))
|
2013-02-21 02:33:05 +01:00
|
|
|
{
|
2018-12-13 14:18:25 +01:00
|
|
|
_logger.LogInformation("MovieDbProvider couldn't find meta for language " + language + ". Trying English...");
|
2013-05-07 00:52:13 +02:00
|
|
|
|
2020-07-04 22:06:27 +02:00
|
|
|
url = string.Format(CultureInfo.InvariantCulture,GetMovieInfo3, id, TmdbUtils.ApiKey) + "&language=en";
|
2016-03-04 18:38:51 +01:00
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(language))
|
|
|
|
{
|
|
|
|
// Get images in english and with no language
|
|
|
|
url += "&include_image_language=" + GetImageLanguagesParam(language);
|
|
|
|
}
|
2013-02-21 02:33:05 +01:00
|
|
|
|
2017-10-20 18:16:56 +02:00
|
|
|
using (var response = await GetMovieDbResponse(new HttpRequestOptions
|
2015-01-31 04:19:41 +01:00
|
|
|
{
|
|
|
|
Url = url,
|
|
|
|
CancellationToken = cancellationToken,
|
2019-08-18 13:20:52 +02:00
|
|
|
AcceptHeader = TmdbUtils.AcceptHeader,
|
2015-01-31 04:19:41 +01:00
|
|
|
CacheMode = cacheMode,
|
|
|
|
CacheLength = cacheLength
|
|
|
|
}).ConfigureAwait(false))
|
|
|
|
{
|
2017-10-20 18:16:56 +02:00
|
|
|
using (var json = response.Content)
|
|
|
|
{
|
2019-08-18 13:20:52 +02:00
|
|
|
var englishResult = await _jsonSerializer.DeserializeFromStreamAsync<MovieResult>(json).ConfigureAwait(false);
|
2015-01-31 04:19:41 +01:00
|
|
|
|
2019-08-18 14:44:13 +02:00
|
|
|
mainResult.Overview = englishResult.Overview;
|
2017-10-20 18:16:56 +02:00
|
|
|
}
|
2013-02-21 02:33:05 +01:00
|
|
|
}
|
|
|
|
}
|
2014-07-05 05:17:24 +02:00
|
|
|
|
2013-02-21 02:33:05 +01:00
|
|
|
return mainResult;
|
|
|
|
}
|
|
|
|
|
2015-12-28 18:27:37 +01:00
|
|
|
private static long _lastRequestTicks;
|
2016-01-09 18:57:12 +01:00
|
|
|
// The limit is 40 requests per 10 seconds
|
2020-02-23 10:53:51 +01:00
|
|
|
private const int RequestIntervalMs = 300;
|
2015-12-28 18:27:37 +01:00
|
|
|
|
2013-05-19 20:42:58 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Gets the movie db response.
|
|
|
|
/// </summary>
|
2017-10-20 18:16:56 +02:00
|
|
|
internal async Task<HttpResponseInfo> GetMovieDbResponse(HttpRequestOptions options)
|
2013-05-19 20:42:58 +02:00
|
|
|
{
|
2020-02-23 10:53:51 +01:00
|
|
|
var delayTicks = (RequestIntervalMs * 10000) - (DateTime.UtcNow.Ticks - _lastRequestTicks);
|
|
|
|
var delayMs = Math.Min(delayTicks / 10000, RequestIntervalMs);
|
2015-12-28 18:27:37 +01:00
|
|
|
|
|
|
|
if (delayMs > 0)
|
|
|
|
{
|
2018-12-13 14:18:25 +01:00
|
|
|
_logger.LogDebug("Throttling Tmdb by {0} ms", delayMs);
|
2015-12-28 18:27:37 +01:00
|
|
|
await Task.Delay(Convert.ToInt32(delayMs)).ConfigureAwait(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
_lastRequestTicks = DateTime.UtcNow.Ticks;
|
2013-05-19 20:42:58 +02:00
|
|
|
|
2016-10-31 19:39:41 +01:00
|
|
|
options.BufferContent = true;
|
2019-01-20 01:12:44 +01:00
|
|
|
options.UserAgent = _appHost.ApplicationUserAgent;
|
2016-01-23 02:58:22 +01:00
|
|
|
|
2020-02-23 10:53:51 +01:00
|
|
|
return await _httpClient.SendAsync(options, HttpMethod.Get).ConfigureAwait(false);
|
2013-05-19 20:42:58 +02:00
|
|
|
}
|
2013-02-21 02:33:05 +01:00
|
|
|
|
2020-02-23 10:53:51 +01:00
|
|
|
/// <inheritdoc />
|
2019-01-13 21:31:14 +01:00
|
|
|
public int Order => 1;
|
2014-02-20 05:53:15 +01:00
|
|
|
|
2020-02-23 10:53:51 +01:00
|
|
|
/// <inheritdoc />
|
2014-02-20 05:53:15 +01:00
|
|
|
public Task<HttpResponseInfo> GetImageResponse(string url, CancellationToken cancellationToken)
|
|
|
|
{
|
2014-03-01 23:34:27 +01:00
|
|
|
return _httpClient.GetResponse(new HttpRequestOptions
|
|
|
|
{
|
|
|
|
CancellationToken = cancellationToken,
|
2016-10-30 01:49:23 +02:00
|
|
|
Url = url
|
2014-03-01 23:34:27 +01:00
|
|
|
});
|
2014-02-20 05:53:15 +01:00
|
|
|
}
|
2013-02-21 02:33:05 +01:00
|
|
|
}
|
|
|
|
}
|