2019-01-13 20:03:10 +00:00
|
|
|
using System;
|
2019-01-13 19:26:31 +00:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Globalization;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Net;
|
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
2016-03-03 01:56:51 +00:00
|
|
|
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;
|
2019-01-13 19:26:31 +00:00
|
|
|
using MediaBrowser.Model.Globalization;
|
|
|
|
using MediaBrowser.Model.IO;
|
2016-03-03 01:56:51 +00:00
|
|
|
using MediaBrowser.Model.Net;
|
|
|
|
using MediaBrowser.Model.Providers;
|
|
|
|
using MediaBrowser.Model.Serialization;
|
2019-01-13 19:26:31 +00:00
|
|
|
using Microsoft.Extensions.Logging;
|
2016-03-03 01:56:51 +00:00
|
|
|
|
2019-08-18 11:20:52 +00:00
|
|
|
namespace MediaBrowser.Providers.Tmdb.TV
|
2016-03-03 01:56:51 +00:00
|
|
|
{
|
2019-08-18 11:20:52 +00:00
|
|
|
public class TmdbEpisodeProvider :
|
|
|
|
TmdbEpisodeProviderBase,
|
2016-03-03 01:56:51 +00:00
|
|
|
IRemoteMetadataProvider<Episode, EpisodeInfo>,
|
|
|
|
IHasOrder
|
|
|
|
{
|
2019-08-18 11:20:52 +00:00
|
|
|
public TmdbEpisodeProvider(IHttpClient httpClient, IServerConfigurationManager configurationManager, IJsonSerializer jsonSerializer, IFileSystem fileSystem, ILocalizationManager localization, ILoggerFactory loggerFactory)
|
2018-12-13 13:18:25 +00:00
|
|
|
: base(httpClient, configurationManager, jsonSerializer, fileSystem, localization, loggerFactory)
|
2016-03-03 01:56:51 +00:00
|
|
|
{ }
|
|
|
|
|
2016-03-24 14:42:06 +00:00
|
|
|
public async Task<IEnumerable<RemoteSearchResult>> GetSearchResults(EpisodeInfo searchInfo, CancellationToken cancellationToken)
|
2016-03-03 01:56:51 +00:00
|
|
|
{
|
2016-03-24 14:42:06 +00:00
|
|
|
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;
|
2016-03-03 01:56:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public async Task<MetadataResult<Episode>> GetMetadata(EpisodeInfo info, CancellationToken cancellationToken)
|
|
|
|
{
|
|
|
|
var result = new MetadataResult<Episode>();
|
|
|
|
|
2016-04-18 04:25:43 +00:00
|
|
|
// Allowing this will dramatically increase scan times
|
2017-08-13 02:09:07 +00:00
|
|
|
if (info.IsMissingEpisode)
|
2016-04-18 04:25:43 +00:00
|
|
|
{
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-01-17 19:24:39 +00:00
|
|
|
info.SeriesProviderIds.TryGetValue(MetadataProviders.Tmdb.ToString(), out string seriesTmdbId);
|
2016-03-03 01:56:51 +00:00
|
|
|
|
|
|
|
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;
|
2016-09-18 04:27:17 +00:00
|
|
|
result.QueriedById = true;
|
2016-03-03 01:56:51 +00:00
|
|
|
|
2016-05-14 01:55:46 +00:00
|
|
|
if (!string.IsNullOrEmpty(response.overview))
|
|
|
|
{
|
|
|
|
// if overview is non-empty, we can assume that localized data was returned
|
|
|
|
result.ResultLanguage = info.MetadataLanguage;
|
|
|
|
}
|
|
|
|
|
2016-03-03 01:56:51 +00:00
|
|
|
var item = new Episode();
|
|
|
|
result.Item = item;
|
|
|
|
|
|
|
|
item.Name = info.Name;
|
|
|
|
item.IndexNumber = info.IndexNumber;
|
|
|
|
item.ParentIndexNumber = info.ParentIndexNumber;
|
|
|
|
item.IndexNumberEnd = info.IndexNumberEnd;
|
|
|
|
|
|
|
|
if (response.external_ids.tvdb_id > 0)
|
|
|
|
{
|
|
|
|
item.SetProviderId(MetadataProviders.Tvdb, response.external_ids.tvdb_id.ToString(CultureInfo.InvariantCulture));
|
|
|
|
}
|
|
|
|
|
|
|
|
item.PremiereDate = response.air_date;
|
|
|
|
item.ProductionYear = result.Item.PremiereDate.Value.Year;
|
|
|
|
|
|
|
|
item.Name = response.name;
|
|
|
|
item.Overview = response.overview;
|
|
|
|
|
|
|
|
item.CommunityRating = (float)response.vote_average;
|
|
|
|
|
2019-08-18 11:38:49 +00:00
|
|
|
if (response.videos?.results != null)
|
2016-06-04 04:30:06 +00:00
|
|
|
{
|
|
|
|
foreach (var video in response.videos.results)
|
|
|
|
{
|
2019-01-07 23:27:46 +00:00
|
|
|
if (video.type.Equals("trailer", System.StringComparison.OrdinalIgnoreCase)
|
2016-06-04 04:30:06 +00:00
|
|
|
|| video.type.Equals("clip", System.StringComparison.OrdinalIgnoreCase))
|
|
|
|
{
|
|
|
|
if (video.site.Equals("youtube", System.StringComparison.OrdinalIgnoreCase))
|
|
|
|
{
|
|
|
|
var videoUrl = string.Format("http://www.youtube.com/watch?v={0}", video.key);
|
2017-08-10 18:01:31 +00:00
|
|
|
item.AddTrailerUrl(videoUrl);
|
2016-06-04 04:30:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-03 01:56:51 +00:00
|
|
|
result.ResetPeople();
|
|
|
|
|
|
|
|
var credits = response.credits;
|
|
|
|
if (credits != null)
|
|
|
|
{
|
|
|
|
//Actors, Directors, Writers - all in People
|
|
|
|
//actors come from cast
|
|
|
|
if (credits.cast != null)
|
|
|
|
{
|
|
|
|
foreach (var actor in credits.cast.OrderBy(a => a.order))
|
|
|
|
{
|
|
|
|
result.AddPerson(new PersonInfo { Name = actor.name.Trim(), Role = actor.character, Type = PersonType.Actor, SortOrder = actor.order });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// guest stars
|
|
|
|
if (credits.guest_stars != null)
|
|
|
|
{
|
|
|
|
foreach (var guest in credits.guest_stars.OrderBy(a => a.order))
|
|
|
|
{
|
|
|
|
result.AddPerson(new PersonInfo { Name = guest.name.Trim(), Role = guest.character, Type = PersonType.GuestStar, SortOrder = guest.order });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//and the rest from crew
|
|
|
|
if (credits.crew != null)
|
|
|
|
{
|
2017-01-31 21:20:01 +00:00
|
|
|
var keepTypes = new[]
|
|
|
|
{
|
|
|
|
PersonType.Director,
|
2019-08-18 11:38:49 +00:00
|
|
|
PersonType.Writer,
|
|
|
|
PersonType.Producer
|
2017-01-31 21:20:01 +00:00
|
|
|
};
|
2016-11-01 18:28:36 +00:00
|
|
|
|
2016-03-03 01:56:51 +00:00
|
|
|
foreach (var person in credits.crew)
|
|
|
|
{
|
2016-11-01 18:28:36 +00:00
|
|
|
// Normalize this
|
2019-08-18 11:38:49 +00:00
|
|
|
var type = TmdbUtils.MapCrewToPersonType(person);
|
2016-11-01 18:28:36 +00:00
|
|
|
|
2019-08-18 11:38:49 +00:00
|
|
|
if (!keepTypes.Contains(type, StringComparer.OrdinalIgnoreCase) &&
|
2016-11-01 18:28:36 +00:00
|
|
|
!keepTypes.Contains(person.job ?? string.Empty, StringComparer.OrdinalIgnoreCase))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
result.AddPerson(new PersonInfo { Name = person.name.Trim(), Role = person.job, Type = type });
|
2016-03-03 01:56:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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);
|
|
|
|
}
|
2019-01-17 19:24:39 +00:00
|
|
|
// After TheTvDb
|
2019-01-13 20:31:14 +00:00
|
|
|
public int Order => 1;
|
2016-03-03 01:56:51 +00:00
|
|
|
|
2019-08-18 11:20:52 +00:00
|
|
|
public string Name => TmdbUtils.ProviderName;
|
2016-03-03 01:56:51 +00:00
|
|
|
}
|
|
|
|
}
|