1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
|
package jikan
import (
"context"
"encoding/json"
"fmt"
"io"
"math"
"metachan/utils/ratelimit"
"net/http"
"os/exec"
"strconv"
"time"
)
var (
// Global Jikan rate limiters
jikanPerSecLimiter = ratelimit.NewRateLimiter(3, time.Second)
jikanPerMinLimiter = ratelimit.NewRateLimiter(60, time.Minute)
jikanLimiter = ratelimit.NewMultiLimiter(jikanPerSecLimiter, jikanPerMinLimiter)
)
// JikanClient provides methods to interact with the Jikan API
type JikanClient struct {
client *http.Client
maxRetries int
baseBackoff time.Duration
}
// NewJikanClient creates a new Jikan API client
func NewJikanClient() *JikanClient {
return &JikanClient{
client: &http.Client{
Timeout: 15 * time.Second,
},
maxRetries: 3,
baseBackoff: 1 * time.Second,
}
}
// WaitForRateLimit waits until a request can be made according to rate limiting rules
func (c *JikanClient) WaitForRateLimit() {
jikanLimiter.Wait()
}
// makeRequest makes an HTTP request with retries and proper error handling
func (c *JikanClient) makeRequest(ctx context.Context, url string) ([]byte, error) {
var bodyBytes []byte
var statusCode int
retries := 0
for retries <= c.maxRetries {
// Wait for rate limiter before attempting request
c.WaitForRateLimit()
// Create the request with timeout context
req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
if err != nil {
return nil, fmt.Errorf("failed to create request: %w", err)
}
// Execute the request
resp, err := c.client.Do(req)
if err != nil {
if retries < c.maxRetries {
retries++
backoffTime := time.Duration(float64(c.baseBackoff) * math.Pow(2, float64(retries-1)))
time.Sleep(backoffTime)
continue
}
return nil, fmt.Errorf("failed to execute request after %d retries: %w", c.maxRetries, err)
}
defer resp.Body.Close()
statusCode = resp.StatusCode
// Handle rate limiting with exponential backoff
if statusCode == http.StatusTooManyRequests {
if retries < c.maxRetries {
retries++
backoffTime := time.Duration(float64(c.baseBackoff) * math.Pow(1.5, float64(retries-1)))
// Respect Retry-After header if available
if retryAfter := resp.Header.Get("Retry-After"); retryAfter != "" {
if seconds, err := strconv.Atoi(retryAfter); err == nil {
backoffTime = time.Duration(seconds) * time.Second
}
}
time.Sleep(backoffTime)
continue
}
return nil, fmt.Errorf("rate limited after %d retries", c.maxRetries)
} else if statusCode != http.StatusOK {
if retries < c.maxRetries {
retries++
backoffTime := time.Duration(float64(c.baseBackoff) * math.Pow(2, float64(retries-1)))
time.Sleep(backoffTime)
continue
}
return nil, fmt.Errorf("request failed with status: %d", statusCode)
}
// Limit response body size to prevent memory issues
bodyBytes, err = io.ReadAll(io.LimitReader(resp.Body, 10*1024*1024)) // 10MB limit
if err != nil {
if retries < c.maxRetries {
retries++
backoffTime := time.Duration(float64(c.baseBackoff) * math.Pow(2, float64(retries-1)))
time.Sleep(backoffTime)
continue
}
return nil, fmt.Errorf("failed to read response body: %w", err)
}
// Success, break the retry loop
return bodyBytes, nil
}
return nil, fmt.Errorf("exhausted all retries with status code: %d", statusCode)
}
// GetAnime fetches basic anime information by MAL ID
func (c *JikanClient) GetAnime(malID int) (*JikanAnimeResponse, error) {
apiURL := fmt.Sprintf("https://api.jikan.moe/v4/anime/%d", malID)
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
bodyBytes, err := c.makeRequest(ctx, apiURL)
if err != nil {
return nil, fmt.Errorf("failed to get anime data: %w", err)
}
var animeResponse JikanAnimeResponse
if err := json.Unmarshal(bodyBytes, &animeResponse); err != nil {
return nil, fmt.Errorf("failed to decode response: %w", err)
}
if animeResponse.Data.MALID == 0 {
return nil, fmt.Errorf("no data found for MAL ID %d", malID)
}
return &animeResponse, nil
}
// GetFullAnime fetches detailed anime information by MAL ID
func (c *JikanClient) GetFullAnime(malID int) (*JikanAnimeResponse, error) {
apiURL := fmt.Sprintf("https://api.jikan.moe/v4/anime/%d/full", malID)
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
bodyBytes, err := c.makeRequest(ctx, apiURL)
if err != nil {
// Fallback to curl if HTTP client fails
var curlErr error
bodyBytes, curlErr = c.makeRequestWithCurl(apiURL)
if curlErr != nil {
return nil, fmt.Errorf("failed to get anime full data via HTTP (%w) and curl (%v)", err, curlErr)
}
}
var animeResponse JikanAnimeResponse
if err := json.Unmarshal(bodyBytes, &animeResponse); err != nil {
return nil, fmt.Errorf("failed to decode response: %w", err)
}
if animeResponse.Data.MALID == 0 {
return nil, fmt.Errorf("no data found for MAL ID %d", malID)
}
return &animeResponse, nil
}
// makeRequestWithCurl uses curl as a fallback when Go HTTP client fails
func (c *JikanClient) makeRequestWithCurl(url string) ([]byte, error) {
c.WaitForRateLimit()
cmd := exec.Command("curl", "-s", "-H", "Accept: application/json", url)
output, err := cmd.Output()
if err != nil {
return nil, fmt.Errorf("curl command failed: %w", err)
}
return output, nil
}
// GetAnimeEpisodes fetches all episodes for an anime by MAL ID
func (c *JikanClient) GetAnimeEpisodes(malID int) (*JikanAnimeEpisodeResponse, error) {
result := JikanAnimeEpisodeResponse{
Data: []JikanAnimeEpisode{},
}
maxPages := 25 // Safety limit to avoid excessive requests
page := 1
maxAttempts := 15 // Maximum number of attempts across all pages
totalAttempts := 0
for page <= maxPages && totalAttempts < maxAttempts {
apiURL := fmt.Sprintf("https://api.jikan.moe/v4/anime/%d/episodes?page=%d", malID, page)
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
totalAttempts++
bodyBytes, err := c.makeRequest(ctx, apiURL)
cancel()
if err != nil {
// If we have some episodes already, return them rather than failing
if len(result.Data) > 0 {
result.Pagination.HasNextPage = false
break
}
return nil, fmt.Errorf("failed to get anime episodes page %d: %w", page, err)
}
var pageResponse JikanAnimeEpisodeResponse
if err := json.Unmarshal(bodyBytes, &pageResponse); err != nil {
// Return what we have if we got some pages successfully
if len(result.Data) > 0 {
result.Pagination.HasNextPage = false
break
}
return nil, fmt.Errorf("failed to decode episodes response: %w", err)
}
// Append episodes from this page
result.Data = append(result.Data, pageResponse.Data...)
result.Pagination = pageResponse.Pagination
// Check if we need to fetch more pages
if !pageResponse.Pagination.HasNextPage {
break
}
page++
}
return &result, nil
}
// GetAnimeCharacters fetches all characters for an anime by MAL ID
func (c *JikanClient) GetAnimeCharacters(malID int) (*JikanAnimeCharacterResponse, error) {
apiURL := fmt.Sprintf("https://api.jikan.moe/v4/anime/%d/characters", malID)
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
bodyBytes, err := c.makeRequest(ctx, apiURL)
if err != nil {
return nil, fmt.Errorf("failed to get anime characters: %w", err)
}
var characterResponse JikanAnimeCharacterResponse
if err := json.Unmarshal(bodyBytes, &characterResponse); err != nil {
return nil, fmt.Errorf("failed to decode characters response: %w", err)
}
return &characterResponse, nil
}
|