Skip to content

Commit

Permalink
Get season id according to selected display order. (#147)
Browse files Browse the repository at this point in the history
  • Loading branch information
scampower3 authored May 25, 2024
1 parent f97dd4d commit 2737e76
Showing 1 changed file with 24 additions and 4 deletions.
28 changes: 24 additions & 4 deletions Jellyfin.Plugin.Tvdb/Providers/TvdbSeasonImageProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,14 @@ public async Task<IEnumerable<RemoteImageInfo>> GetImages(BaseItem item, Cancell

var seriesTvdbId = series.GetTvdbId();
var seasonNumber = season.IndexNumber.Value;
var displayOrder = season.Series.DisplayOrder;

var seasonArtworks = await GetSeasonArtworks(seriesTvdbId, seasonNumber, cancellationToken)
if (string.IsNullOrEmpty(displayOrder))
{
displayOrder = "official";
}

var seasonArtworks = await GetSeasonArtworks(seriesTvdbId, seasonNumber, displayOrder, cancellationToken)
.ConfigureAwait(false);

var remoteImages = new List<RemoteImageInfo>();
Expand All @@ -106,17 +112,31 @@ public async Task<IEnumerable<RemoteImageInfo>> GetImages(BaseItem item, Cancell
return remoteImages.OrderByLanguageDescending(item.GetPreferredMetadataLanguage());
}

private async Task<IReadOnlyList<ArtworkBaseRecord>> GetSeasonArtworks(int seriesTvdbId, int seasonNumber, CancellationToken cancellationToken)
private async Task<IReadOnlyList<ArtworkBaseRecord>> GetSeasonArtworks(int seriesTvdbId, int seasonNumber, string displayOrder, CancellationToken cancellationToken)
{
try
{
var seriesInfo = await _tvdbClientManager.GetSeriesExtendedByIdAsync(seriesTvdbId, string.Empty, cancellationToken, small: true)
.ConfigureAwait(false);
var seasonTvdbId = seriesInfo.Seasons.FirstOrDefault(s => s.Number == seasonNumber)?.Id;
// Get the season information for the particular display order and aired order display order
// Ensure that the aired order is always last in the list
var seasonBaseList = seriesInfo.Seasons.Where(s => s.Number == seasonNumber && (s.Type.Type == displayOrder || s.Type.Type == "official" )).OrderBy(s => s.Type.Type == "official");

var seasonTvdbId = seasonBaseList?.FirstOrDefault()?.Id;
var seasonInfo = await _tvdbClientManager.GetSeasonByIdAsync(seasonTvdbId ?? 0, string.Empty, cancellationToken)
.ConfigureAwait(false);
return seasonInfo.Artwork;

// If no image are found for the particular display order and the display order is not aired order,
// try to get the aired order images
if ((seasonInfo.Artwork is null || seasonInfo.Artwork.Count == 0)
&& !string.Equals(displayOrder, "official", StringComparison.OrdinalIgnoreCase))
{
seasonTvdbId = seasonBaseList?.Skip(1).FirstOrDefault()?.Id;
seasonInfo = await _tvdbClientManager.GetSeasonByIdAsync(seasonTvdbId ?? 0, string.Empty, cancellationToken)
.ConfigureAwait(false);
}

return seasonInfo.Artwork ?? Enumerable.Empty<ArtworkBaseRecord>().ToList();
}
catch (Exception ex) when (
(ex is SeriesException seriesEx && seriesEx.InnerException is JsonException)
Expand Down

0 comments on commit 2737e76

Please sign in to comment.