2020-06-19 20:24:13 +02:00
|
|
|
#pragma warning disable CS1591
|
|
|
|
|
2019-01-30 23:28:43 +01:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
2020-08-17 21:10:02 +02:00
|
|
|
using System.Net.Http;
|
2019-01-30 23:28:43 +01:00
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
2020-08-31 19:05:21 +02:00
|
|
|
using MediaBrowser.Common.Net;
|
2019-01-30 23:28:43 +01:00
|
|
|
using MediaBrowser.Controller.Entities;
|
|
|
|
using MediaBrowser.Controller.Entities.TV;
|
|
|
|
using MediaBrowser.Controller.Providers;
|
|
|
|
using MediaBrowser.Model.Entities;
|
|
|
|
using MediaBrowser.Model.Providers;
|
|
|
|
using Microsoft.Extensions.Logging;
|
2019-02-07 22:42:02 +01:00
|
|
|
using TvDbSharper;
|
2019-01-30 23:28:43 +01:00
|
|
|
using TvDbSharper.Dto;
|
|
|
|
|
2020-03-09 15:53:07 +01:00
|
|
|
namespace MediaBrowser.Providers.Plugins.TheTvdb
|
2019-01-30 23:28:43 +01:00
|
|
|
{
|
|
|
|
/// <summary>
|
2020-05-31 08:15:34 +02:00
|
|
|
/// Class RemoteEpisodeProvider.
|
2019-01-30 23:28:43 +01:00
|
|
|
/// </summary>
|
2019-04-30 20:08:59 +02:00
|
|
|
public class TvdbEpisodeProvider : IRemoteMetadataProvider<Episode, EpisodeInfo>, IHasOrder
|
2019-01-30 23:28:43 +01:00
|
|
|
{
|
2020-08-17 21:10:02 +02:00
|
|
|
private readonly IHttpClientFactory _httpClientFactory;
|
2020-06-06 02:15:56 +02:00
|
|
|
private readonly ILogger<TvdbEpisodeProvider> _logger;
|
2020-03-09 15:53:07 +01:00
|
|
|
private readonly TvdbClientManager _tvdbClientManager;
|
2019-01-30 23:28:43 +01:00
|
|
|
|
2020-08-17 21:10:02 +02:00
|
|
|
public TvdbEpisodeProvider(IHttpClientFactory httpClientFactory, ILogger<TvdbEpisodeProvider> logger, TvdbClientManager tvdbClientManager)
|
2019-01-30 23:28:43 +01:00
|
|
|
{
|
2020-08-17 21:10:02 +02:00
|
|
|
_httpClientFactory = httpClientFactory;
|
2019-01-30 23:28:43 +01:00
|
|
|
_logger = logger;
|
2020-03-09 15:53:07 +01:00
|
|
|
_tvdbClientManager = tvdbClientManager;
|
2019-01-30 23:28:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public async Task<IEnumerable<RemoteSearchResult>> GetSearchResults(EpisodeInfo searchInfo, CancellationToken cancellationToken)
|
|
|
|
{
|
|
|
|
var list = new List<RemoteSearchResult>();
|
|
|
|
|
2019-12-11 23:47:51 +01:00
|
|
|
// Either an episode number or date must be provided; and the dictionary of provider ids must be valid
|
|
|
|
if ((searchInfo.IndexNumber == null && searchInfo.PremiereDate == null)
|
2019-08-15 13:39:56 +02:00
|
|
|
|| !TvdbSeriesProvider.IsValidSeries(searchInfo.SeriesProviderIds))
|
2019-01-30 23:28:43 +01:00
|
|
|
{
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
2019-08-15 13:39:56 +02:00
|
|
|
var metadataResult = await GetEpisode(searchInfo, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
|
|
|
if (!metadataResult.HasMetadata)
|
2019-01-30 23:28:43 +01:00
|
|
|
{
|
2019-08-15 13:39:56 +02:00
|
|
|
return list;
|
2019-01-30 23:28:43 +01:00
|
|
|
}
|
|
|
|
|
2019-08-15 13:39:56 +02:00
|
|
|
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
|
|
|
|
});
|
|
|
|
|
2019-01-30 23:28:43 +01:00
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
public string Name => "TheTVDB";
|
|
|
|
|
|
|
|
public async Task<MetadataResult<Episode>> GetMetadata(EpisodeInfo searchInfo, CancellationToken cancellationToken)
|
|
|
|
{
|
|
|
|
var result = new MetadataResult<Episode>
|
|
|
|
{
|
|
|
|
QueriedById = true
|
|
|
|
};
|
|
|
|
|
|
|
|
if (TvdbSeriesProvider.IsValidSeries(searchInfo.SeriesProviderIds) &&
|
|
|
|
(searchInfo.IndexNumber.HasValue || searchInfo.PremiereDate.HasValue))
|
|
|
|
{
|
2019-08-15 13:39:56 +02:00
|
|
|
result = await GetEpisode(searchInfo, cancellationToken).ConfigureAwait(false);
|
2019-01-30 23:28:43 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-02-20 19:08:47 +01:00
|
|
|
_logger.LogDebug("No series identity found for {EpisodeName}", searchInfo.Name);
|
2019-01-30 23:28:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-08-15 13:39:56 +02:00
|
|
|
private async Task<MetadataResult<Episode>> GetEpisode(EpisodeInfo searchInfo, CancellationToken cancellationToken)
|
|
|
|
{
|
|
|
|
var result = new MetadataResult<Episode>
|
|
|
|
{
|
|
|
|
QueriedById = true
|
|
|
|
};
|
|
|
|
|
2020-06-06 21:17:49 +02:00
|
|
|
string seriesTvdbId = searchInfo.GetProviderId(MetadataProvider.Tvdb);
|
2019-08-15 13:39:56 +02:00
|
|
|
string episodeTvdbId = null;
|
|
|
|
try
|
|
|
|
{
|
2020-03-09 15:53:07 +01:00
|
|
|
episodeTvdbId = await _tvdbClientManager
|
2019-08-15 13:39:56 +02:00
|
|
|
.GetEpisodeTvdbId(searchInfo, searchInfo.MetadataLanguage, cancellationToken)
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
if (string.IsNullOrEmpty(episodeTvdbId))
|
|
|
|
{
|
2020-10-12 20:05:11 +02:00
|
|
|
_logger.LogError(
|
|
|
|
"Episode {SeasonNumber}x{EpisodeNumber} not found for series {SeriesTvdbId}",
|
2019-08-15 13:39:56 +02:00
|
|
|
searchInfo.ParentIndexNumber, searchInfo.IndexNumber, seriesTvdbId);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-03-09 15:53:07 +01:00
|
|
|
var episodeResult = await _tvdbClientManager.GetEpisodesAsync(
|
2019-08-15 13:39:56 +02:00
|
|
|
Convert.ToInt32(episodeTvdbId), searchInfo.MetadataLanguage,
|
|
|
|
cancellationToken).ConfigureAwait(false);
|
|
|
|
|
|
|
|
result = MapEpisodeToResult(searchInfo, episodeResult.Data);
|
|
|
|
}
|
|
|
|
catch (TvDbServerException e)
|
|
|
|
{
|
2019-08-15 13:43:12 +02:00
|
|
|
_logger.LogError(e, "Failed to retrieve episode with id {EpisodeTvDbId}, series id {SeriesTvdbId}", episodeTvdbId, seriesTvdbId);
|
2019-08-15 13:39:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-01-30 23:28:43 +01:00
|
|
|
private static MetadataResult<Episode> MapEpisodeToResult(EpisodeInfo id, EpisodeRecord episode)
|
|
|
|
{
|
|
|
|
var result = new MetadataResult<Episode>
|
|
|
|
{
|
|
|
|
HasMetadata = true,
|
|
|
|
Item = new Episode
|
|
|
|
{
|
|
|
|
IndexNumber = id.IndexNumber,
|
|
|
|
ParentIndexNumber = id.ParentIndexNumber,
|
|
|
|
IndexNumberEnd = id.IndexNumberEnd,
|
|
|
|
AirsBeforeEpisodeNumber = episode.AirsBeforeEpisode,
|
|
|
|
AirsAfterSeasonNumber = episode.AirsAfterSeason,
|
|
|
|
AirsBeforeSeasonNumber = episode.AirsBeforeSeason,
|
|
|
|
Name = episode.EpisodeName,
|
|
|
|
Overview = episode.Overview,
|
|
|
|
CommunityRating = (float?)episode.SiteRating,
|
2020-09-12 23:46:29 +02:00
|
|
|
OfficialRating = episode.ContentRating,
|
2019-01-30 23:28:43 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
result.ResetPeople();
|
|
|
|
|
|
|
|
var item = result.Item;
|
2020-06-06 21:17:49 +02:00
|
|
|
item.SetProviderId(MetadataProvider.Tvdb, episode.Id.ToString());
|
|
|
|
item.SetProviderId(MetadataProvider.Imdb, episode.ImdbId);
|
2019-01-30 23:28:43 +01:00
|
|
|
|
|
|
|
if (string.Equals(id.SeriesDisplayOrder, "dvd", StringComparison.OrdinalIgnoreCase))
|
|
|
|
{
|
|
|
|
item.IndexNumber = Convert.ToInt32(episode.DvdEpisodeNumber ?? episode.AiredEpisodeNumber);
|
|
|
|
item.ParentIndexNumber = episode.DvdSeason ?? episode.AiredSeason;
|
|
|
|
}
|
2020-09-05 02:34:57 +02:00
|
|
|
else if (string.Equals(id.SeriesDisplayOrder, "absolute", StringComparison.OrdinalIgnoreCase))
|
|
|
|
{
|
|
|
|
if (episode.AbsoluteNumber.GetValueOrDefault() != 0)
|
|
|
|
{
|
|
|
|
item.IndexNumber = episode.AbsoluteNumber;
|
|
|
|
}
|
|
|
|
}
|
2019-01-30 23:28:43 +01:00
|
|
|
else if (episode.AiredEpisodeNumber.HasValue)
|
|
|
|
{
|
|
|
|
item.IndexNumber = episode.AiredEpisodeNumber;
|
|
|
|
}
|
|
|
|
else if (episode.AiredSeason.HasValue)
|
|
|
|
{
|
|
|
|
item.ParentIndexNumber = episode.AiredSeason;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (DateTime.TryParse(episode.FirstAired, out var date))
|
|
|
|
{
|
2019-02-11 20:33:44 +01:00
|
|
|
// dates from tvdb are UTC but without offset or Z
|
2019-01-30 23:28:43 +01:00
|
|
|
item.PremiereDate = date;
|
|
|
|
item.ProductionYear = date.Year;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach (var director in episode.Directors)
|
|
|
|
{
|
|
|
|
result.AddPerson(new PersonInfo
|
|
|
|
{
|
|
|
|
Name = director,
|
|
|
|
Type = PersonType.Director
|
|
|
|
});
|
|
|
|
}
|
2019-02-02 16:12:16 +01:00
|
|
|
|
2019-08-15 14:54:22 +02:00
|
|
|
// GuestStars is a weird list of names and roles
|
|
|
|
// Example:
|
|
|
|
// 1: Some Actor (Role1
|
|
|
|
// 2: Role2
|
|
|
|
// 3: Role3)
|
|
|
|
// 4: Another Actor (Role1
|
|
|
|
// ...
|
|
|
|
for (var i = 0; i < episode.GuestStars.Length; ++i)
|
2019-01-30 23:28:43 +01:00
|
|
|
{
|
2019-08-15 14:54:22 +02:00
|
|
|
var currentActor = episode.GuestStars[i];
|
2020-07-29 13:17:01 +02:00
|
|
|
var roleStartIndex = currentActor.IndexOf('(', StringComparison.Ordinal);
|
2019-01-30 23:28:43 +01:00
|
|
|
|
2019-08-16 19:58:44 +02:00
|
|
|
if (roleStartIndex == -1)
|
2019-01-30 23:28:43 +01:00
|
|
|
{
|
2019-08-16 19:58:44 +02:00
|
|
|
result.AddPerson(new PersonInfo
|
2019-08-15 14:54:22 +02:00
|
|
|
{
|
2019-08-16 19:58:44 +02:00
|
|
|
Type = PersonType.GuestStar,
|
|
|
|
Name = currentActor,
|
|
|
|
Role = string.Empty
|
|
|
|
});
|
|
|
|
continue;
|
|
|
|
}
|
2019-08-15 19:54:01 +02:00
|
|
|
|
2020-03-24 16:12:06 +01:00
|
|
|
var roles = new List<string> { currentActor.Substring(roleStartIndex + 1) };
|
2019-01-30 23:28:43 +01:00
|
|
|
|
2019-08-16 19:58:44 +02:00
|
|
|
// Fetch all roles
|
|
|
|
for (var j = i + 1; j < episode.GuestStars.Length; ++j)
|
|
|
|
{
|
|
|
|
var currentRole = episode.GuestStars[j];
|
2020-07-29 13:17:01 +02:00
|
|
|
var roleEndIndex = currentRole.IndexOf(')', StringComparison.Ordinal);
|
2019-08-16 19:58:44 +02:00
|
|
|
|
2019-08-16 20:07:00 +02:00
|
|
|
if (roleEndIndex == -1)
|
2019-08-16 19:58:44 +02:00
|
|
|
{
|
2019-08-16 20:07:00 +02:00
|
|
|
roles.Add(currentRole);
|
2019-08-16 20:11:01 +02:00
|
|
|
continue;
|
2019-08-15 14:54:22 +02:00
|
|
|
}
|
2019-01-30 23:28:43 +01:00
|
|
|
|
2019-08-16 20:07:00 +02:00
|
|
|
roles.Add(currentRole.TrimEnd(')'));
|
|
|
|
// Update the outer index (keep in mind it adds 1 after the iteration)
|
|
|
|
i = j;
|
|
|
|
break;
|
2019-01-30 23:28:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
result.AddPerson(new PersonInfo
|
|
|
|
{
|
|
|
|
Type = PersonType.GuestStar,
|
2019-08-16 19:58:44 +02:00
|
|
|
Name = currentActor.Substring(0, roleStartIndex).Trim(),
|
|
|
|
Role = string.Join(", ", roles)
|
2019-01-30 23:28:43 +01:00
|
|
|
});
|
|
|
|
}
|
2019-02-02 16:12:16 +01:00
|
|
|
|
2019-01-30 23:28:43 +01:00
|
|
|
foreach (var writer in episode.Writers)
|
|
|
|
{
|
|
|
|
result.AddPerson(new PersonInfo
|
|
|
|
{
|
|
|
|
Name = writer,
|
|
|
|
Type = PersonType.Writer
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-02-07 22:42:02 +01:00
|
|
|
result.ResultLanguage = episode.Language.EpisodeName;
|
2019-01-30 23:28:43 +01:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-08-17 21:10:02 +02:00
|
|
|
public Task<HttpResponseMessage> GetImageResponse(string url, CancellationToken cancellationToken)
|
2019-01-30 23:28:43 +01:00
|
|
|
{
|
2020-08-31 19:05:21 +02:00
|
|
|
return _httpClientFactory.CreateClient(NamedClient.Default).GetAsync(url, cancellationToken);
|
2019-01-30 23:28:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public int Order => 0;
|
|
|
|
}
|
|
|
|
}
|