@aronnaxlin/bgm-cli 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +660 -0
- package/README.en.md +122 -0
- package/README.md +127 -0
- package/README.zh-TW.md +122 -0
- package/package.json +41 -0
- package/src/cli.js +993 -0
- package/src/commands/blog.js +213 -0
- package/src/commands/calendar.js +32 -0
- package/src/commands/character.js +291 -0
- package/src/commands/collection.js +418 -0
- package/src/commands/episode.js +379 -0
- package/src/commands/group.js +488 -0
- package/src/commands/index.js +362 -0
- package/src/commands/notify.js +74 -0
- package/src/commands/person.js +297 -0
- package/src/commands/status.js +74 -0
- package/src/commands/subject.js +537 -0
- package/src/commands/timeline.js +288 -0
- package/src/commands/trending.js +58 -0
- package/src/commands/tui.js +1537 -0
- package/src/commands/user.js +83 -0
- package/src/core/client.js +2065 -0
- package/src/core/config.js +450 -0
- package/src/core/http.js +144 -0
- package/src/core/output.js +3888 -0
- package/src/core/proxy.js +47 -0
- package/src/core/reactions.js +62 -0
- package/src/core/turnstile.js +526 -0
- package/src/data/bangumi-emotes.json +311 -0
- package/src/utils/args.js +113 -0
- package/src/utils/auth-flow.js +373 -0
- package/src/utils/auth.js +90 -0
- package/src/utils/calendar.js +23 -0
- package/src/utils/collection-ops.js +333 -0
- package/src/utils/collection.js +240 -0
- package/src/utils/formatters.js +157 -0
- package/src/utils/helpers.js +283 -0
- package/src/utils/hot.js +182 -0
- package/src/utils/install.js +40 -0
- package/src/utils/prompts.js +87 -0
- package/src/utils/relay.js +74 -0
- package/src/utils/status.js +68 -0
- package/src/utils/tui-interactive.js +197 -0
- package/src/utils/tui-render.js +86 -0
- package/src/utils/turnstile-flow.js +154 -0
- package/src/utils/validators.js +412 -0
|
@@ -0,0 +1,2065 @@
|
|
|
1
|
+
import { BangumiApiError, requestJson, requestJsonWithMeta, requestText } from "./http.js";
|
|
2
|
+
import { CommandError } from "./output.js";
|
|
3
|
+
import { normalizeBangumiReactionValue } from "./reactions.js";
|
|
4
|
+
import { extractPrivateSessionId, fallbackUserAgent, deriveDeveloperId } from "../utils/auth.js";
|
|
5
|
+
|
|
6
|
+
const PRIVATE_API_BASE_URL = "https://next.bgm.tv";
|
|
7
|
+
const OAUTH_BASE_URL = "https://bgm.tv";
|
|
8
|
+
const STATUS_BASE_URL = "https://bgm-status.ry.mk";
|
|
9
|
+
const STATUS_FEED_URL = `${STATUS_BASE_URL}/api/feed.atom`;
|
|
10
|
+
const STATUS_API_URL = `${STATUS_BASE_URL}/api/status`;
|
|
11
|
+
const DEFAULT_P1_SUBJECT_PAGE_SIZE = 24;
|
|
12
|
+
|
|
13
|
+
export class BangumiClient {
|
|
14
|
+
constructor(config = {}) {
|
|
15
|
+
this.config = config;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
async getMe() {
|
|
19
|
+
return this.request("/p1/me", {
|
|
20
|
+
auth: true,
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
async login({ email, password, turnstileToken } = {}) {
|
|
25
|
+
const normalizedEmail = typeof email === "string" ? email.trim() : "";
|
|
26
|
+
if (!normalizedEmail) {
|
|
27
|
+
throw new CommandError("Missing email. Pass --email or enter it at the login prompt.");
|
|
28
|
+
}
|
|
29
|
+
if (!password) {
|
|
30
|
+
throw new CommandError("Missing password. Pass --password or enter it at the hidden login prompt.");
|
|
31
|
+
}
|
|
32
|
+
if (!turnstileToken) {
|
|
33
|
+
throw new CommandError("Missing turnstileToken. Run `bgm auth login` in an interactive terminal, or pass --turnstile-token.");
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
let payload;
|
|
37
|
+
let response;
|
|
38
|
+
try {
|
|
39
|
+
({ payload, response } = await requestJsonWithMeta(`${PRIVATE_API_BASE_URL}/p1/login`, {
|
|
40
|
+
method: "POST",
|
|
41
|
+
headers: createHeaders({ ...this.config, privateSessionId: "" }, {
|
|
42
|
+
auth: false,
|
|
43
|
+
path: "/p1/login",
|
|
44
|
+
hasBody: true,
|
|
45
|
+
}),
|
|
46
|
+
body: {
|
|
47
|
+
email: normalizedEmail,
|
|
48
|
+
password,
|
|
49
|
+
turnstileToken,
|
|
50
|
+
},
|
|
51
|
+
}));
|
|
52
|
+
} catch (error) {
|
|
53
|
+
if (error instanceof BangumiApiError) {
|
|
54
|
+
throw new CommandError(formatLoginFailure(error));
|
|
55
|
+
}
|
|
56
|
+
throw error;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const privateSessionId = extractPrivateSessionId(response.headers.get("set-cookie"));
|
|
60
|
+
|
|
61
|
+
return {
|
|
62
|
+
resource: "auth-login",
|
|
63
|
+
user: payload,
|
|
64
|
+
privateSessionId,
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
async logout() {
|
|
69
|
+
return this.request("/p1/logout", {
|
|
70
|
+
method: "POST",
|
|
71
|
+
auth: false,
|
|
72
|
+
body: {},
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
async listNotifications(query = {}) {
|
|
77
|
+
return this.request("/p1/notify", {
|
|
78
|
+
auth: true,
|
|
79
|
+
query,
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
async clearNotifications(ids = []) {
|
|
84
|
+
return this.request("/p1/clear-notify", {
|
|
85
|
+
method: "POST",
|
|
86
|
+
auth: true,
|
|
87
|
+
body: {
|
|
88
|
+
id: ids.length > 0 ? ids : undefined,
|
|
89
|
+
},
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
async getUser(username) {
|
|
94
|
+
if (!username) {
|
|
95
|
+
throw new CommandError("Missing username.");
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
return this.request(`/p1/users/${encodeURIComponent(String(username))}`);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
async getPrivateUser(username) {
|
|
102
|
+
return this.getUser(username);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
async getSubject(subjectId) {
|
|
106
|
+
if (!subjectId) {
|
|
107
|
+
throw new CommandError("Missing subjectId.");
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const subject = await this.request(`/p1/subjects/${encodeURIComponent(String(subjectId))}`);
|
|
111
|
+
return normalizeSubject(subject);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
async getPrivateSubject(subjectId) {
|
|
115
|
+
return this.getSubject(subjectId);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
async getEpisode(episodeId) {
|
|
119
|
+
if (!episodeId) {
|
|
120
|
+
throw new CommandError("Missing episodeId.");
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
const episode = await this.request(`/p1/episodes/${encodeURIComponent(String(episodeId))}`);
|
|
124
|
+
return normalizeEpisode(episode);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
async listEpisodeComments(episodeId) {
|
|
128
|
+
if (!episodeId) {
|
|
129
|
+
throw new CommandError("Missing episodeId.");
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
return this.request(`/p1/episodes/${encodeURIComponent(String(episodeId))}/comments`);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
async createEpisodeComment(episodeId, payload = {}) {
|
|
136
|
+
if (!episodeId) {
|
|
137
|
+
throw new CommandError("Missing episodeId.");
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
return this.request(`/p1/episodes/${encodeURIComponent(String(episodeId))}/comments`, {
|
|
141
|
+
method: "POST",
|
|
142
|
+
auth: true,
|
|
143
|
+
body: payload,
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
async updateEpisodeComment(commentId, payload = {}) {
|
|
148
|
+
if (!commentId) {
|
|
149
|
+
throw new CommandError("Missing commentId.");
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
return this.request(`/p1/episodes/-/comments/${encodeURIComponent(String(commentId))}`, {
|
|
153
|
+
method: "PUT",
|
|
154
|
+
auth: true,
|
|
155
|
+
body: payload,
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
async deleteEpisodeComment(commentId) {
|
|
160
|
+
if (!commentId) {
|
|
161
|
+
throw new CommandError("Missing commentId.");
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
return this.request(`/p1/episodes/-/comments/${encodeURIComponent(String(commentId))}`, {
|
|
165
|
+
method: "DELETE",
|
|
166
|
+
auth: true,
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
async likeEpisodeComment(commentId, value) {
|
|
171
|
+
if (!commentId) {
|
|
172
|
+
throw new CommandError("Missing commentId.");
|
|
173
|
+
}
|
|
174
|
+
const reactionValue = normalizeBangumiReactionValue(value, "episodeComment");
|
|
175
|
+
|
|
176
|
+
return this.request(`/p1/episodes/-/comments/${encodeURIComponent(String(commentId))}/like`, {
|
|
177
|
+
method: "PUT",
|
|
178
|
+
auth: true,
|
|
179
|
+
body: { value: reactionValue },
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
async unlikeEpisodeComment(commentId) {
|
|
184
|
+
if (!commentId) {
|
|
185
|
+
throw new CommandError("Missing commentId.");
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
return this.request(`/p1/episodes/-/comments/${encodeURIComponent(String(commentId))}/like`, {
|
|
189
|
+
method: "DELETE",
|
|
190
|
+
auth: true,
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
async listEpisodes(query) {
|
|
195
|
+
const subjectId = query?.subject_id ?? query?.subjectID ?? query?.subjectId;
|
|
196
|
+
if (!subjectId) {
|
|
197
|
+
throw new CommandError("Missing subjectId.");
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
const result = await this.request(`/p1/subjects/${encodeURIComponent(String(subjectId))}/episodes`, {
|
|
201
|
+
query: {
|
|
202
|
+
type: query?.type,
|
|
203
|
+
limit: query?.limit,
|
|
204
|
+
offset: query?.offset,
|
|
205
|
+
},
|
|
206
|
+
});
|
|
207
|
+
return normalizeEpisodePage(result);
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
async listSubjects(query) {
|
|
211
|
+
return this.listSubjectsByPage(query);
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
async searchSubjects({ limit, offset, keyword, sort, filter }) {
|
|
215
|
+
const result = await this.request("/p1/search/subjects", {
|
|
216
|
+
method: "POST",
|
|
217
|
+
query: { limit, offset },
|
|
218
|
+
body: {
|
|
219
|
+
keyword,
|
|
220
|
+
sort,
|
|
221
|
+
filter: normalizeSubjectSearchFilter(filter),
|
|
222
|
+
},
|
|
223
|
+
});
|
|
224
|
+
return normalizeSubjectPage(result);
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
async listSubjectsByPage(query = {}) {
|
|
228
|
+
const requestedLimit = query.limit ?? undefined;
|
|
229
|
+
const requestedOffset = query.offset ?? 0;
|
|
230
|
+
const sort = query.sort ?? "rank";
|
|
231
|
+
const collected = [];
|
|
232
|
+
let page = Math.floor(requestedOffset / DEFAULT_P1_SUBJECT_PAGE_SIZE) + 1;
|
|
233
|
+
let pageSize = DEFAULT_P1_SUBJECT_PAGE_SIZE;
|
|
234
|
+
let totalPages = null;
|
|
235
|
+
let skipped = 0;
|
|
236
|
+
|
|
237
|
+
while (requestedLimit === undefined || collected.length < requestedLimit) {
|
|
238
|
+
const result = await this.request("/p1/subjects", {
|
|
239
|
+
query: {
|
|
240
|
+
type: query.type,
|
|
241
|
+
sort,
|
|
242
|
+
page,
|
|
243
|
+
cat: query.cat,
|
|
244
|
+
series: query.series,
|
|
245
|
+
year: query.year,
|
|
246
|
+
month: query.month,
|
|
247
|
+
tags: query.tags,
|
|
248
|
+
tagsCat: query.tagsCat,
|
|
249
|
+
},
|
|
250
|
+
});
|
|
251
|
+
|
|
252
|
+
const pageData = Array.isArray(result?.data) ? result.data.map(normalizeSubject) : [];
|
|
253
|
+
if (pageData.length === 0) {
|
|
254
|
+
return {
|
|
255
|
+
data: collected,
|
|
256
|
+
total: estimateP1OffsetTotal(totalPages, pageSize, collected.length),
|
|
257
|
+
limit: requestedLimit,
|
|
258
|
+
offset: requestedOffset,
|
|
259
|
+
};
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
pageSize = pageData.length;
|
|
263
|
+
totalPages = Number.isFinite(result?.total) ? result.total : totalPages;
|
|
264
|
+
|
|
265
|
+
const pageStartOffset = (page - 1) * pageSize;
|
|
266
|
+
const localStart = page === Math.floor(requestedOffset / pageSize) + 1
|
|
267
|
+
? Math.max(0, requestedOffset - pageStartOffset)
|
|
268
|
+
: 0;
|
|
269
|
+
const available = pageData.slice(localStart);
|
|
270
|
+
skipped += localStart;
|
|
271
|
+
const remaining = requestedLimit === undefined ? available.length : requestedLimit - collected.length;
|
|
272
|
+
collected.push(...available.slice(0, remaining));
|
|
273
|
+
|
|
274
|
+
if ((totalPages !== null && page >= totalPages) || (requestedLimit !== undefined && collected.length >= requestedLimit)) {
|
|
275
|
+
break;
|
|
276
|
+
}
|
|
277
|
+
page += 1;
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
return {
|
|
281
|
+
data: collected,
|
|
282
|
+
total: estimateP1OffsetTotal(totalPages, pageSize, requestedOffset + skipped + collected.length),
|
|
283
|
+
limit: requestedLimit,
|
|
284
|
+
offset: requestedOffset,
|
|
285
|
+
};
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
async getCalendar() {
|
|
289
|
+
const data = await this.request("/p1/calendar");
|
|
290
|
+
return normalizePrivateCalendar(data);
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
async listUserFriends(username, query) {
|
|
294
|
+
return this.listUserScopedResource(username, "friends", query);
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
async listUserFollowers(username, query) {
|
|
298
|
+
return this.listUserScopedResource(username, "followers", query);
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
async listUserGroups(username, query) {
|
|
302
|
+
return this.listUserScopedResource(username, "groups", query);
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
async listUserIndexes(username, query) {
|
|
306
|
+
return this.listUserScopedResource(username, "indexes", query);
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
async listUserCharacterCollections(username, query) {
|
|
310
|
+
return this.listUserScopedResource(username, "collections/characters", query);
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
async listUserPersonCollections(username, query) {
|
|
314
|
+
return this.listUserScopedResource(username, "collections/persons", query);
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
async listUserIndexCollections(username, query) {
|
|
318
|
+
return this.listUserScopedResource(username, "collections/indexes", query);
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
async listUserSubjectCollections(username, query) {
|
|
322
|
+
return this.listUserScopedResource(username, "collections/subjects", query);
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
async listUserScopedResource(username, resourcePath, query) {
|
|
326
|
+
if (!username) {
|
|
327
|
+
throw new CommandError("Missing username. Pass a username or log in first.");
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
return this.request(`/p1/users/${encodeURIComponent(String(username))}/${resourcePath}`, {
|
|
331
|
+
query,
|
|
332
|
+
});
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
async searchCharacters({ limit, offset, keyword, filter }) {
|
|
336
|
+
return this.request("/p1/search/characters", {
|
|
337
|
+
method: "POST",
|
|
338
|
+
query: { limit, offset },
|
|
339
|
+
body: {
|
|
340
|
+
keyword,
|
|
341
|
+
filter: Object.keys(filter ?? {}).length > 0 ? filter : undefined,
|
|
342
|
+
},
|
|
343
|
+
});
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
async getCharacter(characterId) {
|
|
347
|
+
return this.getP1Entity("characters", characterId, "characterId");
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
async listCharacterCasts(characterId, query) {
|
|
351
|
+
return this.listP1EntityResource("characters", characterId, "casts", query, "characterId");
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
async listCharacterCollects(characterId, query) {
|
|
355
|
+
return this.listP1EntityResource("characters", characterId, "collects", query, "characterId");
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
async listCharacterComments(characterId, query) {
|
|
359
|
+
return this.listP1EntityResource("characters", characterId, "comments", query, "characterId");
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
async listCharacterIndexes(characterId, query) {
|
|
363
|
+
return this.listP1EntityResource("characters", characterId, "indexes", query, "characterId");
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
async listCharacterPhotos(characterId, query) {
|
|
367
|
+
return this.listP1EntityResource("characters", characterId, "photos", query, "characterId");
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
async listCharacterPhotoPreview(characterId, query) {
|
|
371
|
+
return this.listP1EntityResource("characters", characterId, "photos/preview", query, "characterId");
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
async getCharacterPhoto(characterId, photoId) {
|
|
375
|
+
return this.getP1EntityPhoto("characters", characterId, photoId, "characterId");
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
async listCharacterPhotoComments(characterId, photoId) {
|
|
379
|
+
return this.listP1EntityPhotoComments("characters", characterId, photoId, "characterId");
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
async listCharacterRelations(characterId, query) {
|
|
383
|
+
return this.listP1EntityResource("characters", characterId, "relations", query, "characterId");
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
async searchPersons({ limit, offset, keyword, filter }) {
|
|
387
|
+
return this.request("/p1/search/persons", {
|
|
388
|
+
method: "POST",
|
|
389
|
+
query: { limit, offset },
|
|
390
|
+
body: {
|
|
391
|
+
keyword,
|
|
392
|
+
filter: Object.keys(filter ?? {}).length > 0 ? filter : undefined,
|
|
393
|
+
},
|
|
394
|
+
});
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
async getPerson(personId) {
|
|
398
|
+
return this.getP1Entity("persons", personId, "personId");
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
async listPersonCasts(personId, query) {
|
|
402
|
+
return this.listP1EntityResource("persons", personId, "casts", query, "personId");
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
async listPersonCollects(personId, query) {
|
|
406
|
+
return this.listP1EntityResource("persons", personId, "collects", query, "personId");
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
async listPersonComments(personId, query) {
|
|
410
|
+
return this.listP1EntityResource("persons", personId, "comments", query, "personId");
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
async listPersonIndexes(personId, query) {
|
|
414
|
+
return this.listP1EntityResource("persons", personId, "indexes", query, "personId");
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
async listPersonPhotos(personId, query) {
|
|
418
|
+
return this.listP1EntityResource("persons", personId, "photos", query, "personId");
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
async listPersonPhotoPreview(personId, query) {
|
|
422
|
+
return this.listP1EntityResource("persons", personId, "photos/preview", query, "personId");
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
async getPersonPhoto(personId, photoId) {
|
|
426
|
+
return this.getP1EntityPhoto("persons", personId, photoId, "personId");
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
async listPersonPhotoComments(personId, photoId) {
|
|
430
|
+
return this.listP1EntityPhotoComments("persons", personId, photoId, "personId");
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
async listPersonRelations(personId, query) {
|
|
434
|
+
return this.listP1EntityResource("persons", personId, "relations", query, "personId");
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
async listPersonWorks(personId, query) {
|
|
438
|
+
return this.listP1EntityResource("persons", personId, "works", query, "personId");
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
async listSubjectCharacters(subjectId, query) {
|
|
442
|
+
return this.listP1EntityResource("subjects", subjectId, "characters", query, "subjectId");
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
async listSubjectCollects(subjectId, query) {
|
|
446
|
+
return this.listP1EntityResource("subjects", subjectId, "collects", query, "subjectId");
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
async likeSubjectCollect(collectId, value) {
|
|
450
|
+
if (!collectId) {
|
|
451
|
+
throw new CommandError("Missing collectId.");
|
|
452
|
+
}
|
|
453
|
+
const reactionValue = normalizeBangumiReactionValue(value, "subjectCollect");
|
|
454
|
+
|
|
455
|
+
return this.request(`/p1/subjects/-/collects/${encodeURIComponent(String(collectId))}/like`, {
|
|
456
|
+
method: "PUT",
|
|
457
|
+
auth: true,
|
|
458
|
+
body: { value: reactionValue },
|
|
459
|
+
});
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
async unlikeSubjectCollect(collectId) {
|
|
463
|
+
if (!collectId) {
|
|
464
|
+
throw new CommandError("Missing collectId.");
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
return this.request(`/p1/subjects/-/collects/${encodeURIComponent(String(collectId))}/like`, {
|
|
468
|
+
method: "DELETE",
|
|
469
|
+
auth: true,
|
|
470
|
+
});
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
async listSubjectComments(subjectId, query) {
|
|
474
|
+
return this.listP1EntityResource("subjects", subjectId, "comments", query, "subjectId");
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
async listSubjectIndexes(subjectId, query) {
|
|
478
|
+
return this.listP1EntityResource("subjects", subjectId, "indexes", query, "subjectId");
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
async listSubjectRecommendations(subjectId, query) {
|
|
482
|
+
return this.listP1EntityResource("subjects", subjectId, "recs", query, "subjectId");
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
async listSubjectRelations(subjectId, query) {
|
|
486
|
+
return this.listP1EntityResource("subjects", subjectId, "relations", query, "subjectId");
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
async listSubjectReviews(subjectId, query) {
|
|
490
|
+
return this.listP1EntityResource("subjects", subjectId, "reviews", query, "subjectId");
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
async listSubjectStaffPersons(subjectId, query) {
|
|
494
|
+
return this.listP1EntityResource("subjects", subjectId, "staffs/persons", query, "subjectId");
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
async listSubjectStaffPositions(subjectId, query) {
|
|
498
|
+
return this.listP1EntityResource("subjects", subjectId, "staffs/positions", query, "subjectId");
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
async listSubjectTopics(subjectId, query) {
|
|
502
|
+
return this.listP1EntityResource("subjects", subjectId, "topics", query, "subjectId");
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
async listRecentSubjectTopics(query) {
|
|
506
|
+
return this.request("/p1/subjects/-/topics", {
|
|
507
|
+
query,
|
|
508
|
+
});
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
async getSubjectTopic(topicId) {
|
|
512
|
+
if (!topicId) {
|
|
513
|
+
throw new CommandError("Missing topicId.");
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
return this.request(`/p1/subjects/-/topics/${encodeURIComponent(String(topicId))}`);
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
async createSubjectTopic(subjectId, payload = {}) {
|
|
520
|
+
if (!subjectId) {
|
|
521
|
+
throw new CommandError("Missing subjectId.");
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
return this.request(`/p1/subjects/${encodeURIComponent(String(subjectId))}/topics`, {
|
|
525
|
+
method: "POST",
|
|
526
|
+
auth: true,
|
|
527
|
+
body: payload,
|
|
528
|
+
});
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
async updateSubjectTopic(topicId, payload = {}) {
|
|
532
|
+
if (!topicId) {
|
|
533
|
+
throw new CommandError("Missing topicId.");
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
return this.request(`/p1/subjects/-/topics/${encodeURIComponent(String(topicId))}`, {
|
|
537
|
+
method: "PUT",
|
|
538
|
+
auth: true,
|
|
539
|
+
body: payload,
|
|
540
|
+
});
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
async createSubjectReply(topicId, payload = {}) {
|
|
544
|
+
if (!topicId) {
|
|
545
|
+
throw new CommandError("Missing topicId.");
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
return this.request(`/p1/subjects/-/topics/${encodeURIComponent(String(topicId))}/replies`, {
|
|
549
|
+
method: "POST",
|
|
550
|
+
auth: true,
|
|
551
|
+
body: payload,
|
|
552
|
+
});
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
async getSubjectPost(postId) {
|
|
556
|
+
return this.getTopicPost("subjects", postId);
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
async updateSubjectPost(postId, payload = {}) {
|
|
560
|
+
return this.updateTopicPost("subjects", postId, payload);
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
async deleteSubjectPost(postId) {
|
|
564
|
+
return this.deleteTopicPost("subjects", postId);
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
async likeSubjectPost(postId, value) {
|
|
568
|
+
return this.likeTopicPost("subjects", postId, value);
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
async unlikeSubjectPost(postId) {
|
|
572
|
+
return this.unlikeTopicPost("subjects", postId);
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
async listTrendingSubjects(query) {
|
|
576
|
+
return this.request("/p1/trending/subjects", {
|
|
577
|
+
query,
|
|
578
|
+
});
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
async listTrendingSubjectTopics(query) {
|
|
582
|
+
return this.request("/p1/trending/subjects/topics", {
|
|
583
|
+
query,
|
|
584
|
+
});
|
|
585
|
+
}
|
|
586
|
+
|
|
587
|
+
async getP1Entity(resourcePath, id, label) {
|
|
588
|
+
if (!id) {
|
|
589
|
+
throw new CommandError(`Missing ${label}.`);
|
|
590
|
+
}
|
|
591
|
+
|
|
592
|
+
return this.request(`/p1/${resourcePath}/${encodeURIComponent(String(id))}`);
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
async listP1EntityResource(resourcePath, id, childPath, query, label) {
|
|
596
|
+
if (!id) {
|
|
597
|
+
throw new CommandError(`Missing ${label}.`);
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
return this.request(`/p1/${resourcePath}/${encodeURIComponent(String(id))}/${childPath}`, {
|
|
601
|
+
query,
|
|
602
|
+
});
|
|
603
|
+
}
|
|
604
|
+
|
|
605
|
+
async getP1EntityPhoto(resourcePath, id, photoId, label) {
|
|
606
|
+
if (!id) {
|
|
607
|
+
throw new CommandError(`Missing ${label}.`);
|
|
608
|
+
}
|
|
609
|
+
if (!photoId) {
|
|
610
|
+
throw new CommandError("Missing photoId.");
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
return this.request(`/p1/${resourcePath}/${encodeURIComponent(String(id))}/photos/${encodeURIComponent(String(photoId))}`);
|
|
614
|
+
}
|
|
615
|
+
|
|
616
|
+
async listP1EntityPhotoComments(resourcePath, id, photoId, label) {
|
|
617
|
+
if (!id) {
|
|
618
|
+
throw new CommandError(`Missing ${label}.`);
|
|
619
|
+
}
|
|
620
|
+
if (!photoId) {
|
|
621
|
+
throw new CommandError("Missing photoId.");
|
|
622
|
+
}
|
|
623
|
+
|
|
624
|
+
return this.request(`/p1/${resourcePath}/${encodeURIComponent(String(id))}/photos/${encodeURIComponent(String(photoId))}/comments`);
|
|
625
|
+
}
|
|
626
|
+
|
|
627
|
+
async listGroups(query) {
|
|
628
|
+
return this.request("/p1/groups", {
|
|
629
|
+
query,
|
|
630
|
+
});
|
|
631
|
+
}
|
|
632
|
+
|
|
633
|
+
async getGroup(groupName) {
|
|
634
|
+
if (!groupName) {
|
|
635
|
+
throw new CommandError("Missing groupName.");
|
|
636
|
+
}
|
|
637
|
+
|
|
638
|
+
return this.request(`/p1/groups/${encodeURIComponent(String(groupName))}`);
|
|
639
|
+
}
|
|
640
|
+
|
|
641
|
+
async listGroupMembers(groupName, query) {
|
|
642
|
+
if (!groupName) {
|
|
643
|
+
throw new CommandError("Missing groupName.");
|
|
644
|
+
}
|
|
645
|
+
|
|
646
|
+
return this.request(`/p1/groups/${encodeURIComponent(String(groupName))}/members`, {
|
|
647
|
+
query,
|
|
648
|
+
});
|
|
649
|
+
}
|
|
650
|
+
|
|
651
|
+
async listGroupTopics(groupName, query) {
|
|
652
|
+
if (!groupName) {
|
|
653
|
+
throw new CommandError("Missing groupName.");
|
|
654
|
+
}
|
|
655
|
+
|
|
656
|
+
return this.request(`/p1/groups/${encodeURIComponent(String(groupName))}/topics`, {
|
|
657
|
+
query,
|
|
658
|
+
});
|
|
659
|
+
}
|
|
660
|
+
|
|
661
|
+
async createGroupTopic(groupName, payload = {}) {
|
|
662
|
+
if (!groupName) {
|
|
663
|
+
throw new CommandError("Missing groupName.");
|
|
664
|
+
}
|
|
665
|
+
|
|
666
|
+
return this.request(`/p1/groups/${encodeURIComponent(String(groupName))}/topics`, {
|
|
667
|
+
method: "POST",
|
|
668
|
+
auth: true,
|
|
669
|
+
body: payload,
|
|
670
|
+
});
|
|
671
|
+
}
|
|
672
|
+
|
|
673
|
+
async listRecentGroupTopics(query) {
|
|
674
|
+
return this.request("/p1/groups/-/topics", {
|
|
675
|
+
query,
|
|
676
|
+
});
|
|
677
|
+
}
|
|
678
|
+
|
|
679
|
+
async getGroupTopic(topicId) {
|
|
680
|
+
if (!topicId) {
|
|
681
|
+
throw new CommandError("Missing topicId.");
|
|
682
|
+
}
|
|
683
|
+
|
|
684
|
+
return this.request(`/p1/groups/-/topics/${encodeURIComponent(String(topicId))}`);
|
|
685
|
+
}
|
|
686
|
+
|
|
687
|
+
async updateGroupTopic(topicId, payload = {}) {
|
|
688
|
+
if (!topicId) {
|
|
689
|
+
throw new CommandError("Missing topicId.");
|
|
690
|
+
}
|
|
691
|
+
|
|
692
|
+
return this.request(`/p1/groups/-/topics/${encodeURIComponent(String(topicId))}`, {
|
|
693
|
+
method: "PUT",
|
|
694
|
+
auth: true,
|
|
695
|
+
body: payload,
|
|
696
|
+
});
|
|
697
|
+
}
|
|
698
|
+
|
|
699
|
+
async createGroupReply(topicId, payload = {}) {
|
|
700
|
+
if (!topicId) {
|
|
701
|
+
throw new CommandError("Missing topicId.");
|
|
702
|
+
}
|
|
703
|
+
|
|
704
|
+
return this.request(`/p1/groups/-/topics/${encodeURIComponent(String(topicId))}/replies`, {
|
|
705
|
+
method: "POST",
|
|
706
|
+
auth: true,
|
|
707
|
+
body: payload,
|
|
708
|
+
});
|
|
709
|
+
}
|
|
710
|
+
|
|
711
|
+
async getGroupPost(postId) {
|
|
712
|
+
return this.getTopicPost("groups", postId);
|
|
713
|
+
}
|
|
714
|
+
|
|
715
|
+
async updateGroupPost(postId, payload = {}) {
|
|
716
|
+
return this.updateTopicPost("groups", postId, payload);
|
|
717
|
+
}
|
|
718
|
+
|
|
719
|
+
async deleteGroupPost(postId) {
|
|
720
|
+
return this.deleteTopicPost("groups", postId);
|
|
721
|
+
}
|
|
722
|
+
|
|
723
|
+
async likeGroupPost(postId, value) {
|
|
724
|
+
return this.likeTopicPost("groups", postId, value);
|
|
725
|
+
}
|
|
726
|
+
|
|
727
|
+
async unlikeGroupPost(postId) {
|
|
728
|
+
return this.unlikeTopicPost("groups", postId);
|
|
729
|
+
}
|
|
730
|
+
|
|
731
|
+
async getTopicPost(scope, postId) {
|
|
732
|
+
if (!postId) {
|
|
733
|
+
throw new CommandError("Missing postId.");
|
|
734
|
+
}
|
|
735
|
+
|
|
736
|
+
return this.request(`/p1/${scope}/-/posts/${encodeURIComponent(String(postId))}`);
|
|
737
|
+
}
|
|
738
|
+
|
|
739
|
+
async updateTopicPost(scope, postId, payload = {}) {
|
|
740
|
+
if (!postId) {
|
|
741
|
+
throw new CommandError("Missing postId.");
|
|
742
|
+
}
|
|
743
|
+
|
|
744
|
+
return this.request(`/p1/${scope}/-/posts/${encodeURIComponent(String(postId))}`, {
|
|
745
|
+
method: "PUT",
|
|
746
|
+
auth: true,
|
|
747
|
+
body: payload,
|
|
748
|
+
});
|
|
749
|
+
}
|
|
750
|
+
|
|
751
|
+
async deleteTopicPost(scope, postId) {
|
|
752
|
+
if (!postId) {
|
|
753
|
+
throw new CommandError("Missing postId.");
|
|
754
|
+
}
|
|
755
|
+
|
|
756
|
+
return this.request(`/p1/${scope}/-/posts/${encodeURIComponent(String(postId))}`, {
|
|
757
|
+
method: "DELETE",
|
|
758
|
+
auth: true,
|
|
759
|
+
});
|
|
760
|
+
}
|
|
761
|
+
|
|
762
|
+
async likeTopicPost(scope, postId, value) {
|
|
763
|
+
if (!postId) {
|
|
764
|
+
throw new CommandError("Missing postId.");
|
|
765
|
+
}
|
|
766
|
+
const targetKey = scope === "groups" ? "groupPost" : "subjectPost";
|
|
767
|
+
const reactionValue = normalizeBangumiReactionValue(value, targetKey);
|
|
768
|
+
|
|
769
|
+
return this.request(`/p1/${scope}/-/posts/${encodeURIComponent(String(postId))}/like`, {
|
|
770
|
+
method: "PUT",
|
|
771
|
+
auth: true,
|
|
772
|
+
body: { value: reactionValue },
|
|
773
|
+
});
|
|
774
|
+
}
|
|
775
|
+
|
|
776
|
+
async unlikeTopicPost(scope, postId) {
|
|
777
|
+
if (!postId) {
|
|
778
|
+
throw new CommandError("Missing postId.");
|
|
779
|
+
}
|
|
780
|
+
|
|
781
|
+
return this.request(`/p1/${scope}/-/posts/${encodeURIComponent(String(postId))}/like`, {
|
|
782
|
+
method: "DELETE",
|
|
783
|
+
auth: true,
|
|
784
|
+
});
|
|
785
|
+
}
|
|
786
|
+
|
|
787
|
+
async listUserBlogs(username, query) {
|
|
788
|
+
if (!username) {
|
|
789
|
+
throw new CommandError("Missing username. Pass a username or log in first.");
|
|
790
|
+
}
|
|
791
|
+
|
|
792
|
+
return this.request(`/p1/users/${encodeURIComponent(String(username))}/blogs`, {
|
|
793
|
+
auth: true,
|
|
794
|
+
query,
|
|
795
|
+
});
|
|
796
|
+
}
|
|
797
|
+
|
|
798
|
+
async listTimeline(query) {
|
|
799
|
+
return this.request("/p1/timeline", {
|
|
800
|
+
auth: true,
|
|
801
|
+
query,
|
|
802
|
+
});
|
|
803
|
+
}
|
|
804
|
+
|
|
805
|
+
async listTimelineEvents(query = {}) {
|
|
806
|
+
const { limit, timeoutMs, ...requestQuery } = query;
|
|
807
|
+
return this.requestSseEvents("/p1/timeline/-/events", {
|
|
808
|
+
auth: true,
|
|
809
|
+
query: requestQuery,
|
|
810
|
+
limit,
|
|
811
|
+
timeoutMs,
|
|
812
|
+
});
|
|
813
|
+
}
|
|
814
|
+
|
|
815
|
+
async createTimeline(payload = {}) {
|
|
816
|
+
return this.request("/p1/timeline", {
|
|
817
|
+
method: "POST",
|
|
818
|
+
auth: true,
|
|
819
|
+
body: payload,
|
|
820
|
+
});
|
|
821
|
+
}
|
|
822
|
+
|
|
823
|
+
async listUserTimeline(username, query) {
|
|
824
|
+
if (!username) {
|
|
825
|
+
throw new CommandError("Missing username. Pass a username or log in first.");
|
|
826
|
+
}
|
|
827
|
+
|
|
828
|
+
return this.request(`/p1/users/${encodeURIComponent(String(username))}/timeline`, {
|
|
829
|
+
auth: true,
|
|
830
|
+
query,
|
|
831
|
+
});
|
|
832
|
+
}
|
|
833
|
+
|
|
834
|
+
async listTimelineReplies(timelineId) {
|
|
835
|
+
if (!timelineId) {
|
|
836
|
+
throw new CommandError("Missing timelineId.");
|
|
837
|
+
}
|
|
838
|
+
|
|
839
|
+
return this.request(`/p1/timeline/${encodeURIComponent(String(timelineId))}/replies`, {
|
|
840
|
+
auth: true,
|
|
841
|
+
});
|
|
842
|
+
}
|
|
843
|
+
|
|
844
|
+
async createTimelineReply(timelineId, payload = {}) {
|
|
845
|
+
if (!timelineId) {
|
|
846
|
+
throw new CommandError("Missing timelineId.");
|
|
847
|
+
}
|
|
848
|
+
|
|
849
|
+
return this.request(`/p1/timeline/${encodeURIComponent(String(timelineId))}/replies`, {
|
|
850
|
+
method: "POST",
|
|
851
|
+
auth: true,
|
|
852
|
+
body: payload,
|
|
853
|
+
});
|
|
854
|
+
}
|
|
855
|
+
|
|
856
|
+
async deleteTimeline(timelineId) {
|
|
857
|
+
if (!timelineId) {
|
|
858
|
+
throw new CommandError("Missing timelineId.");
|
|
859
|
+
}
|
|
860
|
+
|
|
861
|
+
return this.request(`/p1/timeline/${encodeURIComponent(String(timelineId))}`, {
|
|
862
|
+
method: "DELETE",
|
|
863
|
+
auth: true,
|
|
864
|
+
});
|
|
865
|
+
}
|
|
866
|
+
|
|
867
|
+
async likeTimeline(timelineId, value) {
|
|
868
|
+
if (!timelineId) {
|
|
869
|
+
throw new CommandError("Missing timelineId.");
|
|
870
|
+
}
|
|
871
|
+
const reactionValue = normalizeBangumiReactionValue(value, "timeline");
|
|
872
|
+
|
|
873
|
+
return this.request(`/p1/timeline/${encodeURIComponent(String(timelineId))}/like`, {
|
|
874
|
+
method: "PUT",
|
|
875
|
+
auth: true,
|
|
876
|
+
body: { value: reactionValue },
|
|
877
|
+
});
|
|
878
|
+
}
|
|
879
|
+
|
|
880
|
+
async unlikeTimeline(timelineId) {
|
|
881
|
+
if (!timelineId) {
|
|
882
|
+
throw new CommandError("Missing timelineId.");
|
|
883
|
+
}
|
|
884
|
+
|
|
885
|
+
return this.request(`/p1/timeline/${encodeURIComponent(String(timelineId))}/like`, {
|
|
886
|
+
method: "DELETE",
|
|
887
|
+
auth: true,
|
|
888
|
+
});
|
|
889
|
+
}
|
|
890
|
+
|
|
891
|
+
async getBlogEntry(entryId) {
|
|
892
|
+
if (!entryId) {
|
|
893
|
+
throw new CommandError("Missing entryId.");
|
|
894
|
+
}
|
|
895
|
+
|
|
896
|
+
return this.request(`/p1/blogs/${encodeURIComponent(String(entryId))}`, {
|
|
897
|
+
auth: true,
|
|
898
|
+
});
|
|
899
|
+
}
|
|
900
|
+
|
|
901
|
+
async listBlogComments(entryId) {
|
|
902
|
+
if (!entryId) {
|
|
903
|
+
throw new CommandError("Missing entryId.");
|
|
904
|
+
}
|
|
905
|
+
|
|
906
|
+
return this.request(`/p1/blogs/${encodeURIComponent(String(entryId))}/comments`, {
|
|
907
|
+
auth: true,
|
|
908
|
+
});
|
|
909
|
+
}
|
|
910
|
+
|
|
911
|
+
async createBlogComment(entryId, payload = {}) {
|
|
912
|
+
if (!entryId) {
|
|
913
|
+
throw new CommandError("Missing entryId.");
|
|
914
|
+
}
|
|
915
|
+
|
|
916
|
+
return this.request(`/p1/blogs/${encodeURIComponent(String(entryId))}/comments`, {
|
|
917
|
+
method: "POST",
|
|
918
|
+
auth: true,
|
|
919
|
+
body: payload,
|
|
920
|
+
});
|
|
921
|
+
}
|
|
922
|
+
|
|
923
|
+
async updateBlogComment(commentId, payload = {}) {
|
|
924
|
+
if (!commentId) {
|
|
925
|
+
throw new CommandError("Missing commentId.");
|
|
926
|
+
}
|
|
927
|
+
|
|
928
|
+
return this.request(`/p1/blogs/-/comments/${encodeURIComponent(String(commentId))}`, {
|
|
929
|
+
method: "PUT",
|
|
930
|
+
auth: true,
|
|
931
|
+
body: payload,
|
|
932
|
+
});
|
|
933
|
+
}
|
|
934
|
+
|
|
935
|
+
async deleteBlogComment(commentId) {
|
|
936
|
+
if (!commentId) {
|
|
937
|
+
throw new CommandError("Missing commentId.");
|
|
938
|
+
}
|
|
939
|
+
|
|
940
|
+
return this.request(`/p1/blogs/-/comments/${encodeURIComponent(String(commentId))}`, {
|
|
941
|
+
method: "DELETE",
|
|
942
|
+
auth: true,
|
|
943
|
+
});
|
|
944
|
+
}
|
|
945
|
+
|
|
946
|
+
async createCharacterComment(characterId, payload = {}) {
|
|
947
|
+
return this.createP1EntityComment("characters", characterId, "characterId", payload);
|
|
948
|
+
}
|
|
949
|
+
|
|
950
|
+
async updateCharacterComment(commentId, payload = {}) {
|
|
951
|
+
return this.updateP1EntityComment("characters", commentId, payload);
|
|
952
|
+
}
|
|
953
|
+
|
|
954
|
+
async deleteCharacterComment(commentId) {
|
|
955
|
+
return this.deleteP1EntityComment("characters", commentId);
|
|
956
|
+
}
|
|
957
|
+
|
|
958
|
+
async createPersonComment(personId, payload = {}) {
|
|
959
|
+
return this.createP1EntityComment("persons", personId, "personId", payload);
|
|
960
|
+
}
|
|
961
|
+
|
|
962
|
+
async updatePersonComment(commentId, payload = {}) {
|
|
963
|
+
return this.updateP1EntityComment("persons", commentId, payload);
|
|
964
|
+
}
|
|
965
|
+
|
|
966
|
+
async deletePersonComment(commentId) {
|
|
967
|
+
return this.deleteP1EntityComment("persons", commentId);
|
|
968
|
+
}
|
|
969
|
+
|
|
970
|
+
async createP1EntityComment(resourcePath, id, label, payload = {}) {
|
|
971
|
+
if (!id) {
|
|
972
|
+
throw new CommandError(`Missing ${label}.`);
|
|
973
|
+
}
|
|
974
|
+
|
|
975
|
+
return this.request(`/p1/${resourcePath}/${encodeURIComponent(String(id))}/comments`, {
|
|
976
|
+
method: "POST",
|
|
977
|
+
auth: true,
|
|
978
|
+
body: payload,
|
|
979
|
+
});
|
|
980
|
+
}
|
|
981
|
+
|
|
982
|
+
async updateP1EntityComment(resourcePath, commentId, payload = {}) {
|
|
983
|
+
if (!commentId) {
|
|
984
|
+
throw new CommandError("Missing commentId.");
|
|
985
|
+
}
|
|
986
|
+
|
|
987
|
+
return this.request(`/p1/${resourcePath}/-/comments/${encodeURIComponent(String(commentId))}`, {
|
|
988
|
+
method: "PUT",
|
|
989
|
+
auth: true,
|
|
990
|
+
body: payload,
|
|
991
|
+
});
|
|
992
|
+
}
|
|
993
|
+
|
|
994
|
+
async deleteP1EntityComment(resourcePath, commentId) {
|
|
995
|
+
if (!commentId) {
|
|
996
|
+
throw new CommandError("Missing commentId.");
|
|
997
|
+
}
|
|
998
|
+
|
|
999
|
+
return this.request(`/p1/${resourcePath}/-/comments/${encodeURIComponent(String(commentId))}`, {
|
|
1000
|
+
method: "DELETE",
|
|
1001
|
+
auth: true,
|
|
1002
|
+
});
|
|
1003
|
+
}
|
|
1004
|
+
|
|
1005
|
+
async listBlogPhotos(entryId, query) {
|
|
1006
|
+
if (!entryId) {
|
|
1007
|
+
throw new CommandError("Missing entryId.");
|
|
1008
|
+
}
|
|
1009
|
+
|
|
1010
|
+
return this.request(`/p1/blogs/${encodeURIComponent(String(entryId))}/photos`, {
|
|
1011
|
+
auth: true,
|
|
1012
|
+
query,
|
|
1013
|
+
});
|
|
1014
|
+
}
|
|
1015
|
+
|
|
1016
|
+
async listBlogSubjects(entryId) {
|
|
1017
|
+
if (!entryId) {
|
|
1018
|
+
throw new CommandError("Missing entryId.");
|
|
1019
|
+
}
|
|
1020
|
+
|
|
1021
|
+
return this.request(`/p1/blogs/${encodeURIComponent(String(entryId))}/subjects`, {
|
|
1022
|
+
auth: true,
|
|
1023
|
+
});
|
|
1024
|
+
}
|
|
1025
|
+
|
|
1026
|
+
async createIndex(payload = {}) {
|
|
1027
|
+
return this.request("/p1/indexes", {
|
|
1028
|
+
method: "POST",
|
|
1029
|
+
auth: true,
|
|
1030
|
+
body: payload,
|
|
1031
|
+
});
|
|
1032
|
+
}
|
|
1033
|
+
|
|
1034
|
+
async getIndex(indexId) {
|
|
1035
|
+
if (!indexId) {
|
|
1036
|
+
throw new CommandError("Missing indexId.");
|
|
1037
|
+
}
|
|
1038
|
+
|
|
1039
|
+
return this.request(`/p1/indexes/${encodeURIComponent(String(indexId))}`, {
|
|
1040
|
+
auth: true,
|
|
1041
|
+
});
|
|
1042
|
+
}
|
|
1043
|
+
|
|
1044
|
+
async updateIndex(indexId, payload = {}) {
|
|
1045
|
+
if (!indexId) {
|
|
1046
|
+
throw new CommandError("Missing indexId.");
|
|
1047
|
+
}
|
|
1048
|
+
|
|
1049
|
+
return this.request(`/p1/indexes/${encodeURIComponent(String(indexId))}`, {
|
|
1050
|
+
method: "PATCH",
|
|
1051
|
+
auth: true,
|
|
1052
|
+
body: payload,
|
|
1053
|
+
});
|
|
1054
|
+
}
|
|
1055
|
+
|
|
1056
|
+
async deleteIndex(indexId) {
|
|
1057
|
+
if (!indexId) {
|
|
1058
|
+
throw new CommandError("Missing indexId.");
|
|
1059
|
+
}
|
|
1060
|
+
|
|
1061
|
+
return this.request(`/p1/indexes/${encodeURIComponent(String(indexId))}`, {
|
|
1062
|
+
method: "DELETE",
|
|
1063
|
+
auth: true,
|
|
1064
|
+
});
|
|
1065
|
+
}
|
|
1066
|
+
|
|
1067
|
+
async listIndexComments(indexId) {
|
|
1068
|
+
if (!indexId) {
|
|
1069
|
+
throw new CommandError("Missing indexId.");
|
|
1070
|
+
}
|
|
1071
|
+
|
|
1072
|
+
return this.request(`/p1/indexes/${encodeURIComponent(String(indexId))}/comments`, {
|
|
1073
|
+
auth: true,
|
|
1074
|
+
});
|
|
1075
|
+
}
|
|
1076
|
+
|
|
1077
|
+
async createIndexComment(indexId, payload = {}) {
|
|
1078
|
+
if (!indexId) {
|
|
1079
|
+
throw new CommandError("Missing indexId.");
|
|
1080
|
+
}
|
|
1081
|
+
|
|
1082
|
+
return this.request(`/p1/indexes/${encodeURIComponent(String(indexId))}/comments`, {
|
|
1083
|
+
method: "POST",
|
|
1084
|
+
auth: true,
|
|
1085
|
+
body: payload,
|
|
1086
|
+
});
|
|
1087
|
+
}
|
|
1088
|
+
|
|
1089
|
+
async updateIndexComment(commentId, payload = {}) {
|
|
1090
|
+
if (!commentId) {
|
|
1091
|
+
throw new CommandError("Missing commentId.");
|
|
1092
|
+
}
|
|
1093
|
+
|
|
1094
|
+
return this.request(`/p1/indexes/-/comments/${encodeURIComponent(String(commentId))}`, {
|
|
1095
|
+
method: "PUT",
|
|
1096
|
+
auth: true,
|
|
1097
|
+
body: payload,
|
|
1098
|
+
});
|
|
1099
|
+
}
|
|
1100
|
+
|
|
1101
|
+
async deleteIndexComment(commentId) {
|
|
1102
|
+
if (!commentId) {
|
|
1103
|
+
throw new CommandError("Missing commentId.");
|
|
1104
|
+
}
|
|
1105
|
+
|
|
1106
|
+
return this.request(`/p1/indexes/-/comments/${encodeURIComponent(String(commentId))}`, {
|
|
1107
|
+
method: "DELETE",
|
|
1108
|
+
auth: true,
|
|
1109
|
+
});
|
|
1110
|
+
}
|
|
1111
|
+
|
|
1112
|
+
async listIndexRelated(indexId, query) {
|
|
1113
|
+
if (!indexId) {
|
|
1114
|
+
throw new CommandError("Missing indexId.");
|
|
1115
|
+
}
|
|
1116
|
+
|
|
1117
|
+
return this.request(`/p1/indexes/${encodeURIComponent(String(indexId))}/related`, {
|
|
1118
|
+
auth: true,
|
|
1119
|
+
query,
|
|
1120
|
+
});
|
|
1121
|
+
}
|
|
1122
|
+
|
|
1123
|
+
async addIndexRelated(indexId, payload = {}) {
|
|
1124
|
+
if (!indexId) {
|
|
1125
|
+
throw new CommandError("Missing indexId.");
|
|
1126
|
+
}
|
|
1127
|
+
|
|
1128
|
+
return this.request(`/p1/indexes/${encodeURIComponent(String(indexId))}/related`, {
|
|
1129
|
+
method: "PUT",
|
|
1130
|
+
auth: true,
|
|
1131
|
+
body: payload,
|
|
1132
|
+
});
|
|
1133
|
+
}
|
|
1134
|
+
|
|
1135
|
+
async updateIndexRelated(indexId, relatedId, payload = {}) {
|
|
1136
|
+
if (!indexId) {
|
|
1137
|
+
throw new CommandError("Missing indexId.");
|
|
1138
|
+
}
|
|
1139
|
+
if (!relatedId) {
|
|
1140
|
+
throw new CommandError("Missing relatedId.");
|
|
1141
|
+
}
|
|
1142
|
+
|
|
1143
|
+
return this.request(
|
|
1144
|
+
`/p1/indexes/${encodeURIComponent(String(indexId))}/related/${encodeURIComponent(String(relatedId))}`,
|
|
1145
|
+
{
|
|
1146
|
+
method: "PATCH",
|
|
1147
|
+
auth: true,
|
|
1148
|
+
body: payload,
|
|
1149
|
+
},
|
|
1150
|
+
);
|
|
1151
|
+
}
|
|
1152
|
+
|
|
1153
|
+
async deleteIndexRelated(indexId, relatedId) {
|
|
1154
|
+
if (!indexId) {
|
|
1155
|
+
throw new CommandError("Missing indexId.");
|
|
1156
|
+
}
|
|
1157
|
+
if (!relatedId) {
|
|
1158
|
+
throw new CommandError("Missing relatedId.");
|
|
1159
|
+
}
|
|
1160
|
+
|
|
1161
|
+
return this.request(
|
|
1162
|
+
`/p1/indexes/${encodeURIComponent(String(indexId))}/related/${encodeURIComponent(String(relatedId))}`,
|
|
1163
|
+
{
|
|
1164
|
+
method: "DELETE",
|
|
1165
|
+
auth: true,
|
|
1166
|
+
},
|
|
1167
|
+
);
|
|
1168
|
+
}
|
|
1169
|
+
|
|
1170
|
+
async listCollections(username, query) {
|
|
1171
|
+
if (!username) {
|
|
1172
|
+
throw new CommandError("Missing username. Pass a username or log in first.");
|
|
1173
|
+
}
|
|
1174
|
+
|
|
1175
|
+
const result = await this.request(`/p1/users/${encodeURIComponent(String(username))}/collections/subjects`, {
|
|
1176
|
+
query: normalizeSubjectCollectionQuery(query),
|
|
1177
|
+
});
|
|
1178
|
+
return normalizeSubjectCollectionPage(result);
|
|
1179
|
+
}
|
|
1180
|
+
|
|
1181
|
+
async getUserCollection(username, subjectId) {
|
|
1182
|
+
if (!username) {
|
|
1183
|
+
throw new CommandError("Missing username. Pass a username or log in first.");
|
|
1184
|
+
}
|
|
1185
|
+
if (!subjectId) {
|
|
1186
|
+
throw new CommandError("Missing subjectId.");
|
|
1187
|
+
}
|
|
1188
|
+
|
|
1189
|
+
const subject = await this.getSubject(subjectId);
|
|
1190
|
+
if (subject?.interest) {
|
|
1191
|
+
return normalizeSubjectInterestCollection(subject, subject.interest);
|
|
1192
|
+
}
|
|
1193
|
+
|
|
1194
|
+
const result = await this.listCollections(username, {
|
|
1195
|
+
limit: 100,
|
|
1196
|
+
offset: 0,
|
|
1197
|
+
});
|
|
1198
|
+
const item = Array.isArray(result?.data)
|
|
1199
|
+
? result.data.find((collection) => Number(collection?.subject_id) === Number(subjectId))
|
|
1200
|
+
: null;
|
|
1201
|
+
|
|
1202
|
+
if (!item) {
|
|
1203
|
+
throw new BangumiApiError(`Collection for subject ${subjectId} was not found.`, {
|
|
1204
|
+
status: 404,
|
|
1205
|
+
});
|
|
1206
|
+
}
|
|
1207
|
+
return item;
|
|
1208
|
+
}
|
|
1209
|
+
|
|
1210
|
+
async upsertMyCollection(subjectId, payload = {}) {
|
|
1211
|
+
if (!subjectId) {
|
|
1212
|
+
throw new CommandError("Missing subjectId.");
|
|
1213
|
+
}
|
|
1214
|
+
|
|
1215
|
+
try {
|
|
1216
|
+
return await this.request(`/p1/collections/subjects/${encodeURIComponent(String(subjectId))}`, {
|
|
1217
|
+
method: "PUT",
|
|
1218
|
+
auth: true,
|
|
1219
|
+
body: normalizeSubjectCollectionMutationPayload(payload),
|
|
1220
|
+
});
|
|
1221
|
+
} catch (error) {
|
|
1222
|
+
if (isNoUpdateError(error)) {
|
|
1223
|
+
return {};
|
|
1224
|
+
}
|
|
1225
|
+
throw error;
|
|
1226
|
+
}
|
|
1227
|
+
}
|
|
1228
|
+
|
|
1229
|
+
async patchMyCollection(subjectId, payload = {}) {
|
|
1230
|
+
if (!subjectId) {
|
|
1231
|
+
throw new CommandError("Missing subjectId.");
|
|
1232
|
+
}
|
|
1233
|
+
|
|
1234
|
+
const progressPayload = normalizeSubjectProgressPayload(payload);
|
|
1235
|
+
if (Object.keys(progressPayload).length > 0) {
|
|
1236
|
+
try {
|
|
1237
|
+
return await this.request(`/p1/collections/subjects/${encodeURIComponent(String(subjectId))}`, {
|
|
1238
|
+
method: "PATCH",
|
|
1239
|
+
auth: true,
|
|
1240
|
+
body: progressPayload,
|
|
1241
|
+
});
|
|
1242
|
+
} catch (error) {
|
|
1243
|
+
if (isNoUpdateError(error)) {
|
|
1244
|
+
return {};
|
|
1245
|
+
}
|
|
1246
|
+
throw error;
|
|
1247
|
+
}
|
|
1248
|
+
}
|
|
1249
|
+
|
|
1250
|
+
try {
|
|
1251
|
+
return await this.request(`/p1/collections/subjects/${encodeURIComponent(String(subjectId))}`, {
|
|
1252
|
+
method: "PUT",
|
|
1253
|
+
auth: true,
|
|
1254
|
+
body: normalizeSubjectCollectionMutationPayload(payload),
|
|
1255
|
+
});
|
|
1256
|
+
} catch (error) {
|
|
1257
|
+
if (isNoUpdateError(error)) {
|
|
1258
|
+
return {};
|
|
1259
|
+
}
|
|
1260
|
+
throw error;
|
|
1261
|
+
}
|
|
1262
|
+
}
|
|
1263
|
+
|
|
1264
|
+
async getMyEpisodeCollection(episodeId) {
|
|
1265
|
+
if (!episodeId) {
|
|
1266
|
+
throw new CommandError("Missing episodeId.");
|
|
1267
|
+
}
|
|
1268
|
+
|
|
1269
|
+
const episode = await this.getEpisode(episodeId);
|
|
1270
|
+
if (!episode?.collection) {
|
|
1271
|
+
throw new BangumiApiError(`Episode collection for episode ${episodeId} was not found.`, {
|
|
1272
|
+
status: 404,
|
|
1273
|
+
});
|
|
1274
|
+
}
|
|
1275
|
+
return normalizeEpisodeCollection(episode);
|
|
1276
|
+
}
|
|
1277
|
+
|
|
1278
|
+
async updateMyEpisodeCollection(episodeId, payload = {}) {
|
|
1279
|
+
if (!episodeId) {
|
|
1280
|
+
throw new CommandError("Missing episodeId.");
|
|
1281
|
+
}
|
|
1282
|
+
|
|
1283
|
+
return this.request(`/p1/collections/episodes/${encodeURIComponent(String(episodeId))}`, {
|
|
1284
|
+
method: "PATCH",
|
|
1285
|
+
auth: true,
|
|
1286
|
+
body: normalizeEpisodeCollectionMutationPayload(payload),
|
|
1287
|
+
});
|
|
1288
|
+
}
|
|
1289
|
+
|
|
1290
|
+
async addCharacterCollection(characterId) {
|
|
1291
|
+
return this.updateSimpleCollection("characters", characterId, "characterId", "PUT");
|
|
1292
|
+
}
|
|
1293
|
+
|
|
1294
|
+
async deleteCharacterCollection(characterId) {
|
|
1295
|
+
return this.updateSimpleCollection("characters", characterId, "characterId", "DELETE");
|
|
1296
|
+
}
|
|
1297
|
+
|
|
1298
|
+
async addPersonCollection(personId) {
|
|
1299
|
+
return this.updateSimpleCollection("persons", personId, "personId", "PUT");
|
|
1300
|
+
}
|
|
1301
|
+
|
|
1302
|
+
async deletePersonCollection(personId) {
|
|
1303
|
+
return this.updateSimpleCollection("persons", personId, "personId", "DELETE");
|
|
1304
|
+
}
|
|
1305
|
+
|
|
1306
|
+
async addIndexCollection(indexId) {
|
|
1307
|
+
return this.updateSimpleCollection("indexes", indexId, "indexId", "PUT");
|
|
1308
|
+
}
|
|
1309
|
+
|
|
1310
|
+
async deleteIndexCollection(indexId) {
|
|
1311
|
+
return this.updateSimpleCollection("indexes", indexId, "indexId", "DELETE");
|
|
1312
|
+
}
|
|
1313
|
+
|
|
1314
|
+
async updateSimpleCollection(resourcePath, id, label, method) {
|
|
1315
|
+
if (!id) {
|
|
1316
|
+
throw new CommandError(`Missing ${label}.`);
|
|
1317
|
+
}
|
|
1318
|
+
|
|
1319
|
+
return this.request(`/p1/collections/${resourcePath}/${encodeURIComponent(String(id))}`, {
|
|
1320
|
+
method,
|
|
1321
|
+
auth: true,
|
|
1322
|
+
});
|
|
1323
|
+
}
|
|
1324
|
+
|
|
1325
|
+
async request(path, options = {}) {
|
|
1326
|
+
const headers = createHeaders(this.config, {
|
|
1327
|
+
auth: options.auth,
|
|
1328
|
+
path,
|
|
1329
|
+
hasBody: options.body !== undefined,
|
|
1330
|
+
});
|
|
1331
|
+
return requestJson(`${resolveApiBaseUrl(path)}${path}`, {
|
|
1332
|
+
method: options.method ?? "GET",
|
|
1333
|
+
headers,
|
|
1334
|
+
query: options.query,
|
|
1335
|
+
body: options.body,
|
|
1336
|
+
});
|
|
1337
|
+
}
|
|
1338
|
+
|
|
1339
|
+
async requestSseEvents(path, options = {}) {
|
|
1340
|
+
const headers = createHeaders(this.config, {
|
|
1341
|
+
auth: options.auth,
|
|
1342
|
+
path,
|
|
1343
|
+
});
|
|
1344
|
+
headers.Accept = "text/event-stream";
|
|
1345
|
+
|
|
1346
|
+
const targetUrl = new URL(`${resolveApiBaseUrl(path)}${path}`);
|
|
1347
|
+
for (const [key, value] of Object.entries(options.query ?? {})) {
|
|
1348
|
+
if (value !== undefined && value !== null && value !== "") {
|
|
1349
|
+
targetUrl.searchParams.set(key, String(value));
|
|
1350
|
+
}
|
|
1351
|
+
}
|
|
1352
|
+
|
|
1353
|
+
const controller = new AbortController();
|
|
1354
|
+
const timeoutMs = options.timeoutMs ?? 10000;
|
|
1355
|
+
const timeout = timeoutMs > 0
|
|
1356
|
+
? setTimeout(() => controller.abort(), timeoutMs)
|
|
1357
|
+
: null;
|
|
1358
|
+
const events = [];
|
|
1359
|
+
|
|
1360
|
+
try {
|
|
1361
|
+
const response = await fetch(targetUrl, {
|
|
1362
|
+
method: "GET",
|
|
1363
|
+
headers,
|
|
1364
|
+
signal: controller.signal,
|
|
1365
|
+
});
|
|
1366
|
+
if (!response.ok) {
|
|
1367
|
+
const payload = await response.text();
|
|
1368
|
+
throw new BangumiApiError(payload || response.statusText, {
|
|
1369
|
+
status: response.status,
|
|
1370
|
+
details: payload,
|
|
1371
|
+
});
|
|
1372
|
+
}
|
|
1373
|
+
|
|
1374
|
+
if (!response.body?.getReader) {
|
|
1375
|
+
return events;
|
|
1376
|
+
}
|
|
1377
|
+
|
|
1378
|
+
const reader = response.body.getReader();
|
|
1379
|
+
const decoder = new TextDecoder();
|
|
1380
|
+
let buffer = "";
|
|
1381
|
+
while (true) {
|
|
1382
|
+
const { value, done } = await reader.read();
|
|
1383
|
+
if (done) {
|
|
1384
|
+
break;
|
|
1385
|
+
}
|
|
1386
|
+
buffer += decoder.decode(value, { stream: true });
|
|
1387
|
+
buffer = collectSseEvents(buffer, events, options.limit);
|
|
1388
|
+
if (options.limit !== undefined && events.length >= options.limit) {
|
|
1389
|
+
await reader.cancel();
|
|
1390
|
+
break;
|
|
1391
|
+
}
|
|
1392
|
+
}
|
|
1393
|
+
buffer += decoder.decode();
|
|
1394
|
+
collectSseEvents(`${buffer}\n\n`, events, options.limit);
|
|
1395
|
+
return events;
|
|
1396
|
+
} catch (error) {
|
|
1397
|
+
if (controller.signal.aborted) {
|
|
1398
|
+
return events;
|
|
1399
|
+
}
|
|
1400
|
+
if (error?.name === "AbortError") {
|
|
1401
|
+
return events;
|
|
1402
|
+
}
|
|
1403
|
+
throw error;
|
|
1404
|
+
} finally {
|
|
1405
|
+
if (timeout) {
|
|
1406
|
+
clearTimeout(timeout);
|
|
1407
|
+
}
|
|
1408
|
+
}
|
|
1409
|
+
}
|
|
1410
|
+
}
|
|
1411
|
+
|
|
1412
|
+
function formatLoginFailure(error) {
|
|
1413
|
+
const code = typeof error.details?.code === "string" ? error.details.code : "";
|
|
1414
|
+
const message = typeof error.message === "string" ? error.message : "";
|
|
1415
|
+
|
|
1416
|
+
if (code === "CAPTCHA_ERROR" || /captcha|turnstile/i.test(`${code} ${message}`)) {
|
|
1417
|
+
return "Bangumi login failed: Turnstile verification was rejected or expired. Run `bgm auth login` again to get a fresh Turnstile token.";
|
|
1418
|
+
}
|
|
1419
|
+
|
|
1420
|
+
if ([400, 401, 403, 404].includes(error.status)) {
|
|
1421
|
+
return "Bangumi login failed: the email/account does not exist, the password is incorrect, or this account cannot log in through the private API right now. Check the credentials and try again.";
|
|
1422
|
+
}
|
|
1423
|
+
|
|
1424
|
+
if (!error.status) {
|
|
1425
|
+
return `Bangumi login failed: could not reach the private API login endpoint. ${message}`;
|
|
1426
|
+
}
|
|
1427
|
+
|
|
1428
|
+
return `Bangumi login failed (${error.status}): ${message || "unknown error"}`;
|
|
1429
|
+
}
|
|
1430
|
+
|
|
1431
|
+
export class BangumiStatusClient {
|
|
1432
|
+
constructor(config = {}) {
|
|
1433
|
+
this.config = config;
|
|
1434
|
+
}
|
|
1435
|
+
|
|
1436
|
+
async getCurrentStatus() {
|
|
1437
|
+
return requestJson(STATUS_API_URL, {
|
|
1438
|
+
headers: {
|
|
1439
|
+
Accept: "application/json",
|
|
1440
|
+
"User-Agent": fallbackUserAgent(this.config),
|
|
1441
|
+
},
|
|
1442
|
+
});
|
|
1443
|
+
}
|
|
1444
|
+
|
|
1445
|
+
async listIncidents() {
|
|
1446
|
+
const xml = await requestText(STATUS_FEED_URL, {
|
|
1447
|
+
headers: {
|
|
1448
|
+
Accept: "application/atom+xml, application/xml;q=0.9, text/xml;q=0.8, text/plain;q=0.1",
|
|
1449
|
+
"User-Agent": fallbackUserAgent(this.config),
|
|
1450
|
+
},
|
|
1451
|
+
});
|
|
1452
|
+
|
|
1453
|
+
return parseStatusFeed(xml);
|
|
1454
|
+
}
|
|
1455
|
+
}
|
|
1456
|
+
|
|
1457
|
+
function resolveApiBaseUrl(path) {
|
|
1458
|
+
return PRIVATE_API_BASE_URL;
|
|
1459
|
+
}
|
|
1460
|
+
|
|
1461
|
+
function normalizeSubjectPage(result) {
|
|
1462
|
+
return {
|
|
1463
|
+
...result,
|
|
1464
|
+
data: Array.isArray(result?.data) ? result.data.map(normalizeSubject) : [],
|
|
1465
|
+
};
|
|
1466
|
+
}
|
|
1467
|
+
|
|
1468
|
+
function normalizeEpisodePage(result) {
|
|
1469
|
+
return {
|
|
1470
|
+
...result,
|
|
1471
|
+
data: Array.isArray(result?.data) ? result.data.map(normalizeEpisode) : [],
|
|
1472
|
+
};
|
|
1473
|
+
}
|
|
1474
|
+
|
|
1475
|
+
function normalizeSubject(subject) {
|
|
1476
|
+
if (!subject || typeof subject !== "object") {
|
|
1477
|
+
return subject;
|
|
1478
|
+
}
|
|
1479
|
+
|
|
1480
|
+
const normalized = {
|
|
1481
|
+
...subject,
|
|
1482
|
+
name_cn: subject.name_cn ?? subject.nameCN,
|
|
1483
|
+
date: subject.date ?? subject.airtime?.date,
|
|
1484
|
+
rank: subject.rank ?? subject.rating?.rank,
|
|
1485
|
+
score: subject.score ?? subject.rating?.score,
|
|
1486
|
+
};
|
|
1487
|
+
|
|
1488
|
+
if (subject.interest) {
|
|
1489
|
+
normalized.interest = normalizeSubjectInterest(subject.interest);
|
|
1490
|
+
}
|
|
1491
|
+
|
|
1492
|
+
return normalized;
|
|
1493
|
+
}
|
|
1494
|
+
|
|
1495
|
+
function normalizeEpisode(episode) {
|
|
1496
|
+
if (!episode || typeof episode !== "object") {
|
|
1497
|
+
return episode;
|
|
1498
|
+
}
|
|
1499
|
+
|
|
1500
|
+
return {
|
|
1501
|
+
...episode,
|
|
1502
|
+
subject_id: episode.subject_id ?? episode.subjectID,
|
|
1503
|
+
name_cn: episode.name_cn ?? episode.nameCN,
|
|
1504
|
+
ep: episode.ep ?? episode.sort,
|
|
1505
|
+
subject: normalizeSubject(episode.subject),
|
|
1506
|
+
collection: episode.collection ? normalizeEpisodeCollectionObject(episode.collection) : episode.collection,
|
|
1507
|
+
};
|
|
1508
|
+
}
|
|
1509
|
+
|
|
1510
|
+
function normalizeSubjectInterest(interest) {
|
|
1511
|
+
if (!interest || typeof interest !== "object") {
|
|
1512
|
+
return interest;
|
|
1513
|
+
}
|
|
1514
|
+
|
|
1515
|
+
return {
|
|
1516
|
+
...interest,
|
|
1517
|
+
ep_status: interest.ep_status ?? interest.epStatus,
|
|
1518
|
+
vol_status: interest.vol_status ?? interest.volStatus,
|
|
1519
|
+
updated_at: interest.updated_at ?? interest.updatedAt,
|
|
1520
|
+
};
|
|
1521
|
+
}
|
|
1522
|
+
|
|
1523
|
+
function normalizeSubjectInterestCollection(subject, interest) {
|
|
1524
|
+
const normalizedSubject = normalizeSubject(subject);
|
|
1525
|
+
const normalizedInterest = normalizeSubjectInterest(interest);
|
|
1526
|
+
return {
|
|
1527
|
+
...normalizedInterest,
|
|
1528
|
+
subject_id: normalizedSubject?.id,
|
|
1529
|
+
subject_type: normalizedSubject?.type,
|
|
1530
|
+
subject: normalizedSubject,
|
|
1531
|
+
};
|
|
1532
|
+
}
|
|
1533
|
+
|
|
1534
|
+
function normalizeSubjectCollectionPage(result) {
|
|
1535
|
+
const data = Array.isArray(result?.data)
|
|
1536
|
+
? result.data.map((subject) => {
|
|
1537
|
+
const normalizedSubject = normalizeSubject(subject);
|
|
1538
|
+
return normalizeSubjectInterestCollection(normalizedSubject, normalizedSubject?.interest ?? {});
|
|
1539
|
+
})
|
|
1540
|
+
: [];
|
|
1541
|
+
|
|
1542
|
+
return {
|
|
1543
|
+
...result,
|
|
1544
|
+
data,
|
|
1545
|
+
};
|
|
1546
|
+
}
|
|
1547
|
+
|
|
1548
|
+
function normalizeEpisodeCollection(episode) {
|
|
1549
|
+
const normalizedEpisode = normalizeEpisode(episode);
|
|
1550
|
+
const collection = normalizeEpisodeCollectionObject(normalizedEpisode?.collection);
|
|
1551
|
+
return {
|
|
1552
|
+
...collection,
|
|
1553
|
+
episode: normalizedEpisode,
|
|
1554
|
+
};
|
|
1555
|
+
}
|
|
1556
|
+
|
|
1557
|
+
function normalizeEpisodeCollectionObject(collection) {
|
|
1558
|
+
if (!collection || typeof collection !== "object") {
|
|
1559
|
+
return collection;
|
|
1560
|
+
}
|
|
1561
|
+
|
|
1562
|
+
return {
|
|
1563
|
+
...collection,
|
|
1564
|
+
type: collection.type ?? collection.status,
|
|
1565
|
+
updated_at: collection.updated_at ?? collection.updatedAt,
|
|
1566
|
+
};
|
|
1567
|
+
}
|
|
1568
|
+
|
|
1569
|
+
function normalizeSubjectSearchFilter(filter = {}) {
|
|
1570
|
+
const normalized = {
|
|
1571
|
+
...filter,
|
|
1572
|
+
metaTags: filter.metaTags ?? filter.meta_tags,
|
|
1573
|
+
date: filter.date ?? filter.air_date,
|
|
1574
|
+
};
|
|
1575
|
+
delete normalized.meta_tags;
|
|
1576
|
+
delete normalized.air_date;
|
|
1577
|
+
return Object.keys(normalized).length > 0 ? normalized : undefined;
|
|
1578
|
+
}
|
|
1579
|
+
|
|
1580
|
+
function normalizeSubjectCollectionQuery(query = {}) {
|
|
1581
|
+
return {
|
|
1582
|
+
subjectType: query.subjectType ?? query.subject_type,
|
|
1583
|
+
type: query.type,
|
|
1584
|
+
since: query.since,
|
|
1585
|
+
limit: query.limit,
|
|
1586
|
+
offset: query.offset,
|
|
1587
|
+
};
|
|
1588
|
+
}
|
|
1589
|
+
|
|
1590
|
+
function normalizeSubjectCollectionMutationPayload(payload = {}) {
|
|
1591
|
+
return {
|
|
1592
|
+
type: payload.type,
|
|
1593
|
+
rate: payload.rate,
|
|
1594
|
+
comment: payload.comment,
|
|
1595
|
+
private: payload.private,
|
|
1596
|
+
tags: payload.tags,
|
|
1597
|
+
progress: payload.progress,
|
|
1598
|
+
};
|
|
1599
|
+
}
|
|
1600
|
+
|
|
1601
|
+
function normalizeSubjectProgressPayload(payload = {}) {
|
|
1602
|
+
const normalized = {};
|
|
1603
|
+
const epStatus = payload.epStatus ?? payload.ep_status;
|
|
1604
|
+
const volStatus = payload.volStatus ?? payload.vol_status;
|
|
1605
|
+
|
|
1606
|
+
if (epStatus !== undefined) {
|
|
1607
|
+
normalized.epStatus = epStatus;
|
|
1608
|
+
}
|
|
1609
|
+
if (volStatus !== undefined) {
|
|
1610
|
+
normalized.volStatus = volStatus;
|
|
1611
|
+
}
|
|
1612
|
+
|
|
1613
|
+
return normalized;
|
|
1614
|
+
}
|
|
1615
|
+
|
|
1616
|
+
function normalizeEpisodeCollectionMutationPayload(payload = {}) {
|
|
1617
|
+
return {
|
|
1618
|
+
type: payload.type,
|
|
1619
|
+
batch: payload.batch,
|
|
1620
|
+
};
|
|
1621
|
+
}
|
|
1622
|
+
|
|
1623
|
+
function estimateP1OffsetTotal(totalPages, pageSize, fallback) {
|
|
1624
|
+
if (Number.isFinite(totalPages) && Number.isFinite(pageSize) && totalPages > 0 && pageSize > 0) {
|
|
1625
|
+
return totalPages * pageSize;
|
|
1626
|
+
}
|
|
1627
|
+
return fallback;
|
|
1628
|
+
}
|
|
1629
|
+
|
|
1630
|
+
function collectSseEvents(buffer, events, limit) {
|
|
1631
|
+
const normalized = buffer.replace(/\r\n/g, "\n");
|
|
1632
|
+
const blocks = normalized.split("\n\n");
|
|
1633
|
+
const rest = blocks.pop() ?? "";
|
|
1634
|
+
|
|
1635
|
+
for (const block of blocks) {
|
|
1636
|
+
if (limit !== undefined && events.length >= limit) {
|
|
1637
|
+
break;
|
|
1638
|
+
}
|
|
1639
|
+
const event = parseSseEvent(block);
|
|
1640
|
+
if (event !== undefined) {
|
|
1641
|
+
events.push(event);
|
|
1642
|
+
}
|
|
1643
|
+
}
|
|
1644
|
+
|
|
1645
|
+
return rest;
|
|
1646
|
+
}
|
|
1647
|
+
|
|
1648
|
+
function parseSseEvent(block) {
|
|
1649
|
+
const dataLines = [];
|
|
1650
|
+
let eventName;
|
|
1651
|
+
|
|
1652
|
+
for (const line of block.split("\n")) {
|
|
1653
|
+
if (line.startsWith("event:")) {
|
|
1654
|
+
eventName = line.slice(6).trim();
|
|
1655
|
+
} else if (line.startsWith("data:")) {
|
|
1656
|
+
dataLines.push(line.slice(5).trimStart());
|
|
1657
|
+
}
|
|
1658
|
+
}
|
|
1659
|
+
|
|
1660
|
+
if (dataLines.length === 0) {
|
|
1661
|
+
return undefined;
|
|
1662
|
+
}
|
|
1663
|
+
|
|
1664
|
+
const rawData = dataLines.join("\n");
|
|
1665
|
+
try {
|
|
1666
|
+
const parsed = JSON.parse(rawData);
|
|
1667
|
+
return eventName && !parsed.event ? { event: eventName, ...parsed } : parsed;
|
|
1668
|
+
} catch {
|
|
1669
|
+
return {
|
|
1670
|
+
event: eventName,
|
|
1671
|
+
data: rawData,
|
|
1672
|
+
};
|
|
1673
|
+
}
|
|
1674
|
+
}
|
|
1675
|
+
|
|
1676
|
+
function isNoUpdateError(error) {
|
|
1677
|
+
if (!(error instanceof BangumiApiError) || error.status !== 400) {
|
|
1678
|
+
return false;
|
|
1679
|
+
}
|
|
1680
|
+
|
|
1681
|
+
const details = error.details;
|
|
1682
|
+
const message = [
|
|
1683
|
+
error.message,
|
|
1684
|
+
typeof details?.message === "string" ? details.message : "",
|
|
1685
|
+
typeof details?.description === "string" ? details.description : "",
|
|
1686
|
+
typeof details?.code === "string" ? details.code : "",
|
|
1687
|
+
].join(" ").toLowerCase();
|
|
1688
|
+
|
|
1689
|
+
return message.includes("no update");
|
|
1690
|
+
}
|
|
1691
|
+
|
|
1692
|
+
function normalizePrivateCalendar(data) {
|
|
1693
|
+
if (Array.isArray(data)) {
|
|
1694
|
+
return data;
|
|
1695
|
+
}
|
|
1696
|
+
|
|
1697
|
+
const weekdayLabels = {
|
|
1698
|
+
1: { en: "Mon", cn: "星期一", ja: "月曜日", id: 1 },
|
|
1699
|
+
2: { en: "Tue", cn: "星期二", ja: "火曜日", id: 2 },
|
|
1700
|
+
3: { en: "Wed", cn: "星期三", ja: "水曜日", id: 3 },
|
|
1701
|
+
4: { en: "Thu", cn: "星期四", ja: "木曜日", id: 4 },
|
|
1702
|
+
5: { en: "Fri", cn: "星期五", ja: "金曜日", id: 5 },
|
|
1703
|
+
6: { en: "Sat", cn: "星期六", ja: "土曜日", id: 6 },
|
|
1704
|
+
7: { en: "Sun", cn: "星期日", ja: "日曜日", id: 7 },
|
|
1705
|
+
};
|
|
1706
|
+
|
|
1707
|
+
return Object.entries(data ?? {})
|
|
1708
|
+
.map(([weekdayId, entries]) => {
|
|
1709
|
+
const id = Number(weekdayId);
|
|
1710
|
+
return {
|
|
1711
|
+
weekday: weekdayLabels[id] ?? { id, en: String(id), cn: String(id), ja: String(id) },
|
|
1712
|
+
items: Array.isArray(entries)
|
|
1713
|
+
? entries.map((entry) => ({
|
|
1714
|
+
...(entry.subject ?? entry),
|
|
1715
|
+
name_cn: entry.subject?.nameCN ?? entry.name_cn,
|
|
1716
|
+
collection: {
|
|
1717
|
+
...(entry.subject?.collection ?? entry.collection ?? {}),
|
|
1718
|
+
doing: entry.watchers ?? entry.subject?.collection?.doing ?? entry.collection?.doing,
|
|
1719
|
+
},
|
|
1720
|
+
watchers: entry.watchers,
|
|
1721
|
+
}))
|
|
1722
|
+
: [],
|
|
1723
|
+
};
|
|
1724
|
+
})
|
|
1725
|
+
.sort((left, right) => left.weekday.id - right.weekday.id);
|
|
1726
|
+
}
|
|
1727
|
+
|
|
1728
|
+
export class BangumiOAuthClient {
|
|
1729
|
+
constructor(config = {}) {
|
|
1730
|
+
this.config = config;
|
|
1731
|
+
}
|
|
1732
|
+
|
|
1733
|
+
createAuthorizationUrl({ clientId, redirectUri, state }) {
|
|
1734
|
+
if (!clientId) {
|
|
1735
|
+
throw new CommandError("Missing clientId. Set it in config or pass --client-id.");
|
|
1736
|
+
}
|
|
1737
|
+
|
|
1738
|
+
const url = new URL(`${OAUTH_BASE_URL}/oauth/authorize`);
|
|
1739
|
+
url.searchParams.set("client_id", clientId);
|
|
1740
|
+
url.searchParams.set("response_type", "code");
|
|
1741
|
+
if (redirectUri) {
|
|
1742
|
+
url.searchParams.set("redirect_uri", redirectUri);
|
|
1743
|
+
}
|
|
1744
|
+
if (state) {
|
|
1745
|
+
url.searchParams.set("state", state);
|
|
1746
|
+
}
|
|
1747
|
+
return url.toString();
|
|
1748
|
+
}
|
|
1749
|
+
|
|
1750
|
+
async exchangeCode({ code, clientId, clientSecret, redirectUri }) {
|
|
1751
|
+
assertOAuthCredentials({ code, clientId, clientSecret, redirectUri });
|
|
1752
|
+
|
|
1753
|
+
return requestJson(`${OAUTH_BASE_URL}/oauth/access_token`, {
|
|
1754
|
+
method: "POST",
|
|
1755
|
+
headers: createHeaders(this.config),
|
|
1756
|
+
body: {
|
|
1757
|
+
grant_type: "authorization_code",
|
|
1758
|
+
client_id: clientId,
|
|
1759
|
+
client_secret: clientSecret,
|
|
1760
|
+
code,
|
|
1761
|
+
redirect_uri: redirectUri,
|
|
1762
|
+
},
|
|
1763
|
+
});
|
|
1764
|
+
}
|
|
1765
|
+
|
|
1766
|
+
async refreshToken({ refreshToken, clientId, clientSecret, redirectUri }) {
|
|
1767
|
+
if (!refreshToken) {
|
|
1768
|
+
throw new CommandError("Missing refreshToken. Set it in config or pass --refresh-token.");
|
|
1769
|
+
}
|
|
1770
|
+
if (!clientId) {
|
|
1771
|
+
throw new CommandError("Missing clientId. Set it in config or pass --client-id.");
|
|
1772
|
+
}
|
|
1773
|
+
if (!clientSecret) {
|
|
1774
|
+
throw new CommandError("Missing clientSecret. Set it in config or pass --client-secret.");
|
|
1775
|
+
}
|
|
1776
|
+
if (!redirectUri) {
|
|
1777
|
+
throw new CommandError("Missing redirectUri. Set it in config or pass --redirect-uri.");
|
|
1778
|
+
}
|
|
1779
|
+
|
|
1780
|
+
return requestJson(`${OAUTH_BASE_URL}/oauth/access_token`, {
|
|
1781
|
+
method: "POST",
|
|
1782
|
+
headers: createHeaders(this.config),
|
|
1783
|
+
body: {
|
|
1784
|
+
grant_type: "refresh_token",
|
|
1785
|
+
client_id: clientId,
|
|
1786
|
+
client_secret: clientSecret,
|
|
1787
|
+
refresh_token: refreshToken,
|
|
1788
|
+
redirect_uri: redirectUri,
|
|
1789
|
+
},
|
|
1790
|
+
});
|
|
1791
|
+
}
|
|
1792
|
+
|
|
1793
|
+
async getTokenStatus({ accessToken }) {
|
|
1794
|
+
if (!accessToken) {
|
|
1795
|
+
throw new CommandError("Missing accessToken. Set it in config or pass --access-token.");
|
|
1796
|
+
}
|
|
1797
|
+
|
|
1798
|
+
try {
|
|
1799
|
+
const me = await requestJson(`${PRIVATE_API_BASE_URL}/p1/me`, {
|
|
1800
|
+
method: "GET",
|
|
1801
|
+
headers: createHeaders(this.config, {
|
|
1802
|
+
auth: true,
|
|
1803
|
+
accessToken,
|
|
1804
|
+
}),
|
|
1805
|
+
});
|
|
1806
|
+
|
|
1807
|
+
return {
|
|
1808
|
+
resource: "access-token-status",
|
|
1809
|
+
valid: true,
|
|
1810
|
+
accessToken,
|
|
1811
|
+
user: me,
|
|
1812
|
+
};
|
|
1813
|
+
} catch (error) {
|
|
1814
|
+
if (error instanceof BangumiApiError && error.status === 401) {
|
|
1815
|
+
return {
|
|
1816
|
+
resource: "access-token-status",
|
|
1817
|
+
valid: false,
|
|
1818
|
+
accessToken,
|
|
1819
|
+
error: error.message,
|
|
1820
|
+
details: error.details,
|
|
1821
|
+
};
|
|
1822
|
+
}
|
|
1823
|
+
throw error;
|
|
1824
|
+
}
|
|
1825
|
+
}
|
|
1826
|
+
}
|
|
1827
|
+
|
|
1828
|
+
export class OAuthBackendClient {
|
|
1829
|
+
constructor(config = {}) {
|
|
1830
|
+
this.config = config;
|
|
1831
|
+
}
|
|
1832
|
+
|
|
1833
|
+
async createSession({ relayUrl } = {}) {
|
|
1834
|
+
const baseUrl = this.getBaseUrl();
|
|
1835
|
+
return requestJson(`${baseUrl}/api/oauth/session`, {
|
|
1836
|
+
method: "POST",
|
|
1837
|
+
headers: createHeaders(this.config, { auth: false }),
|
|
1838
|
+
body: relayUrl ? { relay_url: relayUrl } : undefined,
|
|
1839
|
+
});
|
|
1840
|
+
}
|
|
1841
|
+
|
|
1842
|
+
async getSession(sessionId) {
|
|
1843
|
+
const baseUrl = this.getBaseUrl();
|
|
1844
|
+
return requestJson(`${baseUrl}/api/oauth/session/${encodeURIComponent(String(sessionId))}`, {
|
|
1845
|
+
method: "GET",
|
|
1846
|
+
headers: createHeaders(this.config, { auth: false }),
|
|
1847
|
+
});
|
|
1848
|
+
}
|
|
1849
|
+
|
|
1850
|
+
async claimSession(sessionId) {
|
|
1851
|
+
const baseUrl = this.getBaseUrl();
|
|
1852
|
+
return requestJson(`${baseUrl}/api/oauth/session/${encodeURIComponent(String(sessionId))}/claim`, {
|
|
1853
|
+
method: "POST",
|
|
1854
|
+
headers: createHeaders(this.config, { auth: false }),
|
|
1855
|
+
});
|
|
1856
|
+
}
|
|
1857
|
+
|
|
1858
|
+
async createTurnstileSession({ relayUrl } = {}) {
|
|
1859
|
+
const baseUrl = this.getBaseUrl();
|
|
1860
|
+
return requestJson(`${baseUrl}/api/turnstile/session`, {
|
|
1861
|
+
method: "POST",
|
|
1862
|
+
headers: createHeaders(this.config, { auth: false }),
|
|
1863
|
+
body: relayUrl ? { relay_url: relayUrl } : undefined,
|
|
1864
|
+
});
|
|
1865
|
+
}
|
|
1866
|
+
|
|
1867
|
+
async getTurnstileSession(sessionId, sessionSecret) {
|
|
1868
|
+
const baseUrl = this.getBaseUrl();
|
|
1869
|
+
const url = new URL(`${baseUrl}/api/turnstile/session/${encodeURIComponent(String(sessionId))}`);
|
|
1870
|
+
url.searchParams.set("secret", String(sessionSecret));
|
|
1871
|
+
return requestJson(url.toString(), {
|
|
1872
|
+
method: "GET",
|
|
1873
|
+
headers: createHeaders(this.config, { auth: false }),
|
|
1874
|
+
});
|
|
1875
|
+
}
|
|
1876
|
+
|
|
1877
|
+
async claimTurnstileSession(sessionId, sessionSecret) {
|
|
1878
|
+
const baseUrl = this.getBaseUrl();
|
|
1879
|
+
const url = new URL(`${baseUrl}/api/turnstile/session/${encodeURIComponent(String(sessionId))}/claim`);
|
|
1880
|
+
url.searchParams.set("secret", String(sessionSecret));
|
|
1881
|
+
return requestJson(url.toString(), {
|
|
1882
|
+
method: "POST",
|
|
1883
|
+
headers: createHeaders(this.config, { auth: false }),
|
|
1884
|
+
});
|
|
1885
|
+
}
|
|
1886
|
+
|
|
1887
|
+
getBaseUrl() {
|
|
1888
|
+
const baseUrl = this.config.oauthServerBaseUrl;
|
|
1889
|
+
if (!baseUrl) {
|
|
1890
|
+
throw new CommandError("Missing oauthServerBaseUrl. Set it in config or bangumi-development.");
|
|
1891
|
+
}
|
|
1892
|
+
return String(baseUrl).replace(/\/+$/, "");
|
|
1893
|
+
}
|
|
1894
|
+
}
|
|
1895
|
+
|
|
1896
|
+
function assertOAuthCredentials({ code, clientId, clientSecret, redirectUri }) {
|
|
1897
|
+
if (!code) {
|
|
1898
|
+
throw new CommandError("Missing authorization code. Pass --code.");
|
|
1899
|
+
}
|
|
1900
|
+
if (!clientId) {
|
|
1901
|
+
throw new CommandError("Missing clientId. Set it in config or pass --client-id.");
|
|
1902
|
+
}
|
|
1903
|
+
if (!clientSecret) {
|
|
1904
|
+
throw new CommandError("Missing clientSecret. Set it in config or pass --client-secret.");
|
|
1905
|
+
}
|
|
1906
|
+
if (!redirectUri) {
|
|
1907
|
+
throw new CommandError("Missing redirectUri. Set it in config or pass --redirect-uri.");
|
|
1908
|
+
}
|
|
1909
|
+
}
|
|
1910
|
+
|
|
1911
|
+
function createHeaders(config, options = {}) {
|
|
1912
|
+
const userAgent = config.userAgent ?? fallbackUserAgent(config);
|
|
1913
|
+
const headers = {
|
|
1914
|
+
Accept: "application/json",
|
|
1915
|
+
"User-Agent": userAgent,
|
|
1916
|
+
};
|
|
1917
|
+
|
|
1918
|
+
if (options.contentType) {
|
|
1919
|
+
headers["Content-Type"] = options.contentType;
|
|
1920
|
+
} else if (options.hasBody) {
|
|
1921
|
+
headers["Content-Type"] = "application/json";
|
|
1922
|
+
}
|
|
1923
|
+
|
|
1924
|
+
const privateSessionId = typeof config.privateSessionId === "string" ? config.privateSessionId.trim() : "";
|
|
1925
|
+
const isPrivateApiPath = typeof options.path === "string" && options.path.startsWith("/p1/");
|
|
1926
|
+
if (privateSessionId && isPrivateApiPath) {
|
|
1927
|
+
headers.Cookie = `chiiNextSessionID=${privateSessionId}`;
|
|
1928
|
+
}
|
|
1929
|
+
|
|
1930
|
+
const accessToken = options.accessToken ?? config.accessToken;
|
|
1931
|
+
const shouldAttachAuth = options.auth !== false && Boolean(accessToken) && !(privateSessionId && isPrivateApiPath);
|
|
1932
|
+
if (shouldAttachAuth) {
|
|
1933
|
+
headers.Authorization = `Bearer ${accessToken}`;
|
|
1934
|
+
}
|
|
1935
|
+
|
|
1936
|
+
return headers;
|
|
1937
|
+
}
|
|
1938
|
+
|
|
1939
|
+
|
|
1940
|
+
|
|
1941
|
+
function parseStatusFeed(xml) {
|
|
1942
|
+
const entries = matchAllBlocks(xml, "entry").map((entryXml) => parseStatusEntry(entryXml));
|
|
1943
|
+
|
|
1944
|
+
return {
|
|
1945
|
+
title: decodeXmlText(extractTagText(xml, "title") ?? "Bangumi Status · Incidents"),
|
|
1946
|
+
id: decodeXmlText(extractTagText(xml, "id") ?? ""),
|
|
1947
|
+
updatedAt: decodeXmlText(extractTagText(xml, "updated") ?? ""),
|
|
1948
|
+
link: extractAlternateLink(xml) ?? STATUS_BASE_URL,
|
|
1949
|
+
feedUrl: STATUS_FEED_URL,
|
|
1950
|
+
entries,
|
|
1951
|
+
};
|
|
1952
|
+
}
|
|
1953
|
+
|
|
1954
|
+
function parseStatusEntry(entryXml) {
|
|
1955
|
+
const title = decodeXmlText(extractTagText(entryXml, "title") ?? "");
|
|
1956
|
+
const summary = decodeXmlText(extractTagText(entryXml, "summary") ?? "");
|
|
1957
|
+
const content = decodeXmlText(extractTagText(entryXml, "content") ?? "");
|
|
1958
|
+
const id = decodeXmlText(extractTagText(entryXml, "id") ?? "");
|
|
1959
|
+
const { severity, site, audience } = parseStatusTitle(title);
|
|
1960
|
+
const { date, target } = parseStatusEntryId(id);
|
|
1961
|
+
|
|
1962
|
+
return {
|
|
1963
|
+
id,
|
|
1964
|
+
title,
|
|
1965
|
+
summary,
|
|
1966
|
+
content,
|
|
1967
|
+
updatedAt: decodeXmlText(extractTagText(entryXml, "updated") ?? ""),
|
|
1968
|
+
link: extractAlternateLink(entryXml) ?? STATUS_BASE_URL,
|
|
1969
|
+
severity,
|
|
1970
|
+
site,
|
|
1971
|
+
audience,
|
|
1972
|
+
date,
|
|
1973
|
+
target,
|
|
1974
|
+
};
|
|
1975
|
+
}
|
|
1976
|
+
|
|
1977
|
+
function parseStatusTitle(title) {
|
|
1978
|
+
const match = String(title).match(/^(.+?)\s+[—-]\s+(.+?)\s+[·•]\s+(.+)$/);
|
|
1979
|
+
if (!match) {
|
|
1980
|
+
return {
|
|
1981
|
+
severity: null,
|
|
1982
|
+
site: null,
|
|
1983
|
+
audience: null,
|
|
1984
|
+
};
|
|
1985
|
+
}
|
|
1986
|
+
|
|
1987
|
+
return {
|
|
1988
|
+
severity: match[1].trim(),
|
|
1989
|
+
site: match[2].trim(),
|
|
1990
|
+
audience: match[3].trim(),
|
|
1991
|
+
};
|
|
1992
|
+
}
|
|
1993
|
+
|
|
1994
|
+
function parseStatusEntryId(id) {
|
|
1995
|
+
const match = String(id).match(/^tag:bgm-status\.ry\.mk,(\d{4}-\d{2}-\d{2}):(.*)$/);
|
|
1996
|
+
if (!match) {
|
|
1997
|
+
return {
|
|
1998
|
+
date: null,
|
|
1999
|
+
target: null,
|
|
2000
|
+
};
|
|
2001
|
+
}
|
|
2002
|
+
|
|
2003
|
+
return {
|
|
2004
|
+
date: match[1],
|
|
2005
|
+
target: match[2] || null,
|
|
2006
|
+
};
|
|
2007
|
+
}
|
|
2008
|
+
|
|
2009
|
+
function matchAllBlocks(xml, tagName) {
|
|
2010
|
+
const pattern = new RegExp(`<${tagName}\\b[^>]*>[\\s\\S]*?<\\/${tagName}>`, "g");
|
|
2011
|
+
return Array.from(String(xml).matchAll(pattern), (match) => match[0]);
|
|
2012
|
+
}
|
|
2013
|
+
|
|
2014
|
+
function extractTagText(xml, tagName) {
|
|
2015
|
+
const match = String(xml).match(new RegExp(`<${tagName}\\b[^>]*>([\\s\\S]*?)<\\/${tagName}>`, "i"));
|
|
2016
|
+
if (!match) {
|
|
2017
|
+
return null;
|
|
2018
|
+
}
|
|
2019
|
+
return stripXmlTags(match[1]).trim();
|
|
2020
|
+
}
|
|
2021
|
+
|
|
2022
|
+
function extractAlternateLink(xml) {
|
|
2023
|
+
const linkTags = Array.from(String(xml).matchAll(/<link\b([^>]*)>/gi), (match) => match[1]);
|
|
2024
|
+
|
|
2025
|
+
for (const tag of linkTags) {
|
|
2026
|
+
const attributes = parseXmlAttributes(tag);
|
|
2027
|
+
if (attributes.rel === "alternate" && attributes.href) {
|
|
2028
|
+
return decodeXmlText(attributes.href);
|
|
2029
|
+
}
|
|
2030
|
+
}
|
|
2031
|
+
|
|
2032
|
+
for (const tag of linkTags) {
|
|
2033
|
+
const attributes = parseXmlAttributes(tag);
|
|
2034
|
+
if (attributes.href) {
|
|
2035
|
+
return decodeXmlText(attributes.href);
|
|
2036
|
+
}
|
|
2037
|
+
}
|
|
2038
|
+
|
|
2039
|
+
return null;
|
|
2040
|
+
}
|
|
2041
|
+
|
|
2042
|
+
function parseXmlAttributes(source) {
|
|
2043
|
+
const attributes = {};
|
|
2044
|
+
const pattern = /(\w+)="([^"]*)"/g;
|
|
2045
|
+
|
|
2046
|
+
for (const match of String(source).matchAll(pattern)) {
|
|
2047
|
+
attributes[match[1]] = match[2];
|
|
2048
|
+
}
|
|
2049
|
+
|
|
2050
|
+
return attributes;
|
|
2051
|
+
}
|
|
2052
|
+
|
|
2053
|
+
function stripXmlTags(value) {
|
|
2054
|
+
return String(value).replace(/<[^>]+>/g, "");
|
|
2055
|
+
}
|
|
2056
|
+
|
|
2057
|
+
function decodeXmlText(value) {
|
|
2058
|
+
return String(value)
|
|
2059
|
+
.replace(/<!\[CDATA\[([\s\S]*?)\]\]>/g, "$1")
|
|
2060
|
+
.replace(/</g, "<")
|
|
2061
|
+
.replace(/>/g, ">")
|
|
2062
|
+
.replace(/"/g, '"')
|
|
2063
|
+
.replace(/'/g, "'")
|
|
2064
|
+
.replace(/&/g, "&");
|
|
2065
|
+
}
|