feat: fetch full details for recommendations to prioritize english titles
This commit is contained in:
@@ -167,7 +167,7 @@ func (h *Handler) HandleAPIAnime(w http.ResponseWriter, r *http.Request) {
|
|||||||
relations := h.svc.GetRelations(id)
|
relations := h.svc.GetRelations(id)
|
||||||
templates.AnimeRelationsList(relations).Render(r.Context(), w)
|
templates.AnimeRelationsList(relations).Render(r.Context(), w)
|
||||||
case "recommendations":
|
case "recommendations":
|
||||||
recs, err := h.svc.GetRecommendations(id)
|
recs, err := h.svc.GetRecommendations(id, 10)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("recommendations error for %d: %v", id, err)
|
log.Printf("recommendations error for %d: %v", id, err)
|
||||||
http.Error(w, "Failed to fetch recommendations", http.StatusInternalServerError)
|
http.Error(w, "Failed to fetch recommendations", http.StatusInternalServerError)
|
||||||
|
|||||||
@@ -65,8 +65,8 @@ func (s *Service) GetSchedule(day string) (jikan.ScheduleResult, error) {
|
|||||||
return s.jikanClient.GetSchedule(day)
|
return s.jikanClient.GetSchedule(day)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Service) GetRecommendations(animeID int) ([]jikan.Anime, error) {
|
func (s *Service) GetRecommendations(animeID int, limit int) ([]jikan.Anime, error) {
|
||||||
return s.jikanClient.GetRecommendations(animeID)
|
return s.jikanClient.GetRecommendations(animeID, limit)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Service) GetWatchingAnime(ctx context.Context, userID string) ([]templates.WatchingAnimeWithDetails, error) {
|
func (s *Service) GetWatchingAnime(ctx context.Context, userID string) ([]templates.WatchingAnimeWithDetails, error) {
|
||||||
|
|||||||
@@ -21,9 +21,12 @@ type RecommendationsResponse struct {
|
|||||||
Data []RecommendationEntry `json:"data"`
|
Data []RecommendationEntry `json:"data"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetRecommendations fetches recommendations for an anime
|
// GetRecommendations fetches full details for the top recommended anime
|
||||||
func (c *Client) GetRecommendations(animeID int) ([]Anime, error) {
|
func (c *Client) GetRecommendations(animeID int, limit int) ([]Anime, error) {
|
||||||
if cached, ok := c.recsCache.Get(animeID); ok {
|
if cached, ok := c.recsCache.Get(animeID); ok {
|
||||||
|
if len(cached) > limit {
|
||||||
|
return cached[:limit], nil
|
||||||
|
}
|
||||||
return cached, nil
|
return cached, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -33,27 +36,39 @@ func (c *Client) GetRecommendations(animeID int) ([]Anime, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Convert to Anime slice (partial data)
|
max := len(result.Data)
|
||||||
animes := make([]Anime, 0, len(result.Data))
|
if limit > 0 && max > limit {
|
||||||
for _, rec := range result.Data {
|
max = limit
|
||||||
animes = append(animes, Anime{
|
}
|
||||||
MalID: rec.Entry.MalID,
|
|
||||||
Title: rec.Entry.Title,
|
animes := make([]Anime, 0, max)
|
||||||
Images: struct {
|
for i := 0; i < max; i++ {
|
||||||
Jpg struct {
|
rec := result.Data[i]
|
||||||
LargeImageURL string `json:"large_image_url"`
|
// Fetch full details so we get English/Japanese titles
|
||||||
} `json:"jpg"`
|
fullAnime, err := c.GetAnimeByID(rec.Entry.MalID)
|
||||||
Webp struct {
|
if err == nil {
|
||||||
LargeImageURL string `json:"large_image_url"`
|
animes = append(animes, fullAnime)
|
||||||
} `json:"webp"`
|
} else {
|
||||||
}{
|
// Fallback to partial data if full fetch fails
|
||||||
Webp: struct {
|
animes = append(animes, Anime{
|
||||||
LargeImageURL string `json:"large_image_url"`
|
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"`
|
||||||
}{
|
}{
|
||||||
LargeImageURL: rec.Entry.Images.Webp.LargeImageURL,
|
Webp: struct {
|
||||||
|
LargeImageURL string `json:"large_image_url"`
|
||||||
|
}{
|
||||||
|
LargeImageURL: rec.Entry.Images.Webp.LargeImageURL,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
})
|
||||||
})
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
c.recsCache.Add(animeID, animes)
|
c.recsCache.Add(animeID, animes)
|
||||||
|
|||||||
Reference in New Issue
Block a user