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

558 lines
22 KiB
C#
Raw Normal View History

#pragma warning disable CS1591
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
2020-08-17 12:10:02 -07:00
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading;
using System.Threading.Tasks;
using MediaBrowser.Common.Configuration;
2014-02-08 23:08:10 -07:00
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.TV;
2014-11-17 19:48:22 -07:00
using MediaBrowser.Controller.Library;
2014-02-08 23:08:10 -07:00
using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Globalization;
using MediaBrowser.Model.IO;
using MediaBrowser.Model.Providers;
2014-02-08 23:08:10 -07:00
using MediaBrowser.Model.Serialization;
2020-05-30 23:23:09 -07:00
using MediaBrowser.Providers.Plugins.Tmdb.Models.Search;
using MediaBrowser.Providers.Plugins.Tmdb.Models.TV;
using MediaBrowser.Providers.Plugins.Tmdb.Movies;
using Microsoft.Extensions.Logging;
2014-02-08 23:08:10 -07:00
2020-05-30 23:23:09 -07:00
namespace MediaBrowser.Providers.Plugins.Tmdb.TV
2014-02-08 23:08:10 -07:00
{
public class TmdbSeriesProvider : IRemoteMetadataProvider<Series, SeriesInfo>, IHasOrder
2014-02-08 23:08:10 -07:00
{
2020-05-12 06:05:58 -07: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-08 23:08:10 -07:00
private readonly IJsonSerializer _jsonSerializer;
private readonly IFileSystem _fileSystem;
private readonly IServerConfigurationManager _configurationManager;
2020-06-05 17:15:56 -07:00
private readonly ILogger<TmdbSeriesProvider> _logger;
private readonly ILocalizationManager _localization;
2020-08-17 12:10:02 -07:00
private readonly IHttpClientFactory _httpClientFactory;
2014-11-17 19:48:22 -07:00
private readonly ILibraryManager _libraryManager;
2014-02-08 23:08:10 -07:00
private readonly CultureInfo _usCulture = new CultureInfo("en-US");
internal static TmdbSeriesProvider Current { get; private set; }
public TmdbSeriesProvider(
IJsonSerializer jsonSerializer,
IFileSystem fileSystem,
IServerConfigurationManager configurationManager,
ILogger<TmdbSeriesProvider> logger,
ILocalizationManager localization,
2020-08-17 12:10:02 -07:00
IHttpClientFactory httpClientFactory,
ILibraryManager libraryManager)
2014-02-08 23:08:10 -07:00
{
_jsonSerializer = jsonSerializer;
_fileSystem = fileSystem;
_configurationManager = configurationManager;
_logger = logger;
_localization = localization;
2020-08-17 12:10:02 -07:00
_httpClientFactory = httpClientFactory;
2015-02-20 11:06:08 -07:00
_libraryManager = libraryManager;
2014-02-08 23:08:10 -07:00
Current = this;
}
public string Name => TmdbUtils.ProviderName;
2014-02-08 23:08:10 -07:00
public async Task<IEnumerable<RemoteSearchResult>> GetSearchResults(SeriesInfo searchInfo, CancellationToken cancellationToken)
{
2020-06-06 12:17:49 -07:00
var tmdbId = searchInfo.GetProviderId(MetadataProvider.Tmdb);
2014-03-02 08:42:21 -07:00
if (!string.IsNullOrEmpty(tmdbId))
{
cancellationToken.ThrowIfCancellationRequested();
await EnsureSeriesInfo(tmdbId, searchInfo.MetadataLanguage, cancellationToken).ConfigureAwait(false);
2014-03-02 08:42:21 -07:00
var dataFilePath = GetDataFilePath(tmdbId, searchInfo.MetadataLanguage);
var obj = _jsonSerializer.DeserializeFromFile<SeriesResult>(dataFilePath);
2014-03-02 08:42:21 -07:00
var tmdbSettings = await TmdbMovieProvider.Current.GetTmdbSettings(cancellationToken).ConfigureAwait(false);
2018-09-12 10:26:21 -07:00
var tmdbImageUrl = tmdbSettings.images.GetImageUrl("original");
2014-03-02 11:01:46 -07:00
2014-03-02 08:42:21 -07:00
var remoteResult = new RemoteSearchResult
{
2019-08-18 05:44:13 -07:00
Name = obj.Name,
2014-03-02 08:42:21 -07:00
SearchProviderName = Name,
2019-08-18 05:44:13 -07:00
ImageUrl = string.IsNullOrWhiteSpace(obj.Poster_Path) ? null : tmdbImageUrl + obj.Poster_Path
2014-03-02 08:42:21 -07:00
};
2020-06-06 12:17:49 -07:00
remoteResult.SetProviderId(MetadataProvider.Tmdb, obj.Id.ToString(_usCulture));
remoteResult.SetProviderId(MetadataProvider.Imdb, obj.External_Ids.Imdb_Id);
2014-03-02 08:42:21 -07:00
2019-08-18 05:44:13 -07:00
if (obj.External_Ids.Tvdb_Id > 0)
2014-03-02 08:42:21 -07:00
{
2020-08-06 14:37:11 -07:00
remoteResult.SetProviderId(MetadataProvider.Tvdb, obj.External_Ids.Tvdb_Id.Value.ToString(_usCulture));
2014-03-02 08:42:21 -07:00
}
2014-03-02 08:42:21 -07:00
return new[] { remoteResult };
}
2020-06-06 12:17:49 -07:00
var imdbId = searchInfo.GetProviderId(MetadataProvider.Imdb);
2014-03-02 08:42:21 -07:00
if (!string.IsNullOrEmpty(imdbId))
{
var searchResult = await FindByExternalId(imdbId, "imdb_id", cancellationToken).ConfigureAwait(false);
if (searchResult != null)
{
2014-03-02 11:01:46 -07:00
return new[] { searchResult };
2014-03-02 08:42:21 -07:00
}
}
2020-06-06 12:17:49 -07:00
var tvdbId = searchInfo.GetProviderId(MetadataProvider.Tvdb);
2014-03-02 08:42:21 -07:00
if (!string.IsNullOrEmpty(tvdbId))
{
var searchResult = await FindByExternalId(tvdbId, "tvdb_id", cancellationToken).ConfigureAwait(false);
if (searchResult != null)
{
2014-03-02 11:01:46 -07:00
return new[] { searchResult };
2014-03-02 08:42:21 -07:00
}
}
return await new TmdbSearch(_logger, _jsonSerializer, _libraryManager).GetSearchResults(searchInfo, cancellationToken).ConfigureAwait(false);
}
2014-02-08 23:08:10 -07:00
public async Task<MetadataResult<Series>> GetMetadata(SeriesInfo info, CancellationToken cancellationToken)
{
var result = new MetadataResult<Series>();
result.QueriedById = true;
2014-02-08 23:08:10 -07:00
2020-06-06 12:17:49 -07:00
var tmdbId = info.GetProviderId(MetadataProvider.Tmdb);
2014-02-08 23:08:10 -07:00
2014-02-12 22:11:54 -07:00
if (string.IsNullOrEmpty(tmdbId))
{
2020-06-06 12:17:49 -07:00
var imdbId = info.GetProviderId(MetadataProvider.Imdb);
2014-02-08 23:08:10 -07:00
2014-02-12 22:11:54 -07:00
if (!string.IsNullOrEmpty(imdbId))
{
2014-03-02 08:42:21 -07:00
var searchResult = await FindByExternalId(imdbId, "imdb_id", cancellationToken).ConfigureAwait(false);
if (searchResult != null)
{
2020-06-06 12:17:49 -07:00
tmdbId = searchResult.GetProviderId(MetadataProvider.Tmdb);
2014-03-02 08:42:21 -07:00
}
2014-02-12 22:11:54 -07:00
}
}
if (string.IsNullOrEmpty(tmdbId))
{
2020-06-06 12:17:49 -07:00
var tvdbId = info.GetProviderId(MetadataProvider.Tvdb);
2014-02-12 22:11:54 -07:00
if (!string.IsNullOrEmpty(tvdbId))
{
2014-03-02 08:42:21 -07:00
var searchResult = await FindByExternalId(tvdbId, "tvdb_id", cancellationToken).ConfigureAwait(false);
if (searchResult != null)
{
2020-06-06 12:17:49 -07:00
tmdbId = searchResult.GetProviderId(MetadataProvider.Tmdb);
2014-03-02 08:42:21 -07:00
}
2014-02-12 22:11:54 -07:00
}
}
if (string.IsNullOrEmpty(tmdbId))
2014-02-08 23:08:10 -07:00
{
result.QueriedById = false;
var searchResults = await new TmdbSearch(_logger, _jsonSerializer, _libraryManager).GetSearchResults(info, cancellationToken).ConfigureAwait(false);
2014-03-01 15:34:27 -07:00
var searchResult = searchResults.FirstOrDefault();
2014-02-15 09:36:09 -07:00
if (searchResult != null)
{
2020-06-06 12:17:49 -07:00
tmdbId = searchResult.GetProviderId(MetadataProvider.Tmdb);
2014-02-15 09:36:09 -07:00
}
2014-02-08 23:08:10 -07:00
}
2014-02-12 22:11:54 -07:00
if (!string.IsNullOrEmpty(tmdbId))
2014-02-08 23:08:10 -07:00
{
cancellationToken.ThrowIfCancellationRequested();
result = await FetchMovieData(tmdbId, info.MetadataLanguage, info.MetadataCountryCode, cancellationToken).ConfigureAwait(false);
2014-02-08 23:08:10 -07:00
result.HasMetadata = result.Item != null;
}
2014-03-02 08:42:21 -07:00
2014-02-08 23:08:10 -07:00
return result;
}
private async Task<MetadataResult<Series>> FetchMovieData(string tmdbId, string language, string preferredCountryCode, CancellationToken cancellationToken)
2014-02-08 23:08:10 -07:00
{
SeriesResult seriesInfo = await FetchMainResult(tmdbId, language, cancellationToken).ConfigureAwait(false);
2014-02-08 23:08:10 -07:00
2014-02-12 22:11:54 -07:00
if (seriesInfo == null)
{
return null;
}
2014-02-08 23:08:10 -07:00
2019-08-18 05:44:13 -07:00
tmdbId = seriesInfo.Id.ToString(_usCulture);
2014-02-08 23:08:10 -07:00
string dataFilePath = GetDataFilePath(tmdbId, language);
Directory.CreateDirectory(Path.GetDirectoryName(dataFilePath));
2014-02-12 22:11:54 -07:00
_jsonSerializer.SerializeToFile(seriesInfo, dataFilePath);
2014-02-08 23:08:10 -07:00
await EnsureSeriesInfo(tmdbId, language, cancellationToken).ConfigureAwait(false);
var result = new MetadataResult<Series>();
result.Item = new Series();
result.ResultLanguage = seriesInfo.ResultLanguage;
2014-02-08 23:08:10 -07:00
var settings = await TmdbMovieProvider.Current.GetTmdbSettings(cancellationToken).ConfigureAwait(false);
2018-09-12 10:26:21 -07:00
ProcessMainInfo(result, seriesInfo, preferredCountryCode, settings);
2014-02-08 23:08:10 -07:00
return result;
2014-02-08 23:08:10 -07:00
}
private void ProcessMainInfo(MetadataResult<Series> seriesResult, SeriesResult seriesInfo, string preferredCountryCode, TmdbSettingsResult settings)
2014-02-08 23:08:10 -07:00
{
2018-09-12 10:26:21 -07:00
var series = seriesResult.Item;
2019-08-18 05:44:13 -07:00
series.Name = seriesInfo.Name;
series.OriginalTitle = seriesInfo.Original_Name;
2020-06-06 12:17:49 -07:00
series.SetProviderId(MetadataProvider.Tmdb, seriesInfo.Id.ToString(_usCulture));
2014-02-08 23:08:10 -07:00
2019-08-18 05:44:13 -07:00
string voteAvg = seriesInfo.Vote_Average.ToString(CultureInfo.InvariantCulture);
2014-02-08 23:08:10 -07:00
if (float.TryParse(voteAvg, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out float rating))
2014-02-08 23:08:10 -07:00
{
series.CommunityRating = rating;
}
2019-08-18 05:44:13 -07:00
series.Overview = seriesInfo.Overview;
2014-02-08 23:08:10 -07:00
2019-08-18 05:44:13 -07:00
if (seriesInfo.Networks != null)
2014-02-08 23:08:10 -07:00
{
2019-08-18 05:44:13 -07:00
series.Studios = seriesInfo.Networks.Select(i => i.Name).ToArray();
2014-02-08 23:08:10 -07:00
}
2019-08-18 05:44:13 -07:00
if (seriesInfo.Genres != null)
2014-02-08 23:08:10 -07:00
{
2019-08-18 05:44:13 -07:00
series.Genres = seriesInfo.Genres.Select(i => i.Name).ToArray();
2014-02-08 23:08:10 -07:00
}
series.HomePageUrl = seriesInfo.Homepage;
2019-08-18 05:44:13 -07:00
series.RunTimeTicks = seriesInfo.Episode_Run_Time.Select(i => TimeSpan.FromMinutes(i).Ticks).FirstOrDefault();
2014-02-08 23:08:10 -07:00
2019-08-18 05:44:13 -07:00
if (string.Equals(seriesInfo.Status, "Ended", StringComparison.OrdinalIgnoreCase))
2014-02-08 23:08:10 -07:00
{
series.Status = SeriesStatus.Ended;
2019-08-18 05:44:13 -07:00
series.EndDate = seriesInfo.Last_Air_Date;
2014-02-08 23:08:10 -07:00
}
else
{
series.Status = SeriesStatus.Continuing;
}
2019-08-18 05:44:13 -07:00
series.PremiereDate = seriesInfo.First_Air_Date;
2014-02-08 23:08:10 -07:00
2019-08-18 05:44:13 -07:00
var ids = seriesInfo.External_Ids;
2014-02-08 23:08:10 -07:00
if (ids != null)
{
2019-08-18 05:44:13 -07:00
if (!string.IsNullOrWhiteSpace(ids.Imdb_Id))
2014-02-08 23:08:10 -07:00
{
2020-06-06 12:17:49 -07:00
series.SetProviderId(MetadataProvider.Imdb, ids.Imdb_Id);
2014-02-08 23:08:10 -07:00
}
2020-05-30 23:28:01 -07:00
2019-08-18 05:44:13 -07:00
if (ids.Tvrage_Id > 0)
2014-02-08 23:08:10 -07:00
{
2020-08-06 14:37:11 -07:00
series.SetProviderId(MetadataProvider.TvRage, ids.Tvrage_Id.Value.ToString(_usCulture));
2014-02-08 23:08:10 -07:00
}
2020-05-30 23:28:01 -07:00
2019-08-18 05:44:13 -07:00
if (ids.Tvdb_Id > 0)
2014-02-08 23:08:10 -07:00
{
2020-08-06 14:37:11 -07:00
series.SetProviderId(MetadataProvider.Tvdb, ids.Tvdb_Id.Value.ToString(_usCulture));
2014-02-08 23:08:10 -07:00
}
}
2019-08-18 05:44:13 -07:00
var contentRatings = (seriesInfo.Content_Ratings ?? new ContentRatings()).Results ?? new List<ContentRating>();
2019-08-18 05:44:13 -07:00
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)
{
2019-08-18 05:44:13 -07:00
series.OfficialRating = ourRelease.Rating;
}
else if (usRelease != null)
{
2019-08-18 05:44:13 -07:00
series.OfficialRating = usRelease.Rating;
}
else if (minimumRelease != null)
{
2019-08-18 05:44:13 -07:00
series.OfficialRating = minimumRelease.Rating;
}
2019-08-18 05:44:13 -07:00
if (seriesInfo.Videos != null && seriesInfo.Videos.Results != null)
{
2019-08-18 05:44:13 -07:00
foreach (var video in seriesInfo.Videos.Results)
{
2019-08-18 05:44:13 -07:00
if ((video.Type.Equals("trailer", StringComparison.OrdinalIgnoreCase)
|| video.Type.Equals("clip", StringComparison.OrdinalIgnoreCase))
&& video.Site.Equals("youtube", StringComparison.OrdinalIgnoreCase))
{
2019-08-18 05:44:13 -07:00
series.AddTrailerUrl($"http://www.youtube.com/watch?v={video.Key}");
}
}
}
2018-09-12 10:26:21 -07:00
seriesResult.ResetPeople();
var tmdbImageUrl = settings.images.GetImageUrl("original");
if (seriesInfo.Credits != null)
2018-09-12 10:26:21 -07:00
{
if (seriesInfo.Credits.Cast != null)
2018-09-12 10:26:21 -07:00
{
foreach (var actor in seriesInfo.Credits.Cast.OrderBy(a => a.Order))
2018-09-12 10:26:21 -07:00
{
2020-05-10 06:16:19 -07:00
var personInfo = new PersonInfo
{
Name = actor.Name.Trim(),
Role = actor.Character,
Type = PersonType.Actor,
SortOrder = actor.Order
};
2018-09-12 10:26:21 -07:00
if (!string.IsNullOrWhiteSpace(actor.Profile_Path))
{
personInfo.ImageUrl = tmdbImageUrl + actor.Profile_Path;
}
if (actor.Id > 0)
{
2020-06-06 12:17:49 -07:00
personInfo.SetProviderId(MetadataProvider.Tmdb, actor.Id.ToString(CultureInfo.InvariantCulture));
}
seriesResult.AddPerson(personInfo);
2018-09-12 10:26:21 -07:00
}
}
2018-09-12 10:26:21 -07:00
if (seriesInfo.Credits.Crew != null)
{
var keepTypes = new[]
2018-09-12 10:26:21 -07:00
{
PersonType.Director,
PersonType.Writer,
PersonType.Producer
};
foreach (var person in seriesInfo.Credits.Crew)
{
// Normalize this
var type = TmdbUtils.MapCrewToPersonType(person);
2018-09-12 10:26:21 -07:00
2020-05-13 09:02:54 -07:00
if (!keepTypes.Contains(type, StringComparer.OrdinalIgnoreCase)
&& !keepTypes.Contains(person.Job ?? string.Empty, StringComparer.OrdinalIgnoreCase))
{
continue;
}
2020-05-12 06:05:58 -07:00
seriesResult.AddPerson(new PersonInfo
{
Name = person.Name.Trim(),
Role = person.Job,
Type = type
});
}
2018-09-12 10:26:21 -07:00
}
}
2014-02-08 23:08:10 -07: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 09:37:41 -07:00
var dataPath = Path.Combine(appPaths.CachePath, "tmdb-tv");
2014-02-08 23:08:10 -07:00
return dataPath;
}
2014-03-02 08:42:21 -07:00
2014-02-08 23:08:10 -07:00
internal async Task DownloadSeriesInfo(string id, string preferredMetadataLanguage, CancellationToken cancellationToken)
{
SeriesResult mainResult = await FetchMainResult(id, preferredMetadataLanguage, cancellationToken).ConfigureAwait(false);
2014-02-08 23:08:10 -07:00
if (mainResult == null)
{
return;
}
2014-02-08 23:08:10 -07:00
var dataFilePath = GetDataFilePath(id, preferredMetadataLanguage);
Directory.CreateDirectory(Path.GetDirectoryName(dataFilePath));
2014-02-08 23:08:10 -07:00
_jsonSerializer.SerializeToFile(mainResult, dataFilePath);
}
internal async Task<SeriesResult> FetchMainResult(string id, string language, CancellationToken cancellationToken)
2014-02-08 23:08:10 -07:00
{
var url = string.Format(GetTvInfo3, id, TmdbUtils.ApiKey);
2014-02-08 23:08:10 -07:00
if (!string.IsNullOrEmpty(language))
{
url += "&language=" + TmdbMovieProvider.NormalizeLanguage(language)
+ "&include_image_language=" + TmdbMovieProvider.GetImageLanguagesParam(language); // Get images in english and with no language
}
2014-02-08 23:08:10 -07:00
cancellationToken.ThrowIfCancellationRequested();
2020-08-17 12:10:02 -07:00
using var mainRequestMessage = new HttpRequestMessage(HttpMethod.Get, url);
2020-08-17 12:59:29 -07:00
foreach (var header in TmdbUtils.AcceptHeaders)
{
mainRequestMessage.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue(header));
}
2020-08-17 12:10:02 -07:00
using var mainResponse = await TmdbMovieProvider.Current.GetMovieDbResponse(mainRequestMessage);
await using var mainStream = await mainResponse.Content.ReadAsStreamAsync().ConfigureAwait(false);
var mainResult = await _jsonSerializer.DeserializeFromStreamAsync<SeriesResult>(mainStream).ConfigureAwait(false);
2020-08-17 12:10:02 -07:00
if (!string.IsNullOrEmpty(language))
2014-02-08 23:08:10 -07:00
{
2020-08-17 12:10:02 -07:00
mainResult.ResultLanguage = language;
2014-02-08 23:08:10 -07:00
}
cancellationToken.ThrowIfCancellationRequested();
// If the language preference isn't english, then have the overview fallback to english if it's blank
if (mainResult != null &&
2019-08-18 05:44:13 -07:00
string.IsNullOrEmpty(mainResult.Overview) &&
!string.IsNullOrEmpty(language) &&
!string.Equals(language, "en", StringComparison.OrdinalIgnoreCase))
{
_logger.LogInformation("MovieDbSeriesProvider couldn't find meta for language {Language}. Trying English...", language);
url = string.Format(GetTvInfo3, id, TmdbUtils.ApiKey) + "&language=en";
if (!string.IsNullOrEmpty(language))
{
// Get images in english and with no language
url += "&include_image_language=" + TmdbMovieProvider.GetImageLanguagesParam(language);
}
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)
{
mainRequestMessage.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue(header));
}
2020-08-17 12:10:02 -07:00
using var response = await TmdbMovieProvider.Current.GetMovieDbResponse(requestMessage);
await using var stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false);
var englishResult = await _jsonSerializer.DeserializeFromStreamAsync<SeriesResult>(stream).ConfigureAwait(false);
2020-08-17 12:10:02 -07:00
mainResult.Overview = englishResult.Overview;
mainResult.ResultLanguage = "en";
}
return mainResult;
2014-02-08 23:08:10 -07:00
}
2014-03-02 08:42:21 -07:00
2014-02-08 23:08:10 -07:00
internal Task EnsureSeriesInfo(string tmdbId, string language, CancellationToken cancellationToken)
{
if (string.IsNullOrEmpty(tmdbId))
{
throw new ArgumentNullException(nameof(tmdbId));
2014-02-08 23:08:10 -07: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 10:26:21 -07:00
if ((DateTime.UtcNow - _fileSystem.GetLastWriteTimeUtc(fileInfo)).TotalDays <= 2)
2014-02-08 23:08:10 -07:00
{
2018-09-12 10:26:21 -07:00
return Task.CompletedTask;
2014-02-08 23:08:10 -07:00
}
}
return DownloadSeriesInfo(tmdbId, language, cancellationToken);
}
internal string GetDataFilePath(string tmdbId, string preferredLanguage)
{
if (string.IsNullOrEmpty(tmdbId))
{
throw new ArgumentNullException(nameof(tmdbId));
2014-02-08 23:08:10 -07:00
}
var path = GetSeriesDataPath(_configurationManager.ApplicationPaths, tmdbId);
var filename = string.Format("series-{0}.json", preferredLanguage ?? string.Empty);
2014-02-08 23:08:10 -07:00
return Path.Combine(path, filename);
}
2014-03-02 11:01:46 -07:00
private async Task<RemoteSearchResult> FindByExternalId(string id, string externalSource, CancellationToken cancellationToken)
2014-02-12 22:11:54 -07:00
{
2019-08-18 04:34:44 -07:00
var url = string.Format(TmdbUtils.BaseTmdbApiUrl + @"3/find/{0}?api_key={1}&external_source={2}",
2014-03-02 08:42:21 -07:00
id,
TmdbUtils.ApiKey,
2014-02-12 22:11:54 -07:00
externalSource);
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);
await using var stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false);
var result = await _jsonSerializer.DeserializeFromStreamAsync<ExternalIdLookupResult>(stream).ConfigureAwait(false);
if (result != null && result.Tv_Results != null)
2014-02-12 22:11:54 -07:00
{
2020-08-17 12:10:02 -07:00
var tv = result.Tv_Results.FirstOrDefault();
if (tv != null)
2014-02-12 22:11:54 -07:00
{
2020-08-17 12:10:02 -07:00
var tmdbSettings = await TmdbMovieProvider.Current.GetTmdbSettings(cancellationToken).ConfigureAwait(false);
var tmdbImageUrl = tmdbSettings.images.GetImageUrl("original");
2014-02-12 22:11:54 -07:00
2020-08-17 12:10:02 -07:00
var remoteResult = new RemoteSearchResult
2014-02-12 22:11:54 -07:00
{
2020-08-17 12:10:02 -07:00
Name = tv.Name,
SearchProviderName = Name,
ImageUrl = string.IsNullOrWhiteSpace(tv.Poster_Path)
? null
: tmdbImageUrl + tv.Poster_Path
};
2014-03-02 11:01:46 -07:00
2020-08-17 12:10:02 -07:00
remoteResult.SetProviderId(MetadataProvider.Tmdb, tv.Id.ToString(_usCulture));
2017-10-20 09:16:56 -07:00
2020-08-17 12:10:02 -07:00
return remoteResult;
2014-02-12 22:11:54 -07:00
}
}
return null;
}
// After TheTVDB
public int Order => 1;
2020-08-17 12:10:02 -07:00
public Task<HttpResponseMessage> GetImageResponse(string url, CancellationToken cancellationToken)
{
2020-08-17 12:10:02 -07:00
return _httpClientFactory.CreateClient().GetAsync(url, cancellationToken);
}
2014-02-08 23:08:10 -07:00
}
}