@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.
Files changed (46) hide show
  1. package/LICENSE +660 -0
  2. package/README.en.md +122 -0
  3. package/README.md +127 -0
  4. package/README.zh-TW.md +122 -0
  5. package/package.json +41 -0
  6. package/src/cli.js +993 -0
  7. package/src/commands/blog.js +213 -0
  8. package/src/commands/calendar.js +32 -0
  9. package/src/commands/character.js +291 -0
  10. package/src/commands/collection.js +418 -0
  11. package/src/commands/episode.js +379 -0
  12. package/src/commands/group.js +488 -0
  13. package/src/commands/index.js +362 -0
  14. package/src/commands/notify.js +74 -0
  15. package/src/commands/person.js +297 -0
  16. package/src/commands/status.js +74 -0
  17. package/src/commands/subject.js +537 -0
  18. package/src/commands/timeline.js +288 -0
  19. package/src/commands/trending.js +58 -0
  20. package/src/commands/tui.js +1537 -0
  21. package/src/commands/user.js +83 -0
  22. package/src/core/client.js +2065 -0
  23. package/src/core/config.js +450 -0
  24. package/src/core/http.js +144 -0
  25. package/src/core/output.js +3888 -0
  26. package/src/core/proxy.js +47 -0
  27. package/src/core/reactions.js +62 -0
  28. package/src/core/turnstile.js +526 -0
  29. package/src/data/bangumi-emotes.json +311 -0
  30. package/src/utils/args.js +113 -0
  31. package/src/utils/auth-flow.js +373 -0
  32. package/src/utils/auth.js +90 -0
  33. package/src/utils/calendar.js +23 -0
  34. package/src/utils/collection-ops.js +333 -0
  35. package/src/utils/collection.js +240 -0
  36. package/src/utils/formatters.js +157 -0
  37. package/src/utils/helpers.js +283 -0
  38. package/src/utils/hot.js +182 -0
  39. package/src/utils/install.js +40 -0
  40. package/src/utils/prompts.js +87 -0
  41. package/src/utils/relay.js +74 -0
  42. package/src/utils/status.js +68 -0
  43. package/src/utils/tui-interactive.js +197 -0
  44. package/src/utils/tui-render.js +86 -0
  45. package/src/utils/turnstile-flow.js +154 -0
  46. package/src/utils/validators.js +412 -0
