Skip to content

Commit

Permalink
fix: remove slowness when fetching episodes in APIv2
Browse files Browse the repository at this point in the history
  • Loading branch information
revam committed Nov 14, 2024
1 parent 64e2e99 commit 5b95562
Showing 1 changed file with 29 additions and 23 deletions.
52 changes: 29 additions & 23 deletions Shoko.Server/API/v2/Models/common/Episode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Shoko.Plugin.Abstractions.Enums;
using Shoko.Server.Extensions;
using Shoko.Server.Models;
using Shoko.Server.Models.TMDB;
using Shoko.Server.Providers.TMDB;
using Shoko.Server.Repositories;
using Shoko.Server.Services;
Expand Down Expand Up @@ -101,37 +102,42 @@ internal static Episode GenerateFromAnimeEpisode(HttpContext ctx, SVR_AnimeEpiso
ep.epnumber = cae.EpisodeNumber;
}

// Grab thumbnails/backdrops from first available source.
if (pic > 0 && aep.GetImages() is { } tmdbImages && tmdbImages.Count > 0)
if (aep.TmdbEpisodes is { Count: > 0 } tmdbEpisodes)
{
var thumbnail = tmdbImages
.Where(image => image.ImageType == ImageEntityType.Thumbnail && image.IsLocalAvailable)
.OrderByDescending(image => image.IsPreferred)
.FirstOrDefault();
var backdrop = tmdbImages.Where(image => image.ImageType == ImageEntityType.Backdrop && image.IsLocalAvailable).GetRandomElement() ??
aep.AnimeSeries?.GetImages(ImageEntityType.Backdrop).Where(image => image.IsLocalAvailable).GetRandomElement();
if (thumbnail is not null)
TMDB_Image thumbnail = null;
var tmdbEpisode = tmdbEpisodes[0];
if (pic > 0 && tmdbEpisode.GetImages(ImageEntityType.Thumbnail) is { } thumbnailImages && thumbnailImages.Count > 0)
{
backdrop ??= thumbnail;
ep.art.thumb.Add(new Art
thumbnail = thumbnailImages
.Where(image => image.ImageType == ImageEntityType.Thumbnail && image.IsLocalAvailable)
.OrderByDescending(image => image.IsPreferred)
.FirstOrDefault();
if (thumbnail is not null)
{
index = 0,
url = APIHelper.ConstructImageLinkFromTypeAndId(ctx, thumbnail.ImageType, thumbnail.Source, thumbnail.ID),
});
ep.art.thumb.Add(new Art
{
index = 0,
url = APIHelper.ConstructImageLinkFromTypeAndId(ctx, thumbnail.ImageType, thumbnail.Source, thumbnail.ID),
});
}
}
if (backdrop is not null)
if (pic > 0 && tmdbEpisode.GetImages(ImageEntityType.Backdrop) is { } backdropImages && backdropImages.Count > 0)
{
ep.art.fanart.Add(new Art
var backdrop = backdropImages
.Where(image => image.ImageType == ImageEntityType.Backdrop && image.IsLocalAvailable)
.OrderByDescending(image => image.IsPreferred)
.FirstOrDefault();
if (backdrop is not null)
{
index = 0,
url = APIHelper.ConstructImageLinkFromTypeAndId(ctx, backdrop.ImageType, backdrop.Source, backdrop.ID),
});
backdrop ??= thumbnail;
ep.art.fanart.Add(new Art
{
index = 0,
url = APIHelper.ConstructImageLinkFromTypeAndId(ctx, backdrop.ImageType, backdrop.Source, backdrop.ID),
});
}
}
}

if (aep.TmdbEpisodes is { Count: > 0 } tmdbEpisodes)
{
var tmdbEpisode = tmdbEpisodes[0];
if (!string.IsNullOrEmpty(tmdbEpisode.EnglishTitle))
{
ep.name = tmdbEpisode.EnglishTitle;
Expand Down

0 comments on commit 5b95562

Please sign in to comment.