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

390 lines
15 KiB
C#
Raw Normal View History

#nullable disable
#pragma warning disable CS1591
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
2020-08-17 12:10:02 -07:00
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
2020-08-31 10:05:21 -07:00
using MediaBrowser.Common.Net;
2014-02-08 23:08:10 -07:00
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.TV;
using MediaBrowser.Controller.Library;
2014-02-08 23:08:10 -07: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-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-08-17 12:10:02 -07:00
private readonly IHttpClientFactory _httpClientFactory;
private readonly ILibraryManager _libraryManager;
private readonly TmdbClientManager _tmdbClientManager;
2014-02-08 23:08:10 -07:00
public TmdbSeriesProvider(
ILibraryManager libraryManager,
2020-08-17 12:10:02 -07:00
IHttpClientFactory httpClientFactory,
TmdbClientManager tmdbClientManager)
2014-02-08 23:08:10 -07:00
{
_libraryManager = libraryManager;
2020-08-17 12:10:02 -07:00
_httpClientFactory = httpClientFactory;
_tmdbClientManager = tmdbClientManager;
2014-02-08 23:08:10 -07:00
}
public string Name => TmdbUtils.ProviderName;
2014-02-08 23:08:10 -07:00
2020-09-08 07:12:47 -07: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 08:42:21 -07:00
{
var series = await _tmdbClientManager
.GetSeriesAsync(Convert.ToInt32(tmdbId, CultureInfo.InvariantCulture), searchInfo.MetadataLanguage, searchInfo.MetadataLanguage, cancellationToken)
.ConfigureAwait(false);
2014-03-02 11:01:46 -07:00
if (series != null)
2014-03-02 08:42:21 -07:00
{
var remoteResult = MapTvShowToRemoteSearchResult(series);
2014-03-02 08:42:21 -07:00
return new[] { remoteResult };
2014-03-02 08:42:21 -07:00
}
}
if (searchInfo.TryGetProviderId(MetadataProvider.Imdb, out var imdbId))
2014-03-02 08:42:21 -07:00
{
var findResult = await _tmdbClientManager
.FindByExternalIdAsync(imdbId, FindExternalSource.Imdb, searchInfo.MetadataLanguage, cancellationToken)
.ConfigureAwait(false);
2014-03-02 08:42:21 -07:00
2020-10-06 05:54:17 -07:00
var tvResults = findResult?.TvResults;
if (tvResults != null)
2014-03-02 08:42:21 -07:00
{
2020-10-06 05:54:17 -07:00
var imdbIdResults = new RemoteSearchResult[tvResults.Count];
for (var i = 0; i < tvResults.Count; i++)
{
2020-10-06 05:54:17 -07:00
var remoteResult = MapSearchTvToRemoteSearchResult(tvResults[i]);
remoteResult.SetProviderId(MetadataProvider.Imdb, imdbId);
2020-10-06 05:54:17 -07:00
imdbIdResults[i] = remoteResult;
}
return imdbIdResults;
2014-03-02 08:42:21 -07:00
}
}
if (searchInfo.TryGetProviderId(MetadataProvider.Tvdb, out var tvdbId))
2014-03-02 08:42:21 -07:00
{
var findResult = await _tmdbClientManager
.FindByExternalIdAsync(tvdbId, FindExternalSource.TvDb, searchInfo.MetadataLanguage, cancellationToken)
.ConfigureAwait(false);
2020-10-06 05:54:17 -07:00
var tvResults = findResult?.TvResults;
if (tvResults != null)
{
2020-10-06 05:54:17 -07:00
var tvIdResults = new RemoteSearchResult[tvResults.Count];
for (var i = 0; i < tvResults.Count; i++)
{
2020-10-06 05:54:17 -07:00
var remoteResult = MapSearchTvToRemoteSearchResult(tvResults[i]);
remoteResult.SetProviderId(MetadataProvider.Tvdb, tvdbId);
2020-10-06 05:54:17 -07:00
tvIdResults[i] = remoteResult;
}
return tvIdResults;
}
}
var tvSearchResults = await _tmdbClientManager.SearchSeriesAsync(searchInfo.Name, searchInfo.MetadataLanguage, cancellationToken: cancellationToken)
.ConfigureAwait(false);
2020-10-06 05:54:17 -07:00
var remoteResults = new RemoteSearchResult[tvSearchResults.Count];
for (var i = 0; i < tvSearchResults.Count; i++)
{
2020-10-06 05:54:17 -07: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 08:42:21 -07:00
if (!string.IsNullOrEmpty(series.ExternalIds.TvdbId))
2014-03-02 08:42:21 -07:00
{
remoteResult.SetProviderId(MetadataProvider.Tvdb, series.ExternalIds.TvdbId);
2014-03-02 08:42:21 -07: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-08 23:08:10 -07:00
public async Task<MetadataResult<Series>> GetMetadata(SeriesInfo info, CancellationToken cancellationToken)
{
2020-09-08 07:12:47 -07:00
var result = new MetadataResult<Series>
{
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
if (string.IsNullOrEmpty(tmdbId) && info.TryGetProviderId(MetadataProvider.Imdb, out var imdbId))
2014-02-12 22:11:54 -07:00
{
var searchResult = await _tmdbClientManager.FindByExternalIdAsync(imdbId, FindExternalSource.Imdb, info.MetadataLanguage, cancellationToken).ConfigureAwait(false);
if (searchResult?.TvResults.Count > 0)
2014-02-12 22:11:54 -07:00
{
tmdbId = searchResult.TvResults[0].Id.ToString(CultureInfo.InvariantCulture);
2014-02-12 22:11:54 -07:00
}
}
if (string.IsNullOrEmpty(tmdbId) && info.TryGetProviderId(MetadataProvider.Tvdb, out var tvdbId))
2014-02-12 22:11:54 -07:00
{
var searchResult = await _tmdbClientManager.FindByExternalIdAsync(tvdbId, FindExternalSource.TvDb, info.MetadataLanguage, cancellationToken).ConfigureAwait(false);
if (searchResult?.TvResults.Count > 0)
2014-02-12 22:11:54 -07:00
{
tmdbId = searchResult.TvResults[0].Id.ToString(CultureInfo.InvariantCulture);
2014-02-12 22:11:54 -07:00
}
}
if (string.IsNullOrEmpty(tmdbId))
2014-02-08 23:08:10 -07: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 cleanedName = TmdbUtils.CleanName(parsedName.Name);
var searchResults = await _tmdbClientManager.SearchSeriesAsync(cleanedName, info.MetadataLanguage, info.Year ?? parsedName.Year ?? 0, cancellationToken).ConfigureAwait(false);
2014-02-15 09:36:09 -07:00
if (searchResults.Count > 0)
2014-02-15 09:36:09 -07:00
{
tmdbId = searchResults[0].Id.ToString(CultureInfo.InvariantCulture);
2014-02-15 09:36:09 -07:00
}
2014-02-08 23:08:10 -07:00
}
if (string.IsNullOrEmpty(tmdbId))
2014-02-08 23:08:10 -07:00
{
return result;
}
2014-02-08 23:08:10 -07:00
cancellationToken.ThrowIfCancellationRequested();
2014-03-02 08:42:21 -07:00
var tvShow = await _tmdbClientManager
.GetSeriesAsync(Convert.ToInt32(tmdbId, CultureInfo.InvariantCulture), info.MetadataLanguage, TmdbUtils.GetImageLanguagesParam(info.MetadataLanguage), cancellationToken)
.ConfigureAwait(false);
2014-02-08 23:08:10 -07:00
result = new MetadataResult<Series>
{
Item = MapTvShowToSeries(tvShow, info.MetadataCountryCode),
ResultLanguage = info.MetadataLanguage ?? tvShow.OriginalLanguage
};
2014-02-08 23:08:10 -07:00
foreach (var person in GetPersons(tvShow))
{
result.AddPerson(person);
2014-02-12 22:11:54 -07:00
}
2014-02-08 23:08:10 -07:00
result.HasMetadata = result.Item != null;
return result;
2014-02-08 23:08:10 -07:00
}
private static Series MapTvShowToSeries(TvShow seriesResult, string preferredCountryCode)
2014-02-08 23:08:10 -07:00
{
2020-10-06 05:54:17 -07:00
var series = new Series
{
Name = seriesResult.Name,
OriginalTitle = seriesResult.OriginalName
};
2018-09-12 10:26:21 -07:00
series.SetProviderId(MetadataProvider.Tmdb, seriesResult.Id.ToString(CultureInfo.InvariantCulture));
2014-02-08 23:08:10 -07:00
series.CommunityRating = Convert.ToSingle(seriesResult.VoteAverage);
2014-02-08 23:08:10 -07:00
series.Overview = seriesResult.Overview;
2014-02-08 23:08:10 -07:00
if (seriesResult.Networks != null)
2014-02-08 23:08:10 -07:00
{
series.Studios = seriesResult.Networks.Select(i => i.Name).ToArray();
2014-02-08 23:08:10 -07:00
}
if (seriesResult.Genres != null)
2014-02-08 23:08:10 -07:00
{
series.Genres = seriesResult.Genres.Select(i => i.Name).ToArray();
2014-02-08 23:08:10 -07: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-08 23:08:10 -07:00
if (string.Equals(seriesResult.Status, "Ended", StringComparison.OrdinalIgnoreCase))
2014-02-08 23:08:10 -07:00
{
series.Status = SeriesStatus.Ended;
series.EndDate = seriesResult.LastAirDate;
2014-02-08 23:08:10 -07:00
}
else
{
series.Status = SeriesStatus.Continuing;
}
series.PremiereDate = seriesResult.FirstAirDate;
2014-02-08 23:08:10 -07:00
var ids = seriesResult.ExternalIds;
2014-02-08 23:08:10 -07:00
if (ids != null)
{
if (!string.IsNullOrWhiteSpace(ids.ImdbId))
2014-02-08 23:08:10 -07:00
{
series.SetProviderId(MetadataProvider.Imdb, ids.ImdbId);
2014-02-08 23:08:10 -07:00
}
2020-05-30 23:28:01 -07:00
if (!string.IsNullOrEmpty(ids.TvrageId))
2014-02-08 23:08:10 -07:00
{
series.SetProviderId(MetadataProvider.TvRage, ids.TvrageId);
2014-02-08 23:08:10 -07:00
}
2020-05-30 23:28:01 -07:00
if (!string.IsNullOrEmpty(ids.TvdbId))
2014-02-08 23:08:10 -07:00
{
series.SetProviderId(MetadataProvider.Tvdb, ids.TvdbId);
2014-02-08 23:08:10 -07:00
}
}
var contentRatings = seriesResult.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)
{
series.OfficialRating = TmdbUtils.BuildParentalRating(ourRelease.Iso_3166_1, 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;
}
if (seriesResult.Videos?.Results != null)
{
foreach (var video in seriesResult.Videos.Results)
{
if (TmdbUtils.IsTrailerType(video))
{
2020-10-06 05:54:17 -07:00
series.AddTrailerUrl("https://www.youtube.com/watch?v=" + video.Key);
}
}
}
2018-09-12 10:26:21 -07:00
return series;
}
2018-09-12 10:26:21 -07:00
private IEnumerable<PersonInfo> GetPersons(TvShow seriesResult)
{
if (seriesResult.Credits?.Cast != null)
2018-09-12 10:26:21 -07:00
{
foreach (var actor in seriesResult.Credits.Cast.OrderBy(a => a.Order).Take(TmdbUtils.MaxCastMembers))
2018-09-12 10:26:21 -07:00
{
var personInfo = new PersonInfo
2018-09-12 10:26:21 -07: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 12:59:29 -07:00
}
2014-02-08 23:08:10 -07:00
}
if (seriesResult.Credits?.Crew != null)
2014-02-08 23:08:10 -07:00
{
var keepTypes = new[]
2014-02-08 23:08:10 -07:00
{
PersonType.Director,
PersonType.Writer,
PersonType.Producer
};
2020-08-17 12:10:02 -07:00
foreach (var person in seriesResult.Credits.Crew)
2014-02-12 22:11:54 -07:00
{
// Normalize this
var type = TmdbUtils.MapCrewToPersonType(person);
2014-02-12 22:11:54 -07:00
if (!keepTypes.Contains(type, StringComparer.OrdinalIgnoreCase)
&& !keepTypes.Contains(person.Job ?? string.Empty, StringComparer.OrdinalIgnoreCase))
2014-02-12 22:11:54 -07:00
{
continue;
}
2017-10-20 09:16:56 -07:00
yield return new PersonInfo
{
Name = person.Name.Trim(),
Role = person.Job,
Type = type
};
2014-02-12 22:11:54 -07:00
}
}
}
2020-08-17 12:10:02 -07:00
public Task<HttpResponseMessage> GetImageResponse(string url, CancellationToken cancellationToken)
{
2020-08-31 10:05:21 -07:00
return _httpClientFactory.CreateClient(NamedClient.Default).GetAsync(url, cancellationToken);
}
2014-02-08 23:08:10 -07:00
}
}