-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmyanimelist.go
More file actions
200 lines (165 loc) · 5.52 KB
/
myanimelist.go
File metadata and controls
200 lines (165 loc) · 5.52 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
package main
import (
"context"
"errors"
"fmt"
"time"
"github.com/nstratos/go-myanimelist/mal"
"golang.org/x/oauth2"
)
var errEmptyMalID = errors.New("mal id is empty")
var animeFields = mal.Fields{
"alternative_titles",
"num_episodes",
"my_list_status",
"start_season",
}
var mangaFields = mal.Fields{
"alternative_titles",
"num_volumes",
"num_chapters",
"my_list_status",
"start_date",
}
type MyAnimeListClient struct {
c *mal.Client
username string
httpTimeout time.Duration
}
func NewMyAnimeListClient(ctx context.Context, oauth *OAuth, username string, httpTimeout time.Duration, verbose bool) *MyAnimeListClient {
httpClient := oauth2.NewClient(ctx, oauth.TokenSource(ctx))
httpClient.Transport = NewRetryableTransport(httpClient, 3)
httpClient.Transport = newLoggingRoundTripper(httpClient.Transport, verbose)
client := mal.NewClient(httpClient)
return &MyAnimeListClient{c: client, username: username, httpTimeout: httpTimeout}
}
func (c *MyAnimeListClient) GetUserAnimeList(ctx context.Context) ([]mal.UserAnime, error) {
return fetchAllPages(
ctx,
"MAL get user anime list",
c.httpTimeout,
func(ctx context.Context, offset int) ([]mal.UserAnime, *mal.Response, error) {
return c.c.User.AnimeList(ctx, c.username, animeFields, mal.Offset(offset), mal.Limit(100))
})
}
func (c *MyAnimeListClient) GetAnimesByName(ctx context.Context, name string) ([]mal.Anime, error) {
ctx, cancel := withTimeout(ctx, c.httpTimeout)
defer cancel()
list, _, err := c.c.Anime.List(ctx, name, animeFields, mal.Limit(3))
return list, err
}
func (c *MyAnimeListClient) GetAnimeByID(ctx context.Context, id int) (*mal.Anime, error) {
if id <= 0 {
return nil, errEmptyMalID
}
ctx, cancel := withTimeout(ctx, c.httpTimeout)
defer cancel()
anime, _, err := c.c.Anime.Details(ctx, id, animeFields)
return anime, err
}
func (c *MyAnimeListClient) UpdateAnimeByIDAndOptions(ctx context.Context, id int, opts []mal.UpdateMyAnimeListStatusOption) error {
if len(opts) == 0 {
return nil
}
// Log update details for debugging
LogDebug(ctx, "[MAL] Updating MAL ID %d with opts: %+v", id, opts)
_, _, err := c.c.Anime.UpdateMyListStatus(ctx, id, opts...)
if err != nil {
return fmt.Errorf("failed to update anime %d: %w", id, err)
}
return nil
}
func (c *MyAnimeListClient) GetUserMangaList(ctx context.Context) ([]mal.UserManga, error) {
return fetchAllPages(
ctx,
"MAL get user manga list",
c.httpTimeout,
func(ctx context.Context, offset int) ([]mal.UserManga, *mal.Response, error) {
return c.c.User.MangaList(ctx, c.username, mangaFields, mal.Offset(offset), mal.Limit(100))
})
}
func (c *MyAnimeListClient) GetMangasByName(ctx context.Context, name string) ([]mal.Manga, error) {
ctx, cancel := withTimeout(ctx, c.httpTimeout)
defer cancel()
list, _, err := c.c.Manga.List(ctx, name, mangaFields, mal.Limit(10))
return list, err
}
func (c *MyAnimeListClient) GetMangaByID(ctx context.Context, id int) (*mal.Manga, error) {
if id <= 0 {
return nil, errEmptyMalID
}
ctx, cancel := withTimeout(ctx, c.httpTimeout)
defer cancel()
manga, _, err := c.c.Manga.Details(ctx, id, mangaFields)
return manga, err
}
func (c *MyAnimeListClient) UpdateMangaByIDAndOptions(ctx context.Context, id int, opts []mal.UpdateMyMangaListStatusOption) error {
if len(opts) == 0 {
return nil
}
_, _, err := c.c.Manga.UpdateMyListStatus(ctx, id, opts...)
if err != nil {
return fmt.Errorf("failed to update manga %d: %w", id, err)
}
return nil
}
// newMyAnimeListOAuth creates MAL OAuth with optional initialization.
func newMyAnimeListOAuth(ctx context.Context, config Config, initWithToken bool) (*OAuth, error) {
verifier := oauth2.GenerateVerifier()
oauthMAL, err := NewOAuth(
config.MyAnimeList,
config.OAuth.RedirectURI,
"myanimelist",
[]oauth2.AuthCodeOption{
oauth2.SetAuthURLParam("code_challenge", verifier),
oauth2.SetAuthURLParam("code_challenge_method", "plain"),
oauth2.VerifierOption(verifier),
},
config.TokenFilePath,
)
if err != nil {
return nil, err
}
return initOAuthIfNeeded(ctx, oauthMAL, config.OAuth.Port, initWithToken)
}
func NewMyAnimeListOAuth(ctx context.Context, config Config) (*OAuth, error) {
return newMyAnimeListOAuth(ctx, config, true)
}
// NewMyAnimeListOAuthWithoutInit creates MAL OAuth without starting auth flow.
// Use InitToken() to manually trigger authentication when needed.
func NewMyAnimeListOAuthWithoutInit(ctx context.Context, config Config) (*OAuth, error) {
return newMyAnimeListOAuth(ctx, config, false)
}
// fetchAllPages fetches all pages from a paginated MAL API endpoint using retry logic with timeout.
// The fetch function should return a page of items along with the pagination response.
// Verbose logs are printed to show pagination progress.
func fetchAllPages[T any](
ctx context.Context,
operationName string,
timeout time.Duration,
fetch func(ctx context.Context, offset int) ([]T, *mal.Response, error),
) ([]T, error) {
var result []T
offset := 0
pageNum := 1
for {
var items []T
var resp *mal.Response
ctx, cancel := withTimeout(ctx, timeout)
var err error
items, resp, err = fetch(ctx, offset)
cancel()
if err != nil {
return nil, err
}
LogDebug(ctx, "[MAL] %s: fetched page %d with %d items (next offset: %d)", operationName, pageNum, len(items), resp.NextOffset)
result = append(result, items...)
if resp.NextOffset == 0 {
LogDebug(ctx, "[MAL] %s: finished pagination, total %d items across %d page(s)", operationName, len(result), pageNum)
break
}
offset = resp.NextOffset
pageNum++
}
return result, nil
}