2019-01-13 20:03:10 +00:00
|
|
|
using System;
|
2016-03-03 01:56:51 +00:00
|
|
|
using System.Globalization;
|
|
|
|
using System.IO;
|
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
2019-01-13 19:26:31 +00:00
|
|
|
using MediaBrowser.Common.Net;
|
|
|
|
using MediaBrowser.Controller.Configuration;
|
2016-10-24 02:45:23 +00:00
|
|
|
using MediaBrowser.Model.Globalization;
|
2019-01-13 19:26:31 +00:00
|
|
|
using MediaBrowser.Model.IO;
|
|
|
|
using MediaBrowser.Model.Serialization;
|
|
|
|
using MediaBrowser.Providers.Movies;
|
2019-08-18 11:20:52 +00:00
|
|
|
using MediaBrowser.Providers.Tmdb.Models.TV;
|
|
|
|
using MediaBrowser.Providers.Tmdb.Movies;
|
2019-01-13 19:26:31 +00:00
|
|
|
using Microsoft.Extensions.Logging;
|
2016-03-03 01:56:51 +00:00
|
|
|
|
2019-08-18 11:20:52 +00:00
|
|
|
namespace MediaBrowser.Providers.Tmdb.TV
|
2016-03-03 01:56:51 +00:00
|
|
|
{
|
2019-08-18 11:20:52 +00:00
|
|
|
public abstract class TmdbEpisodeProviderBase
|
2016-03-03 01:56:51 +00:00
|
|
|
{
|
2019-08-18 11:34:44 +00: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";
|
2016-03-03 01:56:51 +00:00
|
|
|
private readonly IHttpClient _httpClient;
|
|
|
|
private readonly IServerConfigurationManager _configurationManager;
|
|
|
|
private readonly IJsonSerializer _jsonSerializer;
|
|
|
|
private readonly IFileSystem _fileSystem;
|
|
|
|
private readonly ILocalizationManager _localization;
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
2019-08-18 11:20:52 +00:00
|
|
|
protected TmdbEpisodeProviderBase(IHttpClient httpClient, IServerConfigurationManager configurationManager, IJsonSerializer jsonSerializer, IFileSystem fileSystem, ILocalizationManager localization, ILoggerFactory loggerFactory)
|
2016-03-03 01:56:51 +00:00
|
|
|
{
|
|
|
|
_httpClient = httpClient;
|
|
|
|
_configurationManager = configurationManager;
|
|
|
|
_jsonSerializer = jsonSerializer;
|
|
|
|
_fileSystem = fileSystem;
|
|
|
|
_localization = localization;
|
2018-12-13 13:18:25 +00:00
|
|
|
_logger = loggerFactory.CreateLogger(GetType().Name);
|
2016-03-03 01:56:51 +00:00
|
|
|
}
|
|
|
|
|
2019-01-13 20:31:14 +00:00
|
|
|
protected ILogger Logger => _logger;
|
2016-03-03 01:56:51 +00:00
|
|
|
|
2019-08-18 11:20:52 +00:00
|
|
|
protected async Task<EpisodeResult> GetEpisodeInfo(string seriesTmdbId, int season, int episodeNumber, string preferredMetadataLanguage,
|
2016-03-03 01:56:51 +00:00
|
|
|
CancellationToken cancellationToken)
|
|
|
|
{
|
|
|
|
await EnsureEpisodeInfo(seriesTmdbId, season, episodeNumber, preferredMetadataLanguage, cancellationToken)
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
|
|
|
var dataFilePath = GetDataFilePath(seriesTmdbId, season, episodeNumber, preferredMetadataLanguage);
|
|
|
|
|
2019-08-18 11:20:52 +00:00
|
|
|
return _jsonSerializer.DeserializeFromFile<EpisodeResult>(dataFilePath);
|
2016-03-03 01:56:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
internal Task EnsureEpisodeInfo(string tmdbId, int seasonNumber, int episodeNumber, string language, CancellationToken cancellationToken)
|
|
|
|
{
|
|
|
|
if (string.IsNullOrEmpty(tmdbId))
|
|
|
|
{
|
2019-01-06 20:50:43 +00:00
|
|
|
throw new ArgumentNullException(nameof(tmdbId));
|
2016-03-03 01:56:51 +00:00
|
|
|
}
|
|
|
|
if (string.IsNullOrEmpty(language))
|
|
|
|
{
|
2019-01-06 20:50:43 +00:00
|
|
|
throw new ArgumentNullException(nameof(language));
|
2016-03-03 01:56:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 17:26:21 +00:00
|
|
|
if ((DateTime.UtcNow - _fileSystem.GetLastWriteTimeUtc(fileInfo)).TotalDays <= 2)
|
2016-03-03 01:56:51 +00:00
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
return Task.CompletedTask;
|
2016-03-03 01:56:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return DownloadEpisodeInfo(tmdbId, seasonNumber, episodeNumber, language, cancellationToken);
|
|
|
|
}
|
|
|
|
|
|
|
|
internal string GetDataFilePath(string tmdbId, int seasonNumber, int episodeNumber, string preferredLanguage)
|
|
|
|
{
|
|
|
|
if (string.IsNullOrEmpty(tmdbId))
|
|
|
|
{
|
2019-01-06 20:50:43 +00:00
|
|
|
throw new ArgumentNullException(nameof(tmdbId));
|
2016-03-03 01:56:51 +00:00
|
|
|
}
|
|
|
|
if (string.IsNullOrEmpty(preferredLanguage))
|
|
|
|
{
|
2019-01-06 20:50:43 +00:00
|
|
|
throw new ArgumentNullException(nameof(preferredLanguage));
|
2016-03-03 01:56:51 +00:00
|
|
|
}
|
|
|
|
|
2019-08-18 11:20:52 +00:00
|
|
|
var path = TmdbSeriesProvider.GetSeriesDataPath(_configurationManager.ApplicationPaths, tmdbId);
|
2016-03-03 01:56:51 +00:00
|
|
|
|
|
|
|
var filename = string.Format("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);
|
|
|
|
|
2019-01-26 21:08:04 +00:00
|
|
|
Directory.CreateDirectory(Path.GetDirectoryName(dataFilePath));
|
2016-03-03 01:56:51 +00:00
|
|
|
_jsonSerializer.SerializeToFile(mainResult, dataFilePath);
|
|
|
|
}
|
|
|
|
|
2019-08-18 11:20:52 +00:00
|
|
|
internal async Task<EpisodeResult> FetchMainResult(string urlPattern, string id, int seasonNumber, int episodeNumber, string language, CancellationToken cancellationToken)
|
2016-03-03 01:56:51 +00:00
|
|
|
{
|
2019-08-18 11:20:52 +00:00
|
|
|
var url = string.Format(urlPattern, id, seasonNumber.ToString(CultureInfo.InvariantCulture), episodeNumber, TmdbUtils.ApiKey);
|
2016-03-03 01:56:51 +00:00
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(language))
|
|
|
|
{
|
|
|
|
url += string.Format("&language={0}", language);
|
|
|
|
}
|
|
|
|
|
2019-08-18 11:20:52 +00:00
|
|
|
var includeImageLanguageParam = TmdbMovieProvider.GetImageLanguagesParam(language);
|
2016-03-03 01:56:51 +00:00
|
|
|
// Get images in english and with no language
|
|
|
|
url += "&include_image_language=" + includeImageLanguageParam;
|
|
|
|
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
|
|
|
2019-08-18 11:20:52 +00:00
|
|
|
using (var response = await TmdbMovieProvider.Current.GetMovieDbResponse(new HttpRequestOptions
|
2016-03-03 01:56:51 +00:00
|
|
|
{
|
|
|
|
Url = url,
|
|
|
|
CancellationToken = cancellationToken,
|
2019-08-18 11:20:52 +00:00
|
|
|
AcceptHeader = TmdbUtils.AcceptHeader
|
2016-03-03 01:56:51 +00:00
|
|
|
|
|
|
|
}).ConfigureAwait(false))
|
|
|
|
{
|
2017-10-20 16:16:56 +00:00
|
|
|
using (var json = response.Content)
|
|
|
|
{
|
2019-08-18 11:20:52 +00:00
|
|
|
return await _jsonSerializer.DeserializeFromStreamAsync<EpisodeResult>(json).ConfigureAwait(false);
|
2017-10-20 16:16:56 +00:00
|
|
|
}
|
2016-03-03 01:56:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected Task<HttpResponseInfo> GetResponse(string url, CancellationToken cancellationToken)
|
|
|
|
{
|
|
|
|
return _httpClient.GetResponse(new HttpRequestOptions
|
|
|
|
{
|
|
|
|
CancellationToken = cancellationToken,
|
2017-04-30 02:37:51 +00:00
|
|
|
Url = url
|
2016-03-03 01:56:51 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|