@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,379 @@
|
|
|
1
|
+
import { BangumiClient } from "../core/client.js";
|
|
2
|
+
import { getConfig } from "../core/config.js";
|
|
3
|
+
import { CommandError, printResult } from "../core/output.js";
|
|
4
|
+
import { normalizeBangumiReactionValue } from "../core/reactions.js";
|
|
5
|
+
import { firstPositional, getPositional, parseFlags } from "../utils/args.js";
|
|
6
|
+
import {
|
|
7
|
+
normalizeEpisodePageSize,
|
|
8
|
+
normalizeNonNegativeInteger,
|
|
9
|
+
normalizePositiveInteger,
|
|
10
|
+
normalizePositiveNumber,
|
|
11
|
+
} from "../utils/helpers.js";
|
|
12
|
+
import {
|
|
13
|
+
EPISODE_COLLECTION_STATUS_MAP,
|
|
14
|
+
EPISODE_TYPE_MAP,
|
|
15
|
+
normalizeEpisodeCollectionStatusValue,
|
|
16
|
+
normalizeEpisodeTypeFilter,
|
|
17
|
+
} from "../utils/validators.js";
|
|
18
|
+
import { fetchAllEpisodes } from "../utils/collection.js";
|
|
19
|
+
import {
|
|
20
|
+
buildEpisodeActionResult,
|
|
21
|
+
fetchMyEpisodeCollectionVerified,
|
|
22
|
+
formatEpisodeCollectionStatusForError,
|
|
23
|
+
handleEpisodeListError,
|
|
24
|
+
mapEpisodeMutationError,
|
|
25
|
+
} from "../utils/collection-ops.js";
|
|
26
|
+
import { resolveTurnstileTokenForMutation } from "../utils/turnstile-flow.js";
|
|
27
|
+
|
|
28
|
+
export async function runEpisodeCommand(command, args, context) {
|
|
29
|
+
switch (command) {
|
|
30
|
+
case "list": {
|
|
31
|
+
const result = await executeEpisodeListCommand(args);
|
|
32
|
+
printResult(result, context);
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
case "status": {
|
|
36
|
+
const result = await executeEpisodeStatusCommand(args);
|
|
37
|
+
printResult(result, context);
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
case "watch": {
|
|
41
|
+
const result = await executeEpisodeWatchCommand(args);
|
|
42
|
+
printResult(result, context);
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
case "get": {
|
|
46
|
+
const result = await executeEpisodeGetCommand(args);
|
|
47
|
+
printResult(result, context);
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
case "comments": {
|
|
51
|
+
const result = await executeEpisodeCommentsCommand(args);
|
|
52
|
+
printResult(result, context);
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
case "comment": {
|
|
56
|
+
const result = await executeEpisodeCommentCommand(args, context);
|
|
57
|
+
printResult(result, context);
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
case "edit-comment": {
|
|
61
|
+
const result = await executeEpisodeEditCommentCommand(args);
|
|
62
|
+
printResult(result, context);
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
case "delete-comment": {
|
|
66
|
+
const result = await executeEpisodeDeleteCommentCommand(args);
|
|
67
|
+
printResult(result, context);
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
case "like-comment": {
|
|
71
|
+
const result = await executeEpisodeLikeCommentCommand(args);
|
|
72
|
+
printResult(result, context);
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
case "unlike-comment": {
|
|
76
|
+
const result = await executeEpisodeUnlikeCommentCommand(args);
|
|
77
|
+
printResult(result, context);
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
default:
|
|
81
|
+
throw new CommandError(
|
|
82
|
+
"Usage: bgm episode <list|status|watch|get|comments|comment|edit-comment|delete-comment|like-comment|unlike-comment> ...",
|
|
83
|
+
);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export async function executeEpisodeGetCommand(args) {
|
|
88
|
+
const options = parseFlags(args);
|
|
89
|
+
const episodeId = firstPositional(options);
|
|
90
|
+
if (!episodeId) {
|
|
91
|
+
throw new CommandError("Usage: bgm episode get <episode_id>");
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
return new BangumiClient(getConfig()).getEpisode(episodeId);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export async function executeEpisodeListCommand(args) {
|
|
98
|
+
const options = parseFlags(args);
|
|
99
|
+
const client = new BangumiClient(getConfig());
|
|
100
|
+
const subjectId = firstPositional(options);
|
|
101
|
+
if (!subjectId) {
|
|
102
|
+
throw new CommandError(
|
|
103
|
+
"Usage: bgm episode list <subject_id> [--type <main|sp|op|ed|op_ed|trailer|pv|mad|other>] [--limit n] [--offset n]",
|
|
104
|
+
);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const typeFilter = normalizeEpisodeTypeFilter(options.type);
|
|
108
|
+
const limit = normalizeEpisodePageSize(options.limit);
|
|
109
|
+
const offset = normalizeNonNegativeInteger(options.offset, "offset");
|
|
110
|
+
let result;
|
|
111
|
+
let filtered;
|
|
112
|
+
|
|
113
|
+
try {
|
|
114
|
+
if (typeFilter.matchTypes) {
|
|
115
|
+
const episodes = await fetchAllEpisodes(client, subjectId);
|
|
116
|
+
const matched = episodes.filter((episode) => typeFilter.matchTypes.has(Number(episode?.type)));
|
|
117
|
+
filtered = matched.slice(offset ?? 0, limit !== undefined ? (offset ?? 0) + limit : undefined);
|
|
118
|
+
result = {
|
|
119
|
+
data: filtered,
|
|
120
|
+
total: matched.length,
|
|
121
|
+
limit: limit ?? matched.length,
|
|
122
|
+
offset: offset ?? 0,
|
|
123
|
+
};
|
|
124
|
+
} else {
|
|
125
|
+
result = await client.listEpisodes({
|
|
126
|
+
subject_id: subjectId,
|
|
127
|
+
type: typeFilter.queryType,
|
|
128
|
+
limit,
|
|
129
|
+
offset,
|
|
130
|
+
});
|
|
131
|
+
const episodes = Array.isArray(result.data) ? result.data : [];
|
|
132
|
+
filtered = episodes;
|
|
133
|
+
}
|
|
134
|
+
} catch (error) {
|
|
135
|
+
handleEpisodeListError(error, subjectId);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
return {
|
|
139
|
+
...result,
|
|
140
|
+
resource: "episode-list",
|
|
141
|
+
subjectId: Number(subjectId),
|
|
142
|
+
data: filtered,
|
|
143
|
+
total: result.total ?? filtered.length,
|
|
144
|
+
filters: {
|
|
145
|
+
type: typeFilter.label,
|
|
146
|
+
limit,
|
|
147
|
+
offset,
|
|
148
|
+
},
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
export async function executeEpisodeStatusCommand(args) {
|
|
153
|
+
const options = parseFlags(args);
|
|
154
|
+
const client = new BangumiClient(getConfig());
|
|
155
|
+
const episodeId = firstPositional(options);
|
|
156
|
+
const rawStatus = options.status ?? getPositional(options, 1);
|
|
157
|
+
if (!episodeId || !rawStatus) {
|
|
158
|
+
throw new CommandError(
|
|
159
|
+
"Usage: bgm episode status <episode_id> <queue|watched|drop|remove>",
|
|
160
|
+
);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
const episode = await client.getEpisode(episodeId);
|
|
164
|
+
const requestedType = normalizeEpisodeCollectionStatusValue(rawStatus);
|
|
165
|
+
try {
|
|
166
|
+
await client.updateMyEpisodeCollection(episodeId, { type: requestedType });
|
|
167
|
+
} catch (error) {
|
|
168
|
+
throw mapEpisodeMutationError(error, {
|
|
169
|
+
action: requestedType === 0 ? "clear episode progress" : `set episode status to ${formatEpisodeCollectionStatusForError(requestedType)}`,
|
|
170
|
+
episodeId,
|
|
171
|
+
subjectId: episode?.subject_id,
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
const collection = await fetchMyEpisodeCollectionVerified(client, episodeId, {
|
|
175
|
+
expected: { type: requestedType },
|
|
176
|
+
actionLabel: "Episode status update",
|
|
177
|
+
mismatchMessage: (latest) =>
|
|
178
|
+
`Bangumi did not persist the requested episode status. Requested ${formatEpisodeCollectionStatusForError(requestedType)}, but read back ${formatEpisodeCollectionStatusForError(latest?.type)}.`,
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
return buildEpisodeActionResult({
|
|
182
|
+
action: "status",
|
|
183
|
+
actionLabel: requestedType === 0 ? "Episode status cleared" : "Episode status updated",
|
|
184
|
+
episodeId,
|
|
185
|
+
episode,
|
|
186
|
+
collection,
|
|
187
|
+
requestedType,
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
export async function executeEpisodeWatchCommand(args) {
|
|
192
|
+
const options = parseFlags(args);
|
|
193
|
+
const client = new BangumiClient(getConfig());
|
|
194
|
+
const subjectId = firstPositional(options);
|
|
195
|
+
const episodeNumber = normalizePositiveNumber(getPositional(options, 1) ?? options.number, "episode number");
|
|
196
|
+
if (!subjectId || episodeNumber === undefined) {
|
|
197
|
+
throw new CommandError("Usage: bgm episode watch <subject_id> <episode_number>");
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
const episodes = await fetchAllEpisodes(client, subjectId, { type: EPISODE_TYPE_MAP.main });
|
|
201
|
+
const episode = episodes.find((item) => Number(item?.type) === EPISODE_TYPE_MAP.main && Number(item?.ep) === episodeNumber);
|
|
202
|
+
if (!episode) {
|
|
203
|
+
throw new CommandError(`Could not find main episode ${episodeNumber} under subject ${subjectId}.`);
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
try {
|
|
207
|
+
await client.updateMyEpisodeCollection(episode.id, { type: EPISODE_COLLECTION_STATUS_MAP.watched });
|
|
208
|
+
} catch (error) {
|
|
209
|
+
throw mapEpisodeMutationError(error, {
|
|
210
|
+
action: `mark episode ${episodeNumber} as watched`,
|
|
211
|
+
episodeId: episode.id,
|
|
212
|
+
subjectId,
|
|
213
|
+
});
|
|
214
|
+
}
|
|
215
|
+
const collection = await fetchMyEpisodeCollectionVerified(client, episode.id, {
|
|
216
|
+
expected: { type: EPISODE_COLLECTION_STATUS_MAP.watched },
|
|
217
|
+
actionLabel: "Episode watch update",
|
|
218
|
+
mismatchMessage: (latest) =>
|
|
219
|
+
`Bangumi did not persist the requested episode status. Requested watched, but read back ${formatEpisodeCollectionStatusForError(latest?.type)}.`,
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
return buildEpisodeActionResult({
|
|
223
|
+
action: "watch",
|
|
224
|
+
actionLabel: "Episode marked watched",
|
|
225
|
+
subjectId,
|
|
226
|
+
episodeId: episode.id,
|
|
227
|
+
episode,
|
|
228
|
+
collection,
|
|
229
|
+
requestedType: EPISODE_COLLECTION_STATUS_MAP.watched,
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
export async function executeEpisodeCommentsCommand(args) {
|
|
234
|
+
const options = parseFlags(args);
|
|
235
|
+
const client = new BangumiClient(getConfig());
|
|
236
|
+
const first = firstPositional(options);
|
|
237
|
+
const second = getPositional(options, 1);
|
|
238
|
+
let episodeId = first;
|
|
239
|
+
let episode;
|
|
240
|
+
let subjectId;
|
|
241
|
+
let episodeNumber;
|
|
242
|
+
let typeFilter;
|
|
243
|
+
|
|
244
|
+
if (options.subject || options.subjectId || second !== undefined || options.number !== undefined || options.ep !== undefined) {
|
|
245
|
+
subjectId = options.subject ?? options.subjectId ?? first;
|
|
246
|
+
episodeNumber = normalizePositiveNumber(options.number ?? options.ep ?? second, "episode number");
|
|
247
|
+
if (!subjectId || episodeNumber === undefined) {
|
|
248
|
+
throw new CommandError(
|
|
249
|
+
"Usage: bgm episode comments <episode_id> OR bgm episode comments <subject_id> <episode_number> [--type <main|sp|op|ed|op_ed|trailer|pv|mad|other>]",
|
|
250
|
+
);
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
typeFilter = normalizeEpisodeTypeFilter(options.type ?? "main");
|
|
254
|
+
episode = await resolveEpisodeByNumber(client, subjectId, episodeNumber, typeFilter);
|
|
255
|
+
episodeId = episode.id;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
if (!episodeId) {
|
|
259
|
+
throw new CommandError(
|
|
260
|
+
"Usage: bgm episode comments <episode_id> OR bgm episode comments <subject_id> <episode_number> [--type <main|sp|op|ed|op_ed|trailer|pv|mad|other>]",
|
|
261
|
+
);
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
const data = await client.listEpisodeComments(episodeId);
|
|
265
|
+
return {
|
|
266
|
+
resource: "episode-comments",
|
|
267
|
+
episodeId: Number(episodeId),
|
|
268
|
+
subjectId: subjectId !== undefined ? Number(subjectId) : undefined,
|
|
269
|
+
episodeNumber,
|
|
270
|
+
episode,
|
|
271
|
+
filters: typeFilter ? { type: typeFilter.label ?? "main" } : undefined,
|
|
272
|
+
data,
|
|
273
|
+
};
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
export async function executeEpisodeCommentCommand(args, context = {}) {
|
|
277
|
+
const options = parseFlags(args);
|
|
278
|
+
const episodeId = firstPositional(options);
|
|
279
|
+
const content = getPositional(options, 1) ?? options.content;
|
|
280
|
+
const replyTo = normalizeNonNegativeInteger(options.replyTo, "reply-to") ?? 0;
|
|
281
|
+
|
|
282
|
+
if (!episodeId || !content) {
|
|
283
|
+
throw new CommandError("Usage: bgm episode comment <episode_id> <content> [--reply-to <comment_id>] [--turnstile-token <token>] [--manual]");
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
const turnstileToken = await resolveTurnstileTokenForMutation(options, {
|
|
287
|
+
actionLabel: "create an episode comment",
|
|
288
|
+
context,
|
|
289
|
+
});
|
|
290
|
+
const result = await new BangumiClient(getConfig()).createEpisodeComment(episodeId, {
|
|
291
|
+
content,
|
|
292
|
+
replyTo,
|
|
293
|
+
turnstileToken,
|
|
294
|
+
});
|
|
295
|
+
|
|
296
|
+
return buildEpisodeCommentMutationResult("create", {
|
|
297
|
+
episodeId,
|
|
298
|
+
commentId: result.id,
|
|
299
|
+
replyTo,
|
|
300
|
+
});
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
export async function executeEpisodeEditCommentCommand(args) {
|
|
304
|
+
const options = parseFlags(args);
|
|
305
|
+
const commentId = firstPositional(options);
|
|
306
|
+
const content = getPositional(options, 1) ?? options.content;
|
|
307
|
+
if (!commentId || !content) {
|
|
308
|
+
throw new CommandError("Usage: bgm episode edit-comment <comment_id> <content>");
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
await new BangumiClient(getConfig()).updateEpisodeComment(commentId, { content });
|
|
312
|
+
return buildEpisodeCommentMutationResult("edit", { commentId });
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
export async function executeEpisodeDeleteCommentCommand(args) {
|
|
316
|
+
const options = parseFlags(args);
|
|
317
|
+
const commentId = firstPositional(options);
|
|
318
|
+
if (!commentId) {
|
|
319
|
+
throw new CommandError("Usage: bgm episode delete-comment <comment_id>");
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
await new BangumiClient(getConfig()).deleteEpisodeComment(commentId);
|
|
323
|
+
return buildEpisodeCommentMutationResult("delete", { commentId });
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
export async function executeEpisodeLikeCommentCommand(args) {
|
|
327
|
+
const options = parseFlags(args);
|
|
328
|
+
const commentId = firstPositional(options);
|
|
329
|
+
const value = normalizeBangumiReactionValue(getPositional(options, 1) ?? options.value, "episodeComment");
|
|
330
|
+
if (!commentId || value === undefined) {
|
|
331
|
+
throw new CommandError("Usage: bgm episode like-comment <comment_id> <value>");
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
await new BangumiClient(getConfig()).likeEpisodeComment(commentId, value);
|
|
335
|
+
return buildEpisodeCommentMutationResult("like", { commentId, value });
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
export async function executeEpisodeUnlikeCommentCommand(args) {
|
|
339
|
+
const options = parseFlags(args);
|
|
340
|
+
const commentId = firstPositional(options);
|
|
341
|
+
if (!commentId) {
|
|
342
|
+
throw new CommandError("Usage: bgm episode unlike-comment <comment_id>");
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
await new BangumiClient(getConfig()).unlikeEpisodeComment(commentId);
|
|
346
|
+
return buildEpisodeCommentMutationResult("unlike", { commentId });
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
function buildEpisodeCommentMutationResult(action, details) {
|
|
350
|
+
const normalized = { ...details };
|
|
351
|
+
for (const key of ["episodeId", "commentId", "replyTo"]) {
|
|
352
|
+
if (normalized[key] !== undefined) {
|
|
353
|
+
normalized[key] = Number(normalized[key]);
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
return {
|
|
358
|
+
resource: "episode-comment-mutation",
|
|
359
|
+
action,
|
|
360
|
+
...normalized,
|
|
361
|
+
};
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
async function resolveEpisodeByNumber(client, subjectId, episodeNumber, typeFilter) {
|
|
365
|
+
const episodes = await fetchAllEpisodes(client, subjectId, {
|
|
366
|
+
type: typeFilter.matchTypes ? undefined : typeFilter.queryType,
|
|
367
|
+
});
|
|
368
|
+
const candidates = typeFilter.matchTypes
|
|
369
|
+
? episodes.filter((item) => typeFilter.matchTypes.has(Number(item?.type)))
|
|
370
|
+
: episodes;
|
|
371
|
+
const episode = candidates.find((item) => Number(item?.ep) === episodeNumber);
|
|
372
|
+
|
|
373
|
+
if (!episode) {
|
|
374
|
+
const typeLabel = typeFilter.label ?? "main";
|
|
375
|
+
throw new CommandError(`Could not find ${typeLabel} episode ${episodeNumber} under subject ${subjectId}.`);
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
return episode;
|
|
379
|
+
}
|