From 6dfc1e9fa89c02bdd50287660c23fd20dbce62b4 Mon Sep 17 00:00:00 2001 From: mkelvers Date: Wed, 8 Apr 2026 17:31:45 +0200 Subject: [PATCH] feat: use cached anime data for recommendations to show english titles when possible --- internal/jikan/recommendations.go | 50 +++++++++++++++++-------------- 1 file changed, 28 insertions(+), 22 deletions(-) diff --git a/internal/jikan/recommendations.go b/internal/jikan/recommendations.go index cbda9ad..d6bacbc 100644 --- a/internal/jikan/recommendations.go +++ b/internal/jikan/recommendations.go @@ -50,29 +50,35 @@ func (c *Client) GetRecommendations(animeID int, limit int) ([]Anime, error) { for i := 0; i < max; i++ { rec := result.Data[i] - // Map the recommendation data directly into an Anime struct. - // By doing this locally, we avoid N additional rate-limited API calls - // which was destroying the Jikan limit buckets. - anime := Anime{ - MalID: rec.Entry.MalID, - Title: rec.Entry.Title, - Images: struct { - Jpg struct { - LargeImageURL string `json:"large_image_url"` - } `json:"jpg"` - Webp struct { - LargeImageURL string `json:"large_image_url"` - } `json:"webp"` - }{ - Webp: struct { - LargeImageURL string `json:"large_image_url"` - }{ - LargeImageURL: rec.Entry.Images.Webp.LargeImageURL, - }, - }, - } + // Try to see if we already have the full anime details in our local cache. + // If we do, we can use it to get the English title without making an API call! + var fullAnime Anime + animeCacheKey := fmt.Sprintf("anime:%d", rec.Entry.MalID) - animes = append(animes, anime) + if c.getCache(animeCacheKey, &fullAnime) { + animes = append(animes, fullAnime) + } else { + // Otherwise, map the basic recommendation data directly into an Anime struct. + anime := Anime{ + MalID: rec.Entry.MalID, + Title: rec.Entry.Title, + Images: struct { + Jpg struct { + LargeImageURL string `json:"large_image_url"` + } `json:"jpg"` + Webp struct { + LargeImageURL string `json:"large_image_url"` + } `json:"webp"` + }{ + Webp: struct { + LargeImageURL string `json:"large_image_url"` + }{ + LargeImageURL: rec.Entry.Images.Webp.LargeImageURL, + }, + }, + } + animes = append(animes, anime) + } } c.setCache(cacheKey, animes, time.Hour*24)