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

213 lines
7.9 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
using MediaBrowser.Common.Net;
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.TV;
using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Globalization;
using MediaBrowser.Model.IO;
using MediaBrowser.Model.Net;
using MediaBrowser.Model.Providers;
using MediaBrowser.Model.Serialization;
using Microsoft.Extensions.Logging;
2020-05-30 23:23:09 -07:00
namespace MediaBrowser.Providers.Plugins.Tmdb.TV
{
public class TmdbEpisodeProvider :
TmdbEpisodeProviderBase,
IRemoteMetadataProvider<Episode, EpisodeInfo>,
IHasOrder
{
public TmdbEpisodeProvider(IHttpClient httpClient, IServerConfigurationManager configurationManager, IJsonSerializer jsonSerializer, IFileSystem fileSystem, ILocalizationManager localization, ILoggerFactory loggerFactory)
: base(httpClient, configurationManager, jsonSerializer, fileSystem, localization, loggerFactory)
{ }
public async Task<IEnumerable<RemoteSearchResult>> GetSearchResults(EpisodeInfo searchInfo, CancellationToken cancellationToken)
{
var list = new List<RemoteSearchResult>();
// The search query must either provide an episode number or date
if (!searchInfo.IndexNumber.HasValue || !searchInfo.ParentIndexNumber.HasValue)
{
return list;
}
var metadataResult = await GetMetadata(searchInfo, cancellationToken);
if (metadataResult.HasMetadata)
{
var item = metadataResult.Item;
list.Add(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
});
}
return list;
}
public async Task<MetadataResult<Episode>> GetMetadata(EpisodeInfo info, CancellationToken cancellationToken)
{
var result = new MetadataResult<Episode>();
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
{
return result;
}
info.SeriesProviderIds.TryGetValue(MetadataProviders.Tmdb.ToString(), out string seriesTmdbId);
if (string.IsNullOrEmpty(seriesTmdbId))
{
return result;
}
var seasonNumber = info.ParentIndexNumber;
var episodeNumber = info.IndexNumber;
if (!seasonNumber.HasValue || !episodeNumber.HasValue)
{
return result;
}
try
{
var response = await GetEpisodeInfo(seriesTmdbId, seasonNumber.Value, episodeNumber.Value, info.MetadataLanguage, cancellationToken).ConfigureAwait(false);
result.HasMetadata = true;
result.QueriedById = true;
2019-08-18 05:44:13 -07:00
if (!string.IsNullOrEmpty(response.Overview))
{
// if overview is non-empty, we can assume that localized data was returned
result.ResultLanguage = info.MetadataLanguage;
}
var item = new Episode();
result.Item = item;
item.Name = info.Name;
item.IndexNumber = info.IndexNumber;
item.ParentIndexNumber = info.ParentIndexNumber;
item.IndexNumberEnd = info.IndexNumberEnd;
2019-08-18 05:44:13 -07:00
if (response.External_Ids.Tvdb_Id > 0)
{
2019-08-18 05:44:13 -07:00
item.SetProviderId(MetadataProviders.Tvdb, response.External_Ids.Tvdb_Id.ToString(CultureInfo.InvariantCulture));
}
2019-08-18 05:44:13 -07:00
item.PremiereDate = response.Air_Date;
item.ProductionYear = result.Item.PremiereDate.Value.Year;
2019-08-18 05:44:13 -07:00
item.Name = response.Name;
item.Overview = response.Overview;
2019-08-18 05:44:13 -07:00
item.CommunityRating = (float)response.Vote_Average;
2019-08-18 05:44:13 -07:00
if (response.Videos?.Results != null)
{
2019-08-18 05:44:13 -07:00
foreach (var video in response.Videos.Results)
{
2019-08-18 05:44:13 -07:00
if (video.Type.Equals("trailer", System.StringComparison.OrdinalIgnoreCase)
|| video.Type.Equals("clip", System.StringComparison.OrdinalIgnoreCase))
{
2019-08-18 05:44:13 -07:00
if (video.Site.Equals("youtube", System.StringComparison.OrdinalIgnoreCase))
{
2019-08-18 05:44:13 -07:00
var videoUrl = string.Format("http://www.youtube.com/watch?v={0}", video.Key);
2017-08-10 11:01:31 -07:00
item.AddTrailerUrl(videoUrl);
}
}
}
}
result.ResetPeople();
2019-08-18 05:44:13 -07:00
var credits = response.Credits;
if (credits != null)
{
//Actors, Directors, Writers - all in People
//actors come from cast
2019-08-18 05:44:13 -07:00
if (credits.Cast != null)
{
2019-08-18 05:44:13 -07:00
foreach (var actor in credits.Cast.OrderBy(a => a.Order))
{
2019-08-18 05:44:13 -07:00
result.AddPerson(new PersonInfo { Name = actor.Name.Trim(), Role = actor.Character, Type = PersonType.Actor, SortOrder = actor.Order });
}
}
// guest stars
2019-08-18 05:44:13 -07:00
if (credits.Guest_Stars != null)
{
2019-08-18 05:44:13 -07:00
foreach (var guest in credits.Guest_Stars.OrderBy(a => a.Order))
{
2019-08-18 05:44:13 -07:00
result.AddPerson(new PersonInfo { Name = guest.Name.Trim(), Role = guest.Character, Type = PersonType.GuestStar, SortOrder = guest.Order });
}
}
//and the rest from crew
2019-08-18 05:44:13 -07:00
if (credits.Crew != null)
{
2017-01-31 14:20:01 -07:00
var keepTypes = new[]
{
PersonType.Director,
2019-08-18 04:38:49 -07:00
PersonType.Writer,
PersonType.Producer
2017-01-31 14:20:01 -07:00
};
2016-11-01 11:28:36 -07:00
2019-08-18 05:44:13 -07:00
foreach (var person in credits.Crew)
{
2016-11-01 11:28:36 -07:00
// Normalize this
2019-08-18 04:38:49 -07:00
var type = TmdbUtils.MapCrewToPersonType(person);
2016-11-01 11:28:36 -07:00
2019-08-18 04:38:49 -07:00
if (!keepTypes.Contains(type, StringComparer.OrdinalIgnoreCase) &&
2019-08-18 05:44:13 -07:00
!keepTypes.Contains(person.Job ?? string.Empty, StringComparer.OrdinalIgnoreCase))
2016-11-01 11:28:36 -07:00
{
continue;
}
2019-08-18 05:44:13 -07:00
result.AddPerson(new PersonInfo { Name = person.Name.Trim(), Role = person.Job, Type = type });
}
}
}
}
catch (HttpException ex)
{
if (ex.StatusCode.HasValue && ex.StatusCode.Value == HttpStatusCode.NotFound)
{
return result;
}
throw;
}
return result;
}
public Task<HttpResponseInfo> GetImageResponse(string url, CancellationToken cancellationToken)
{
return GetResponse(url, cancellationToken);
}
2020-05-30 23:28:01 -07:00
// After TheTvDb
public int Order => 1;
public string Name => TmdbUtils.ProviderName;
}
}