refactor: split jikan client into smaller domain files
This commit is contained in:
19
internal/jikan/anime.go
Normal file
19
internal/jikan/anime.go
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
package jikan
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
// GetAnimeByID fetches full details for a single anime
|
||||||
|
func (c *Client) GetAnimeByID(id int) (Anime, error) {
|
||||||
|
if cached, ok := c.animeCache.Get(id); ok {
|
||||||
|
return cached, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var result AnimeResponse
|
||||||
|
reqURL := fmt.Sprintf("%s/anime/%d/full", c.baseURL, id)
|
||||||
|
if err := c.fetchWithRetry(reqURL, &result); err != nil {
|
||||||
|
return Anime{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
c.animeCache.Add(id, result.Data)
|
||||||
|
return result.Data, nil
|
||||||
|
}
|
||||||
@@ -4,22 +4,11 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/hashicorp/golang-lru/v2/expirable"
|
"github.com/hashicorp/golang-lru/v2/expirable"
|
||||||
)
|
)
|
||||||
|
|
||||||
type SearchResult struct {
|
|
||||||
Animes []Anime
|
|
||||||
HasNextPage bool
|
|
||||||
}
|
|
||||||
|
|
||||||
type TopAnimeResult struct {
|
|
||||||
Animes []Anime
|
|
||||||
HasNextPage bool
|
|
||||||
}
|
|
||||||
|
|
||||||
type Client struct {
|
type Client struct {
|
||||||
httpClient *http.Client
|
httpClient *http.Client
|
||||||
baseURL string
|
baseURL string
|
||||||
@@ -74,162 +63,3 @@ func (c *Client) fetchWithRetry(urlStr string, out interface{}) error {
|
|||||||
}
|
}
|
||||||
return fmt.Errorf("max retries exceeded for %s", urlStr)
|
return fmt.Errorf("max retries exceeded for %s", urlStr)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Search returns the anime list with pagination support
|
|
||||||
func (c *Client) Search(query string, page int) (SearchResult, error) {
|
|
||||||
if query == "" {
|
|
||||||
return SearchResult{}, nil
|
|
||||||
}
|
|
||||||
if page < 1 {
|
|
||||||
page = 1
|
|
||||||
}
|
|
||||||
|
|
||||||
cacheKey := fmt.Sprintf("search:%s:%d", query, page)
|
|
||||||
if cached, ok := c.cache.Get(cacheKey); ok {
|
|
||||||
return cached, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
var result SearchResponse
|
|
||||||
reqURL := fmt.Sprintf("%s/anime?q=%s&page=%d", c.baseURL, url.QueryEscape(query), page)
|
|
||||||
if err := c.fetchWithRetry(reqURL, &result); err != nil {
|
|
||||||
return SearchResult{}, err
|
|
||||||
}
|
|
||||||
|
|
||||||
res := SearchResult{
|
|
||||||
Animes: result.Data,
|
|
||||||
HasNextPage: result.Pagination.HasNextPage,
|
|
||||||
}
|
|
||||||
|
|
||||||
c.cache.Add(cacheKey, res)
|
|
||||||
return res, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetTopAnime fetches the top anime by popularity
|
|
||||||
func (c *Client) GetTopAnime(page int) (TopAnimeResult, error) {
|
|
||||||
if page < 1 {
|
|
||||||
page = 1
|
|
||||||
}
|
|
||||||
if cached, ok := c.topCache.Get(page); ok {
|
|
||||||
return cached, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
var result TopAnimeResponse
|
|
||||||
reqURL := fmt.Sprintf("%s/top/anime?filter=bypopularity&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.topCache.Add(page, res)
|
|
||||||
return res, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetAnimeByID fetches full details for a single anime
|
|
||||||
func (c *Client) GetAnimeByID(id int) (Anime, error) {
|
|
||||||
if cached, ok := c.animeCache.Get(id); ok {
|
|
||||||
return cached, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
var result AnimeResponse
|
|
||||||
reqURL := fmt.Sprintf("%s/anime/%d/full", c.baseURL, id)
|
|
||||||
if err := c.fetchWithRetry(reqURL, &result); err != nil {
|
|
||||||
return Anime{}, err
|
|
||||||
}
|
|
||||||
|
|
||||||
c.animeCache.Add(id, result.Data)
|
|
||||||
return result.Data, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetRelationsData fetches the raw relationships for an anime
|
|
||||||
func (c *Client) GetRelationsData(id int) (JikanRelationsResponse, error) {
|
|
||||||
if cached, ok := c.relationsCache.Get(id); ok {
|
|
||||||
return cached, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
var result JikanRelationsResponse
|
|
||||||
reqURL := fmt.Sprintf("%s/anime/%d/relations", c.baseURL, id)
|
|
||||||
if err := c.fetchWithRetry(reqURL, &result); err != nil {
|
|
||||||
return JikanRelationsResponse{}, err
|
|
||||||
}
|
|
||||||
|
|
||||||
c.relationsCache.Add(id, result)
|
|
||||||
return result, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// findFirstAnimeRelation extracts the first related anime ID for a specific relation type
|
|
||||||
func findFirstAnimeRelation(res JikanRelationsResponse, relType string) *int {
|
|
||||||
for _, group := range res.Data {
|
|
||||||
if group.Relation == relType {
|
|
||||||
for _, entry := range group.Entry {
|
|
||||||
if entry.Type == "anime" {
|
|
||||||
id := entry.MalID
|
|
||||||
return &id
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// fetchChain recursively builds the relational chain (Prequels or Sequels)
|
|
||||||
func (c *Client) fetchChain(startID int, direction string, visited map[int]bool) []RelationEntry {
|
|
||||||
rels, err := c.GetRelationsData(startID)
|
|
||||||
if err != nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
nextIDPtr := findFirstAnimeRelation(rels, direction)
|
|
||||||
if nextIDPtr == nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
nextID := *nextIDPtr
|
|
||||||
if visited[nextID] { // prevent loops
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
visited[nextID] = true
|
|
||||||
|
|
||||||
anime, err := c.GetAnimeByID(nextID)
|
|
||||||
if err != nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
entry := RelationEntry{Anime: anime, IsCurrent: false}
|
|
||||||
rest := c.fetchChain(nextID, direction, visited)
|
|
||||||
|
|
||||||
if direction == "Prequel" {
|
|
||||||
return append(rest, entry)
|
|
||||||
}
|
|
||||||
return append([]RelationEntry{entry}, rest...)
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetFullRelations resolves the full Prequel/Sequel chronological chain synchronously
|
|
||||||
func (c *Client) GetFullRelations(id int) []RelationEntry {
|
|
||||||
currentAnime, err := c.GetAnimeByID(id)
|
|
||||||
if err != nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
visited := map[int]bool{id: true}
|
|
||||||
|
|
||||||
prequels := c.fetchChain(id, "Prequel", visited)
|
|
||||||
|
|
||||||
// Clone visited set for sequels so we don't block valid paths if there's weird branching
|
|
||||||
visitedSeq := make(map[int]bool)
|
|
||||||
for k, v := range visited {
|
|
||||||
visitedSeq[k] = v
|
|
||||||
}
|
|
||||||
|
|
||||||
sequels := c.fetchChain(id, "Sequel", visitedSeq)
|
|
||||||
|
|
||||||
var result []RelationEntry
|
|
||||||
result = append(result, prequels...)
|
|
||||||
result = append(result, RelationEntry{Anime: currentAnime, IsCurrent: true})
|
|
||||||
result = append(result, sequels...)
|
|
||||||
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
|
|||||||
93
internal/jikan/relations.go
Normal file
93
internal/jikan/relations.go
Normal file
@@ -0,0 +1,93 @@
|
|||||||
|
package jikan
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
// GetRelationsData fetches the raw relationships for an anime
|
||||||
|
func (c *Client) GetRelationsData(id int) (JikanRelationsResponse, error) {
|
||||||
|
if cached, ok := c.relationsCache.Get(id); ok {
|
||||||
|
return cached, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var result JikanRelationsResponse
|
||||||
|
reqURL := fmt.Sprintf("%s/anime/%d/relations", c.baseURL, id)
|
||||||
|
if err := c.fetchWithRetry(reqURL, &result); err != nil {
|
||||||
|
return JikanRelationsResponse{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
c.relationsCache.Add(id, result)
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// findFirstAnimeRelation extracts the first related anime ID for a specific relation type
|
||||||
|
func findFirstAnimeRelation(res JikanRelationsResponse, relType string) *int {
|
||||||
|
for _, group := range res.Data {
|
||||||
|
if group.Relation == relType {
|
||||||
|
for _, entry := range group.Entry {
|
||||||
|
if entry.Type == "anime" {
|
||||||
|
id := entry.MalID
|
||||||
|
return &id
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// fetchChain recursively builds the relational chain (Prequels or Sequels)
|
||||||
|
func (c *Client) fetchChain(startID int, direction string, visited map[int]bool) []RelationEntry {
|
||||||
|
rels, err := c.GetRelationsData(startID)
|
||||||
|
if err != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
nextIDPtr := findFirstAnimeRelation(rels, direction)
|
||||||
|
if nextIDPtr == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
nextID := *nextIDPtr
|
||||||
|
if visited[nextID] { // prevent loops
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
visited[nextID] = true
|
||||||
|
|
||||||
|
anime, err := c.GetAnimeByID(nextID)
|
||||||
|
if err != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
entry := RelationEntry{Anime: anime, IsCurrent: false}
|
||||||
|
rest := c.fetchChain(nextID, direction, visited)
|
||||||
|
|
||||||
|
if direction == "Prequel" {
|
||||||
|
return append(rest, entry)
|
||||||
|
}
|
||||||
|
return append([]RelationEntry{entry}, rest...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetFullRelations resolves the full Prequel/Sequel chronological chain synchronously
|
||||||
|
func (c *Client) GetFullRelations(id int) []RelationEntry {
|
||||||
|
currentAnime, err := c.GetAnimeByID(id)
|
||||||
|
if err != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
visited := map[int]bool{id: true}
|
||||||
|
|
||||||
|
prequels := c.fetchChain(id, "Prequel", visited)
|
||||||
|
|
||||||
|
// Clone visited set for sequels so we don't block valid paths if there's weird branching
|
||||||
|
visitedSeq := make(map[int]bool)
|
||||||
|
for k, v := range visited {
|
||||||
|
visitedSeq[k] = v
|
||||||
|
}
|
||||||
|
|
||||||
|
sequels := c.fetchChain(id, "Sequel", visitedSeq)
|
||||||
|
|
||||||
|
var result []RelationEntry
|
||||||
|
result = append(result, prequels...)
|
||||||
|
result = append(result, RelationEntry{Anime: currentAnime, IsCurrent: true})
|
||||||
|
result = append(result, sequels...)
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
59
internal/jikan/search.go
Normal file
59
internal/jikan/search.go
Normal file
@@ -0,0 +1,59 @@
|
|||||||
|
package jikan
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net/url"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Search returns the anime list with pagination support
|
||||||
|
func (c *Client) Search(query string, page int) (SearchResult, error) {
|
||||||
|
if query == "" {
|
||||||
|
return SearchResult{}, nil
|
||||||
|
}
|
||||||
|
if page < 1 {
|
||||||
|
page = 1
|
||||||
|
}
|
||||||
|
|
||||||
|
cacheKey := fmt.Sprintf("search:%s:%d", query, page)
|
||||||
|
if cached, ok := c.cache.Get(cacheKey); ok {
|
||||||
|
return cached, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var result SearchResponse
|
||||||
|
reqURL := fmt.Sprintf("%s/anime?q=%s&page=%d", c.baseURL, url.QueryEscape(query), page)
|
||||||
|
if err := c.fetchWithRetry(reqURL, &result); err != nil {
|
||||||
|
return SearchResult{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
res := SearchResult{
|
||||||
|
Animes: result.Data,
|
||||||
|
HasNextPage: result.Pagination.HasNextPage,
|
||||||
|
}
|
||||||
|
|
||||||
|
c.cache.Add(cacheKey, res)
|
||||||
|
return res, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetTopAnime fetches the top anime by popularity
|
||||||
|
func (c *Client) GetTopAnime(page int) (TopAnimeResult, error) {
|
||||||
|
if page < 1 {
|
||||||
|
page = 1
|
||||||
|
}
|
||||||
|
if cached, ok := c.topCache.Get(page); ok {
|
||||||
|
return cached, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var result TopAnimeResponse
|
||||||
|
reqURL := fmt.Sprintf("%s/top/anime?filter=bypopularity&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.topCache.Add(page, res)
|
||||||
|
return res, nil
|
||||||
|
}
|
||||||
@@ -145,3 +145,13 @@ func (a Anime) DisplayTitle() string {
|
|||||||
}
|
}
|
||||||
return a.Title
|
return a.Title
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type SearchResult struct {
|
||||||
|
Animes []Anime
|
||||||
|
HasNextPage bool
|
||||||
|
}
|
||||||
|
|
||||||
|
type TopAnimeResult struct {
|
||||||
|
Animes []Anime
|
||||||
|
HasNextPage bool
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user