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;
|
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using MediaBrowser.Common.Configuration;
|
2014-02-09 06:08:10 +00:00
|
|
|
using MediaBrowser.Common.Net;
|
|
|
|
using MediaBrowser.Controller.Configuration;
|
|
|
|
using MediaBrowser.Controller.Entities;
|
|
|
|
using MediaBrowser.Controller.Entities.TV;
|
2014-11-18 02:48:22 +00:00
|
|
|
using MediaBrowser.Controller.Library;
|
2014-02-09 06:08:10 +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-02-09 06:08:10 +00:00
|
|
|
using MediaBrowser.Model.Serialization;
|
|
|
|
using MediaBrowser.Providers.Movies;
|
2019-08-18 11:20:52 +00:00
|
|
|
using MediaBrowser.Providers.Tmdb.Models.Search;
|
|
|
|
using MediaBrowser.Providers.Tmdb.Models.TV;
|
|
|
|
using MediaBrowser.Providers.Tmdb.Movies;
|
2019-01-13 19:26:31 +00:00
|
|
|
using Microsoft.Extensions.Logging;
|
2014-02-09 06:08:10 +00:00
|
|
|
|
2019-08-18 11:20:52 +00:00
|
|
|
namespace MediaBrowser.Providers.Tmdb.TV
|
2014-02-09 06:08:10 +00:00
|
|
|
{
|
2019-08-18 11:20:52 +00:00
|
|
|
public class TmdbSeriesProvider : IRemoteMetadataProvider<Series, SeriesInfo>, IHasOrder
|
2014-02-09 06:08:10 +00:00
|
|
|
{
|
2019-08-18 11:34:44 +00:00
|
|
|
private const string GetTvInfo3 = TmdbUtils.BaseTmdbApiUrl + @"3/tv/{0}?api_key={1}&append_to_response=credits,images,keywords,external_ids,videos,content_ratings";
|
2014-02-09 06:08:10 +00:00
|
|
|
private readonly CultureInfo _usCulture = new CultureInfo("en-US");
|
|
|
|
|
2019-08-18 11:20:52 +00:00
|
|
|
internal static TmdbSeriesProvider Current { get; private set; }
|
2014-02-09 06:08:10 +00:00
|
|
|
|
|
|
|
private readonly IJsonSerializer _jsonSerializer;
|
|
|
|
private readonly IFileSystem _fileSystem;
|
|
|
|
private readonly IServerConfigurationManager _configurationManager;
|
|
|
|
private readonly ILogger _logger;
|
2014-02-17 21:35:08 +00:00
|
|
|
private readonly ILocalizationManager _localization;
|
2014-03-01 22:34:27 +00:00
|
|
|
private readonly IHttpClient _httpClient;
|
2014-11-18 02:48:22 +00:00
|
|
|
private readonly ILibraryManager _libraryManager;
|
2014-02-09 06:08:10 +00:00
|
|
|
|
2019-08-18 11:20:52 +00:00
|
|
|
public TmdbSeriesProvider(IJsonSerializer jsonSerializer, IFileSystem fileSystem, IServerConfigurationManager configurationManager, ILogger logger, ILocalizationManager localization, IHttpClient httpClient, ILibraryManager libraryManager)
|
2014-02-09 06:08:10 +00:00
|
|
|
{
|
|
|
|
_jsonSerializer = jsonSerializer;
|
|
|
|
_fileSystem = fileSystem;
|
|
|
|
_configurationManager = configurationManager;
|
|
|
|
_logger = logger;
|
2014-02-17 21:35:08 +00:00
|
|
|
_localization = localization;
|
2014-03-01 22:34:27 +00:00
|
|
|
_httpClient = httpClient;
|
2015-02-20 18:06:08 +00:00
|
|
|
_libraryManager = libraryManager;
|
2014-02-09 06:08:10 +00:00
|
|
|
Current = this;
|
|
|
|
}
|
|
|
|
|
2019-08-18 11:20:52 +00:00
|
|
|
public string Name => TmdbUtils.ProviderName;
|
2014-02-09 06:08:10 +00:00
|
|
|
|
2014-02-20 04:53:15 +00:00
|
|
|
public async Task<IEnumerable<RemoteSearchResult>> GetSearchResults(SeriesInfo searchInfo, CancellationToken cancellationToken)
|
|
|
|
{
|
2014-03-02 15:42:21 +00:00
|
|
|
var tmdbId = searchInfo.GetProviderId(MetadataProviders.Tmdb);
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(tmdbId))
|
|
|
|
{
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
|
|
|
|
|
|
await EnsureSeriesInfo(tmdbId, searchInfo.MetadataLanguage, cancellationToken).ConfigureAwait(false);
|
2015-01-31 03:19:41 +00:00
|
|
|
|
2014-03-02 15:42:21 +00:00
|
|
|
var dataFilePath = GetDataFilePath(tmdbId, searchInfo.MetadataLanguage);
|
|
|
|
|
2019-08-18 11:20:52 +00:00
|
|
|
var obj = _jsonSerializer.DeserializeFromFile<SeriesResult>(dataFilePath);
|
2014-03-02 15:42:21 +00:00
|
|
|
|
2019-08-18 11:20:52 +00:00
|
|
|
var tmdbSettings = await TmdbMovieProvider.Current.GetTmdbSettings(cancellationToken).ConfigureAwait(false);
|
2018-09-12 17:26:21 +00:00
|
|
|
var tmdbImageUrl = tmdbSettings.images.GetImageUrl("original");
|
2014-03-02 18:01:46 +00:00
|
|
|
|
2014-03-02 15:42:21 +00:00
|
|
|
var remoteResult = new RemoteSearchResult
|
|
|
|
{
|
|
|
|
Name = obj.name,
|
|
|
|
SearchProviderName = Name,
|
|
|
|
ImageUrl = string.IsNullOrWhiteSpace(obj.poster_path) ? null : tmdbImageUrl + obj.poster_path
|
|
|
|
};
|
|
|
|
|
|
|
|
remoteResult.SetProviderId(MetadataProviders.Tmdb, obj.id.ToString(_usCulture));
|
|
|
|
remoteResult.SetProviderId(MetadataProviders.Imdb, obj.external_ids.imdb_id);
|
|
|
|
|
|
|
|
if (obj.external_ids.tvdb_id > 0)
|
|
|
|
{
|
|
|
|
remoteResult.SetProviderId(MetadataProviders.Tvdb, obj.external_ids.tvdb_id.ToString(_usCulture));
|
|
|
|
}
|
2015-01-31 03:19:41 +00:00
|
|
|
|
2014-03-02 15:42:21 +00:00
|
|
|
return new[] { remoteResult };
|
|
|
|
}
|
|
|
|
|
|
|
|
var imdbId = searchInfo.GetProviderId(MetadataProviders.Imdb);
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(imdbId))
|
|
|
|
{
|
|
|
|
var searchResult = await FindByExternalId(imdbId, "imdb_id", cancellationToken).ConfigureAwait(false);
|
|
|
|
|
|
|
|
if (searchResult != null)
|
|
|
|
{
|
2014-03-02 18:01:46 +00:00
|
|
|
return new[] { searchResult };
|
2014-03-02 15:42:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var tvdbId = searchInfo.GetProviderId(MetadataProviders.Tvdb);
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(tvdbId))
|
|
|
|
{
|
|
|
|
var searchResult = await FindByExternalId(tvdbId, "tvdb_id", cancellationToken).ConfigureAwait(false);
|
|
|
|
|
|
|
|
if (searchResult != null)
|
|
|
|
{
|
2014-03-02 18:01:46 +00:00
|
|
|
return new[] { searchResult };
|
2014-03-02 15:42:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-18 11:20:52 +00:00
|
|
|
return await new TmdbSearch(_logger, _jsonSerializer, _libraryManager).GetSearchResults(searchInfo, cancellationToken).ConfigureAwait(false);
|
2014-02-20 04:53:15 +00:00
|
|
|
}
|
|
|
|
|
2014-02-09 06:08:10 +00:00
|
|
|
public async Task<MetadataResult<Series>> GetMetadata(SeriesInfo info, CancellationToken cancellationToken)
|
|
|
|
{
|
|
|
|
var result = new MetadataResult<Series>();
|
2016-09-18 04:27:17 +00:00
|
|
|
result.QueriedById = true;
|
2014-02-09 06:08:10 +00:00
|
|
|
|
|
|
|
var tmdbId = info.GetProviderId(MetadataProviders.Tmdb);
|
|
|
|
|
2014-02-13 05:11:54 +00:00
|
|
|
if (string.IsNullOrEmpty(tmdbId))
|
|
|
|
{
|
|
|
|
var imdbId = info.GetProviderId(MetadataProviders.Imdb);
|
2014-02-09 06:08:10 +00:00
|
|
|
|
2014-02-13 05:11:54 +00:00
|
|
|
if (!string.IsNullOrEmpty(imdbId))
|
|
|
|
{
|
2014-03-02 15:42:21 +00:00
|
|
|
var searchResult = await FindByExternalId(imdbId, "imdb_id", cancellationToken).ConfigureAwait(false);
|
|
|
|
|
|
|
|
if (searchResult != null)
|
|
|
|
{
|
2014-03-02 18:01:46 +00:00
|
|
|
tmdbId = searchResult.GetProviderId(MetadataProviders.Tmdb);
|
2014-03-02 15:42:21 +00:00
|
|
|
}
|
2014-02-13 05:11:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(tmdbId))
|
|
|
|
{
|
|
|
|
var tvdbId = info.GetProviderId(MetadataProviders.Tvdb);
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(tvdbId))
|
|
|
|
{
|
2014-03-02 15:42:21 +00:00
|
|
|
var searchResult = await FindByExternalId(tvdbId, "tvdb_id", cancellationToken).ConfigureAwait(false);
|
|
|
|
|
|
|
|
if (searchResult != null)
|
|
|
|
{
|
2014-03-02 18:01:46 +00:00
|
|
|
tmdbId = searchResult.GetProviderId(MetadataProviders.Tmdb);
|
2014-03-02 15:42:21 +00:00
|
|
|
}
|
2014-02-13 05:11:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(tmdbId))
|
2014-02-09 06:08:10 +00:00
|
|
|
{
|
2016-09-18 04:27:17 +00:00
|
|
|
result.QueriedById = false;
|
2019-08-18 11:20:52 +00:00
|
|
|
var searchResults = await new TmdbSearch(_logger, _jsonSerializer, _libraryManager).GetSearchResults(info, 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)
|
|
|
|
{
|
2014-03-02 17:09:35 +00:00
|
|
|
tmdbId = searchResult.GetProviderId(MetadataProviders.Tmdb);
|
2014-02-15 16:36:09 +00:00
|
|
|
}
|
2014-02-09 06:08:10 +00:00
|
|
|
}
|
|
|
|
|
2014-02-13 05:11:54 +00:00
|
|
|
if (!string.IsNullOrEmpty(tmdbId))
|
2014-02-09 06:08:10 +00:00
|
|
|
{
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
|
|
|
2016-05-14 01:55:46 +00:00
|
|
|
result = await FetchMovieData(tmdbId, info.MetadataLanguage, info.MetadataCountryCode, cancellationToken).ConfigureAwait(false);
|
2014-02-09 06:08:10 +00:00
|
|
|
|
|
|
|
result.HasMetadata = result.Item != null;
|
|
|
|
}
|
2014-03-02 15:42:21 +00:00
|
|
|
|
2014-02-09 06:08:10 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-05-14 01:55:46 +00:00
|
|
|
private async Task<MetadataResult<Series>> FetchMovieData(string tmdbId, string language, string preferredCountryCode, CancellationToken cancellationToken)
|
2014-02-09 06:08:10 +00:00
|
|
|
{
|
2019-08-18 11:20:52 +00:00
|
|
|
SeriesResult seriesInfo = await FetchMainResult(tmdbId, language, cancellationToken).ConfigureAwait(false);
|
2014-02-09 06:08:10 +00:00
|
|
|
|
2014-02-13 05:11:54 +00:00
|
|
|
if (seriesInfo == null)
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
2014-02-09 06:08:10 +00:00
|
|
|
|
2014-02-13 05:11:54 +00:00
|
|
|
tmdbId = seriesInfo.id.ToString(_usCulture);
|
2014-02-09 06:08:10 +00:00
|
|
|
|
2019-01-18 19:40:08 +00:00
|
|
|
string dataFilePath = GetDataFilePath(tmdbId, language);
|
2019-01-26 21:08:04 +00:00
|
|
|
Directory.CreateDirectory(Path.GetDirectoryName(dataFilePath));
|
2014-02-13 05:11:54 +00:00
|
|
|
_jsonSerializer.SerializeToFile(seriesInfo, dataFilePath);
|
2014-02-09 06:08:10 +00:00
|
|
|
|
|
|
|
await EnsureSeriesInfo(tmdbId, language, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
2016-05-14 01:55:46 +00:00
|
|
|
var result = new MetadataResult<Series>();
|
|
|
|
result.Item = new Series();
|
|
|
|
result.ResultLanguage = seriesInfo.ResultLanguage;
|
2014-02-09 06:08:10 +00:00
|
|
|
|
2019-08-18 11:20:52 +00:00
|
|
|
var settings = await TmdbMovieProvider.Current.GetTmdbSettings(cancellationToken).ConfigureAwait(false);
|
2018-09-12 17:26:21 +00:00
|
|
|
|
|
|
|
ProcessMainInfo(result, seriesInfo, preferredCountryCode, settings);
|
2014-02-09 06:08:10 +00:00
|
|
|
|
2016-05-14 01:55:46 +00:00
|
|
|
return result;
|
2014-02-09 06:08:10 +00:00
|
|
|
}
|
|
|
|
|
2019-08-18 11:20:52 +00:00
|
|
|
private void ProcessMainInfo(MetadataResult<Series> seriesResult, SeriesResult seriesInfo, string preferredCountryCode, TmdbSettingsResult settings)
|
2014-02-09 06:08:10 +00:00
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
var series = seriesResult.Item;
|
|
|
|
|
2014-02-09 06:08:10 +00:00
|
|
|
series.Name = seriesInfo.name;
|
|
|
|
series.SetProviderId(MetadataProviders.Tmdb, seriesInfo.id.ToString(_usCulture));
|
|
|
|
|
2017-06-03 07:36:32 +00:00
|
|
|
//series.VoteCount = seriesInfo.vote_count;
|
2014-02-09 06:08:10 +00:00
|
|
|
|
|
|
|
string voteAvg = seriesInfo.vote_average.ToString(CultureInfo.InvariantCulture);
|
|
|
|
|
2019-01-18 19:40:08 +00:00
|
|
|
if (float.TryParse(voteAvg, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out float rating))
|
2014-02-09 06:08:10 +00:00
|
|
|
{
|
|
|
|
series.CommunityRating = rating;
|
|
|
|
}
|
|
|
|
|
2014-03-02 15:42:21 +00:00
|
|
|
series.Overview = seriesInfo.overview;
|
2014-02-09 06:08:10 +00:00
|
|
|
|
|
|
|
if (seriesInfo.networks != null)
|
|
|
|
{
|
2018-12-28 15:48:26 +00:00
|
|
|
series.Studios = seriesInfo.networks.Select(i => i.name).ToArray();
|
2014-02-09 06:08:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (seriesInfo.genres != null)
|
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
series.Genres = seriesInfo.genres.Select(i => i.name).ToArray();
|
2014-02-09 06:08:10 +00:00
|
|
|
}
|
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
//series.HomePageUrl = seriesInfo.homepage;
|
2015-01-31 03:19:41 +00:00
|
|
|
|
2014-02-09 06:08:10 +00:00
|
|
|
series.RunTimeTicks = seriesInfo.episode_run_time.Select(i => TimeSpan.FromMinutes(i).Ticks).FirstOrDefault();
|
|
|
|
|
|
|
|
if (string.Equals(seriesInfo.status, "Ended", StringComparison.OrdinalIgnoreCase))
|
|
|
|
{
|
|
|
|
series.Status = SeriesStatus.Ended;
|
2014-02-17 21:35:08 +00:00
|
|
|
series.EndDate = seriesInfo.last_air_date;
|
2014-02-09 06:08:10 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
series.Status = SeriesStatus.Continuing;
|
|
|
|
}
|
|
|
|
|
|
|
|
series.PremiereDate = seriesInfo.first_air_date;
|
|
|
|
|
|
|
|
var ids = seriesInfo.external_ids;
|
|
|
|
if (ids != null)
|
|
|
|
{
|
|
|
|
if (!string.IsNullOrWhiteSpace(ids.imdb_id))
|
|
|
|
{
|
|
|
|
series.SetProviderId(MetadataProviders.Imdb, ids.imdb_id);
|
|
|
|
}
|
|
|
|
if (ids.tvrage_id > 0)
|
|
|
|
{
|
2014-02-09 07:27:44 +00:00
|
|
|
series.SetProviderId(MetadataProviders.TvRage, ids.tvrage_id.ToString(_usCulture));
|
2014-02-09 06:08:10 +00:00
|
|
|
}
|
|
|
|
if (ids.tvdb_id > 0)
|
|
|
|
{
|
|
|
|
series.SetProviderId(MetadataProviders.Tvdb, ids.tvdb_id.ToString(_usCulture));
|
|
|
|
}
|
|
|
|
}
|
2016-04-19 03:14:45 +00:00
|
|
|
|
|
|
|
var contentRatings = (seriesInfo.content_ratings ?? new ContentRatings()).results ?? new List<ContentRating>();
|
|
|
|
|
|
|
|
var ourRelease = contentRatings.FirstOrDefault(c => string.Equals(c.iso_3166_1, preferredCountryCode, StringComparison.OrdinalIgnoreCase));
|
|
|
|
var usRelease = contentRatings.FirstOrDefault(c => string.Equals(c.iso_3166_1, "US", StringComparison.OrdinalIgnoreCase));
|
|
|
|
var minimumRelease = contentRatings.FirstOrDefault();
|
|
|
|
|
|
|
|
if (ourRelease != null)
|
|
|
|
{
|
|
|
|
series.OfficialRating = ourRelease.rating;
|
|
|
|
}
|
|
|
|
else if (usRelease != null)
|
|
|
|
{
|
|
|
|
series.OfficialRating = usRelease.rating;
|
|
|
|
}
|
|
|
|
else if (minimumRelease != null)
|
|
|
|
{
|
|
|
|
series.OfficialRating = minimumRelease.rating;
|
|
|
|
}
|
|
|
|
|
2016-06-04 04:30:06 +00:00
|
|
|
if (seriesInfo.videos != null && seriesInfo.videos.results != null)
|
|
|
|
{
|
|
|
|
foreach (var video in seriesInfo.videos.results)
|
|
|
|
{
|
2019-01-18 19:40:08 +00:00
|
|
|
if ((video.type.Equals("trailer", StringComparison.OrdinalIgnoreCase)
|
|
|
|
|| video.type.Equals("clip", StringComparison.OrdinalIgnoreCase))
|
|
|
|
&& video.site.Equals("youtube", StringComparison.OrdinalIgnoreCase))
|
2016-06-04 04:30:06 +00:00
|
|
|
{
|
2019-01-18 19:40:08 +00:00
|
|
|
series.AddTrailerUrl($"http://www.youtube.com/watch?v={video.key}");
|
2016-06-04 04:30:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-09-12 17:26:21 +00:00
|
|
|
|
|
|
|
seriesResult.ResetPeople();
|
|
|
|
var tmdbImageUrl = settings.images.GetImageUrl("original");
|
|
|
|
|
|
|
|
if (seriesInfo.credits != null && seriesInfo.credits.cast != null)
|
|
|
|
{
|
|
|
|
foreach (var actor in seriesInfo.credits.cast.OrderBy(a => a.order))
|
|
|
|
{
|
|
|
|
var personInfo = new PersonInfo
|
|
|
|
{
|
|
|
|
Name = actor.name.Trim(),
|
|
|
|
Role = actor.character,
|
|
|
|
Type = PersonType.Actor,
|
|
|
|
SortOrder = actor.order
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(actor.profile_path))
|
|
|
|
{
|
|
|
|
personInfo.ImageUrl = tmdbImageUrl + actor.profile_path;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (actor.id > 0)
|
|
|
|
{
|
|
|
|
personInfo.SetProviderId(MetadataProviders.Tmdb, actor.id.ToString(CultureInfo.InvariantCulture));
|
|
|
|
}
|
|
|
|
|
|
|
|
seriesResult.AddPerson(personInfo);
|
|
|
|
}
|
|
|
|
}
|
2014-02-09 06:08:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
internal static string GetSeriesDataPath(IApplicationPaths appPaths, string tmdbId)
|
|
|
|
{
|
|
|
|
var dataPath = GetSeriesDataPath(appPaths);
|
|
|
|
|
|
|
|
return Path.Combine(dataPath, tmdbId);
|
|
|
|
}
|
|
|
|
|
|
|
|
internal static string GetSeriesDataPath(IApplicationPaths appPaths)
|
|
|
|
{
|
2014-02-20 16:37:41 +00:00
|
|
|
var dataPath = Path.Combine(appPaths.CachePath, "tmdb-tv");
|
2014-02-09 06:08:10 +00:00
|
|
|
|
|
|
|
return dataPath;
|
|
|
|
}
|
2014-03-02 15:42:21 +00:00
|
|
|
|
2014-02-09 06:08:10 +00:00
|
|
|
internal async Task DownloadSeriesInfo(string id, string preferredMetadataLanguage, CancellationToken cancellationToken)
|
|
|
|
{
|
2019-08-18 11:20:52 +00:00
|
|
|
SeriesResult mainResult = await FetchMainResult(id, preferredMetadataLanguage, cancellationToken).ConfigureAwait(false);
|
2014-02-09 06:08:10 +00:00
|
|
|
|
2019-01-18 19:40:08 +00:00
|
|
|
if (mainResult == null)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2014-02-09 06:08:10 +00:00
|
|
|
|
|
|
|
var dataFilePath = GetDataFilePath(id, preferredMetadataLanguage);
|
|
|
|
|
2019-01-26 21:08:04 +00:00
|
|
|
Directory.CreateDirectory(Path.GetDirectoryName(dataFilePath));
|
2014-02-09 06:08:10 +00:00
|
|
|
|
|
|
|
_jsonSerializer.SerializeToFile(mainResult, dataFilePath);
|
|
|
|
}
|
|
|
|
|
2019-08-18 11:20:52 +00:00
|
|
|
internal async Task<SeriesResult> FetchMainResult(string id, string language, CancellationToken cancellationToken)
|
2014-02-09 06:08:10 +00:00
|
|
|
{
|
2019-08-18 11:20:52 +00:00
|
|
|
var url = string.Format(GetTvInfo3, id, TmdbUtils.ApiKey);
|
2014-02-09 06:08:10 +00:00
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(language))
|
|
|
|
{
|
2019-08-18 11:20:52 +00:00
|
|
|
url += "&language=" + TmdbMovieProvider.NormalizeLanguage(language)
|
|
|
|
+ "&include_image_language=" + TmdbMovieProvider.GetImageLanguagesParam(language); // Get images in english and with no language
|
2016-03-04 17:38:51 +00:00
|
|
|
}
|
2014-02-09 06:08:10 +00:00
|
|
|
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
|
|
|
2019-08-18 11:20:52 +00:00
|
|
|
SeriesResult mainResult;
|
2015-01-31 03:19:41 +00:00
|
|
|
|
2019-08-18 11:20:52 +00:00
|
|
|
using (var response = await TmdbMovieProvider.Current.GetMovieDbResponse(new HttpRequestOptions
|
2014-02-09 06:08:10 +00:00
|
|
|
{
|
|
|
|
Url = url,
|
|
|
|
CancellationToken = cancellationToken,
|
2019-08-18 11:20:52 +00:00
|
|
|
AcceptHeader = TmdbUtils.AcceptHeader
|
2014-02-09 06:08:10 +00:00
|
|
|
|
|
|
|
}).ConfigureAwait(false))
|
|
|
|
{
|
2017-10-20 16:16:56 +00:00
|
|
|
using (var json = response.Content)
|
2016-05-14 01:55:46 +00:00
|
|
|
{
|
2019-08-18 11:20:52 +00:00
|
|
|
mainResult = await _jsonSerializer.DeserializeFromStreamAsync<SeriesResult>(json).ConfigureAwait(false);
|
2017-10-20 16:16:56 +00:00
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(language))
|
|
|
|
{
|
|
|
|
mainResult.ResultLanguage = language;
|
|
|
|
}
|
2016-05-14 01:55:46 +00:00
|
|
|
}
|
2014-02-09 06:08:10 +00:00
|
|
|
}
|
2015-01-31 03:19:41 +00:00
|
|
|
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
|
|
|
|
|
|
// If the language preference isn't english, then have the overview fallback to english if it's blank
|
|
|
|
if (mainResult != null &&
|
|
|
|
string.IsNullOrEmpty(mainResult.overview) &&
|
|
|
|
!string.IsNullOrEmpty(language) &&
|
|
|
|
!string.Equals(language, "en", StringComparison.OrdinalIgnoreCase))
|
|
|
|
{
|
2019-01-18 19:40:08 +00:00
|
|
|
_logger.LogInformation("MovieDbSeriesProvider couldn't find meta for language {Language}. Trying English...", language);
|
2015-01-31 03:19:41 +00:00
|
|
|
|
2019-08-18 11:20:52 +00:00
|
|
|
url = string.Format(GetTvInfo3, 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
|
|
|
}
|
2015-01-31 03:19:41 +00:00
|
|
|
|
2019-08-18 11:20:52 +00:00
|
|
|
using (var response = await TmdbMovieProvider.Current.GetMovieDbResponse(new HttpRequestOptions
|
2015-01-31 03:19:41 +00:00
|
|
|
{
|
|
|
|
Url = url,
|
|
|
|
CancellationToken = cancellationToken,
|
2019-08-18 11:20:52 +00:00
|
|
|
AcceptHeader = TmdbUtils.AcceptHeader
|
2015-01-31 03:19:41 +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
|
|
|
var englishResult = await _jsonSerializer.DeserializeFromStreamAsync<SeriesResult>(json).ConfigureAwait(false);
|
2015-01-31 03:19:41 +00:00
|
|
|
|
2017-10-20 16:16:56 +00:00
|
|
|
mainResult.overview = englishResult.overview;
|
|
|
|
mainResult.ResultLanguage = "en";
|
|
|
|
}
|
2015-01-31 03:19:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return mainResult;
|
2014-02-09 06:08:10 +00:00
|
|
|
}
|
2014-03-02 15:42:21 +00:00
|
|
|
|
2014-02-09 06:08:10 +00:00
|
|
|
internal Task EnsureSeriesInfo(string tmdbId, string language, CancellationToken cancellationToken)
|
|
|
|
{
|
|
|
|
if (string.IsNullOrEmpty(tmdbId))
|
|
|
|
{
|
2019-01-06 20:50:43 +00:00
|
|
|
throw new ArgumentNullException(nameof(tmdbId));
|
2014-02-09 06:08:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var path = GetDataFilePath(tmdbId, 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)
|
2014-02-09 06:08:10 +00:00
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
return Task.CompletedTask;
|
2014-02-09 06:08:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return DownloadSeriesInfo(tmdbId, language, cancellationToken);
|
|
|
|
}
|
|
|
|
|
|
|
|
internal string GetDataFilePath(string tmdbId, string preferredLanguage)
|
|
|
|
{
|
|
|
|
if (string.IsNullOrEmpty(tmdbId))
|
|
|
|
{
|
2019-01-06 20:50:43 +00:00
|
|
|
throw new ArgumentNullException(nameof(tmdbId));
|
2014-02-09 06:08:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var path = GetSeriesDataPath(_configurationManager.ApplicationPaths, tmdbId);
|
|
|
|
|
2016-03-04 17:38:51 +00:00
|
|
|
var filename = string.Format("series-{0}.json", preferredLanguage ?? string.Empty);
|
2014-02-09 06:08:10 +00:00
|
|
|
|
|
|
|
return Path.Combine(path, filename);
|
|
|
|
}
|
|
|
|
|
2014-03-02 18:01:46 +00:00
|
|
|
private async Task<RemoteSearchResult> FindByExternalId(string id, string externalSource, CancellationToken cancellationToken)
|
2014-02-13 05:11:54 +00:00
|
|
|
{
|
2019-08-18 11:34:44 +00:00
|
|
|
var url = string.Format(TmdbUtils.BaseTmdbApiUrl + @"3/find/{0}?api_key={1}&external_source={2}",
|
2014-03-02 15:42:21 +00:00
|
|
|
id,
|
2019-08-18 11:20:52 +00:00
|
|
|
TmdbUtils.ApiKey,
|
2014-02-13 05:11:54 +00:00
|
|
|
externalSource);
|
|
|
|
|
2019-08-18 11:20:52 +00:00
|
|
|
using (var response = await TmdbMovieProvider.Current.GetMovieDbResponse(new HttpRequestOptions
|
2014-02-13 05:11:54 +00:00
|
|
|
{
|
|
|
|
Url = url,
|
|
|
|
CancellationToken = cancellationToken,
|
2019-08-18 11:20:52 +00:00
|
|
|
AcceptHeader = TmdbUtils.AcceptHeader
|
2014-02-13 05:11:54 +00:00
|
|
|
|
|
|
|
}).ConfigureAwait(false))
|
|
|
|
{
|
2017-10-20 16:16:56 +00:00
|
|
|
using (var json = response.Content)
|
2014-02-13 05:11:54 +00:00
|
|
|
{
|
2019-08-18 11:20:52 +00:00
|
|
|
var result = await _jsonSerializer.DeserializeFromStreamAsync<ExternalIdLookupResult>(json).ConfigureAwait(false);
|
2014-02-13 05:11:54 +00:00
|
|
|
|
2017-10-20 16:16:56 +00:00
|
|
|
if (result != null && result.tv_results != null)
|
2014-02-13 05:11:54 +00:00
|
|
|
{
|
2017-10-20 16:16:56 +00:00
|
|
|
var tv = result.tv_results.FirstOrDefault();
|
2014-03-02 18:01:46 +00:00
|
|
|
|
2017-10-20 16:16:56 +00:00
|
|
|
if (tv != null)
|
2014-03-02 18:01:46 +00:00
|
|
|
{
|
2019-08-18 11:20:52 +00:00
|
|
|
var tmdbSettings = await TmdbMovieProvider.Current.GetTmdbSettings(cancellationToken).ConfigureAwait(false);
|
2018-09-12 17:26:21 +00:00
|
|
|
var tmdbImageUrl = tmdbSettings.images.GetImageUrl("original");
|
2014-03-02 18:01:46 +00:00
|
|
|
|
2017-10-20 16:16:56 +00:00
|
|
|
var remoteResult = new RemoteSearchResult
|
|
|
|
{
|
|
|
|
Name = tv.name,
|
|
|
|
SearchProviderName = Name,
|
|
|
|
ImageUrl = string.IsNullOrWhiteSpace(tv.poster_path) ? null : tmdbImageUrl + tv.poster_path
|
|
|
|
};
|
2014-03-02 18:01:46 +00:00
|
|
|
|
2017-10-20 16:16:56 +00:00
|
|
|
remoteResult.SetProviderId(MetadataProviders.Tmdb, tv.id.ToString(_usCulture));
|
|
|
|
|
|
|
|
return remoteResult;
|
|
|
|
}
|
2014-02-13 05:11:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-01-17 19:24:39 +00:00
|
|
|
// After TheTVDB
|
2019-01-13 20:31:14 +00:00
|
|
|
public int Order => 1;
|
2014-02-20 04:53:15 +00:00
|
|
|
|
|
|
|
public Task<HttpResponseInfo> GetImageResponse(string url, CancellationToken cancellationToken)
|
|
|
|
{
|
2014-03-01 22:34:27 +00:00
|
|
|
return _httpClient.GetResponse(new HttpRequestOptions
|
|
|
|
{
|
|
|
|
CancellationToken = cancellationToken,
|
2016-10-31 18:39:41 +00:00
|
|
|
Url = url
|
2014-03-01 22:34:27 +00:00
|
|
|
});
|
2014-02-20 04:53:15 +00:00
|
|
|
}
|
2014-02-09 06:08:10 +00:00
|
|
|
}
|
|
|
|
}
|