@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,362 @@
1
+ import { BangumiClient } from "../core/client.js";
2
+ import { getConfig } from "../core/config.js";
3
+ import { CommandError, printResult } from "../core/output.js";
4
+ import { firstPositional, getPositional, parseFlags } from "../utils/args.js";
5
+ import {
6
+ normalizeNonNegativeInteger,
7
+ normalizePageSize,
8
+ normalizePositiveInteger,
9
+ parseOptionalBoolean,
10
+ resolveUsernameOrMe,
11
+ } from "../utils/helpers.js";
12
+ import {
13
+ normalizeIndexRelatedCategory,
14
+ normalizeSubjectType,
15
+ } from "../utils/validators.js";
16
+ import { resolveTurnstileTokenForMutation } from "../utils/turnstile-flow.js";
17
+
18
+ export async function runIndexCommand(command, args, context) {
19
+ switch (command) {
20
+ case "create": {
21
+ const result = await executeIndexCreateCommand(args);
22
+ printResult(result, context);
23
+ return;
24
+ }
25
+ case "get": {
26
+ const result = await executeIndexGetCommand(args);
27
+ printResult(result, context);
28
+ return;
29
+ }
30
+ case "update": {
31
+ const result = await executeIndexUpdateCommand(args);
32
+ printResult(result, context);
33
+ return;
34
+ }
35
+ case "delete": {
36
+ const result = await executeIndexDeleteCommand(args);
37
+ printResult(result, context);
38
+ return;
39
+ }
40
+ case "comments": {
41
+ const result = await executeIndexCommentsCommand(args);
42
+ printResult(result, context);
43
+ return;
44
+ }
45
+ case "comment": {
46
+ const result = await executeIndexCommentCommand(args, context);
47
+ printResult(result, context);
48
+ return;
49
+ }
50
+ case "edit-comment": {
51
+ const result = await executeIndexEditCommentCommand(args);
52
+ printResult(result, context);
53
+ return;
54
+ }
55
+ case "delete-comment": {
56
+ const result = await executeIndexDeleteCommentCommand(args);
57
+ printResult(result, context);
58
+ return;
59
+ }
60
+ case "related": {
61
+ const result = await executeIndexRelatedCommand(args);
62
+ printResult(result, context);
63
+ return;
64
+ }
65
+ case "user": {
66
+ const result = await executeUserIndexesCommand(args);
67
+ printResult(result, context);
68
+ return;
69
+ }
70
+ case "add-related": {
71
+ const result = await executeIndexAddRelatedCommand(args);
72
+ printResult(result, context);
73
+ return;
74
+ }
75
+ case "update-related": {
76
+ const result = await executeIndexUpdateRelatedCommand(args);
77
+ printResult(result, context);
78
+ return;
79
+ }
80
+ case "delete-related": {
81
+ const result = await executeIndexDeleteRelatedCommand(args);
82
+ printResult(result, context);
83
+ return;
84
+ }
85
+ default:
86
+ throw new CommandError(
87
+ "Usage: bgm index <create|get|update|delete|comments|comment|edit-comment|delete-comment|related|user|add-related|update-related|delete-related> ...",
88
+ );
89
+ }
90
+ }
91
+
92
+ export async function executeIndexCreateCommand(args) {
93
+ const options = parseFlags(args);
94
+ const client = new BangumiClient(getConfig());
95
+ const title = firstPositional(options) ?? options.title;
96
+ const desc = getPositional(options, 1) ?? options.desc;
97
+ if (!title || desc === undefined) {
98
+ throw new CommandError("Usage: bgm index create <title> <desc> [--private <true|false>]");
99
+ }
100
+
101
+ const isPrivate = parseOptionalBoolean(options.private) ?? false;
102
+ const result = await client.createIndex({
103
+ title,
104
+ desc,
105
+ private: isPrivate,
106
+ });
107
+
108
+ return {
109
+ resource: "index-mutation",
110
+ action: "create",
111
+ indexId: result.id,
112
+ title: String(title),
113
+ private: isPrivate,
114
+ };
115
+ }
116
+
117
+ export async function executeIndexGetCommand(args) {
118
+ const options = parseFlags(args);
119
+ const client = new BangumiClient(getConfig());
120
+ const indexId = firstPositional(options);
121
+ if (!indexId) {
122
+ throw new CommandError("Usage: bgm index get <index_id>");
123
+ }
124
+
125
+ return client.getIndex(indexId);
126
+ }
127
+
128
+ export async function executeIndexUpdateCommand(args) {
129
+ const options = parseFlags(args);
130
+ const client = new BangumiClient(getConfig());
131
+ const indexId = firstPositional(options);
132
+ if (!indexId) {
133
+ throw new CommandError("Usage: bgm index update <index_id> [--title <title>] [--desc <desc>] [--private <true|false>]");
134
+ }
135
+
136
+ const payload = {};
137
+ if (options.title !== undefined) {
138
+ payload.title = options.title;
139
+ }
140
+ if (options.desc !== undefined) {
141
+ payload.desc = options.desc;
142
+ }
143
+ if (options.private !== undefined) {
144
+ payload.private = parseOptionalBoolean(options.private);
145
+ }
146
+ if (Object.keys(payload).length === 0) {
147
+ throw new CommandError("At least one of --title, --desc, or --private is required.");
148
+ }
149
+
150
+ await client.updateIndex(indexId, payload);
151
+ return {
152
+ resource: "index-mutation",
153
+ action: "update",
154
+ indexId: Number(indexId),
155
+ ...payload,
156
+ };
157
+ }
158
+
159
+ export async function executeIndexDeleteCommand(args) {
160
+ const options = parseFlags(args);
161
+ const client = new BangumiClient(getConfig());
162
+ const indexId = firstPositional(options);
163
+ if (!indexId) {
164
+ throw new CommandError("Usage: bgm index delete <index_id>");
165
+ }
166
+
167
+ await client.deleteIndex(indexId);
168
+ return {
169
+ resource: "index-mutation",
170
+ action: "delete",
171
+ indexId: Number(indexId),
172
+ };
173
+ }
174
+
175
+ export async function executeIndexCommentsCommand(args) {
176
+ const options = parseFlags(args);
177
+ const client = new BangumiClient(getConfig());
178
+ const indexId = firstPositional(options);
179
+ if (!indexId) {
180
+ throw new CommandError("Usage: bgm index comments <index_id>");
181
+ }
182
+
183
+ const data = await client.listIndexComments(indexId);
184
+ return {
185
+ resource: "index-comments",
186
+ indexId: Number(indexId),
187
+ data,
188
+ };
189
+ }
190
+
191
+ export async function executeIndexCommentCommand(args, context = {}) {
192
+ const options = parseFlags(args);
193
+ const client = new BangumiClient(getConfig());
194
+ const indexId = firstPositional(options);
195
+ const content = getPositional(options, 1) ?? options.content;
196
+ const replyTo = normalizeNonNegativeInteger(options.replyTo, "reply-to") ?? 0;
197
+ if (!indexId || !content) {
198
+ throw new CommandError("Usage: bgm index comment <index_id> <content> [--reply-to <comment_id>] [--turnstile-token <token>] [--manual] [--listen-host <host>] [--port <n>] [--public-origin <url>] [--timeout-seconds <n>]");
199
+ }
200
+
201
+ const turnstileToken = await resolveTurnstileTokenForMutation(options, {
202
+ actionLabel: "comment on an index",
203
+ context,
204
+ });
205
+
206
+ const result = await client.createIndexComment(indexId, {
207
+ content,
208
+ replyTo,
209
+ turnstileToken,
210
+ });
211
+
212
+ return {
213
+ resource: "index-comment-mutation",
214
+ action: "reply",
215
+ indexId: Number(indexId),
216
+ commentId: result.id,
217
+ replyTo,
218
+ url: `https://bgm.tv/index/${indexId}`,
219
+ };
220
+ }
221
+
222
+ export async function executeIndexEditCommentCommand(args) {
223
+ const options = parseFlags(args);
224
+ const client = new BangumiClient(getConfig());
225
+ const commentId = firstPositional(options);
226
+ const content = getPositional(options, 1) ?? options.content;
227
+ if (!commentId || !content) {
228
+ throw new CommandError("Usage: bgm index edit-comment <comment_id> <content>");
229
+ }
230
+
231
+ await client.updateIndexComment(commentId, { content });
232
+ return {
233
+ resource: "index-comment-mutation",
234
+ action: "edit",
235
+ commentId: Number(commentId),
236
+ };
237
+ }
238
+
239
+ export async function executeIndexDeleteCommentCommand(args) {
240
+ const options = parseFlags(args);
241
+ const client = new BangumiClient(getConfig());
242
+ const commentId = firstPositional(options);
243
+ if (!commentId) {
244
+ throw new CommandError("Usage: bgm index delete-comment <comment_id>");
245
+ }
246
+
247
+ await client.deleteIndexComment(commentId);
248
+ return {
249
+ resource: "index-comment-mutation",
250
+ action: "delete",
251
+ commentId: Number(commentId),
252
+ };
253
+ }
254
+
255
+ export async function executeIndexRelatedCommand(args) {
256
+ const options = parseFlags(args);
257
+ const client = new BangumiClient(getConfig());
258
+ const indexId = firstPositional(options);
259
+ if (!indexId) {
260
+ throw new CommandError("Usage: bgm index related <index_id> [--cat <subject|character|person|ep|blog|group_topic|subject_topic>] [--type <book|anime|music|game|real>] [--limit n] [--offset n]");
261
+ }
262
+
263
+ const cat = normalizeIndexRelatedCategory(options.cat);
264
+ const type = normalizeSubjectType(options.type);
265
+ const limit = normalizePageSize(options.limit);
266
+ const offset = normalizeNonNegativeInteger(options.offset, "offset");
267
+ const result = await client.listIndexRelated(indexId, { cat, type, limit, offset });
268
+ return {
269
+ ...result,
270
+ resource: "index-related",
271
+ indexId: Number(indexId),
272
+ filters: { cat, type, limit, offset },
273
+ };
274
+ }
275
+
276
+ export async function executeUserIndexesCommand(args) {
277
+ const options = parseFlags(args);
278
+ const client = new BangumiClient(getConfig());
279
+ const username = await resolveUsernameOrMe(client, firstPositional(options));
280
+
281
+ const limit = normalizePageSize(options.limit);
282
+ const offset = normalizeNonNegativeInteger(options.offset, "offset");
283
+ const result = await client.listUserIndexes(username, { limit, offset });
284
+ return {
285
+ ...result,
286
+ resource: "index-list",
287
+ title: "User indexes",
288
+ username: String(username),
289
+ filters: { limit, offset },
290
+ };
291
+ }
292
+
293
+ export async function executeIndexAddRelatedCommand(args) {
294
+ const options = parseFlags(args);
295
+ const client = new BangumiClient(getConfig());
296
+ const indexId = firstPositional(options);
297
+ const cat = normalizeIndexRelatedCategory(options.cat);
298
+ const sid = normalizePositiveInteger(options.sid, "sid");
299
+ if (!indexId || cat === undefined || sid === undefined) {
300
+ throw new CommandError("Usage: bgm index add-related <index_id> --cat <subject|character|person|ep|blog|group_topic|subject_topic> --sid <sid> [--order <n>] [--comment <text>] [--award <text>]");
301
+ }
302
+
303
+ const order = normalizeNonNegativeInteger(options.order, "order");
304
+ const result = await client.addIndexRelated(indexId, {
305
+ cat,
306
+ sid,
307
+ order,
308
+ comment: options.comment,
309
+ award: options.award,
310
+ });
311
+ return {
312
+ resource: "index-related-mutation",
313
+ action: "add",
314
+ indexId: Number(indexId),
315
+ relatedId: result.id,
316
+ cat,
317
+ sid,
318
+ order: order ?? 0,
319
+ };
320
+ }
321
+
322
+ export async function executeIndexUpdateRelatedCommand(args) {
323
+ const options = parseFlags(args);
324
+ const client = new BangumiClient(getConfig());
325
+ const indexId = firstPositional(options);
326
+ const relatedId = getPositional(options, 1);
327
+ const order = normalizeNonNegativeInteger(options.order, "order");
328
+ const comment = options.comment;
329
+ if (!indexId || !relatedId || order === undefined || comment === undefined) {
330
+ throw new CommandError("Usage: bgm index update-related <index_id> <related_id> --order <n> --comment <text>");
331
+ }
332
+
333
+ await client.updateIndexRelated(indexId, relatedId, {
334
+ order,
335
+ comment,
336
+ });
337
+ return {
338
+ resource: "index-related-mutation",
339
+ action: "update",
340
+ indexId: Number(indexId),
341
+ relatedId: Number(relatedId),
342
+ order,
343
+ };
344
+ }
345
+
346
+ export async function executeIndexDeleteRelatedCommand(args) {
347
+ const options = parseFlags(args);
348
+ const client = new BangumiClient(getConfig());
349
+ const indexId = firstPositional(options);
350
+ const relatedId = getPositional(options, 1);
351
+ if (!indexId || !relatedId) {
352
+ throw new CommandError("Usage: bgm index delete-related <index_id> <related_id>");
353
+ }
354
+
355
+ await client.deleteIndexRelated(indexId, relatedId);
356
+ return {
357
+ resource: "index-related-mutation",
358
+ action: "delete",
359
+ indexId: Number(indexId),
360
+ relatedId: Number(relatedId),
361
+ };
362
+ }
@@ -0,0 +1,74 @@
1
+ import { BangumiClient } from "../core/client.js";
2
+ import { getConfig } from "../core/config.js";
3
+ import { CommandError, printResult } from "../core/output.js";
4
+ import { parseFlags } from "../utils/args.js";
5
+ import {
6
+ normalizeNonNegativeInteger,
7
+ normalizePositiveInteger,
8
+ parseOptionalBoolean,
9
+ } from "../utils/helpers.js";
10
+
11
+ export async function runNotifyCommand(command, args, context) {
12
+ if (!command || String(command).startsWith("--")) {
13
+ const result = await executeNotifyListCommand(command ? [command, ...args] : args);
14
+ printResult(result, context);
15
+ return;
16
+ }
17
+
18
+ switch (command) {
19
+ case "list": {
20
+ const result = await executeNotifyListCommand(args);
21
+ printResult(result, context);
22
+ return;
23
+ }
24
+ case "clear": {
25
+ const result = await executeNotifyClearCommand(args);
26
+ printResult(result, context);
27
+ return;
28
+ }
29
+ default:
30
+ throw new CommandError("Usage: bgm notify [list|clear] ...");
31
+ }
32
+ }
33
+
34
+ export async function executeNotifyListCommand(args) {
35
+ const options = parseFlags(args);
36
+ const limit = normalizeNotifyLimit(options.limit);
37
+ const unread = parseOptionalBoolean(options.unread);
38
+ const result = await new BangumiClient(getConfig()).listNotifications({
39
+ limit,
40
+ unread,
41
+ });
42
+
43
+ return {
44
+ ...result,
45
+ resource: "notifications",
46
+ filters: {
47
+ limit,
48
+ unread,
49
+ },
50
+ };
51
+ }
52
+
53
+ export async function executeNotifyClearCommand(args) {
54
+ const options = parseFlags(args);
55
+ const ids = options._.map((id) => normalizePositiveInteger(id, "notification id"));
56
+
57
+ await new BangumiClient(getConfig()).clearNotifications(ids);
58
+ return {
59
+ resource: "notification-clear",
60
+ cleared: ids.length > 0 ? "selected" : "all",
61
+ ids,
62
+ };
63
+ }
64
+
65
+ function normalizeNotifyLimit(value) {
66
+ const limit = normalizeNonNegativeInteger(value, "limit");
67
+ if (limit === undefined) {
68
+ return undefined;
69
+ }
70
+ if (limit > 40) {
71
+ throw new CommandError(`Expected limit to be <= 40, received: ${value}`);
72
+ }
73
+ return limit;
74
+ }