2020-06-19 11:24:13 -07:00
#pragma warning disable CS1591
2019-01-13 13:03:10 -07:00
using System ;
2019-01-13 12:26:31 -07:00
using System.Collections.Generic ;
using System.Globalization ;
using System.IO ;
using System.Net ;
2020-08-17 12:10:02 -07:00
using System.Net.Http ;
using System.Net.Http.Headers ;
2019-01-13 12:26:31 -07:00
using System.Threading ;
using System.Threading.Tasks ;
2014-07-04 20:10:09 -07:00
using MediaBrowser.Controller.Configuration ;
using MediaBrowser.Controller.Providers ;
using MediaBrowser.Model.Entities ;
2019-01-13 12:26:31 -07:00
using MediaBrowser.Model.Globalization ;
using MediaBrowser.Model.IO ;
2014-07-08 19:53:04 -07:00
using MediaBrowser.Model.Net ;
2014-07-04 20:10:09 -07:00
using MediaBrowser.Model.Providers ;
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 ;
2019-01-13 12:26:31 -07:00
using Microsoft.Extensions.Logging ;
2019-08-18 04:20:52 -07:00
using Season = MediaBrowser . Controller . Entities . TV . Season ;
2014-07-04 20:10:09 -07:00
2020-05-30 23:23:09 -07:00
namespace MediaBrowser.Providers.Plugins.Tmdb.TV
2014-07-04 20:10:09 -07:00
{
2019-08-18 04:20:52 -07:00
public class TmdbSeasonProvider : IRemoteMetadataProvider < Season , SeasonInfo >
2014-07-04 20:10:09 -07:00
{
2019-08-18 04:34:44 -07:00
private const string GetTvInfo3 = TmdbUtils . BaseTmdbApiUrl + @"3/tv/{0}/season/{1}?api_key={2}&append_to_response=images,keywords,external_ids,credits,videos" ;
2020-08-17 12:10:02 -07:00
private readonly IHttpClientFactory _httpClientFactory ;
2014-07-04 20:10:09 -07:00
private readonly IServerConfigurationManager _configurationManager ;
private readonly IJsonSerializer _jsonSerializer ;
private readonly IFileSystem _fileSystem ;
private readonly ILocalizationManager _localization ;
2020-06-05 17:15:56 -07:00
private readonly ILogger < TmdbSeasonProvider > _logger ;
2014-07-04 20:10:09 -07:00
2019-12-18 09:18:55 -07:00
internal static TmdbSeasonProvider Current { get ; private set ; }
2020-08-17 12:10:02 -07:00
public TmdbSeasonProvider ( IHttpClientFactory httpClientFactory , IServerConfigurationManager configurationManager , IFileSystem fileSystem , ILocalizationManager localization , IJsonSerializer jsonSerializer , ILogger < TmdbSeasonProvider > logger )
2014-07-04 20:10:09 -07:00
{
2020-08-17 12:10:02 -07:00
_httpClientFactory = httpClientFactory ;
2014-07-04 20:10:09 -07:00
_configurationManager = configurationManager ;
_fileSystem = fileSystem ;
_localization = localization ;
_jsonSerializer = jsonSerializer ;
2020-06-05 17:15:56 -07:00
_logger = logger ;
2019-12-18 09:18:55 -07:00
Current = this ;
2014-07-04 20:10:09 -07:00
}
public async Task < MetadataResult < Season > > GetMetadata ( SeasonInfo info , CancellationToken cancellationToken )
{
var result = new MetadataResult < Season > ( ) ;
2020-06-06 12:17:49 -07:00
info . SeriesProviderIds . TryGetValue ( MetadataProvider . Tmdb . ToString ( ) , out string seriesTmdbId ) ;
2014-07-04 20:10:09 -07:00
2014-07-08 19:53:04 -07:00
var seasonNumber = info . IndexNumber ;
if ( ! string . IsNullOrWhiteSpace ( seriesTmdbId ) & & seasonNumber . HasValue )
2014-07-04 20:10:09 -07:00
{
2014-07-08 19:53:04 -07:00
try
2014-07-04 20:10:09 -07:00
{
2014-07-08 19:53:04 -07:00
var seasonInfo = await GetSeasonInfo ( seriesTmdbId , seasonNumber . Value , info . MetadataLanguage , cancellationToken )
. ConfigureAwait ( false ) ;
2014-07-04 20:10:09 -07:00
2014-12-20 11:58:55 -07:00
result . HasMetadata = true ;
result . Item = new Season ( ) ;
2017-10-18 18:19:13 -07:00
// Don't use moviedb season names for now until if/when we have field-level configuration
2020-06-14 02:11:11 -07:00
// result.Item.Name = seasonInfo.name;
2017-10-18 18:19:13 -07:00
result . Item . Name = info . Name ;
2014-07-08 19:53:04 -07:00
result . Item . IndexNumber = seasonNumber ;
2019-08-18 05:44:13 -07:00
result . Item . Overview = seasonInfo . Overview ;
2014-12-20 11:58:55 -07:00
2019-08-18 05:44:13 -07:00
if ( seasonInfo . External_Ids . Tvdb_Id > 0 )
2014-07-04 20:10:09 -07:00
{
2020-08-06 14:37:11 -07:00
result . Item . SetProviderId ( MetadataProvider . Tvdb , seasonInfo . External_Ids . Tvdb_Id . Value . ToString ( CultureInfo . InvariantCulture ) ) ;
2014-07-04 20:10:09 -07:00
}
2019-08-18 05:44:13 -07:00
var credits = seasonInfo . Credits ;
2014-07-08 19:53:04 -07:00
if ( credits ! = null )
2014-07-04 20:10:09 -07:00
{
2020-06-14 02:11:11 -07:00
// Actors, Directors, Writers - all in People
// actors come from cast
2019-08-18 05:44:13 -07:00
if ( credits . Cast ! = null )
2014-07-08 19:53:04 -07:00
{
2020-06-14 02:11:11 -07:00
// foreach (var actor in credits.cast.OrderBy(a => a.order)) result.Item.AddPerson(new PersonInfo { Name = actor.name.Trim(), Role = actor.character, Type = PersonType.Actor, SortOrder = actor.order });
2014-07-08 19:53:04 -07:00
}
2020-06-14 02:11:11 -07:00
// and the rest from crew
2019-08-18 05:44:13 -07:00
if ( credits . Crew ! = null )
2014-07-08 19:53:04 -07:00
{
2020-06-14 02:11:11 -07:00
// foreach (var person in credits.crew) result.Item.AddPerson(new PersonInfo { Name = person.name.Trim(), Role = person.job, Type = person.department });
2014-07-08 19:53:04 -07:00
}
2014-07-04 20:10:09 -07:00
}
2014-07-08 19:53:04 -07:00
2019-08-18 05:44:13 -07:00
result . Item . PremiereDate = seasonInfo . Air_Date ;
2014-07-08 19:53:04 -07:00
result . Item . ProductionYear = result . Item . PremiereDate . Value . Year ;
2014-07-04 20:10:09 -07:00
}
2014-07-08 19:53:04 -07:00
catch ( HttpException ex )
{
2018-12-20 05:11:26 -07:00
_logger . LogError ( ex , "No metadata found for {0}" , seasonNumber . Value ) ;
2014-07-08 19:53:04 -07:00
if ( ex . StatusCode . HasValue & & ex . StatusCode . Value = = HttpStatusCode . NotFound )
{
return result ;
}
2014-07-04 20:10:09 -07:00
2014-07-08 19:53:04 -07:00
throw ;
}
2014-07-04 20:10:09 -07:00
}
return result ;
}
2019-08-18 04:20:52 -07:00
public string Name = > TmdbUtils . ProviderName ;
2014-07-04 20:10:09 -07:00
public Task < IEnumerable < RemoteSearchResult > > GetSearchResults ( SeasonInfo searchInfo , CancellationToken cancellationToken )
{
return Task . FromResult < IEnumerable < RemoteSearchResult > > ( new List < RemoteSearchResult > ( ) ) ;
}
2020-08-17 12:10:02 -07:00
public Task < HttpResponseMessage > GetImageResponse ( string url , CancellationToken cancellationToken )
2014-07-04 20:10:09 -07:00
{
2020-08-17 12:10:02 -07:00
return _httpClientFactory . CreateClient ( ) . GetAsync ( url , cancellationToken ) ;
2014-07-04 20:10:09 -07:00
}
2019-08-18 04:20:52 -07:00
private async Task < SeasonResult > GetSeasonInfo ( string seriesTmdbId , int season , string preferredMetadataLanguage ,
2014-07-04 20:10:09 -07:00
CancellationToken cancellationToken )
{
2014-07-07 07:23:08 -07:00
await EnsureSeasonInfo ( seriesTmdbId , season , preferredMetadataLanguage , cancellationToken )
2014-07-04 20:10:09 -07:00
. ConfigureAwait ( false ) ;
var dataFilePath = GetDataFilePath ( seriesTmdbId , season , preferredMetadataLanguage ) ;
2019-08-18 04:20:52 -07:00
return _jsonSerializer . DeserializeFromFile < SeasonResult > ( dataFilePath ) ;
2014-07-04 20:10:09 -07:00
}
2014-07-07 07:23:08 -07:00
internal Task EnsureSeasonInfo ( string tmdbId , int seasonNumber , string language , CancellationToken cancellationToken )
2014-07-04 20:10:09 -07:00
{
if ( string . IsNullOrEmpty ( tmdbId ) )
{
2019-01-06 13:50:43 -07:00
throw new ArgumentNullException ( nameof ( tmdbId ) ) ;
2014-07-04 20:10:09 -07:00
}
2020-05-30 23:28:01 -07:00
2014-07-04 20:10:09 -07:00
if ( string . IsNullOrEmpty ( language ) )
{
2019-01-06 13:50:43 -07:00
throw new ArgumentNullException ( nameof ( language ) ) ;
2014-07-04 20:10:09 -07:00
}
var path = GetDataFilePath ( tmdbId , seasonNumber , 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 )
2014-07-04 20:10:09 -07:00
{
2018-09-12 10:26:21 -07:00
return Task . CompletedTask ;
2014-07-04 20:10:09 -07:00
}
}
return DownloadSeasonInfo ( tmdbId , seasonNumber , language , cancellationToken ) ;
}
internal string GetDataFilePath ( string tmdbId , int seasonNumber , string preferredLanguage )
{
if ( string . IsNullOrEmpty ( tmdbId ) )
{
2019-01-06 13:50:43 -07:00
throw new ArgumentNullException ( nameof ( tmdbId ) ) ;
2014-07-04 20:10:09 -07:00
}
2020-05-30 23:28:01 -07:00
2014-07-04 20:10:09 -07:00
if ( string . IsNullOrEmpty ( preferredLanguage ) )
{
2019-01-06 13:50:43 -07:00
throw new ArgumentNullException ( nameof ( preferredLanguage ) ) ;
2014-07-04 20:10:09 -07:00
}
2019-08-18 04:20:52 -07:00
var path = TmdbSeriesProvider . GetSeriesDataPath ( _configurationManager . ApplicationPaths , tmdbId ) ;
2014-07-04 20:10:09 -07:00
var filename = string . Format ( "season-{0}-{1}.json" ,
seasonNumber . ToString ( CultureInfo . InvariantCulture ) ,
preferredLanguage ) ;
return Path . Combine ( path , filename ) ;
}
internal async Task DownloadSeasonInfo ( string id , int seasonNumber , string preferredMetadataLanguage , CancellationToken cancellationToken )
{
var mainResult = await FetchMainResult ( id , seasonNumber , preferredMetadataLanguage , cancellationToken ) . ConfigureAwait ( false ) ;
var dataFilePath = GetDataFilePath ( id , seasonNumber , preferredMetadataLanguage ) ;
2019-01-26 14:08:04 -07:00
Directory . CreateDirectory ( Path . GetDirectoryName ( dataFilePath ) ) ;
2014-07-04 20:10:09 -07:00
_jsonSerializer . SerializeToFile ( mainResult , dataFilePath ) ;
}
2019-08-18 04:20:52 -07:00
internal async Task < SeasonResult > FetchMainResult ( string id , int seasonNumber , string language , CancellationToken cancellationToken )
2014-07-04 20:10:09 -07:00
{
2019-08-18 04:20:52 -07:00
var url = string . Format ( GetTvInfo3 , id , seasonNumber . ToString ( CultureInfo . InvariantCulture ) , TmdbUtils . ApiKey ) ;
2014-07-04 20:10:09 -07:00
if ( ! string . IsNullOrEmpty ( language ) )
{
2019-08-18 04:20:52 -07:00
url + = string . Format ( "&language={0}" , TmdbMovieProvider . NormalizeLanguage ( language ) ) ;
2014-07-04 20:10:09 -07:00
}
2019-08-18 04:20:52 -07:00
var includeImageLanguageParam = TmdbMovieProvider . GetImageLanguagesParam ( language ) ;
2014-07-04 20:10:09 -07:00
// Get images in english and with no language
2014-10-06 16:58:46 -07:00
url + = "&include_image_language=" + includeImageLanguageParam ;
2014-07-04 20:10:09 -07:00
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-08-17 12:10:02 -07:00
using var response = await TmdbMovieProvider . Current . GetMovieDbResponse ( requestMessage ) . ConfigureAwait ( false ) ;
await using var stream = await response . Content . ReadAsStreamAsync ( ) . ConfigureAwait ( false ) ;
return await _jsonSerializer . DeserializeFromStreamAsync < SeasonResult > ( stream ) . ConfigureAwait ( false ) ;
2014-07-04 20:10:09 -07:00
}
}
}