chore: cleanup files
This commit is contained in:
@@ -5,7 +5,6 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
// GetAnimeByID fetches full details for a single anime
|
||||
func (c *Client) GetAnimeByID(id int) (Anime, error) {
|
||||
cacheKey := fmt.Sprintf("anime:%d", id)
|
||||
var cached Anime
|
||||
|
||||
@@ -43,7 +43,7 @@ func (c *Client) waitRateLimit() {
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Client) getCache(key string, out interface{}) bool {
|
||||
func (c *Client) getCache(key string, out any) bool {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
|
||||
defer cancel()
|
||||
|
||||
@@ -56,7 +56,7 @@ func (c *Client) getCache(key string, out interface{}) bool {
|
||||
return err == nil
|
||||
}
|
||||
|
||||
func (c *Client) setCache(key string, data interface{}, ttl time.Duration) {
|
||||
func (c *Client) setCache(key string, data any, ttl time.Duration) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
|
||||
defer cancel()
|
||||
|
||||
@@ -72,10 +72,9 @@ func (c *Client) setCache(key string, data interface{}, ttl time.Duration) {
|
||||
})
|
||||
}
|
||||
|
||||
// fetchWithRetry provides robust fetching respecting Jikan's strict 3 req/sec rate limit
|
||||
func (c *Client) fetchWithRetry(urlStr string, out interface{}) error {
|
||||
func (c *Client) fetchWithRetry(urlStr string, out any) error {
|
||||
maxRetries := 5
|
||||
for i := 0; i < maxRetries; i++ {
|
||||
for range maxRetries {
|
||||
c.waitRateLimit()
|
||||
|
||||
resp, err := c.httpClient.Get(urlStr)
|
||||
|
||||
@@ -5,7 +5,6 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
// RecommendationEntry represents a single recommendation
|
||||
type RecommendationEntry struct {
|
||||
Entry struct {
|
||||
MalID int `json:"mal_id"`
|
||||
@@ -24,7 +23,6 @@ type RecommendationsResponse struct {
|
||||
Data []RecommendationEntry `json:"data"`
|
||||
}
|
||||
|
||||
// GetRecommendations fetches recommended anime
|
||||
func (c *Client) GetRecommendations(animeID int, limit int) ([]Anime, error) {
|
||||
cacheKey := fmt.Sprintf("recs:%d", animeID)
|
||||
var cached []Anime
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package jikan
|
||||
|
||||
// findFirstAnimeRelation extracts the first related anime ID for a specific relation type
|
||||
import "maps"
|
||||
|
||||
func findFirstAnimeRelation(groups []JikanRelationGroup, relType string) *int {
|
||||
for _, group := range groups {
|
||||
if group.Relation == relType {
|
||||
@@ -15,7 +16,6 @@ func findFirstAnimeRelation(groups []JikanRelationGroup, relType string) *int {
|
||||
return nil
|
||||
}
|
||||
|
||||
// fetchChain recursively builds the relational chain (Prequels or Sequels)
|
||||
func (c *Client) fetchChain(startID int, direction string, visited map[int]bool) ([]RelationEntry, error) {
|
||||
anime, err := c.GetAnimeByID(startID)
|
||||
if err != nil {
|
||||
@@ -24,7 +24,7 @@ func (c *Client) fetchChain(startID int, direction string, visited map[int]bool)
|
||||
|
||||
nextIDPtr := findFirstAnimeRelation(anime.Relations, direction)
|
||||
if nextIDPtr == nil {
|
||||
return nil, nil // normal end of chain
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
nextID := *nextIDPtr
|
||||
@@ -50,7 +50,6 @@ func (c *Client) fetchChain(startID int, direction string, visited map[int]bool)
|
||||
return append([]RelationEntry{entry}, rest...), nil
|
||||
}
|
||||
|
||||
// GetFullRelations resolves the full Prequel/Sequel chronological chain synchronously
|
||||
func (c *Client) GetFullRelations(id int) ([]RelationEntry, error) {
|
||||
currentAnime, err := c.GetAnimeByID(id)
|
||||
if err != nil {
|
||||
@@ -62,9 +61,7 @@ func (c *Client) GetFullRelations(id int) ([]RelationEntry, error) {
|
||||
prequels, err1 := c.fetchChain(id, "Prequel", visited)
|
||||
|
||||
visitedSeq := make(map[int]bool)
|
||||
for k, v := range visited {
|
||||
visitedSeq[k] = v
|
||||
}
|
||||
maps.Copy(visitedSeq, visited)
|
||||
|
||||
sequels, err2 := c.fetchChain(id, "Sequel", visitedSeq)
|
||||
|
||||
|
||||
@@ -6,7 +6,6 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
// Search returns the anime list with pagination support
|
||||
func (c *Client) Search(query string, page int) (SearchResult, error) {
|
||||
if query == "" {
|
||||
return SearchResult{}, nil
|
||||
@@ -36,7 +35,6 @@ func (c *Client) Search(query string, page int) (SearchResult, error) {
|
||||
return res, nil
|
||||
}
|
||||
|
||||
// GetTopAnime fetches the top anime by popularity (default) or other filters
|
||||
func (c *Client) GetTopAnime(page int) (TopAnimeResult, error) {
|
||||
if page < 1 {
|
||||
page = 1
|
||||
|
||||
@@ -6,14 +6,11 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
// ScheduleResult contains anime grouped by day
|
||||
type ScheduleResult struct {
|
||||
Animes []Anime
|
||||
HasNextPage bool
|
||||
}
|
||||
|
||||
// GetSchedule fetches anime airing on a specific day
|
||||
// day can be: monday, tuesday, wednesday, thursday, friday, saturday, sunday, unknown, other
|
||||
func (c *Client) GetSchedule(day string) (ScheduleResult, error) {
|
||||
day = strings.ToLower(day)
|
||||
cacheKey := fmt.Sprintf("schedule_%s", day)
|
||||
@@ -38,7 +35,6 @@ func (c *Client) GetSchedule(day string) (ScheduleResult, error) {
|
||||
return res, nil
|
||||
}
|
||||
|
||||
// GetFullSchedule fetches all days at once
|
||||
func (c *Client) GetFullSchedule() (map[string][]Anime, error) {
|
||||
days := []string{"monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"}
|
||||
schedule := make(map[string][]Anime)
|
||||
@@ -54,7 +50,6 @@ func (c *Client) GetFullSchedule() (map[string][]Anime, error) {
|
||||
return schedule, nil
|
||||
}
|
||||
|
||||
// GetSeasonsNow fetches currently airing anime
|
||||
func (c *Client) GetSeasonsNow(page int) (TopAnimeResult, error) {
|
||||
if page < 1 {
|
||||
page = 1
|
||||
@@ -80,7 +75,6 @@ func (c *Client) GetSeasonsNow(page int) (TopAnimeResult, error) {
|
||||
return res, nil
|
||||
}
|
||||
|
||||
// GetSeasonsUpcoming fetches upcoming anime
|
||||
func (c *Client) GetSeasonsUpcoming(page int) (TopAnimeResult, error) {
|
||||
if page < 1 {
|
||||
page = 1
|
||||
|
||||
@@ -12,20 +12,17 @@ type TopAnimeResult struct {
|
||||
HasNextPage bool
|
||||
}
|
||||
|
||||
// NamedEntity represents genres, studios, producers, etc.
|
||||
type NamedEntity struct {
|
||||
MalID int `json:"mal_id"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
// Aired represents the airing date range
|
||||
type Aired struct {
|
||||
From string `json:"from"`
|
||||
To string `json:"to"`
|
||||
String string `json:"string"`
|
||||
}
|
||||
|
||||
// Anime struct matching the Jikan v4 API structure
|
||||
type Anime struct {
|
||||
MalID int `json:"mal_id"`
|
||||
Title string `json:"title"`
|
||||
@@ -73,12 +70,10 @@ type Anime struct {
|
||||
Relations []JikanRelationGroup `json:"relations"`
|
||||
}
|
||||
|
||||
// ImageURL returns the webp large image URL
|
||||
func (a Anime) ImageURL() string {
|
||||
return a.Images.Webp.LargeImageURL
|
||||
}
|
||||
|
||||
// ShortRating returns abbreviated rating (e.g., "PG-13" from "PG-13 - Teens 13 or older")
|
||||
func (a Anime) ShortRating() string {
|
||||
if a.Rating == "" {
|
||||
return ""
|
||||
@@ -92,7 +87,6 @@ func (a Anime) ShortRating() string {
|
||||
return a.Rating
|
||||
}
|
||||
|
||||
// ShortDuration returns abbreviated duration (e.g., "23m" from "23 min per ep")
|
||||
func (a Anime) ShortDuration() string {
|
||||
if a.Duration == "" {
|
||||
return ""
|
||||
@@ -112,7 +106,6 @@ func (a Anime) ShortDuration() string {
|
||||
return a.Duration
|
||||
}
|
||||
|
||||
// Premiered returns season + year (e.g., "Fall 2002")
|
||||
func (a Anime) Premiered() string {
|
||||
if a.Season != "" && a.Year > 0 {
|
||||
return fmt.Sprintf("%s %d", a.Season, a.Year)
|
||||
@@ -138,7 +131,6 @@ type TopAnimeResponse struct {
|
||||
Pagination Pagination `json:"pagination"`
|
||||
}
|
||||
|
||||
// Relation Types
|
||||
type JikanRelationEntry struct {
|
||||
MalID int `json:"mal_id"`
|
||||
Type string `json:"type"`
|
||||
@@ -160,7 +152,6 @@ type RelationEntry struct {
|
||||
IsCurrent bool
|
||||
}
|
||||
|
||||
// DisplayTitle prefers English, falls back to Japanese, then standard Title
|
||||
func (a Anime) DisplayTitle() string {
|
||||
if a.TitleEnglish != "" {
|
||||
return a.TitleEnglish
|
||||
|
||||
Reference in New Issue
Block a user