-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathanime.go
More file actions
501 lines (479 loc) · 15.1 KB
/
Copy pathanime.go
File metadata and controls
501 lines (479 loc) · 15.1 KB
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
package jikan
import (
"context"
"fmt"
"net/http"
"net/url"
"strconv"
)
type AnimeService struct {
c *Client
}
type Anime struct {
MalID ID `json:"mal_id"`
URL string `json:"url"`
Images ImageSet `json:"images"`
Trailer struct {
YoutubeID string `json:"youtube_id"`
URL string `json:"url"`
EmbedURL string `json:"embed_url"`
Images struct {
ImageURL string `json:"image_url"`
SmallImageURL string `json:"small_image_url"`
MediumImageURL string `json:"medium_image_url"`
LargeImageURL string `json:"large_image_url"`
MaximumImageURL string `json:"maximum_image_url"`
} `json:"images"`
} `json:"trailer"`
Title string `json:"title"`
TitleEnglish string `json:"title_english"`
TitleJapanese string `json:"title_japanese"`
TitleSynonyms []string `json:"title_synonyms"`
Type string `json:"type"`
Source string `json:"source"`
Episodes int `json:"episodes"`
Status string `json:"status"`
Airing bool `json:"airing"`
Aired DateRange `json:"aired"`
Duration string `json:"duration"`
Rating string `json:"rating"`
Score float64 `json:"score"`
ScoredBy int `json:"scored_by"`
Rank int `json:"rank"`
Popularity int `json:"popularity"`
Members int `json:"members"`
Favorites int `json:"favorites"`
Synopsis string `json:"synopsis"`
Background string `json:"background"`
Season string `json:"season"`
Year int `json:"year"`
Broadcast struct {
Day string `json:"day"`
Time string `json:"time"`
Timezone string `json:"timezone"`
String string `json:"string"`
} `json:"broadcast"`
Producers []Resource `json:"producers"`
Licensors []Resource `json:"licensors"`
Studios []Resource `json:"studios"`
Genres []Resource `json:"genres"`
ExplicitGenres []Resource `json:"explicit_genres"`
Themes []Resource `json:"themes"`
Demographics []Resource `json:"demographics"`
}
func (s *AnimeService) ByID(ctx context.Context, id ID) (*Anime, error) {
var r struct{ Data Anime }
if err := s.c.Do(ctx, http.MethodGet, fmt.Sprintf("/anime/%d", id), nil, &r); err != nil {
return nil, err
}
return &r.Data, nil
}
func (s *AnimeService) Characters(ctx context.Context, id ID) ([]struct {
Character Resource `json:"character"`
Role string `json:"role"`
VoiceActors []struct {
Person Resource `json:"person"`
Language string `json:"language"`
} `json:"voice_actors"`
}, error) {
var r struct {
Data []struct {
Character Resource `json:"character"`
Role string `json:"role"`
VoiceActors []struct {
Person Resource `json:"person"`
Language string `json:"language"`
} `json:"voice_actors"`
}
}
if err := s.c.Do(ctx, http.MethodGet, fmt.Sprintf("/anime/%d/characters", id), nil, &r); err != nil {
return nil, err
}
return r.Data, nil
}
func (s *AnimeService) Staff(ctx context.Context, id ID) ([]struct {
Person Resource `json:"person"`
Positions []string `json:"positions"`
}, error) {
var r struct {
Data []struct {
Person Resource `json:"person"`
Positions []string `json:"positions"`
}
}
if err := s.c.Do(ctx, http.MethodGet, fmt.Sprintf("/anime/%d/staff", id), nil, &r); err != nil {
return nil, err
}
return r.Data, nil
}
func (s *AnimeService) Episodes(ctx context.Context, id ID, page int) ([]struct {
MalID ID `json:"mal_id"`
URL string `json:"url"`
Title string `json:"title"`
TitleJapanese string `json:"title_japanese"`
TitleRomanji string `json:"title_romanji"`
Aired string `json:"aired"`
Score float64 `json:"score"`
Filler bool `json:"filler"`
Recap bool `json:"recap"`
}, *Pagination, error) {
q := url.Values{"page": {strconv.Itoa(page)}}
var r struct {
Data []struct {
MalID ID `json:"mal_id"`
URL string `json:"url"`
Title string `json:"title"`
TitleJapanese string `json:"title_japanese"`
TitleRomanji string `json:"title_romanji"`
Aired string `json:"aired"`
Score float64 `json:"score"`
Filler bool `json:"filler"`
Recap bool `json:"recap"`
} `json:"data"`
Pagination Pagination `json:"pagination"`
}
if err := s.c.Do(ctx, http.MethodGet, fmt.Sprintf("/anime/%d/episodes", id), q, &r); err != nil {
return nil, nil, err
}
return r.Data, &r.Pagination, nil
}
func (s *AnimeService) EpisodeByID(ctx context.Context, animeID ID, episode int) (*struct {
MalID ID `json:"mal_id"`
URL string `json:"url"`
Title string `json:"title"`
TitleJapanese string `json:"title_japanese"`
TitleRomanji string `json:"title_romanji"`
Synopsis string `json:"synopsis"`
Aired string `json:"aired"`
Score float64 `json:"score"`
Filler bool `json:"filler"`
Recap bool `json:"recap"`
}, error) {
var r struct {
Data struct {
MalID ID `json:"mal_id"`
URL string `json:"url"`
Title string `json:"title"`
TitleJapanese string `json:"title_japanese"`
TitleRomanji string `json:"title_romanji"`
Synopsis string `json:"synopsis"`
Aired string `json:"aired"`
Score float64 `json:"score"`
Filler bool `json:"filler"`
Recap bool `json:"recap"`
}
}
if err := s.c.Do(ctx, http.MethodGet, fmt.Sprintf("/anime/%d/episodes/%d", animeID, episode), nil, &r); err != nil {
return nil, err
}
return &r.Data, nil
}
func (s *AnimeService) News(ctx context.Context, id ID, page int) ([]struct {
MalID ID `json:"mal_id"`
Title string `json:"title"`
Date string `json:"date"`
Author string `json:"author_username"`
Comments int `json:"comments"`
Excerpt string `json:"excerpt"`
}, *Pagination, error) {
q := url.Values{"page": {strconv.Itoa(page)}}
var r struct {
Data []struct {
MalID ID `json:"mal_id"`
Title string `json:"title"`
Date string `json:"date"`
Author string `json:"author_username"`
Comments int `json:"comments"`
Excerpt string `json:"excerpt"`
} `json:"data"`
Pagination Pagination `json:"pagination"`
}
if err := s.c.Do(ctx, http.MethodGet, fmt.Sprintf("/anime/%d/news", id), q, &r); err != nil {
return nil, nil, err
}
return r.Data, &r.Pagination, nil
}
type ForumFilter string
const (
ForumFilterAll ForumFilter = "all"
ForumFilterEpisode ForumFilter = "episode"
ForumFilterOther ForumFilter = "other"
)
func (s *AnimeService) Forum(ctx context.Context, id ID, filter ForumFilter) ([]struct {
MalID ID `json:"mal_id"`
Title string `json:"title"`
Date string `json:"date"`
Author string `json:"author_username"`
Comments int `json:"comments"`
}, error) {
q := url.Values{}
if filter != "" {
q.Set("filter", string(filter))
}
var r struct {
Data []struct {
MalID ID `json:"mal_id"`
Title string `json:"title"`
Date string `json:"date"`
Author string `json:"author_username"`
Comments int `json:"comments"`
} `json:"data"`
}
if err := s.c.Do(ctx, http.MethodGet, fmt.Sprintf("/anime/%d/forum", id), q, &r); err != nil {
return nil, err
}
return r.Data, nil
}
func (s *AnimeService) Videos(ctx context.Context, id ID) (*struct {
Promos []struct {
Title string `json:"title"`
Trailer struct {
YoutubeID string `json:"youtube_id"`
URL string `json:"url"`
EmbedURL string `json:"embed_url"`
Images struct {
DefaultImageURL string `json:"default_image_url"`
SmallImageURL string `json:"small_image_url"`
MediumImageURL string `json:"medium_image_url"`
LargeImageURL string `json:"large_image_url"`
MaximumImageURL string `json:"maximum_image_url"`
} `json:"images"`
} `json:"trailer"`
} `json:"promos"`
Episodes []struct {
MalID ID `json:"mal_id"`
URL string `json:"url"`
Title string `json:"title"`
Episode string `json:"episode"`
Images struct {
JPG struct {
ImageURL string `json:"image_url"`
} `json:"jpg"`
} `json:"images"`
} `json:"episodes"`
}, error) {
var r struct {
Data struct {
Promos []struct {
Title string `json:"title"`
Trailer struct {
YoutubeID string `json:"youtube_id"`
URL string `json:"url"`
EmbedURL string `json:"embed_url"`
Images struct {
DefaultImageURL string `json:"default_image_url"`
SmallImageURL string `json:"small_image_url"`
MediumImageURL string `json:"medium_image_url"`
LargeImageURL string `json:"large_image_url"`
MaximumImageURL string `json:"maximum_image_url"`
} `json:"images"`
} `json:"trailer"`
} `json:"promos"`
Episodes []struct {
MalID ID `json:"mal_id"`
URL string `json:"url"`
Title string `json:"title"`
Episode string `json:"episode"`
Images struct {
JPG struct {
ImageURL string `json:"image_url"`
} `json:"jpg"`
} `json:"images"`
} `json:"episodes"`
}
}
if err := s.c.Do(ctx, http.MethodGet, fmt.Sprintf("/anime/%d/videos", id), nil, &r); err != nil {
return nil, err
}
return &r.Data, nil
}
func (s *AnimeService) Pictures(ctx context.Context, id ID) ([]ImageSet, error) {
var r struct {
Data []struct {
Images ImageSet `json:"images"`
} `json:"data"`
}
if err := s.c.Do(ctx, http.MethodGet, fmt.Sprintf("/anime/%d/pictures", id), nil, &r); err != nil {
return nil, err
}
sets := make([]ImageSet, len(r.Data))
for i, d := range r.Data {
sets[i] = d.Images
}
return sets, nil
}
// Statistics returns full stats with score breakdown
func (s *AnimeService) Statistics(ctx context.Context, id ID) (*struct {
Watching int `json:"watching"`
Completed int `json:"completed"`
OnHold int `json:"on_hold"`
Dropped int `json:"dropped"`
PlanToWatch int `json:"plan_to_watch"`
Total int `json:"total"`
Scores []struct {
Score int `json:"score"`
Votes int `json:"votes"`
Percentage float64 `json:"percentage"`
} `json:"scores"`
}, error) {
var r struct {
Data struct {
Watching int `json:"watching"`
Completed int `json:"completed"`
OnHold int `json:"on_hold"`
Dropped int `json:"dropped"`
PlanToWatch int `json:"plan_to_watch"`
Total int `json:"total"`
Scores []struct {
Score int `json:"score"`
Votes int `json:"votes"`
Percentage float64 `json:"percentage"`
} `json:"scores"`
}
}
if err := s.c.Do(ctx, http.MethodGet, fmt.Sprintf("/anime/%d/statistics", id), nil, &r); err != nil {
return nil, err
}
return &r.Data, nil
}
func (s *AnimeService) MoreInfo(ctx context.Context, id ID) (string, error) {
var r struct {
Data struct {
MoreInfo string `json:"moreinfo"`
} `json:"data"`
}
if err := s.c.Do(ctx, http.MethodGet, fmt.Sprintf("/anime/%d/moreinfo", id), nil, &r); err != nil {
return "", err
}
return r.Data.MoreInfo, nil
}
func (s *AnimeService) Recommendations(ctx context.Context, id ID) ([]struct {
Entry Resource `json:"entry"`
URL string `json:"url"`
Votes int `json:"votes"`
}, error) {
var r struct {
Data []struct {
Entry Resource `json:"entry"`
URL string `json:"url"`
Votes int `json:"votes"`
} `json:"data"`
}
if err := s.c.Do(ctx, http.MethodGet, fmt.Sprintf("/anime/%d/recommendations", id), nil, &r); err != nil {
return nil, err
}
return r.Data, nil
}
func (s *AnimeService) UserUpdates(ctx context.Context, id ID, page int) ([]struct {
User Resource `json:"user"`
Score float64 `json:"score"`
Status string `json:"status"`
EpisodesSeen int `json:"episodes_seen"`
EpisodesTotal int `json:"episodes_total"`
Date string `json:"date"`
}, *Pagination, error) {
q := url.Values{"page": {strconv.Itoa(page)}}
var r struct {
Data []struct {
User Resource `json:"user"`
Score float64 `json:"score"`
Status string `json:"status"`
EpisodesSeen int `json:"episodes_seen"`
EpisodesTotal int `json:"episodes_total"`
Date string `json:"date"`
} `json:"data"`
Pagination Pagination `json:"pagination"`
}
if err := s.c.Do(ctx, http.MethodGet, fmt.Sprintf("/anime/%d/userupdates", id), q, &r); err != nil {
return nil, nil, err
}
return r.Data, &r.Pagination, nil
}
func (s *AnimeService) Reviews(ctx context.Context, id ID, page int) ([]struct {
User Resource `json:"user"`
MalID int `json:"mal_id"`
Score int `json:"score"`
Review string `json:"review"`
EpisodesWatched int `json:"episodes_watched"`
Date string `json:"date"`
Votes int `json:"votes"`
Scores struct {
Overall int `json:"overall"`
Story int `json:"story"`
Animation int `json:"animation"`
Sound int `json:"sound"`
Character int `json:"character"`
Enjoyment int `json:"enjoyment"`
} `json:"scores"`
}, *Pagination, error) {
q := url.Values{"page": {strconv.Itoa(page)}}
var r struct {
Data []struct {
User Resource `json:"user"`
MalID int `json:"mal_id"`
Score int `json:"score"`
Review string `json:"review"`
EpisodesWatched int `json:"episodes_watched"`
Date string `json:"date"`
Votes int `json:"votes"`
Scores struct {
Overall int `json:"overall"`
Story int `json:"story"`
Animation int `json:"animation"`
Sound int `json:"sound"`
Character int `json:"character"`
Enjoyment int `json:"enjoyment"`
} `json:"scores"`
} `json:"data"`
Pagination Pagination `json:"pagination"`
}
if err := s.c.Do(ctx, http.MethodGet, fmt.Sprintf("/anime/%d/reviews", id), q, &r); err != nil {
return nil, nil, err
}
return r.Data, &r.Pagination, nil
}
func (s *AnimeService) Relations(ctx context.Context, id ID) ([]struct {
Relation string `json:"relation"`
Entry []Resource `json:"entry"`
}, error) {
var r struct {
Data []struct {
Relation string `json:"relation"`
Entry []Resource `json:"entry"`
} `json:"data"`
}
if err := s.c.Do(ctx, http.MethodGet, fmt.Sprintf("/anime/%d/relations", id), nil, &r); err != nil {
return nil, err
}
return r.Data, nil
}
func (s *AnimeService) Themes(ctx context.Context, id ID) (*struct {
Openings []string `json:"openings"`
Endings []string `json:"endings"`
}, error) {
var r struct {
Data struct {
Openings []string `json:"openings"`
Endings []string `json:"endings"`
}
}
if err := s.c.Do(ctx, http.MethodGet, fmt.Sprintf("/anime/%d/themes", id), nil, &r); err != nil {
return nil, err
}
return &r.Data, nil
}
func (s *AnimeService) External(ctx context.Context, id ID) ([]struct {
Name string `json:"name"`
URL string `json:"url"`
}, error) {
var r struct {
Data []struct {
Name string `json:"name"`
URL string `json:"url"`
} `json:"data"`
}
if err := s.c.Do(ctx, http.MethodGet, fmt.Sprintf("/anime/%d/external", id), nil, &r); err != nil {
return nil, err
}
return r.Data, nil
}