jellyfin/MediaBrowser.Providers/Plugins/Tmdb/TV/TmdbEpisodeProviderBase.cs

157 lines
6.6 KiB
C#
Raw Normal View History

#pragma warning disable CS1591
using System;
using System.Globalization;
using System.IO;
2020-08-17 12:10:02 -07:00
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading;
using System.Threading.Tasks;
2020-08-31 10:05:21 -07:00
using MediaBrowser.Common.Net;
using MediaBrowser.Controller.Configuration;
2016-10-23 19:45:23 -07:00
using MediaBrowser.Model.Globalization;
using MediaBrowser.Model.IO;
using MediaBrowser.Model.Serialization;
2020-05-30 23:23:09 -07:00
using MediaBrowser.Providers.Plugins.Tmdb.Models.TV;
using MediaBrowser.Providers.Plugins.Tmdb.Movies;
using Microsoft.Extensions.Logging;
2020-05-30 23:23:09 -07:00
namespace MediaBrowser.Providers.Plugins.Tmdb.TV
{
public abstract class TmdbEpisodeProviderBase
{
2019-08-18 04:34:44 -07:00
private const string EpisodeUrlPattern = TmdbUtils.BaseTmdbApiUrl + @"3/tv/{0}/season/{1}/episode/{2}?api_key={3}&append_to_response=images,external_ids,credits,videos";
2020-09-08 07:12:47 -07:00
2020-08-17 12:10:02 -07:00
private readonly IHttpClientFactory _httpClientFactory;
private readonly IServerConfigurationManager _configurationManager;
private readonly IJsonSerializer _jsonSerializer;
private readonly IFileSystem _fileSystem;
2020-06-05 17:29:58 -07:00
private readonly ILogger<TmdbEpisodeProviderBase> _logger;
2020-08-17 12:10:02 -07:00
protected TmdbEpisodeProviderBase(IHttpClientFactory httpClientFactory, IServerConfigurationManager configurationManager, IJsonSerializer jsonSerializer, IFileSystem fileSystem, ILocalizationManager localization, ILoggerFactory loggerFactory)
{
2020-08-17 12:10:02 -07:00
_httpClientFactory = httpClientFactory;
_configurationManager = configurationManager;
_jsonSerializer = jsonSerializer;
_fileSystem = fileSystem;
2020-06-05 17:29:58 -07:00
_logger = loggerFactory.CreateLogger<TmdbEpisodeProviderBase>();
}
protected ILogger Logger => _logger;
2020-09-08 07:12:47 -07:00
protected async Task<EpisodeResult> GetEpisodeInfo(
string seriesTmdbId,
int season,
int episodeNumber,
string preferredMetadataLanguage,
CancellationToken cancellationToken)
{
await EnsureEpisodeInfo(seriesTmdbId, season, episodeNumber, preferredMetadataLanguage, cancellationToken)
.ConfigureAwait(false);
var dataFilePath = GetDataFilePath(seriesTmdbId, season, episodeNumber, preferredMetadataLanguage);
return _jsonSerializer.DeserializeFromFile<EpisodeResult>(dataFilePath);
}
internal Task EnsureEpisodeInfo(string tmdbId, int seasonNumber, int episodeNumber, string language, CancellationToken cancellationToken)
{
if (string.IsNullOrEmpty(tmdbId))
{
throw new ArgumentNullException(nameof(tmdbId));
}
2020-05-30 23:28:01 -07:00
if (string.IsNullOrEmpty(language))
{
throw new ArgumentNullException(nameof(language));
}
var path = GetDataFilePath(tmdbId, seasonNumber, episodeNumber, language);
var fileInfo = _fileSystem.GetFileSystemInfo(path);
if (fileInfo.Exists)
{
// If it's recent or automatic updates are enabled, don't re-download
2018-09-12 10:26:21 -07:00
if ((DateTime.UtcNow - _fileSystem.GetLastWriteTimeUtc(fileInfo)).TotalDays <= 2)
{
2018-09-12 10:26:21 -07:00
return Task.CompletedTask;
}
}
return DownloadEpisodeInfo(tmdbId, seasonNumber, episodeNumber, language, cancellationToken);
}
internal string GetDataFilePath(string tmdbId, int seasonNumber, int episodeNumber, string preferredLanguage)
{
if (string.IsNullOrEmpty(tmdbId))
{
throw new ArgumentNullException(nameof(tmdbId));
}
2020-05-30 23:28:01 -07:00
if (string.IsNullOrEmpty(preferredLanguage))
{
throw new ArgumentNullException(nameof(preferredLanguage));
}
var path = TmdbSeriesProvider.GetSeriesDataPath(_configurationManager.ApplicationPaths, tmdbId);
2020-09-08 07:12:47 -07:00
var filename = string.Format(
CultureInfo.InvariantCulture,
"season-{0}-episode-{1}-{2}.json",
seasonNumber.ToString(CultureInfo.InvariantCulture),
episodeNumber.ToString(CultureInfo.InvariantCulture),
preferredLanguage);
return Path.Combine(path, filename);
}
internal async Task DownloadEpisodeInfo(string id, int seasonNumber, int episodeNumber, string preferredMetadataLanguage, CancellationToken cancellationToken)
{
var mainResult = await FetchMainResult(EpisodeUrlPattern, id, seasonNumber, episodeNumber, preferredMetadataLanguage, cancellationToken).ConfigureAwait(false);
var dataFilePath = GetDataFilePath(id, seasonNumber, episodeNumber, preferredMetadataLanguage);
Directory.CreateDirectory(Path.GetDirectoryName(dataFilePath));
_jsonSerializer.SerializeToFile(mainResult, dataFilePath);
}
internal async Task<EpisodeResult> FetchMainResult(string urlPattern, string id, int seasonNumber, int episodeNumber, string language, CancellationToken cancellationToken)
{
2020-09-07 04:31:05 -07:00
var url = string.Format(
CultureInfo.InvariantCulture,
urlPattern,
id,
seasonNumber.ToString(CultureInfo.InvariantCulture),
episodeNumber,
TmdbUtils.ApiKey);
if (!string.IsNullOrEmpty(language))
{
2020-08-07 10:26:28 -07:00
url += string.Format(CultureInfo.InvariantCulture, "&language={0}", language);
}
var includeImageLanguageParam = TmdbMovieProvider.GetImageLanguagesParam(language);
// Get images in english and with no language
url += "&include_image_language=" + includeImageLanguageParam;
cancellationToken.ThrowIfCancellationRequested();
2020-08-17 12:10:02 -07:00
using var requestMessage = new HttpRequestMessage(HttpMethod.Get, url);
2020-08-17 12:59:29 -07:00
foreach (var header in TmdbUtils.AcceptHeaders)
{
requestMessage.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue(header));
}
2020-09-07 04:31:05 -07:00
using var response = await TmdbMovieProvider.Current.GetMovieDbResponse(requestMessage, cancellationToken).ConfigureAwait(false);
2020-08-17 12:10:02 -07:00
await using var stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false);
return await _jsonSerializer.DeserializeFromStreamAsync<EpisodeResult>(stream).ConfigureAwait(false);
}
2020-08-17 12:10:02 -07:00
protected Task<HttpResponseMessage> GetResponse(string url, CancellationToken cancellationToken)
{
2020-08-31 10:05:21 -07:00
return _httpClientFactory.CreateClient(NamedClient.Default).GetAsync(url, cancellationToken);
}
}
}