feat: add discover page for airing anime
This commit is contained in:
@@ -14,6 +14,8 @@ type Client struct {
|
||||
baseURL string
|
||||
cache *expirable.LRU[string, SearchResult]
|
||||
topCache *expirable.LRU[int, TopAnimeResult]
|
||||
airingCache *expirable.LRU[int, TopAnimeResult]
|
||||
upcomingCache *expirable.LRU[int, TopAnimeResult]
|
||||
animeCache *expirable.LRU[int, Anime]
|
||||
relationsCache *expirable.LRU[int, JikanRelationsResponse]
|
||||
}
|
||||
@@ -21,6 +23,8 @@ type Client struct {
|
||||
func NewClient() *Client {
|
||||
cache := expirable.NewLRU[string, SearchResult](500, nil, time.Hour*1)
|
||||
topCache := expirable.NewLRU[int, TopAnimeResult](100, nil, time.Hour*1)
|
||||
airingCache := expirable.NewLRU[int, TopAnimeResult](100, nil, time.Hour*1)
|
||||
upcomingCache := expirable.NewLRU[int, TopAnimeResult](100, nil, time.Hour*1)
|
||||
animeCache := expirable.NewLRU[int, Anime](1000, nil, time.Hour*24)
|
||||
relationsCache := expirable.NewLRU[int, JikanRelationsResponse](1000, nil, time.Hour*24)
|
||||
|
||||
@@ -29,6 +33,8 @@ func NewClient() *Client {
|
||||
baseURL: "https://api.jikan.moe/v4",
|
||||
cache: cache,
|
||||
topCache: topCache,
|
||||
airingCache: airingCache,
|
||||
upcomingCache: upcomingCache,
|
||||
animeCache: animeCache,
|
||||
relationsCache: relationsCache,
|
||||
}
|
||||
|
||||
51
internal/jikan/seasons.go
Normal file
51
internal/jikan/seasons.go
Normal file
@@ -0,0 +1,51 @@
|
||||
package jikan
|
||||
|
||||
import "fmt"
|
||||
|
||||
// GetSeasonsNow fetches currently airing anime
|
||||
func (c *Client) GetSeasonsNow(page int) (TopAnimeResult, error) {
|
||||
if page < 1 {
|
||||
page = 1
|
||||
}
|
||||
if cached, ok := c.airingCache.Get(page); ok {
|
||||
return cached, nil
|
||||
}
|
||||
|
||||
var result TopAnimeResponse
|
||||
reqURL := fmt.Sprintf("%s/seasons/now?page=%d", c.baseURL, page)
|
||||
if err := c.fetchWithRetry(reqURL, &result); err != nil {
|
||||
return TopAnimeResult{}, err
|
||||
}
|
||||
|
||||
res := TopAnimeResult{
|
||||
Animes: result.Data,
|
||||
HasNextPage: result.Pagination.HasNextPage,
|
||||
}
|
||||
|
||||
c.airingCache.Add(page, res)
|
||||
return res, nil
|
||||
}
|
||||
|
||||
// GetSeasonsUpcoming fetches upcoming anime
|
||||
func (c *Client) GetSeasonsUpcoming(page int) (TopAnimeResult, error) {
|
||||
if page < 1 {
|
||||
page = 1
|
||||
}
|
||||
if cached, ok := c.upcomingCache.Get(page); ok {
|
||||
return cached, nil
|
||||
}
|
||||
|
||||
var result TopAnimeResponse
|
||||
reqURL := fmt.Sprintf("%s/seasons/upcoming?page=%d", c.baseURL, page)
|
||||
if err := c.fetchWithRetry(reqURL, &result); err != nil {
|
||||
return TopAnimeResult{}, err
|
||||
}
|
||||
|
||||
res := TopAnimeResult{
|
||||
Animes: result.Data,
|
||||
HasNextPage: result.Pagination.HasNextPage,
|
||||
}
|
||||
|
||||
c.upcomingCache.Add(page, res)
|
||||
return res, nil
|
||||
}
|
||||
Reference in New Issue
Block a user