2020-06-19 18:24:13 +00:00
|
|
|
#pragma warning disable CS1591
|
|
|
|
|
2019-01-30 22:28:43 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using MediaBrowser.Common.Net;
|
|
|
|
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 21:42:02 +00:00
|
|
|
using TvDbSharper;
|
2019-01-30 22:28:43 +00:00
|
|
|
using TvDbSharper.Dto;
|
|
|
|
|
2020-03-09 14:53:07 +00:00
|
|
|
namespace MediaBrowser.Providers.Plugins.TheTvdb
|
2019-01-30 22:28:43 +00:00
|
|
|
{
|
|
|
|
/// <summary>
|
2020-05-31 06:15:34 +00:00
|
|
|
/// Class RemoteEpisodeProvider.
|
2019-01-30 22:28:43 +00:00
|
|
|
/// </summary>
|
2019-04-30 18:08:59 +00:00
|
|
|
public class TvdbEpisodeProvider : IRemoteMetadataProvider<Episode, EpisodeInfo>, IHasOrder
|
2019-01-30 22:28:43 +00:00
|
|
|
{
|
|
|
|
private readonly IHttpClient _httpClient;
|
2020-06-06 00:15:56 +00:00
|
|
|
private readonly ILogger<TvdbEpisodeProvider> _logger;
|
2020-03-09 14:53:07 +00:00
|
|
|
private readonly TvdbClientManager _tvdbClientManager;
|
2019-01-30 22:28:43 +00:00
|
|
|
|
2020-03-09 14:53:07 +00:00
|
|
|
public TvdbEpisodeProvider(IHttpClient httpClient, ILogger<TvdbEpisodeProvider> logger, TvdbClientManager tvdbClientManager)
|
2019-01-30 22:28:43 +00:00
|
|
|
{
|
|
|
|
_httpClient = httpClient;
|
|
|
|
_logger = logger;
|
2020-03-09 14:53:07 +00:00
|
|
|
_tvdbClientManager = tvdbClientManager;
|
2019-01-30 22:28:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public async Task<IEnumerable<RemoteSearchResult>> GetSearchResults(EpisodeInfo searchInfo, CancellationToken cancellationToken)
|
|
|
|
{
|
|
|
|
var list = new List<RemoteSearchResult>();
|
|
|
|
|
2019-12-11 22:47:51 +00: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 11:39:56 +00:00
|
|
|
|| !TvdbSeriesProvider.IsValidSeries(searchInfo.SeriesProviderIds))
|
2019-01-30 22:28:43 +00:00
|
|
|
{
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
2019-08-15 11:39:56 +00:00
|
|
|
var metadataResult = await GetEpisode(searchInfo, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
|
|
|
if (!metadataResult.HasMetadata)
|
2019-01-30 22:28:43 +00:00
|
|
|
{
|
2019-08-15 11:39:56 +00:00
|
|
|
return list;
|
2019-01-30 22:28:43 +00:00
|
|
|
}
|
|
|
|
|
2019-08-15 11:39:56 +00: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 22:28:43 +00: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 11:39:56 +00:00
|
|
|
result = await GetEpisode(searchInfo, cancellationToken).ConfigureAwait(false);
|
2019-01-30 22:28:43 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-02-20 18:08:47 +00:00
|
|
|
_logger.LogDebug("No series identity found for {EpisodeName}", searchInfo.Name);
|
2019-01-30 22:28:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-08-15 11:39:56 +00:00
|
|
|
private async Task<MetadataResult<Episode>> GetEpisode(EpisodeInfo searchInfo, CancellationToken cancellationToken)
|
|
|
|
{
|
|
|
|
var result = new MetadataResult<Episode>
|
|
|
|
{
|
|
|
|
QueriedById = true
|
|
|
|
};
|
|
|
|
|
2020-06-06 19:17:49 +00:00
|
|
|
string seriesTvdbId = searchInfo.GetProviderId(MetadataProvider.Tvdb);
|
2019-08-15 11:39:56 +00:00
|
|
|
string episodeTvdbId = null;
|
|
|
|
try
|
|
|
|
{
|
2020-03-09 14:53:07 +00:00
|
|
|
episodeTvdbId = await _tvdbClientManager
|
2019-08-15 11:39:56 +00:00
|
|
|
.GetEpisodeTvdbId(searchInfo, searchInfo.MetadataLanguage, cancellationToken)
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
if (string.IsNullOrEmpty(episodeTvdbId))
|
|
|
|
{
|
|
|
|
_logger.LogError("Episode {SeasonNumber}x{EpisodeNumber} not found for series {SeriesTvdbId}",
|
|
|
|
searchInfo.ParentIndexNumber, searchInfo.IndexNumber, seriesTvdbId);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-03-09 14:53:07 +00:00
|
|
|
var episodeResult = await _tvdbClientManager.GetEpisodesAsync(
|
2019-08-15 11:39:56 +00:00
|
|
|
Convert.ToInt32(episodeTvdbId), searchInfo.MetadataLanguage,
|
|
|
|
cancellationToken).ConfigureAwait(false);
|
|
|
|
|
|
|
|
result = MapEpisodeToResult(searchInfo, episodeResult.Data);
|
|
|
|
}
|
|
|
|
catch (TvDbServerException e)
|
|
|
|
{
|
2019-08-15 11:43:12 +00:00
|
|
|
_logger.LogError(e, "Failed to retrieve episode with id {EpisodeTvDbId}, series id {SeriesTvdbId}", episodeTvdbId, seriesTvdbId);
|
2019-08-15 11:39:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-01-30 22:28:43 +00: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,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
result.ResetPeople();
|
|
|
|
|
|
|
|
var item = result.Item;
|
2020-06-06 19:17:49 +00:00
|
|
|
item.SetProviderId(MetadataProvider.Tvdb, episode.Id.ToString());
|
|
|
|
item.SetProviderId(MetadataProvider.Imdb, episode.ImdbId);
|
2019-01-30 22:28:43 +00:00
|
|
|
|
|
|
|
if (string.Equals(id.SeriesDisplayOrder, "dvd", StringComparison.OrdinalIgnoreCase))
|
|
|
|
{
|
|
|
|
item.IndexNumber = Convert.ToInt32(episode.DvdEpisodeNumber ?? episode.AiredEpisodeNumber);
|
|
|
|
item.ParentIndexNumber = episode.DvdSeason ?? episode.AiredSeason;
|
|
|
|
}
|
|
|
|
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 19:33:44 +00:00
|
|
|
// dates from tvdb are UTC but without offset or Z
|
2019-01-30 22:28:43 +00: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 15:12:16 +00:00
|
|
|
|
2019-08-15 12:54:22 +00: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 22:28:43 +00:00
|
|
|
{
|
2019-08-15 12:54:22 +00:00
|
|
|
var currentActor = episode.GuestStars[i];
|
2020-07-29 11:17:01 +00:00
|
|
|
var roleStartIndex = currentActor.IndexOf('(', StringComparison.Ordinal);
|
2019-01-30 22:28:43 +00:00
|
|
|
|
2019-08-16 17:58:44 +00:00
|
|
|
if (roleStartIndex == -1)
|
2019-01-30 22:28:43 +00:00
|
|
|
{
|
2019-08-16 17:58:44 +00:00
|
|
|
result.AddPerson(new PersonInfo
|
2019-08-15 12:54:22 +00:00
|
|
|
{
|
2019-08-16 17:58:44 +00:00
|
|
|
Type = PersonType.GuestStar,
|
|
|
|
Name = currentActor,
|
|
|
|
Role = string.Empty
|
|
|
|
});
|
|
|
|
continue;
|
|
|
|
}
|
2019-08-15 17:54:01 +00:00
|
|
|
|
2020-03-24 15:12:06 +00:00
|
|
|
var roles = new List<string> { currentActor.Substring(roleStartIndex + 1) };
|
2019-01-30 22:28:43 +00:00
|
|
|
|
2019-08-16 17:58:44 +00:00
|
|
|
// Fetch all roles
|
|
|
|
for (var j = i + 1; j < episode.GuestStars.Length; ++j)
|
|
|
|
{
|
|
|
|
var currentRole = episode.GuestStars[j];
|
2020-07-29 11:17:01 +00:00
|
|
|
var roleEndIndex = currentRole.IndexOf(')', StringComparison.Ordinal);
|
2019-08-16 17:58:44 +00:00
|
|
|
|
2019-08-16 18:07:00 +00:00
|
|
|
if (roleEndIndex == -1)
|
2019-08-16 17:58:44 +00:00
|
|
|
{
|
2019-08-16 18:07:00 +00:00
|
|
|
roles.Add(currentRole);
|
2019-08-16 18:11:01 +00:00
|
|
|
continue;
|
2019-08-15 12:54:22 +00:00
|
|
|
}
|
2019-01-30 22:28:43 +00:00
|
|
|
|
2019-08-16 18:07:00 +00:00
|
|
|
roles.Add(currentRole.TrimEnd(')'));
|
|
|
|
// Update the outer index (keep in mind it adds 1 after the iteration)
|
|
|
|
i = j;
|
|
|
|
break;
|
2019-01-30 22:28:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
result.AddPerson(new PersonInfo
|
|
|
|
{
|
|
|
|
Type = PersonType.GuestStar,
|
2019-08-16 17:58:44 +00:00
|
|
|
Name = currentActor.Substring(0, roleStartIndex).Trim(),
|
|
|
|
Role = string.Join(", ", roles)
|
2019-01-30 22:28:43 +00:00
|
|
|
});
|
|
|
|
}
|
2019-02-02 15:12:16 +00:00
|
|
|
|
2019-01-30 22:28:43 +00:00
|
|
|
foreach (var writer in episode.Writers)
|
|
|
|
{
|
|
|
|
result.AddPerson(new PersonInfo
|
|
|
|
{
|
|
|
|
Name = writer,
|
|
|
|
Type = PersonType.Writer
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-02-07 21:42:02 +00:00
|
|
|
result.ResultLanguage = episode.Language.EpisodeName;
|
2019-01-30 22:28:43 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Task<HttpResponseInfo> GetImageResponse(string url, CancellationToken cancellationToken)
|
|
|
|
{
|
|
|
|
return _httpClient.GetResponse(new HttpRequestOptions
|
|
|
|
{
|
|
|
|
CancellationToken = cancellationToken,
|
|
|
|
Url = url
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public int Order => 0;
|
|
|
|
}
|
|
|
|
}
|