2021-10-26 06:49:01 -07:00
|
|
|
#nullable disable
|
|
|
|
|
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.Linq;
|
2020-08-17 12:10:02 -07:00
|
|
|
using System.Net.Http;
|
2019-01-13 12:26:31 -07:00
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
2020-09-24 10:49:35 -07:00
|
|
|
using MediaBrowser.Common.Net;
|
2016-03-02 18:56:51 -07:00
|
|
|
using MediaBrowser.Controller.Entities;
|
|
|
|
using MediaBrowser.Controller.Entities.TV;
|
|
|
|
using MediaBrowser.Controller.Providers;
|
|
|
|
using MediaBrowser.Model.Entities;
|
|
|
|
using MediaBrowser.Model.Providers;
|
|
|
|
|
2020-05-30 23:23:09 -07:00
|
|
|
namespace MediaBrowser.Providers.Plugins.Tmdb.TV
|
2016-03-02 18:56:51 -07:00
|
|
|
{
|
2020-09-24 10:49:35 -07:00
|
|
|
public class TmdbEpisodeProvider : IRemoteMetadataProvider<Episode, EpisodeInfo>, IHasOrder
|
2016-03-02 18:56:51 -07:00
|
|
|
{
|
2020-09-24 10:49:35 -07:00
|
|
|
private readonly IHttpClientFactory _httpClientFactory;
|
|
|
|
private readonly TmdbClientManager _tmdbClientManager;
|
|
|
|
|
|
|
|
public TmdbEpisodeProvider(IHttpClientFactory httpClientFactory, TmdbClientManager tmdbClientManager)
|
2020-09-08 07:12:47 -07:00
|
|
|
{
|
2020-09-24 10:49:35 -07:00
|
|
|
_httpClientFactory = httpClientFactory;
|
|
|
|
_tmdbClientManager = tmdbClientManager;
|
2020-09-08 07:12:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// After TheTvDb
|
|
|
|
public int Order => 1;
|
|
|
|
|
|
|
|
public string Name => TmdbUtils.ProviderName;
|
2016-03-02 18:56:51 -07:00
|
|
|
|
2016-03-24 07:42:06 -07:00
|
|
|
public async Task<IEnumerable<RemoteSearchResult>> GetSearchResults(EpisodeInfo searchInfo, CancellationToken cancellationToken)
|
2016-03-02 18:56:51 -07:00
|
|
|
{
|
2016-03-24 07:42:06 -07:00
|
|
|
// The search query must either provide an episode number or date
|
|
|
|
if (!searchInfo.IndexNumber.HasValue || !searchInfo.ParentIndexNumber.HasValue)
|
|
|
|
{
|
2020-09-24 10:49:35 -07:00
|
|
|
return Enumerable.Empty<RemoteSearchResult>();
|
2016-03-24 07:42:06 -07:00
|
|
|
}
|
|
|
|
|
2020-10-06 05:54:17 -07:00
|
|
|
var metadataResult = await GetMetadata(searchInfo, cancellationToken).ConfigureAwait(false);
|
2016-03-24 07:42:06 -07:00
|
|
|
|
2020-09-24 10:49:35 -07:00
|
|
|
if (!metadataResult.HasMetadata)
|
2016-03-24 07:42:06 -07:00
|
|
|
{
|
2020-09-24 10:49:35 -07:00
|
|
|
return Enumerable.Empty<RemoteSearchResult>();
|
2016-03-24 07:42:06 -07:00
|
|
|
}
|
|
|
|
|
2020-09-24 10:49:35 -07:00
|
|
|
var item = metadataResult.Item;
|
|
|
|
|
2020-10-06 05:54:17 -07:00
|
|
|
return new[]
|
2020-09-24 10:49:35 -07:00
|
|
|
{
|
2020-10-06 05:54:17 -07:00
|
|
|
new RemoteSearchResult
|
|
|
|
{
|
|
|
|
IndexNumber = item.IndexNumber,
|
|
|
|
Name = item.Name,
|
|
|
|
ParentIndexNumber = item.ParentIndexNumber,
|
|
|
|
PremiereDate = item.PremiereDate,
|
|
|
|
ProductionYear = item.ProductionYear,
|
|
|
|
ProviderIds = item.ProviderIds,
|
|
|
|
SearchProviderName = Name,
|
|
|
|
IndexNumberEnd = item.IndexNumberEnd
|
|
|
|
}
|
|
|
|
};
|
2016-03-02 18:56:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public async Task<MetadataResult<Episode>> GetMetadata(EpisodeInfo info, CancellationToken cancellationToken)
|
|
|
|
{
|
2020-09-24 10:49:35 -07:00
|
|
|
var metadataResult = new MetadataResult<Episode>();
|
2016-03-02 18:56:51 -07:00
|
|
|
|
2016-04-17 21:25:43 -07:00
|
|
|
// Allowing this will dramatically increase scan times
|
2017-08-12 19:09:07 -07:00
|
|
|
if (info.IsMissingEpisode)
|
2016-04-17 21:25:43 -07:00
|
|
|
{
|
2020-09-24 10:49:35 -07:00
|
|
|
return metadataResult;
|
2016-04-17 21:25:43 -07:00
|
|
|
}
|
|
|
|
|
2020-09-24 10:49:35 -07:00
|
|
|
info.SeriesProviderIds.TryGetValue(MetadataProvider.Tmdb.ToString(), out string tmdbId);
|
2016-03-02 18:56:51 -07:00
|
|
|
|
2020-09-24 10:49:35 -07:00
|
|
|
var seriesTmdbId = Convert.ToInt32(tmdbId, CultureInfo.InvariantCulture);
|
|
|
|
if (seriesTmdbId <= 0)
|
2016-03-02 18:56:51 -07:00
|
|
|
{
|
2020-09-24 10:49:35 -07:00
|
|
|
return metadataResult;
|
2016-03-02 18:56:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
var seasonNumber = info.ParentIndexNumber;
|
|
|
|
var episodeNumber = info.IndexNumber;
|
|
|
|
|
|
|
|
if (!seasonNumber.HasValue || !episodeNumber.HasValue)
|
|
|
|
{
|
2020-09-24 10:49:35 -07:00
|
|
|
return metadataResult;
|
2016-03-02 18:56:51 -07:00
|
|
|
}
|
|
|
|
|
2020-09-24 10:49:35 -07:00
|
|
|
var episodeResult = await _tmdbClientManager
|
2021-04-09 09:02:23 -07:00
|
|
|
.GetEpisodeAsync(seriesTmdbId, seasonNumber.Value, episodeNumber.Value, info.SeriesDisplayOrder, info.MetadataLanguage, TmdbUtils.GetImageLanguagesParam(info.MetadataLanguage), cancellationToken)
|
2020-09-24 10:49:35 -07:00
|
|
|
.ConfigureAwait(false);
|
2016-03-02 18:56:51 -07:00
|
|
|
|
2020-09-24 10:49:35 -07:00
|
|
|
if (episodeResult == null)
|
|
|
|
{
|
|
|
|
return metadataResult;
|
|
|
|
}
|
2016-03-02 18:56:51 -07:00
|
|
|
|
2020-09-24 10:49:35 -07:00
|
|
|
metadataResult.HasMetadata = true;
|
|
|
|
metadataResult.QueriedById = true;
|
2016-05-13 18:55:46 -07:00
|
|
|
|
2020-09-24 10:49:35 -07:00
|
|
|
if (!string.IsNullOrEmpty(episodeResult.Overview))
|
|
|
|
{
|
|
|
|
// if overview is non-empty, we can assume that localized data was returned
|
|
|
|
metadataResult.ResultLanguage = info.MetadataLanguage;
|
|
|
|
}
|
2016-03-02 18:56:51 -07:00
|
|
|
|
2020-09-24 10:49:35 -07:00
|
|
|
var item = new Episode
|
|
|
|
{
|
|
|
|
IndexNumber = info.IndexNumber,
|
|
|
|
ParentIndexNumber = info.ParentIndexNumber,
|
2021-03-02 13:17:25 -07:00
|
|
|
IndexNumberEnd = info.IndexNumberEnd,
|
|
|
|
Name = episodeResult.Name,
|
|
|
|
PremiereDate = episodeResult.AirDate,
|
|
|
|
ProductionYear = episodeResult.AirDate?.Year,
|
|
|
|
Overview = episodeResult.Overview,
|
|
|
|
CommunityRating = Convert.ToSingle(episodeResult.VoteAverage)
|
2020-09-24 10:49:35 -07:00
|
|
|
};
|
2016-03-02 18:56:51 -07:00
|
|
|
|
2021-04-20 23:33:29 -07:00
|
|
|
var externalIds = episodeResult.ExternalIds;
|
|
|
|
if (!string.IsNullOrEmpty(externalIds?.TvdbId))
|
2020-09-24 10:49:35 -07:00
|
|
|
{
|
2021-04-20 23:33:29 -07:00
|
|
|
item.SetProviderId(MetadataProvider.Tvdb, externalIds.TvdbId);
|
|
|
|
}
|
|
|
|
|
2021-04-21 07:15:31 -07:00
|
|
|
if (!string.IsNullOrEmpty(externalIds?.ImdbId))
|
2021-04-20 23:33:29 -07:00
|
|
|
{
|
|
|
|
item.SetProviderId(MetadataProvider.Imdb, externalIds.ImdbId);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(externalIds?.TvrageId))
|
|
|
|
{
|
|
|
|
item.SetProviderId(MetadataProvider.TvRage, externalIds.TvrageId);
|
2020-09-24 10:49:35 -07:00
|
|
|
}
|
2016-03-02 18:56:51 -07:00
|
|
|
|
2020-09-24 10:49:35 -07:00
|
|
|
if (episodeResult.Videos?.Results != null)
|
|
|
|
{
|
|
|
|
foreach (var video in episodeResult.Videos.Results)
|
2016-06-03 21:30:06 -07:00
|
|
|
{
|
2020-09-24 10:49:35 -07:00
|
|
|
if (TmdbUtils.IsTrailerType(video))
|
2016-06-03 21:30:06 -07:00
|
|
|
{
|
2020-10-06 05:54:17 -07:00
|
|
|
item.AddTrailerUrl("https://www.youtube.com/watch?v=" + video.Key);
|
2016-06-03 21:30:06 -07:00
|
|
|
}
|
|
|
|
}
|
2020-09-24 10:49:35 -07:00
|
|
|
}
|
2016-06-03 21:30:06 -07:00
|
|
|
|
2020-09-24 10:49:35 -07:00
|
|
|
var credits = episodeResult.Credits;
|
2016-03-02 18:56:51 -07:00
|
|
|
|
2020-09-24 10:49:35 -07:00
|
|
|
if (credits?.Cast != null)
|
|
|
|
{
|
|
|
|
foreach (var actor in credits.Cast.OrderBy(a => a.Order).Take(TmdbUtils.MaxCastMembers))
|
2016-03-02 18:56:51 -07:00
|
|
|
{
|
2020-09-24 10:49:35 -07:00
|
|
|
metadataResult.AddPerson(new PersonInfo
|
2016-03-02 18:56:51 -07:00
|
|
|
{
|
2020-09-24 10:49:35 -07:00
|
|
|
Name = actor.Name.Trim(),
|
|
|
|
Role = actor.Character,
|
|
|
|
Type = PersonType.Actor,
|
|
|
|
SortOrder = actor.Order
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2016-03-02 18:56:51 -07:00
|
|
|
|
2020-09-24 10:49:35 -07:00
|
|
|
if (credits?.GuestStars != null)
|
|
|
|
{
|
|
|
|
foreach (var guest in credits.GuestStars.OrderBy(a => a.Order).Take(TmdbUtils.MaxCastMembers))
|
|
|
|
{
|
|
|
|
metadataResult.AddPerson(new PersonInfo
|
2016-03-02 18:56:51 -07:00
|
|
|
{
|
2020-09-24 10:49:35 -07:00
|
|
|
Name = guest.Name.Trim(),
|
|
|
|
Role = guest.Character,
|
|
|
|
Type = PersonType.GuestStar,
|
|
|
|
SortOrder = guest.Order
|
|
|
|
});
|
2016-03-02 18:56:51 -07:00
|
|
|
}
|
|
|
|
}
|
2020-09-24 10:49:35 -07:00
|
|
|
|
|
|
|
// and the rest from crew
|
|
|
|
if (credits?.Crew != null)
|
2016-03-02 18:56:51 -07:00
|
|
|
{
|
2020-09-24 10:49:35 -07:00
|
|
|
foreach (var person in credits.Crew)
|
2016-03-02 18:56:51 -07:00
|
|
|
{
|
2020-09-24 10:49:35 -07:00
|
|
|
// Normalize this
|
|
|
|
var type = TmdbUtils.MapCrewToPersonType(person);
|
|
|
|
|
|
|
|
if (!TmdbUtils.WantedCrewTypes.Contains(type, StringComparer.OrdinalIgnoreCase)
|
|
|
|
&& !TmdbUtils.WantedCrewTypes.Contains(person.Job ?? string.Empty, StringComparer.OrdinalIgnoreCase))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2016-03-02 18:56:51 -07:00
|
|
|
|
2020-09-24 10:49:35 -07:00
|
|
|
metadataResult.AddPerson(new PersonInfo
|
|
|
|
{
|
|
|
|
Name = person.Name.Trim(),
|
|
|
|
Role = person.Job,
|
|
|
|
Type = type
|
|
|
|
});
|
|
|
|
}
|
2016-03-02 18:56:51 -07:00
|
|
|
}
|
|
|
|
|
2020-09-24 10:49:35 -07:00
|
|
|
metadataResult.Item = item;
|
|
|
|
|
|
|
|
return metadataResult;
|
2016-03-02 18:56:51 -07:00
|
|
|
}
|
|
|
|
|
2020-08-17 12:10:02 -07:00
|
|
|
public Task<HttpResponseMessage> GetImageResponse(string url, CancellationToken cancellationToken)
|
2016-03-02 18:56:51 -07:00
|
|
|
{
|
2020-09-24 10:49:35 -07:00
|
|
|
return _httpClientFactory.CreateClient(NamedClient.Default).GetAsync(url, cancellationToken);
|
2016-03-02 18:56:51 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|