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

387 lines
14 KiB
C#
Raw Normal View History

#pragma warning disable CS1591
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
2020-08-17 19:10:02 +00:00
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
2020-08-31 17:05:21 +00:00
using MediaBrowser.Common.Net;
2014-02-09 06:08:10 +00:00
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.TV;
using MediaBrowser.Controller.Library;
2014-02-09 06:08:10 +00:00
using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Providers;
using TMDbLib.Objects.Find;
using TMDbLib.Objects.Search;
using TMDbLib.Objects.TvShows;
2014-02-09 06:08:10 +00:00
2020-05-31 06:23:09 +00:00
namespace MediaBrowser.Providers.Plugins.Tmdb.TV
2014-02-09 06:08:10 +00:00
{
public class TmdbSeriesProvider : IRemoteMetadataProvider<Series, SeriesInfo>, IHasOrder
2014-02-09 06:08:10 +00:00
{
2020-08-17 19:10:02 +00:00
private readonly IHttpClientFactory _httpClientFactory;
private readonly ILibraryManager _libraryManager;
private readonly TmdbClientManager _tmdbClientManager;
2014-02-09 06:08:10 +00:00
public TmdbSeriesProvider(
ILibraryManager libraryManager,
2020-08-17 19:10:02 +00:00
IHttpClientFactory httpClientFactory,
TmdbClientManager tmdbClientManager)
2014-02-09 06:08:10 +00:00
{
_libraryManager = libraryManager;
2020-08-17 19:10:02 +00:00
_httpClientFactory = httpClientFactory;
_tmdbClientManager = tmdbClientManager;
2014-02-09 06:08:10 +00:00
}
public string Name => TmdbUtils.ProviderName;
2014-02-09 06:08:10 +00:00
2020-09-08 14:12:47 +00:00
// After TheTVDB
public int Order => 1;
public async Task<IEnumerable<RemoteSearchResult>> GetSearchResults(SeriesInfo searchInfo, CancellationToken cancellationToken)
{
if (searchInfo.TryGetProviderId(MetadataProvider.Tmdb, out var tmdbId))
2014-03-02 15:42:21 +00:00
{
var series = await _tmdbClientManager
.GetSeriesAsync(Convert.ToInt32(tmdbId, CultureInfo.InvariantCulture), searchInfo.MetadataLanguage, searchInfo.MetadataLanguage, cancellationToken)
.ConfigureAwait(false);
2014-03-02 18:01:46 +00:00
if (series != null)
2014-03-02 15:42:21 +00:00
{
var remoteResult = MapTvShowToRemoteSearchResult(series);
2014-03-02 15:42:21 +00:00
return new[] { remoteResult };
2014-03-02 15:42:21 +00:00
}
}
if (searchInfo.TryGetProviderId(MetadataProvider.Imdb, out var imdbId))
2014-03-02 15:42:21 +00:00
{
var findResult = await _tmdbClientManager
.FindByExternalIdAsync(imdbId, FindExternalSource.Imdb, searchInfo.MetadataLanguage, cancellationToken)
.ConfigureAwait(false);
2014-03-02 15:42:21 +00:00
2020-10-06 12:54:17 +00:00
var tvResults = findResult?.TvResults;
if (tvResults != null)
2014-03-02 15:42:21 +00:00
{
2020-10-06 12:54:17 +00:00
var imdbIdResults = new RemoteSearchResult[tvResults.Count];
for (var i = 0; i < tvResults.Count; i++)
{
2020-10-06 12:54:17 +00:00
var remoteResult = MapSearchTvToRemoteSearchResult(tvResults[i]);
remoteResult.SetProviderId(MetadataProvider.Imdb, imdbId);
2020-10-06 12:54:17 +00:00
imdbIdResults[i] = remoteResult;
}
return imdbIdResults;
2014-03-02 15:42:21 +00:00
}
}
if (searchInfo.TryGetProviderId(MetadataProvider.Tvdb, out var tvdbId))
2014-03-02 15:42:21 +00:00
{
var findResult = await _tmdbClientManager
.FindByExternalIdAsync(tvdbId, FindExternalSource.TvDb, searchInfo.MetadataLanguage, cancellationToken)
.ConfigureAwait(false);
2020-10-06 12:54:17 +00:00
var tvResults = findResult?.TvResults;
if (tvResults != null)
{
2020-10-06 12:54:17 +00:00
var tvIdResults = new RemoteSearchResult[tvResults.Count];
for (var i = 0; i < tvResults.Count; i++)
{
2020-10-06 12:54:17 +00:00
var remoteResult = MapSearchTvToRemoteSearchResult(tvResults[i]);
remoteResult.SetProviderId(MetadataProvider.Tvdb, tvdbId);
2020-10-06 12:54:17 +00:00
tvIdResults[i] = remoteResult;
}
return tvIdResults;
}
}
var tvSearchResults = await _tmdbClientManager.SearchSeriesAsync(searchInfo.Name, searchInfo.MetadataLanguage, cancellationToken: cancellationToken)
.ConfigureAwait(false);
2020-10-06 12:54:17 +00:00
var remoteResults = new RemoteSearchResult[tvSearchResults.Count];
for (var i = 0; i < tvSearchResults.Count; i++)
{
2020-10-06 12:54:17 +00:00
remoteResults[i] = MapSearchTvToRemoteSearchResult(tvSearchResults[i]);
}
return remoteResults;
}
private RemoteSearchResult MapTvShowToRemoteSearchResult(TvShow series)
{
var remoteResult = new RemoteSearchResult
{
Name = series.Name ?? series.OriginalName,
SearchProviderName = Name,
ImageUrl = _tmdbClientManager.GetPosterUrl(series.PosterPath),
Overview = series.Overview
};
remoteResult.SetProviderId(MetadataProvider.Tmdb, series.Id.ToString(CultureInfo.InvariantCulture));
if (series.ExternalIds != null)
{
if (!string.IsNullOrEmpty(series.ExternalIds.ImdbId))
{
remoteResult.SetProviderId(MetadataProvider.Imdb, series.ExternalIds.ImdbId);
}
2014-03-02 15:42:21 +00:00
if (!string.IsNullOrEmpty(series.ExternalIds.TvdbId))
2014-03-02 15:42:21 +00:00
{
remoteResult.SetProviderId(MetadataProvider.Tvdb, series.ExternalIds.TvdbId);
2014-03-02 15:42:21 +00:00
}
}
remoteResult.PremiereDate = series.FirstAirDate?.ToUniversalTime();
return remoteResult;
}
private RemoteSearchResult MapSearchTvToRemoteSearchResult(SearchTv series)
{
var remoteResult = new RemoteSearchResult
{
Name = series.Name ?? series.OriginalName,
SearchProviderName = Name,
ImageUrl = _tmdbClientManager.GetPosterUrl(series.PosterPath),
Overview = series.Overview
};
remoteResult.SetProviderId(MetadataProvider.Tmdb, series.Id.ToString(CultureInfo.InvariantCulture));
remoteResult.PremiereDate = series.FirstAirDate?.ToUniversalTime();
return remoteResult;
}
2014-02-09 06:08:10 +00:00
public async Task<MetadataResult<Series>> GetMetadata(SeriesInfo info, CancellationToken cancellationToken)
{
2020-09-08 14:12:47 +00:00
var result = new MetadataResult<Series>
{
QueriedById = true
};
2014-02-09 06:08:10 +00:00
2020-06-06 19:17:49 +00:00
var tmdbId = info.GetProviderId(MetadataProvider.Tmdb);
2014-02-09 06:08:10 +00:00
if (string.IsNullOrEmpty(tmdbId) && info.TryGetProviderId(MetadataProvider.Imdb, out var imdbId))
2014-02-13 05:11:54 +00:00
{
var searchResult = await _tmdbClientManager.FindByExternalIdAsync(imdbId, FindExternalSource.Imdb, info.MetadataLanguage, cancellationToken).ConfigureAwait(false);
if (searchResult?.TvResults.Count > 0)
2014-02-13 05:11:54 +00:00
{
tmdbId = searchResult.TvResults[0].Id.ToString(CultureInfo.InvariantCulture);
2014-02-13 05:11:54 +00:00
}
}
if (string.IsNullOrEmpty(tmdbId) && info.TryGetProviderId(MetadataProvider.Tvdb, out var tvdbId))
2014-02-13 05:11:54 +00:00
{
var searchResult = await _tmdbClientManager.FindByExternalIdAsync(tvdbId, FindExternalSource.TvDb, info.MetadataLanguage, cancellationToken).ConfigureAwait(false);
if (searchResult?.TvResults.Count > 0)
2014-02-13 05:11:54 +00:00
{
tmdbId = searchResult.TvResults[0].Id.ToString(CultureInfo.InvariantCulture);
2014-02-13 05:11:54 +00:00
}
}
if (string.IsNullOrEmpty(tmdbId))
2014-02-09 06:08:10 +00:00
{
result.QueriedById = false;
// ParseName is required here.
// Caller provides the filename with extension stripped and NOT the parsed filename
var parsedName = _libraryManager.ParseName(info.Name);
var searchResults = await _tmdbClientManager.SearchSeriesAsync(parsedName.Name, info.MetadataLanguage, info.Year ?? 0, cancellationToken).ConfigureAwait(false);
2014-02-15 16:36:09 +00:00
if (searchResults.Count > 0)
2014-02-15 16:36:09 +00:00
{
tmdbId = searchResults[0].Id.ToString(CultureInfo.InvariantCulture);
2014-02-15 16:36:09 +00:00
}
2014-02-09 06:08:10 +00:00
}
if (string.IsNullOrEmpty(tmdbId))
2014-02-09 06:08:10 +00:00
{
return result;
}
2014-02-09 06:08:10 +00:00
cancellationToken.ThrowIfCancellationRequested();
2014-03-02 15:42:21 +00:00
var tvShow = await _tmdbClientManager
.GetSeriesAsync(Convert.ToInt32(tmdbId, CultureInfo.InvariantCulture), info.MetadataLanguage, TmdbUtils.GetImageLanguagesParam(info.MetadataLanguage), cancellationToken)
.ConfigureAwait(false);
2014-02-09 06:08:10 +00:00
result = new MetadataResult<Series>
{
Item = MapTvShowToSeries(tvShow, info.MetadataCountryCode),
ResultLanguage = info.MetadataLanguage ?? tvShow.OriginalLanguage
};
2014-02-09 06:08:10 +00:00
foreach (var person in GetPersons(tvShow))
{
result.AddPerson(person);
2014-02-13 05:11:54 +00:00
}
2014-02-09 06:08:10 +00:00
result.HasMetadata = result.Item != null;
return result;
2014-02-09 06:08:10 +00:00
}
private static Series MapTvShowToSeries(TvShow seriesResult, string preferredCountryCode)
2014-02-09 06:08:10 +00:00
{
2020-10-06 12:54:17 +00:00
var series = new Series
{
Name = seriesResult.Name,
OriginalTitle = seriesResult.OriginalName
};
2018-09-12 17:26:21 +00:00
series.SetProviderId(MetadataProvider.Tmdb, seriesResult.Id.ToString(CultureInfo.InvariantCulture));
2014-02-09 06:08:10 +00:00
series.CommunityRating = Convert.ToSingle(seriesResult.VoteAverage);
2014-02-09 06:08:10 +00:00
series.Overview = seriesResult.Overview;
2014-02-09 06:08:10 +00:00
if (seriesResult.Networks != null)
2014-02-09 06:08:10 +00:00
{
series.Studios = seriesResult.Networks.Select(i => i.Name).ToArray();
2014-02-09 06:08:10 +00:00
}
if (seriesResult.Genres != null)
2014-02-09 06:08:10 +00:00
{
series.Genres = seriesResult.Genres.Select(i => i.Name).ToArray();
2014-02-09 06:08:10 +00:00
}
if (seriesResult.Keywords?.Results != null)
{
for (var i = 0; i < seriesResult.Keywords.Results.Count; i++)
{
series.AddTag(seriesResult.Keywords.Results[i].Name);
}
}
series.HomePageUrl = seriesResult.Homepage;
series.RunTimeTicks = seriesResult.EpisodeRunTime.Select(i => TimeSpan.FromMinutes(i).Ticks).FirstOrDefault();
2014-02-09 06:08:10 +00:00
if (string.Equals(seriesResult.Status, "Ended", StringComparison.OrdinalIgnoreCase))
2014-02-09 06:08:10 +00:00
{
series.Status = SeriesStatus.Ended;
series.EndDate = seriesResult.LastAirDate;
2014-02-09 06:08:10 +00:00
}
else
{
series.Status = SeriesStatus.Continuing;
}
series.PremiereDate = seriesResult.FirstAirDate;
2014-02-09 06:08:10 +00:00
var ids = seriesResult.ExternalIds;
2014-02-09 06:08:10 +00:00
if (ids != null)
{
if (!string.IsNullOrWhiteSpace(ids.ImdbId))
2014-02-09 06:08:10 +00:00
{
series.SetProviderId(MetadataProvider.Imdb, ids.ImdbId);
2014-02-09 06:08:10 +00:00
}
2020-05-31 06:28:01 +00:00
if (!string.IsNullOrEmpty(ids.TvrageId))
2014-02-09 06:08:10 +00:00
{
series.SetProviderId(MetadataProvider.TvRage, ids.TvrageId);
2014-02-09 06:08:10 +00:00
}
2020-05-31 06:28:01 +00:00
if (!string.IsNullOrEmpty(ids.TvdbId))
2014-02-09 06:08:10 +00:00
{
series.SetProviderId(MetadataProvider.Tvdb, ids.TvdbId);
2014-02-09 06:08:10 +00:00
}
}
var contentRatings = seriesResult.ContentRatings.Results ?? new List<ContentRating>();
2019-08-18 12:44:13 +00: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 12:44:13 +00:00
series.OfficialRating = ourRelease.Rating;
}
else if (usRelease != null)
{
2019-08-18 12:44:13 +00:00
series.OfficialRating = usRelease.Rating;
}
else if (minimumRelease != null)
{
2019-08-18 12:44:13 +00:00
series.OfficialRating = minimumRelease.Rating;
}
if (seriesResult.Videos?.Results != null)
{
foreach (var video in seriesResult.Videos.Results)
{
if (TmdbUtils.IsTrailerType(video))
{
2020-10-06 12:54:17 +00:00
series.AddTrailerUrl("https://www.youtube.com/watch?v=" + video.Key);
}
}
}
2018-09-12 17:26:21 +00:00
return series;
}
2018-09-12 17:26:21 +00:00
private IEnumerable<PersonInfo> GetPersons(TvShow seriesResult)
{
if (seriesResult.Credits?.Cast != null)
2018-09-12 17:26:21 +00:00
{
foreach (var actor in seriesResult.Credits.Cast.OrderBy(a => a.Order).Take(TmdbUtils.MaxCastMembers))
2018-09-12 17:26:21 +00:00
{
var personInfo = new PersonInfo
2018-09-12 17:26:21 +00:00
{
Name = actor.Name.Trim(),
Role = actor.Character,
Type = PersonType.Actor,
SortOrder = actor.Order,
ImageUrl = _tmdbClientManager.GetPosterUrl(actor.ProfilePath)
};
if (actor.Id > 0)
{
personInfo.SetProviderId(MetadataProvider.Tmdb, actor.Id.ToString(CultureInfo.InvariantCulture));
}
yield return personInfo;
2020-08-17 19:59:29 +00:00
}
2014-02-09 06:08:10 +00:00
}
if (seriesResult.Credits?.Crew != null)
2014-02-09 06:08:10 +00:00
{
var keepTypes = new[]
2014-02-09 06:08:10 +00:00
{
PersonType.Director,
PersonType.Writer,
PersonType.Producer
};
2020-08-17 19:10:02 +00:00
foreach (var person in seriesResult.Credits.Crew)
2014-02-13 05:11:54 +00:00
{
// Normalize this
var type = TmdbUtils.MapCrewToPersonType(person);
2014-02-13 05:11:54 +00:00
if (!keepTypes.Contains(type, StringComparer.OrdinalIgnoreCase)
&& !keepTypes.Contains(person.Job ?? string.Empty, StringComparer.OrdinalIgnoreCase))
2014-02-13 05:11:54 +00:00
{
continue;
}
2017-10-20 16:16:56 +00:00
yield return new PersonInfo
{
Name = person.Name.Trim(),
Role = person.Job,
Type = type
};
2014-02-13 05:11:54 +00:00
}
}
}
2020-08-17 19:10:02 +00:00
public Task<HttpResponseMessage> GetImageResponse(string url, CancellationToken cancellationToken)
{
2020-08-31 17:05:21 +00:00
return _httpClientFactory.CreateClient(NamedClient.Default).GetAsync(url, cancellationToken);
}
2014-02-09 06:08:10 +00:00
}
}