@@ -0,0 +1,537 @@
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 { ensureArray, firstPositional, getPositional, parseFlags } from "../utils/args.js";
6
+ import {
7
+ normalizeNonNegativeInteger,
8
+ normalizePageSize,
9
+ parseOptionalBoolean,
10
+ parseOptionalInteger,
11
+ } from "../utils/helpers.js";
12
+ import {
13
+ normalizeCollectionStatusValue,
14
+ normalizeSubjectType,
15
+ } from "../utils/validators.js";
16
+ import { fetchAllSubjects, sortSubjectsByRank } from "../utils/collection.js";
17
+ import { resolveTurnstileTokenForMutation } from "../utils/turnstile-flow.js";
18
+
19
+ export async function runSubjectCommand(command, args, context) {
20
+ switch (command) {
21
+ case "get": {
22
+ const options = parseFlags(args);
23
+ const client = new BangumiClient(getConfig());
24
+ const subjectId = firstPositional(options);
25
+ if (!subjectId) {
26
+ throw new CommandError("Usage: bgm subject get <subject_id> [--verbose]");
27
+ }
28
+
29
+ const subject = await client.getSubject(subjectId);
30
+ context.verbose = Boolean(options.verbose);
31
+ printResult(subject, context);
32
+ return;
33
+ }
34
+ case "list": {
35
+ const subjects = await executeSubjectListCommand(args);
36
+ printResult(subjects, context);
37
+ return;
38
+ }
39
+ case "search": {
40
+ const result = await executeSubjectSearchCommand(args);
41
+ printResult(result, context);
42
+ return;
43
+ }
44
+ case "characters":
45
+ case "collects":
46
+ case "comments":
47
+ case "indexes":
48
+ case "recs":
49
+ case "relations":
50
+ case "reviews":
51
+ case "staff":
52
+ case "staff-positions":
53
+ case "topics": {
54
+ const result = await executeSubjectP1ListCommand(command, args);
55
+ printResult(result, context);
56
+ return;
57
+ }
58
+ case "recent-topics":
59
+ case "latest-topics": {
60
+ const result = await executeRecentSubjectTopicsCommand(args);
61
+ printResult(result, context);
62
+ return;
63
+ }
64
+ case "topic": {
65
+ const result = await executeSubjectTopicCommand(args);
66
+ printResult(result, context);
67
+ return;
68
+ }
69
+ case "create-topic": {
70
+ const result = await executeSubjectCreateTopicCommand(args, context);
71
+ printResult(result, context);
72
+ return;
73
+ }
74
+ case "edit-topic": {
75
+ const result = await executeSubjectEditTopicCommand(args);
76
+ printResult(result, context);
77
+ return;
78
+ }
79
+ case "reply": {
80
+ const result = await executeSubjectReplyCommand(args, context);
81
+ printResult(result, context);
82
+ return;
83
+ }
84
+ case "post": {
85
+ const result = await executeSubjectPostCommand(args);
86
+ printResult(result, context);
87
+ return;
88
+ }
89
+ case "edit-post": {
90
+ const result = await executeSubjectEditPostCommand(args);
91
+ printResult(result, context);
92
+ return;
93
+ }
94
+ case "delete-post": {
95
+ const result = await executeSubjectDeletePostCommand(args);
96
+ printResult(result, context);
97
+ return;
98
+ }
99
+ case "like-post": {
100
+ const result = await executeSubjectLikePostCommand(args);
101
+ printResult(result, context);
102
+ return;
103
+ }
104
+ case "unlike-post": {
105
+ const result = await executeSubjectUnlikePostCommand(args);
106
+ printResult(result, context);
107
+ return;
108
+ }
109
+ case "like-collect": {
110
+ const result = await executeSubjectLikeCollectCommand(args);
111
+ printResult(result, context);
112
+ return;
113
+ }
114
+ case "unlike-collect": {
115
+ const result = await executeSubjectUnlikeCollectCommand(args);
116
+ printResult(result, context);
117
+ return;
118
+ }
119
+ default:
120
+ throw new CommandError("Usage: bgm subject <get|list|search|characters|collects|comments|indexes|recs|relations|reviews|staff|staff-positions|topics|recent-topics|topic|create-topic|edit-topic|reply|post|edit-post|delete-post|like-post|unlike-post|like-collect|unlike-collect> ...");
121
+ }
122
+ }
123
+
124
+ export async function executeSubjectListCommand(args) {
125
+ const options = parseFlags(args);
126
+ const client = new BangumiClient(getConfig());
127
+ const type = normalizeSubjectType(options.type);
128
+ if (!type) {
129
+ throw new CommandError("Usage: bgm subject list --type <book|anime|music|game|real> [options]");
130
+ }
131
+
132
+ const limit = parseOptionalInteger(options.limit);
133
+ const offset = parseOptionalInteger(options.offset);
134
+ const query = {
135
+ type,
136
+ cat: options.cat,
137
+ series: parseOptionalBoolean(options.series),
138
+ platform: options.platform,
139
+ sort: options.sort,
140
+ year: parseOptionalInteger(options.year),
141
+ month: parseOptionalInteger(options.month),
142
+ };
143
+
144
+ let result;
145
+ if (limit !== undefined && limit > 100) {
146
+ result = await fetchAllSubjects(client, { ...query, limit, offset });
147
+ } else {
148
+ result = await client.listSubjects({
149
+ ...query,
150
+ limit,
151
+ offset,
152
+ });
153
+ }
154
+
155
+ if (String(options.sort ?? "").toLowerCase() === "rank" && Array.isArray(result.data)) {
156
+ result.data = sortSubjectsByRank(result.data);
157
+ }
158
+ return {
159
+ ...result,
160
+ filters: {
161
+ mode: "list",
162
+ type,
163
+ sort: options.sort ?? "rank",
164
+ year: parseOptionalInteger(options.year),
165
+ month: parseOptionalInteger(options.month),
166
+ cat: options.cat,
167
+ series: parseOptionalBoolean(options.series),
168
+ platform: options.platform,
169
+ },
170
+ };
171
+ }
172
+
173
+ export async function executeSubjectSearchCommand(args) {
174
+ const options = parseFlags(args);
175
+ const client = new BangumiClient(getConfig());
176
+ const keyword = firstPositional(options);
177
+ if (!keyword) {
178
+ throw new CommandError("Usage: bgm subject search <keyword> [options]");
179
+ }
180
+
181
+ const filter = {};
182
+ const normalizedType = normalizeSubjectType(options.type);
183
+ if (normalizedType) {
184
+ filter.type = [normalizedType];
185
+ }
186
+ if (options.tag) {
187
+ filter.tag = ensureArray(options.tag);
188
+ }
189
+ if (options.metaTag) {
190
+ filter.meta_tags = ensureArray(options.metaTag);
191
+ }
192
+ if (options.airDate) {
193
+ filter.air_date = ensureArray(options.airDate);
194
+ }
195
+ if (options.rating) {
196
+ filter.rating = ensureArray(options.rating);
197
+ }
198
+ if (options.ratingCount) {
199
+ filter.rating_count = ensureArray(options.ratingCount);
200
+ }
201
+ if (options.rank) {
202
+ filter.rank = ensureArray(options.rank);
203
+ }
204
+ if (options.nsfw !== undefined) {
205
+ filter.nsfw = parseOptionalBoolean(options.nsfw);
206
+ }
207
+
208
+ const result = await client.searchSubjects({
209
+ limit: parseOptionalInteger(options.limit),
210
+ offset: parseOptionalInteger(options.offset),
211
+ keyword,
212
+ sort: options.sort,
213
+ filter,
214
+ });
215
+ if (String(options.sort ?? "").toLowerCase() === "rank" && Array.isArray(result.data)) {
216
+ result.data = sortSubjectsByRank(result.data);
217
+ }
218
+ return {
219
+ ...result,
220
+ filters: {
221
+ mode: "search",
222
+ keyword,
223
+ type: normalizedType,
224
+ sort: options.sort ?? "match",
225
+ tag: options.tag ? ensureArray(options.tag) : [],
226
+ metaTags: options.metaTag ? ensureArray(options.metaTag) : [],
227
+ airDate: options.airDate ? ensureArray(options.airDate) : [],
228
+ rating: options.rating ? ensureArray(options.rating) : [],
229
+ ratingCount: options.ratingCount ? ensureArray(options.ratingCount) : [],
230
+ rank: options.rank ? ensureArray(options.rank) : [],
231
+ nsfw: options.nsfw !== undefined ? parseOptionalBoolean(options.nsfw) : undefined,
232
+ },
233
+ };
234
+ }
235
+
236
+ export async function executeSubjectP1ListCommand(command, args) {
237
+ const options = parseFlags(args);
238
+ const client = new BangumiClient(getConfig());
239
+ const subjectId = firstPositional(options);
240
+ if (!subjectId) {
241
+ throw new CommandError(`Usage: bgm subject ${command} <subject_id> [--limit n] [--offset n]`);
242
+ }
243
+
244
+ const limit = normalizePageSize(options.limit);
245
+ const offset = normalizeNonNegativeInteger(options.offset, "offset");
246
+ const method = {
247
+ characters: "listSubjectCharacters",
248
+ collects: "listSubjectCollects",
249
+ comments: "listSubjectComments",
250
+ indexes: "listSubjectIndexes",
251
+ recs: "listSubjectRecommendations",
252
+ relations: "listSubjectRelations",
253
+ reviews: "listSubjectReviews",
254
+ staff: "listSubjectStaffPersons",
255
+ "staff-positions": "listSubjectStaffPositions",
256
+ topics: "listSubjectTopics",
257
+ }[command];
258
+ const query = {
259
+ limit,
260
+ offset,
261
+ type: normalizeSubjectP1Type(command, options.type),
262
+ mode: options.mode,
263
+ offprint: parseOptionalBoolean(options.offprint),
264
+ position: parseOptionalInteger(options.position),
265
+ };
266
+ const result = normalizeListResult(await client[method](subjectId, query));
267
+ return {
268
+ ...result,
269
+ resource: `subject-${command}`,
270
+ title: `Subject ${command}`,
271
+ subjectId: Number(subjectId),
272
+ filters: {
273
+ subjectId: Number(subjectId),
274
+ limit,
275
+ offset,
276
+ type: query.type,
277
+ mode: query.mode,
278
+ offprint: query.offprint,
279
+ position: query.position,
280
+ },
281
+ };
282
+ }
283
+
284
+ export async function executeRecentSubjectTopicsCommand(args) {
285
+ const options = parseFlags(args);
286
+ const limit = normalizePageSize(options.limit);
287
+ const offset = normalizeNonNegativeInteger(options.offset, "offset");
288
+ const result = await new BangumiClient(getConfig()).listRecentSubjectTopics({
289
+ limit,
290
+ offset,
291
+ });
292
+
293
+ return {
294
+ ...result,
295
+ resource: "subject-recent-topics",
296
+ title: "Recent subject topics",
297
+ filters: {
298
+ limit,
299
+ offset,
300
+ },
301
+ };
302
+ }
303
+
304
+ function normalizeListResult(result) {
305
+ if (Array.isArray(result)) {
306
+ return {
307
+ data: result,
308
+ total: result.length,
309
+ };
310
+ }
311
+
312
+ return {
313
+ ...result,
314
+ data: Array.isArray(result?.data) ? result.data : [],
315
+ total: result?.total ?? (Array.isArray(result?.data) ? result.data.length : 0),
316
+ };
317
+ }
318
+
319
+ export async function executeSubjectTopicCommand(args) {
320
+ const options = parseFlags(args);
321
+ const topicId = firstPositional(options);
322
+ if (!topicId) {
323
+ throw new CommandError("Usage: bgm subject topic <topic_id>");
324
+ }
325
+
326
+ return new BangumiClient(getConfig()).getSubjectTopic(topicId);
327
+ }
328
+
329
+ export async function executeSubjectCreateTopicCommand(args, context = {}) {
330
+ const options = parseFlags(args);
331
+ const client = new BangumiClient(getConfig());
332
+ const subjectId = firstPositional(options);
333
+ const title = getPositional(options, 1) ?? options.title;
334
+ const content = getPositional(options, 2) ?? options.content;
335
+
336
+ if (!subjectId || !title || !content) {
337
+ throw new CommandError("Usage: bgm subject create-topic <subject_id> <title> <content> [--turnstile-token <token>] [--manual]");
338
+ }
339
+
340
+ const turnstileToken = await resolveTurnstileTokenForMutation(options, {
341
+ actionLabel: "create a subject topic",
342
+ context,
343
+ });
344
+ const result = await client.createSubjectTopic(subjectId, {
345
+ title,
346
+ content,
347
+ turnstileToken,
348
+ });
349
+
350
+ return buildTopicMutationResult("subject", "create-topic", {
351
+ subjectId,
352
+ topicId: result.id,
353
+ title,
354
+ url: result.id ? `https://bgm.tv/subject/topic/${result.id}` : undefined,
355
+ });
356
+ }
357
+
358
+ export async function executeSubjectEditTopicCommand(args) {
359
+ const options = parseFlags(args);
360
+ const topicId = firstPositional(options);
361
+ const title = getPositional(options, 1) ?? options.title;
362
+ const content = getPositional(options, 2) ?? options.content;
363
+ if (!topicId || !title || !content) {
364
+ throw new CommandError("Usage: bgm subject edit-topic <topic_id> <title> <content>");
365
+ }
366
+
367
+ await new BangumiClient(getConfig()).updateSubjectTopic(topicId, { title, content });
368
+ return buildTopicMutationResult("subject", "edit-topic", {
369
+ topicId,
370
+ title,
371
+ url: `https://bgm.tv/subject/topic/${topicId}`,
372
+ });
373
+ }
374
+
375
+ export async function executeSubjectReplyCommand(args, context = {}) {
376
+ const options = parseFlags(args);
377
+ const client = new BangumiClient(getConfig());
378
+ const topicId = firstPositional(options);
379
+ const content = getPositional(options, 1) ?? options.content;
380
+ const replyTo = normalizeNonNegativeInteger(options.replyTo, "reply-to") ?? 0;
381
+ if (!topicId || !content) {
382
+ throw new CommandError("Usage: bgm subject reply <topic_id> <content> [--reply-to <post_id>] [--turnstile-token <token>] [--manual]");
383
+ }
384
+
385
+ const turnstileToken = await resolveTurnstileTokenForMutation(options, {
386
+ actionLabel: "reply to a subject topic",
387
+ context,
388
+ });
389
+ const result = await client.createSubjectReply(topicId, {
390
+ content,
391
+ replyTo,
392
+ turnstileToken,
393
+ });
394
+
395
+ return buildTopicMutationResult("subject", "reply", {
396
+ topicId,
397
+ postId: result.id,
398
+ replyTo,
399
+ url: `https://bgm.tv/subject/topic/${topicId}`,
400
+ });
401
+ }
402
+
403
+ export async function executeSubjectPostCommand(args) {
404
+ const options = parseFlags(args);
405
+ const postId = firstPositional(options);
406
+ if (!postId) {
407
+ throw new CommandError("Usage: bgm subject post <post_id>");
408
+ }
409
+
410
+ return {
411
+ resource: "topic-post",
412
+ scope: "subject",
413
+ postId: Number(postId),
414
+ data: await new BangumiClient(getConfig()).getSubjectPost(postId),
415
+ };
416
+ }
417
+
418
+ export async function executeSubjectEditPostCommand(args) {
419
+ const options = parseFlags(args);
420
+ const postId = firstPositional(options);
421
+ const content = getPositional(options, 1) ?? options.content;
422
+ if (!postId || !content) {
423
+ throw new CommandError("Usage: bgm subject edit-post <post_id> <content>");
424
+ }
425
+
426
+ await new BangumiClient(getConfig()).updateSubjectPost(postId, { content });
427
+ return buildPostMutationResult("subject", "edit-post", { postId });
428
+ }
429
+
430
+ export async function executeSubjectDeletePostCommand(args) {
431
+ const options = parseFlags(args);
432
+ const postId = firstPositional(options);
433
+ if (!postId) {
434
+ throw new CommandError("Usage: bgm subject delete-post <post_id>");
435
+ }
436
+
437
+ await new BangumiClient(getConfig()).deleteSubjectPost(postId);
438
+ return buildPostMutationResult("subject", "delete-post", { postId });
439
+ }
440
+
441
+ export async function executeSubjectLikePostCommand(args) {
442
+ const options = parseFlags(args);
443
+ const postId = firstPositional(options);
444
+ const value = normalizeBangumiReactionValue(options.value ?? getPositional(options, 1), "subjectPost");
445
+ if (!postId || value === undefined) {
446
+ throw new CommandError("Usage: bgm subject like-post <post_id> <value>");
447
+ }
448
+
449
+ await new BangumiClient(getConfig()).likeSubjectPost(postId, value);
450
+ return buildPostMutationResult("subject", "like-post", { postId, value });
451
+ }
452
+
453
+ export async function executeSubjectUnlikePostCommand(args) {
454
+ const options = parseFlags(args);
455
+ const postId = firstPositional(options);
456
+ if (!postId) {
457
+ throw new CommandError("Usage: bgm subject unlike-post <post_id>");
458
+ }
459
+
460
+ await new BangumiClient(getConfig()).unlikeSubjectPost(postId);
461
+ return buildPostMutationResult("subject", "unlike-post", { postId });
462
+ }
463
+
464
+ export async function executeSubjectLikeCollectCommand(args) {
465
+ const options = parseFlags(args);
466
+ const collectId = firstPositional(options);
467
+ const value = normalizeBangumiReactionValue(options.value ?? getPositional(options, 1), "subjectCollect");
468
+ if (!collectId || value === undefined) {
469
+ throw new CommandError("Usage: bgm subject like-collect <collect_id> <value>");
470
+ }
471
+
472
+ await new BangumiClient(getConfig()).likeSubjectCollect(collectId, value);
473
+ return {
474
+ resource: "subject-collect-mutation",
475
+ action: "like-collect",
476
+ collectId: Number(collectId),
477
+ value,
478
+ };
479
+ }
480
+
481
+ export async function executeSubjectUnlikeCollectCommand(args) {
482
+ const options = parseFlags(args);
483
+ const collectId = firstPositional(options);
484
+ if (!collectId) {
485
+ throw new CommandError("Usage: bgm subject unlike-collect <collect_id>");
486
+ }
487
+
488
+ await new BangumiClient(getConfig()).unlikeSubjectCollect(collectId);
489
+ return {
490
+ resource: "subject-collect-mutation",
491
+ action: "unlike-collect",
492
+ collectId: Number(collectId),
493
+ };
494
+ }
495
+
496
+ function buildTopicMutationResult(scope, action, details) {
497
+ return {
498
+ resource: "topic-mutation",
499
+ scope,
500
+ action,
501
+ ...normalizeNumericIds(details),
502
+ };
503
+ }
504
+
505
+ function buildPostMutationResult(scope, action, details) {
506
+ return {
507
+ resource: "post-mutation",
508
+ scope,
509
+ action,
510
+ ...normalizeNumericIds(details),
511
+ };
512
+ }
513
+
514
+ function normalizeNumericIds(details) {
515
+ const normalized = { ...details };
516
+ for (const key of ["subjectId", "topicId", "postId", "replyTo"]) {
517
+ if (normalized[key] !== undefined) {
518
+ normalized[key] = Number(normalized[key]);
519
+ }
520
+ }
521
+ return normalized;
522
+ }
523
+
524
+ function normalizeSubjectP1Type(command, value) {
525
+ if (value === undefined || value === null || value === "") {
526
+ return undefined;
527
+ }
528
+
529
+ if (command === "relations") {
530
+ return normalizeSubjectType(value);
531
+ }
532
+ if (command === "collects" || command === "comments") {
533
+ return normalizeCollectionStatusValue(value);
534
+ }
535
+
536
+ return parseOptionalInteger(value);
537
+ }