2020-06-19 18:24:13 +00:00
|
|
|
#pragma warning disable CS1591
|
|
|
|
|
2019-01-13 20:03:10 +00:00
|
|
|
using System;
|
2019-01-13 19:26:31 +00:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Globalization;
|
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
2020-08-17 19:10:02 +00:00
|
|
|
using System.Net.Http;
|
|
|
|
using System.Net.Http.Headers;
|
2019-01-13 19:26:31 +00:00
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using MediaBrowser.Common.Configuration;
|
2014-01-30 21:23:54 +00:00
|
|
|
using MediaBrowser.Controller.Configuration;
|
|
|
|
using MediaBrowser.Controller.Entities.Movies;
|
2014-11-18 02:48:22 +00:00
|
|
|
using MediaBrowser.Controller.Library;
|
2014-01-30 21:23:54 +00:00
|
|
|
using MediaBrowser.Controller.Providers;
|
|
|
|
using MediaBrowser.Model.Entities;
|
2019-01-13 19:26:31 +00:00
|
|
|
using MediaBrowser.Model.Globalization;
|
|
|
|
using MediaBrowser.Model.IO;
|
2014-02-20 04:53:15 +00:00
|
|
|
using MediaBrowser.Model.Providers;
|
2014-01-30 21:23:54 +00:00
|
|
|
using MediaBrowser.Model.Serialization;
|
2020-05-31 06:23:09 +00:00
|
|
|
using MediaBrowser.Providers.Plugins.Tmdb.Models.Collections;
|
|
|
|
using MediaBrowser.Providers.Plugins.Tmdb.Models.General;
|
|
|
|
using MediaBrowser.Providers.Plugins.Tmdb.Movies;
|
2019-01-13 19:26:31 +00:00
|
|
|
using Microsoft.Extensions.Logging;
|
2014-01-30 21:23:54 +00:00
|
|
|
|
2020-05-31 06:23:09 +00:00
|
|
|
namespace MediaBrowser.Providers.Plugins.Tmdb.BoxSets
|
2014-01-30 21:23:54 +00:00
|
|
|
{
|
2019-08-18 11:20:52 +00:00
|
|
|
public class TmdbBoxSetProvider : IRemoteMetadataProvider<BoxSet, BoxSetInfo>
|
2014-01-30 21:23:54 +00:00
|
|
|
{
|
2019-08-18 11:34:44 +00:00
|
|
|
private const string GetCollectionInfo3 = TmdbUtils.BaseTmdbApiUrl + @"3/collection/{0}?api_key={1}&append_to_response=images";
|
2014-01-30 21:47:13 +00:00
|
|
|
|
2019-08-18 11:20:52 +00:00
|
|
|
internal static TmdbBoxSetProvider Current;
|
2014-01-30 21:47:13 +00:00
|
|
|
|
2020-06-06 00:15:56 +00:00
|
|
|
private readonly ILogger<TmdbBoxSetProvider> _logger;
|
2014-01-30 21:23:54 +00:00
|
|
|
private readonly IJsonSerializer _json;
|
|
|
|
private readonly IServerConfigurationManager _config;
|
|
|
|
private readonly IFileSystem _fileSystem;
|
2014-02-18 03:02:41 +00:00
|
|
|
private readonly ILocalizationManager _localization;
|
2020-08-17 19:10:02 +00:00
|
|
|
private readonly IHttpClientFactory _httpClientFactory;
|
2014-11-18 02:48:22 +00:00
|
|
|
private readonly ILibraryManager _libraryManager;
|
2014-01-30 21:23:54 +00:00
|
|
|
|
2020-03-03 22:07:10 +00:00
|
|
|
public TmdbBoxSetProvider(
|
|
|
|
ILogger<TmdbBoxSetProvider> logger,
|
|
|
|
IJsonSerializer json,
|
|
|
|
IServerConfigurationManager config,
|
|
|
|
IFileSystem fileSystem,
|
|
|
|
ILocalizationManager localization,
|
2020-08-17 19:10:02 +00:00
|
|
|
IHttpClientFactory httpClientFactory,
|
2020-03-03 22:07:10 +00:00
|
|
|
ILibraryManager libraryManager)
|
2014-01-30 21:23:54 +00:00
|
|
|
{
|
|
|
|
_logger = logger;
|
|
|
|
_json = json;
|
|
|
|
_config = config;
|
|
|
|
_fileSystem = fileSystem;
|
2014-02-18 03:02:41 +00:00
|
|
|
_localization = localization;
|
2020-08-17 19:10:02 +00:00
|
|
|
_httpClientFactory = httpClientFactory;
|
2014-11-18 02:48:22 +00:00
|
|
|
_libraryManager = libraryManager;
|
2014-01-30 21:47:13 +00:00
|
|
|
Current = this;
|
2014-01-30 21:23:54 +00:00
|
|
|
}
|
|
|
|
|
2014-03-01 22:34:27 +00:00
|
|
|
private readonly CultureInfo _usCulture = new CultureInfo("en-US");
|
2019-01-07 23:27:46 +00:00
|
|
|
|
2014-02-20 04:53:15 +00:00
|
|
|
public async Task<IEnumerable<RemoteSearchResult>> GetSearchResults(BoxSetInfo searchInfo, CancellationToken cancellationToken)
|
|
|
|
{
|
2020-06-06 19:17:49 +00:00
|
|
|
var tmdbId = searchInfo.GetProviderId(MetadataProvider.Tmdb);
|
2014-03-01 22:34:27 +00:00
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(tmdbId))
|
|
|
|
{
|
|
|
|
await EnsureInfo(tmdbId, searchInfo.MetadataLanguage, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
|
|
|
var dataFilePath = GetDataFilePath(_config.ApplicationPaths, tmdbId, searchInfo.MetadataLanguage);
|
2019-08-18 11:20:52 +00:00
|
|
|
var info = _json.DeserializeFromFile<CollectionResult>(dataFilePath);
|
2014-03-01 22:34:27 +00:00
|
|
|
|
2019-08-18 12:44:13 +00:00
|
|
|
var images = (info.Images ?? new CollectionImages()).Posters ?? new List<Poster>();
|
2014-09-22 21:56:54 +00:00
|
|
|
|
2019-08-18 11:20:52 +00:00
|
|
|
var tmdbSettings = await TmdbMovieProvider.Current.GetTmdbSettings(cancellationToken).ConfigureAwait(false);
|
2014-09-22 21:56:54 +00:00
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
var tmdbImageUrl = tmdbSettings.images.GetImageUrl("original");
|
2014-09-22 21:56:54 +00:00
|
|
|
|
2014-03-01 22:34:27 +00:00
|
|
|
var result = new RemoteSearchResult
|
|
|
|
{
|
2019-08-18 12:44:13 +00:00
|
|
|
Name = info.Name,
|
2014-03-01 22:34:27 +00:00
|
|
|
SearchProviderName = Name,
|
2019-08-18 12:44:13 +00:00
|
|
|
ImageUrl = images.Count == 0 ? null : (tmdbImageUrl + images[0].File_Path)
|
2014-03-01 22:34:27 +00:00
|
|
|
};
|
|
|
|
|
2020-06-06 19:17:49 +00:00
|
|
|
result.SetProviderId(MetadataProvider.Tmdb, info.Id.ToString(_usCulture));
|
2014-03-01 22:34:27 +00:00
|
|
|
|
|
|
|
return new[] { result };
|
|
|
|
}
|
|
|
|
|
2019-08-18 11:20:52 +00:00
|
|
|
return await new TmdbSearch(_logger, _json, _libraryManager).GetSearchResults(searchInfo, cancellationToken).ConfigureAwait(false);
|
2014-02-20 04:53:15 +00:00
|
|
|
}
|
|
|
|
|
2014-02-07 03:10:13 +00:00
|
|
|
public async Task<MetadataResult<BoxSet>> GetMetadata(BoxSetInfo id, CancellationToken cancellationToken)
|
2014-01-30 21:23:54 +00:00
|
|
|
{
|
2020-06-06 19:17:49 +00:00
|
|
|
var tmdbId = id.GetProviderId(MetadataProvider.Tmdb);
|
2014-01-30 21:23:54 +00:00
|
|
|
|
|
|
|
// We don't already have an Id, need to fetch it
|
|
|
|
if (string.IsNullOrEmpty(tmdbId))
|
|
|
|
{
|
2019-08-18 11:20:52 +00:00
|
|
|
var searchResults = await new TmdbSearch(_logger, _json, _libraryManager).GetSearchResults(id, cancellationToken).ConfigureAwait(false);
|
2014-03-01 22:34:27 +00:00
|
|
|
|
|
|
|
var searchResult = searchResults.FirstOrDefault();
|
2014-02-15 16:36:09 +00:00
|
|
|
|
|
|
|
if (searchResult != null)
|
|
|
|
{
|
2020-06-06 19:17:49 +00:00
|
|
|
tmdbId = searchResult.GetProviderId(MetadataProvider.Tmdb);
|
2014-02-15 16:36:09 +00:00
|
|
|
}
|
2014-01-30 21:23:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var result = new MetadataResult<BoxSet>();
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(tmdbId))
|
|
|
|
{
|
2014-01-30 21:47:13 +00:00
|
|
|
var mainResult = await GetMovieDbResult(tmdbId, id.MetadataLanguage, cancellationToken).ConfigureAwait(false);
|
2014-01-30 21:23:54 +00:00
|
|
|
|
2014-01-30 21:47:13 +00:00
|
|
|
if (mainResult != null)
|
2014-01-30 21:23:54 +00:00
|
|
|
{
|
|
|
|
result.HasMetadata = true;
|
|
|
|
result.Item = GetItem(mainResult);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-08-18 11:20:52 +00:00
|
|
|
internal async Task<CollectionResult> GetMovieDbResult(string tmdbId, string language, CancellationToken cancellationToken)
|
2014-01-30 21:47:13 +00:00
|
|
|
{
|
|
|
|
if (string.IsNullOrEmpty(tmdbId))
|
|
|
|
{
|
2019-01-06 20:50:43 +00:00
|
|
|
throw new ArgumentNullException(nameof(tmdbId));
|
2014-01-30 21:47:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
await EnsureInfo(tmdbId, language, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
|
|
|
var dataFilePath = GetDataFilePath(_config.ApplicationPaths, tmdbId, language);
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(dataFilePath))
|
|
|
|
{
|
2019-08-18 11:20:52 +00:00
|
|
|
return _json.DeserializeFromFile<CollectionResult>(dataFilePath);
|
2014-01-30 21:47:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-08-18 11:20:52 +00:00
|
|
|
private BoxSet GetItem(CollectionResult obj)
|
2014-01-30 21:23:54 +00:00
|
|
|
{
|
2014-02-10 18:39:41 +00:00
|
|
|
var item = new BoxSet
|
|
|
|
{
|
2019-08-18 12:44:13 +00:00
|
|
|
Name = obj.Name,
|
|
|
|
Overview = obj.Overview
|
2014-02-10 18:39:41 +00:00
|
|
|
};
|
2014-01-30 21:23:54 +00:00
|
|
|
|
2020-06-06 19:17:49 +00:00
|
|
|
item.SetProviderId(MetadataProvider.Tmdb, obj.Id.ToString(_usCulture));
|
2014-01-30 21:23:54 +00:00
|
|
|
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
|
|
|
private async Task DownloadInfo(string tmdbId, string preferredMetadataLanguage, CancellationToken cancellationToken)
|
|
|
|
{
|
|
|
|
var mainResult = await FetchMainResult(tmdbId, preferredMetadataLanguage, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
2020-06-20 08:35:29 +00:00
|
|
|
if (mainResult == null)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2014-01-30 21:23:54 +00:00
|
|
|
|
|
|
|
var dataFilePath = GetDataFilePath(_config.ApplicationPaths, tmdbId, preferredMetadataLanguage);
|
|
|
|
|
2019-01-26 21:08:04 +00:00
|
|
|
Directory.CreateDirectory(Path.GetDirectoryName(dataFilePath));
|
2014-01-30 21:23:54 +00:00
|
|
|
|
|
|
|
_json.SerializeToFile(mainResult, dataFilePath);
|
|
|
|
}
|
|
|
|
|
2019-08-18 11:20:52 +00:00
|
|
|
private async Task<CollectionResult> FetchMainResult(string id, string language, CancellationToken cancellationToken)
|
2014-01-30 21:23:54 +00:00
|
|
|
{
|
2019-08-18 11:20:52 +00:00
|
|
|
var url = string.Format(GetCollectionInfo3, id, TmdbUtils.ApiKey);
|
2014-01-30 21:23:54 +00:00
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(language))
|
|
|
|
{
|
2019-08-18 11:20:52 +00:00
|
|
|
url += string.Format("&language={0}", TmdbMovieProvider.NormalizeLanguage(language));
|
2014-01-30 21:23:54 +00:00
|
|
|
|
2016-03-04 17:38:51 +00:00
|
|
|
// Get images in english and with no language
|
2019-08-18 11:20:52 +00:00
|
|
|
url += "&include_image_language=" + TmdbMovieProvider.GetImageLanguagesParam(language);
|
2016-03-04 17:38:51 +00:00
|
|
|
}
|
2014-02-18 03:02:41 +00:00
|
|
|
|
2014-01-30 21:23:54 +00:00
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
|
|
|
2020-08-17 19:10:02 +00:00
|
|
|
using var requestMessage = new HttpRequestMessage(HttpMethod.Get, url);
|
|
|
|
requestMessage.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue(TmdbUtils.AcceptHeader));
|
|
|
|
using var mainResponse = await TmdbMovieProvider.Current.GetMovieDbResponse(requestMessage);
|
|
|
|
await using var stream = await mainResponse.Content.ReadAsStreamAsync().ConfigureAwait(false);
|
|
|
|
var mainResult = await _json.DeserializeFromStreamAsync<CollectionResult>(stream).ConfigureAwait(false);
|
2014-01-30 21:23:54 +00:00
|
|
|
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
|
|
|
2019-08-18 12:44:13 +00:00
|
|
|
if (mainResult != null && string.IsNullOrEmpty(mainResult.Name))
|
2014-01-30 21:23:54 +00:00
|
|
|
{
|
|
|
|
if (!string.IsNullOrEmpty(language) && !string.Equals(language, "en", StringComparison.OrdinalIgnoreCase))
|
|
|
|
{
|
2019-08-18 11:20:52 +00:00
|
|
|
url = string.Format(GetCollectionInfo3, id, TmdbUtils.ApiKey) + "&language=en";
|
2016-03-04 17:38:51 +00:00
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(language))
|
|
|
|
{
|
|
|
|
// Get images in english and with no language
|
2019-08-18 11:20:52 +00:00
|
|
|
url += "&include_image_language=" + TmdbMovieProvider.GetImageLanguagesParam(language);
|
2016-03-04 17:38:51 +00:00
|
|
|
}
|
2014-01-30 21:23:54 +00:00
|
|
|
|
2020-08-17 19:10:02 +00:00
|
|
|
using var langRequestMessage = new HttpRequestMessage(HttpMethod.Get, url);
|
|
|
|
langRequestMessage.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue(TmdbUtils.AcceptHeader));
|
|
|
|
await using var langStream = await mainResponse.Content.ReadAsStreamAsync().ConfigureAwait(false);
|
|
|
|
mainResult = await _json.DeserializeFromStreamAsync<CollectionResult>(langStream).ConfigureAwait(false);
|
2014-01-30 21:23:54 +00:00
|
|
|
}
|
|
|
|
}
|
2020-05-31 06:28:01 +00:00
|
|
|
|
2014-01-30 21:23:54 +00:00
|
|
|
return mainResult;
|
|
|
|
}
|
2014-02-03 17:44:13 +00:00
|
|
|
|
2014-01-30 21:23:54 +00:00
|
|
|
internal Task EnsureInfo(string tmdbId, string preferredMetadataLanguage, CancellationToken cancellationToken)
|
|
|
|
{
|
|
|
|
var path = GetDataFilePath(_config.ApplicationPaths, tmdbId, preferredMetadataLanguage);
|
|
|
|
|
|
|
|
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 <= 2)
|
2014-01-30 21:23:54 +00:00
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
return Task.CompletedTask;
|
2014-01-30 21:23:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return DownloadInfo(tmdbId, preferredMetadataLanguage, cancellationToken);
|
|
|
|
}
|
2014-02-15 16:36:09 +00:00
|
|
|
|
2019-08-18 11:20:52 +00:00
|
|
|
public string Name => TmdbUtils.ProviderName;
|
2014-01-30 21:23:54 +00:00
|
|
|
|
|
|
|
private static string GetDataFilePath(IApplicationPaths appPaths, string tmdbId, string preferredLanguage)
|
|
|
|
{
|
|
|
|
var path = GetDataPath(appPaths, tmdbId);
|
|
|
|
|
2016-03-04 17:38:51 +00:00
|
|
|
var filename = string.Format("all-{0}.json", preferredLanguage ?? string.Empty);
|
2014-01-30 21:23:54 +00:00
|
|
|
|
|
|
|
return Path.Combine(path, filename);
|
|
|
|
}
|
2014-02-15 16:36:09 +00:00
|
|
|
|
2014-01-30 21:23:54 +00:00
|
|
|
private static string GetDataPath(IApplicationPaths appPaths, string tmdbId)
|
|
|
|
{
|
|
|
|
var dataPath = GetCollectionsDataPath(appPaths);
|
|
|
|
|
|
|
|
return Path.Combine(dataPath, tmdbId);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static string GetCollectionsDataPath(IApplicationPaths appPaths)
|
|
|
|
{
|
2014-02-20 16:37:41 +00:00
|
|
|
var dataPath = Path.Combine(appPaths.CachePath, "tmdb-collections");
|
2014-01-30 21:23:54 +00:00
|
|
|
|
|
|
|
return dataPath;
|
|
|
|
}
|
|
|
|
|
2020-08-17 19:10:02 +00:00
|
|
|
public Task<HttpResponseMessage> GetImageResponse(string url, CancellationToken cancellationToken)
|
2014-02-20 04:53:15 +00:00
|
|
|
{
|
2020-08-17 19:10:02 +00:00
|
|
|
return _httpClientFactory.CreateClient().GetAsync(url, cancellationToken);
|
2014-02-20 04:53:15 +00:00
|
|
|
}
|
2014-01-30 21:23:54 +00:00
|
|
|
}
|
|
|
|
}
|