fix: resolve syntax error and duplicate code in GetAllEpisodes
This commit is contained in:
@@ -211,54 +211,3 @@ func (c *Client) GetAllEpisodes(ctx context.Context, animeID int) ([]Episode, er
|
|||||||
|
|
||||||
return allEpisodes, nil
|
return allEpisodes, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
totalEpisodes := anime.Episodes
|
|
||||||
if totalEpisodes <= 0 {
|
|
||||||
// Fallback to simple page 1 fetch if count is unknown
|
|
||||||
resp, err := c.GetEpisodes(ctx, animeID, 1)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return resp.Data, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Jikan /episodes returns 100 per page
|
|
||||||
lastPage := (totalEpisodes + 99) / 100
|
|
||||||
var allEpisodes []Episode
|
|
||||||
|
|
||||||
// Fetch last page first (to get most recent episodes immediately)
|
|
||||||
lastResp, err := c.GetEpisodes(ctx, animeID, lastPage)
|
|
||||||
if err == nil {
|
|
||||||
allEpisodes = append(allEpisodes, lastResp.Data...)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Fetch first page
|
|
||||||
if lastPage > 1 {
|
|
||||||
firstResp, err := c.GetEpisodes(ctx, animeID, 1)
|
|
||||||
if err == nil {
|
|
||||||
allEpisodes = append(allEpisodes, firstResp.Data...)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Background fetching for intermediate pages (Working BACKWARDS)
|
|
||||||
if lastPage > 2 {
|
|
||||||
go func() {
|
|
||||||
bgCtx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
|
|
||||||
defer cancel()
|
|
||||||
|
|
||||||
// Count backwards from the second-to-last page to page 2
|
|
||||||
for p := lastPage - 1; p >= 2; p-- {
|
|
||||||
_, _ = c.GetEpisodes(bgCtx, animeID, p)
|
|
||||||
|
|
||||||
select {
|
|
||||||
case <-bgCtx.Done():
|
|
||||||
return
|
|
||||||
case <-time.After(600 * time.Millisecond): // Slightly slower to be safe
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
}
|
|
||||||
|
|
||||||
return allEpisodes, nil
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user