From 5b95562f2e5cee757ea0a96c97879e6c6d9b82e3 Mon Sep 17 00:00:00 2001 From: Mikal Stordal Date: Thu, 14 Nov 2024 21:05:52 +0100 Subject: [PATCH] fix: remove slowness when fetching episodes in APIv2 --- Shoko.Server/API/v2/Models/common/Episode.cs | 52 +++++++++++--------- 1 file changed, 29 insertions(+), 23 deletions(-) diff --git a/Shoko.Server/API/v2/Models/common/Episode.cs b/Shoko.Server/API/v2/Models/common/Episode.cs index acbe75ccc..e303d82cf 100644 --- a/Shoko.Server/API/v2/Models/common/Episode.cs +++ b/Shoko.Server/API/v2/Models/common/Episode.cs @@ -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; @@ -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;