From 6d6446f73cdec5cc42a1a3d9eb3b0fbf1f4bd8d0 Mon Sep 17 00:00:00 2001 From: mkelvers Date: Tue, 5 May 2026 13:41:36 +0200 Subject: [PATCH] feat: add amount of episodes --- api/anime/handler.go | 22 ++++++++++++++++++++++ integrations/jikan/types.go | 3 ++- templates/anime.gohtml | 6 +++++- 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/api/anime/handler.go b/api/anime/handler.go index eb642a1..d1e36c3 100644 --- a/api/anime/handler.go +++ b/api/anime/handler.go @@ -270,6 +270,7 @@ func (h *Handler) HandleAnimeDetails(w http.ResponseWriter, r *http.Request) { recommendations []jikan.RecommendationEntry watchlist []database.GetUserWatchListRow status string + episodesCount int ) g, gCtx := errgroup.WithContext(r.Context()) @@ -277,6 +278,26 @@ func (h *Handler) HandleAnimeDetails(w http.ResponseWriter, r *http.Request) { g.Go(func() error { var err error anime, err = h.jikanClient.GetAnimeByID(gCtx, id) + if err == nil && anime.Airing { + // If airing, we want to know how many episodes are released so far. + // The episodes endpoint with page 1 gives us the last visible page in pagination. + eps, err := h.jikanClient.GetEpisodes(gCtx, id, 1) + if err == nil { + if eps.Pagination.LastVisiblePage > 1 { + // Fetch last page to get the true count + lastEps, err := h.jikanClient.GetEpisodes(gCtx, id, eps.Pagination.LastVisiblePage) + if err == nil && len(lastEps.Data) > 0 { + lastEp := lastEps.Data[len(lastEps.Data)-1] + count, _ := strconv.Atoi(lastEp.Episode) + episodesCount = count + } + } else if len(eps.Data) > 0 { + lastEp := eps.Data[len(eps.Data)-1] + count, _ := strconv.Atoi(lastEp.Episode) + episodesCount = count + } + } + } return err }) @@ -336,6 +357,7 @@ func (h *Handler) HandleAnimeDetails(w http.ResponseWriter, r *http.Request) { "Status": status, "CurrentPath": r.URL.Path, "WatchlistIDs": watchlistIDs, + "EpisodesCount": episodesCount, }); err != nil { log.Printf("render error: %v", err) http.Error(w, "Internal Server Error", http.StatusInternalServerError) diff --git a/integrations/jikan/types.go b/integrations/jikan/types.go index 9b0d51b..206632b 100644 --- a/integrations/jikan/types.go +++ b/integrations/jikan/types.go @@ -303,7 +303,8 @@ type SearchResponse struct { } type Pagination struct { - HasNextPage bool `json:"has_next_page"` + LastVisiblePage int `json:"last_visible_page"` + HasNextPage bool `json:"has_next_page"` } type TopAnimeResponse struct { diff --git a/templates/anime.gohtml b/templates/anime.gohtml index df81aea..d8acf85 100644 --- a/templates/anime.gohtml +++ b/templates/anime.gohtml @@ -35,7 +35,11 @@ {{end}} {{if $anime.Type}}{{$anime.Type}}{{end}} - {{if $anime.Episodes}}{{$anime.Episodes}} episodes{{end}} + {{if and $anime.Airing .EpisodesCount}} + {{.EpisodesCount}}{{if $anime.Episodes}}/{{$anime.Episodes}}{{end}} episodes + {{else if $anime.Episodes}} + {{$anime.Episodes}} episodes + {{end}} {{if $anime.Status}}{{$anime.Status}}{{end}} {{if $anime.Season}}{{$anime.Premiered}}{{end}} {{if $anime.ShortRating}}{{$anime.ShortRating}}{{end}}