@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,297 @@
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, splitFilterValues } from "../utils/args.js";
5
+ import {
6
+ normalizeNonNegativeInteger,
7
+ normalizePageSize,
8
+ parseOptionalInteger,
9
+ } from "../utils/helpers.js";
10
+ import { normalizeSubjectType } from "../utils/validators.js";
11
+ import { resolveTurnstileTokenForMutation } from "../utils/turnstile-flow.js";
12
+
13
+ export async function runPersonCommand(command, args, context) {
14
+ const executor = {
15
+ get: executePersonGetCommand,
16
+ search: executePersonSearchCommand,
17
+ casts: executePersonCastsCommand,
18
+ collects: executePersonCollectsCommand,
19
+ comments: executePersonCommentsCommand,
20
+ indexes: executePersonIndexesCommand,
21
+ photos: executePersonPhotosCommand,
22
+ "photos-preview": executePersonPhotoPreviewCommand,
23
+ photo: executePersonPhotoCommand,
24
+ "photo-comments": executePersonPhotoCommentsCommand,
25
+ relations: executePersonRelationsCommand,
26
+ works: executePersonWorksCommand,
27
+ comment: executePersonCommentCommand,
28
+ "edit-comment": executePersonEditCommentCommand,
29
+ "delete-comment": executePersonDeleteCommentCommand,
30
+ }[command];
31
+
32
+ if (!executor) {
33
+ throw new CommandError("Usage: bgm person <get|search|casts|collects|comments|indexes|photos|photos-preview|photo|photo-comments|relations|works|comment|edit-comment|delete-comment> ...");
34
+ }
35
+
36
+ const result = await executor(args, context);
37
+ printResult(result, context);
38
+ }
39
+
40
+ async function executePersonGetCommand(args) {
41
+ const options = parseFlags(args);
42
+ const personId = firstPositional(options);
43
+ if (!personId) {
44
+ throw new CommandError("Usage: bgm person get <person_id>");
45
+ }
46
+
47
+ return {
48
+ resource: "person",
49
+ data: await new BangumiClient(getConfig()).getPerson(personId),
50
+ };
51
+ }
52
+
53
+ async function executePersonSearchCommand(args) {
54
+ const options = parseFlags(args);
55
+ const keyword = firstPositional(options);
56
+ if (!keyword) {
57
+ throw new CommandError("Usage: bgm person search <keyword> [--career <seiyu,writer,...>] [--limit n] [--offset n]");
58
+ }
59
+
60
+ const limit = normalizePageSize(options.limit);
61
+ const offset = normalizeNonNegativeInteger(options.offset, "offset");
62
+ const career = normalizeCareerFilter(options.career);
63
+ const result = await new BangumiClient(getConfig()).searchPersons({
64
+ keyword,
65
+ limit,
66
+ offset,
67
+ filter: career ? { career } : {},
68
+ });
69
+ return wrapList(result, "person-search", "Persons", { keyword, limit, offset, career: career ?? [] });
70
+ }
71
+
72
+ async function executePersonCastsCommand(args) {
73
+ const { client, id, query } = parsePersonListArgs(args, "casts", "[--type <main|support|guest|n>] [--subject-type <book|anime|music|game|real>]");
74
+ const result = await client.listPersonCasts(id, {
75
+ ...query,
76
+ type: normalizeCastType(query.type),
77
+ subjectType: normalizeSubjectType(query.subjectType),
78
+ });
79
+ return wrapList(result, "person-casts", "Person casts", { personId: Number(id), ...query });
80
+ }
81
+
82
+ async function executePersonCollectsCommand(args) {
83
+ const { client, id, query } = parsePersonListArgs(args, "collects");
84
+ const result = await client.listPersonCollects(id, query);
85
+ return wrapList(result, "person-collects", "Person collectors", { personId: Number(id), ...query });
86
+ }
87
+
88
+ async function executePersonCommentsCommand(args) {
89
+ const { client, id, query } = parsePersonListArgs(args, "comments");
90
+ const result = await client.listPersonComments(id, query);
91
+ return wrapList(result, "person-comments", "Person comments", { personId: Number(id), ...query });
92
+ }
93
+
94
+ async function executePersonIndexesCommand(args) {
95
+ const { client, id, query } = parsePersonListArgs(args, "indexes");
96
+ const result = await client.listPersonIndexes(id, query);
97
+ return wrapList(result, "person-indexes", "Person indexes", { personId: Number(id), ...query });
98
+ }
99
+
100
+ async function executePersonPhotosCommand(args) {
101
+ const { client, id, query } = parsePersonListArgs(args, "photos");
102
+ const result = await client.listPersonPhotos(id, query);
103
+ return wrapList(result, "person-photos", "Person photos", { personId: Number(id), ...query });
104
+ }
105
+
106
+ async function executePersonPhotoPreviewCommand(args) {
107
+ const options = parseFlags(args);
108
+ const personId = firstPositional(options);
109
+ if (!personId) {
110
+ throw new CommandError("Usage: bgm person photos-preview <person_id> [--limit n]");
111
+ }
112
+
113
+ const limit = normalizePhotoPreviewLimit(options.limit);
114
+ const result = await new BangumiClient(getConfig()).listPersonPhotoPreview(personId, { limit });
115
+ return wrapList(result, "person-photos-preview", "Person photo preview", { personId: Number(personId), limit });
116
+ }
117
+
118
+ async function executePersonPhotoCommand(args) {
119
+ const options = parseFlags(args);
120
+ const personId = firstPositional(options);
121
+ const photoId = getPositional(options, 1);
122
+ if (!personId || !photoId) {
123
+ throw new CommandError("Usage: bgm person photo <person_id> <photo_id>");
124
+ }
125
+
126
+ return {
127
+ resource: "mono-photo",
128
+ scope: "person",
129
+ targetId: Number(personId),
130
+ photoId: Number(photoId),
131
+ data: await new BangumiClient(getConfig()).getPersonPhoto(personId, photoId),
132
+ };
133
+ }
134
+
135
+ async function executePersonPhotoCommentsCommand(args) {
136
+ const options = parseFlags(args);
137
+ const personId = firstPositional(options);
138
+ const photoId = getPositional(options, 1);
139
+ if (!personId || !photoId) {
140
+ throw new CommandError("Usage: bgm person photo-comments <person_id> <photo_id>");
141
+ }
142
+
143
+ const data = await new BangumiClient(getConfig()).listPersonPhotoComments(personId, photoId);
144
+ return {
145
+ resource: "mono-photo-comments",
146
+ scope: "person",
147
+ targetId: Number(personId),
148
+ photoId: Number(photoId),
149
+ data,
150
+ };
151
+ }
152
+
153
+ async function executePersonRelationsCommand(args) {
154
+ const { client, id, query } = parsePersonListArgs(args, "relations");
155
+ const result = await client.listPersonRelations(id, query);
156
+ return wrapList(result, "person-relations", "Person relations", { personId: Number(id), ...query });
157
+ }
158
+
159
+ async function executePersonWorksCommand(args) {
160
+ const { client, id, query } = parsePersonListArgs(args, "works");
161
+ const result = await client.listPersonWorks(id, query);
162
+ return wrapList(result, "person-works", "Person works", { personId: Number(id), ...query });
163
+ }
164
+
165
+ async function executePersonCommentCommand(args, context = {}) {
166
+ const options = parseFlags(args);
167
+ const personId = firstPositional(options);
168
+ const content = getPositional(options, 1) ?? options.content;
169
+ const replyTo = normalizeNonNegativeInteger(options.replyTo, "reply-to") ?? 0;
170
+ if (!personId || !content) {
171
+ throw new CommandError("Usage: bgm person comment <person_id> <content> [--reply-to <comment_id>] [--turnstile-token <token>] [--manual]");
172
+ }
173
+
174
+ const turnstileToken = await resolveTurnstileTokenForMutation(options, {
175
+ actionLabel: "create a person comment",
176
+ context,
177
+ });
178
+ const result = await new BangumiClient(getConfig()).createPersonComment(personId, {
179
+ content,
180
+ replyTo,
181
+ turnstileToken,
182
+ });
183
+
184
+ return buildCommentMutationResult("person", "create", {
185
+ targetId: personId,
186
+ commentId: result.id,
187
+ replyTo,
188
+ });
189
+ }
190
+
191
+ async function executePersonEditCommentCommand(args) {
192
+ const options = parseFlags(args);
193
+ const commentId = firstPositional(options);
194
+ const content = getPositional(options, 1) ?? options.content;
195
+ if (!commentId || !content) {
196
+ throw new CommandError("Usage: bgm person edit-comment <comment_id> <content>");
197
+ }
198
+
199
+ await new BangumiClient(getConfig()).updatePersonComment(commentId, { content });
200
+ return buildCommentMutationResult("person", "edit", { commentId });
201
+ }
202
+
203
+ async function executePersonDeleteCommentCommand(args) {
204
+ const options = parseFlags(args);
205
+ const commentId = firstPositional(options);
206
+ if (!commentId) {
207
+ throw new CommandError("Usage: bgm person delete-comment <comment_id>");
208
+ }
209
+
210
+ await new BangumiClient(getConfig()).deletePersonComment(commentId);
211
+ return buildCommentMutationResult("person", "delete", { commentId });
212
+ }
213
+
214
+ function parsePersonListArgs(args, subcommand, suffix = "") {
215
+ const options = parseFlags(args);
216
+ const id = firstPositional(options);
217
+ if (!id) {
218
+ throw new CommandError(`Usage: bgm person ${subcommand} <person_id> ${suffix} [--limit n] [--offset n]`.trim());
219
+ }
220
+
221
+ return {
222
+ client: new BangumiClient(getConfig()),
223
+ id,
224
+ query: {
225
+ limit: normalizePageSize(options.limit),
226
+ offset: normalizeNonNegativeInteger(options.offset, "offset"),
227
+ type: options.type,
228
+ subjectType: options.subjectType,
229
+ },
230
+ };
231
+ }
232
+
233
+ function normalizeCastType(value) {
234
+ if (value === undefined || value === null || value === "") {
235
+ return undefined;
236
+ }
237
+
238
+ const mapped = {
239
+ main: 1,
240
+ major: 1,
241
+ support: 2,
242
+ supporting: 2,
243
+ guest: 3,
244
+ }[String(value).toLowerCase()];
245
+ return mapped ?? parseOptionalInteger(value);
246
+ }
247
+
248
+ function normalizePhotoPreviewLimit(value) {
249
+ const parsed = parseOptionalInteger(value);
250
+ if (parsed === undefined) {
251
+ return undefined;
252
+ }
253
+ if (parsed < 1 || parsed > 20) {
254
+ throw new CommandError(`Expected limit to be between 1 and 20, received: ${value}`);
255
+ }
256
+ return parsed;
257
+ }
258
+
259
+ function wrapList(result, resource, title, filters) {
260
+ const normalized = normalizeListResult(result);
261
+ return {
262
+ ...normalized,
263
+ resource,
264
+ title,
265
+ filters,
266
+ };
267
+ }
268
+
269
+ function normalizeCareerFilter(value) {
270
+ return value === undefined ? undefined : splitFilterValues(value);
271
+ }
272
+
273
+ function normalizeListResult(result) {
274
+ if (Array.isArray(result)) {
275
+ return {
276
+ data: result,
277
+ total: result.length,
278
+ };
279
+ }
280
+
281
+ return {
282
+ ...result,
283
+ data: Array.isArray(result?.data) ? result.data : [],
284
+ total: result?.total ?? (Array.isArray(result?.data) ? result.data.length : 0),
285
+ };
286
+ }
287
+
288
+ function buildCommentMutationResult(scope, action, details) {
289
+ return {
290
+ resource: "mono-comment-mutation",
291
+ scope,
292
+ action,
293
+ targetId: details.targetId !== undefined ? Number(details.targetId) : undefined,
294
+ commentId: details.commentId !== undefined ? Number(details.commentId) : undefined,
295
+ replyTo: details.replyTo !== undefined ? Number(details.replyTo) : undefined,
296
+ };
297
+ }
@@ -0,0 +1,74 @@
1
+ import { BangumiStatusClient } 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 { normalizePageSize } from "../utils/helpers.js";
6
+ import { normalizeStatusAudience, normalizeStatusSite } from "../utils/validators.js";
7
+ import { buildStatusCurrentPayload } from "../utils/status.js";
8
+
9
+ export async function runStatusCommand(command, args, context) {
10
+ if (!command || String(command).startsWith("--")) {
11
+ const result = await executeStatusCurrentCommand(command ? [command, ...args] : args);
12
+ printResult(result, context);
13
+ return;
14
+ }
15
+
16
+ switch (command) {
17
+ case "current": {
18
+ const result = await executeStatusCurrentCommand(args);
19
+ printResult(result, context);
20
+ return;
21
+ }
22
+ case "incidents": {
23
+ const result = await executeStatusIncidentsCommand(args);
24
+ printResult(result, context);
25
+ return;
26
+ }
27
+ default:
28
+ throw new CommandError("Usage: bgm status [current|incidents] ...");
29
+ }
30
+ }
31
+
32
+ export async function executeStatusCurrentCommand(args) {
33
+ const options = parseFlags(args);
34
+ const client = new BangumiStatusClient(getConfig());
35
+ const site = normalizeStatusSite(options.site);
36
+ const audience = normalizeStatusAudience(options.audience);
37
+ const current = await client.getCurrentStatus();
38
+
39
+ return buildStatusCurrentPayload(current, { site, audience });
40
+ }
41
+
42
+ export async function executeStatusIncidentsCommand(args) {
43
+ const options = parseFlags(args);
44
+ const client = new BangumiStatusClient(getConfig());
45
+ const site = normalizeStatusSite(options.site);
46
+ const audience = normalizeStatusAudience(options.audience);
47
+ const limit = normalizePageSize(options.limit) ?? 10;
48
+ const [current, feed] = await Promise.all([client.getCurrentStatus(), client.listIncidents()]);
49
+ const filtered = feed.entries.filter((entry) => {
50
+ if (site && entry.site !== site) {
51
+ return false;
52
+ }
53
+ if (audience && normalizeStatusAudience(entry.audience) !== audience) {
54
+ return false;
55
+ }
56
+ return true;
57
+ });
58
+ buildStatusCurrentPayload(current, { site, audience });
59
+
60
+ return {
61
+ resource: "status-incidents",
62
+ title: feed.title,
63
+ source: feed.link,
64
+ feedUrl: feed.feedUrl,
65
+ feedUpdatedAt: feed.updatedAt,
66
+ total: filtered.length,
67
+ filters: {
68
+ site,
69
+ audience,
70
+ limit,
71
+ },
72
+ data: filtered.slice(0, limit),
73
+ };
74
+ }