Skip to content

Commit

Permalink
Fix retrieval of episode num for virtual episode. (#142)
Browse files Browse the repository at this point in the history
  • Loading branch information
scampower3 authored May 19, 2024
1 parent 236d28e commit 7c62aa8
Showing 1 changed file with 21 additions and 24 deletions.
45 changes: 21 additions & 24 deletions Jellyfin.Plugin.Tvdb/Providers/TvdbEpisodeProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ private async Task<MetadataResult<Episode>> GetEpisode(EpisodeInfo searchInfo, C
searchInfo.MetadataLanguage,
cancellationToken).ConfigureAwait(false);

result = MapEpisodeToResult(searchInfo, episodeResult);
result = await MapEpisodeToResult(searchInfo, episodeResult, cancellationToken).ConfigureAwait(false);
}
catch (Exception e)
{
Expand All @@ -205,7 +205,7 @@ private async Task<MetadataResult<Episode>> GetEpisode(EpisodeInfo searchInfo, C
return result;
}

private static MetadataResult<Episode> MapEpisodeToResult(EpisodeInfo id, EpisodeExtendedRecord episode)
private async Task<MetadataResult<Episode>> MapEpisodeToResult(EpisodeInfo id, EpisodeExtendedRecord episode, CancellationToken cancellationToken)
{
var result = new MetadataResult<Episode>
{
Expand All @@ -232,33 +232,30 @@ private static MetadataResult<Episode> MapEpisodeToResult(EpisodeInfo id, Episod
var imdbID = episode.RemoteIds.FirstOrDefault(x => string.Equals(x.SourceName, "IMDB", StringComparison.OrdinalIgnoreCase))?.Id;
item.SetProviderIdIfHasValue(MetadataProvider.Imdb, imdbID);

if (string.Equals(id.SeriesDisplayOrder, "dvd", StringComparison.OrdinalIgnoreCase))
// Missing episodes loses the episode number when refreshed.
if (id.IsMissingEpisode)
{
var dvdInfo = episode.Seasons.FirstOrDefault(x => string.Equals(x.Type.Name, "dvd", StringComparison.OrdinalIgnoreCase));
if (dvdInfo is null)
id.SeriesProviderIds.TryGetValue(MetadataProvider.Tvdb.ToString(), out var seriesTvdbIdString);
if (!string.IsNullOrEmpty(seriesTvdbIdString))
{
item.IndexNumber = episode.Number;
}
else
{
item.IndexNumber = Convert.ToInt32(dvdInfo.Number, CultureInfo.InvariantCulture);
}
int seriesTvdbId = int.Parse(seriesTvdbIdString, CultureInfo.InvariantCulture);
var displayOrder = id.SeriesDisplayOrder;
if (string.IsNullOrEmpty(displayOrder))
{
displayOrder = "default";
}

item.ParentIndexNumber = episode.SeasonNumber;
}
else if (string.Equals(id.SeriesDisplayOrder, "absolute", StringComparison.OrdinalIgnoreCase))
{
var absoluteInfo = episode.Seasons.FirstOrDefault(x => string.Equals(x.Type.Name, "absolute", StringComparison.OrdinalIgnoreCase));
if (absoluteInfo is not null)
{
item.IndexNumber = Convert.ToInt32(absoluteInfo.Number, CultureInfo.InvariantCulture);
// EpisodeExtendedRecord does not provide all the episode numbers for various display orders. So have to get all episodes for the series and search for the episode.
var allEpisodes = await _tvdbClientManager.GetSeriesEpisodesAsync(seriesTvdbId, item.PreferredMetadataLanguage, displayOrder, cancellationToken).ConfigureAwait(false);
var info = allEpisodes.Episodes.FirstOrDefault(x => x.Id == episode.Id);

if (info is not null)
{
item.ParentIndexNumber = info.SeasonNumber;
item.IndexNumber = info.Number;
}
}
}
else
{
item.IndexNumber = episode.Number;
item.ParentIndexNumber = episode.SeasonNumber;
}

if (DateTime.TryParse(episode.Aired, out var date))
{
Expand Down

0 comments on commit 7c62aa8

Please sign in to comment.