@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,288 @@
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
+ normalizeNonNegativeInteger,
8
+ normalizePositiveInteger,
9
+ resolveUsernameOrMe,
10
+ } from "../utils/helpers.js";
11
+ import {
12
+ normalizeTimelineLimit,
13
+ normalizeTimelineMode,
14
+ } from "../utils/validators.js";
15
+ import { resolveTurnstileTokenForMutation } from "../utils/turnstile-flow.js";
16
+
17
+ export async function runTimelineCommand(command, args, context) {
18
+ switch (command) {
19
+ case "list": {
20
+ const result = await executeTimelineListCommand(args);
21
+ printResult(result, context);
22
+ return;
23
+ }
24
+ case "events": {
25
+ const result = await executeTimelineEventsCommand(args);
26
+ printResult(result, context);
27
+ return;
28
+ }
29
+ case "user": {
30
+ const result = await executeTimelineUserCommand(args);
31
+ printResult(result, context);
32
+ return;
33
+ }
34
+ case "replies": {
35
+ const result = await executeTimelineRepliesCommand(args);
36
+ printResult(result, context);
37
+ return;
38
+ }
39
+ case "say": {
40
+ const result = await executeTimelineSayCommand(args, context);
41
+ printResult(result, context);
42
+ return;
43
+ }
44
+ case "reply": {
45
+ const result = await executeTimelineReplyCommand(args, context);
46
+ printResult(result, context);
47
+ return;
48
+ }
49
+ case "delete": {
50
+ const result = await executeTimelineDeleteCommand(args);
51
+ printResult(result, context);
52
+ return;
53
+ }
54
+ case "like": {
55
+ const result = await executeTimelineLikeCommand(args);
56
+ printResult(result, context);
57
+ return;
58
+ }
59
+ case "unlike": {
60
+ const result = await executeTimelineUnlikeCommand(args);
61
+ printResult(result, context);
62
+ return;
63
+ }
64
+ default:
65
+ throw new CommandError("Usage: bgm timeline <list|events|user|replies|say|reply|delete|like|unlike> ...");
66
+ }
67
+ }
68
+
69
+ export async function executeTimelineListCommand(args) {
70
+ const options = parseFlags(args);
71
+ const client = new BangumiClient(getConfig());
72
+ const mode = normalizeTimelineMode(options.mode);
73
+ const limit = normalizeTimelineLimit(options.limit);
74
+ const until = normalizeNonNegativeInteger(options.until, "until");
75
+ const data = await client.listTimeline({
76
+ mode,
77
+ limit,
78
+ until,
79
+ });
80
+
81
+ return {
82
+ resource: "timeline-list",
83
+ filters: {
84
+ mode,
85
+ limit,
86
+ until,
87
+ },
88
+ data,
89
+ };
90
+ }
91
+
92
+ export async function executeTimelineEventsCommand(args) {
93
+ const options = parseFlags(args);
94
+ const client = new BangumiClient(getConfig());
95
+ const mode = normalizeTimelineMode(options.mode);
96
+ const limit = normalizeTimelineLimit(options.limit) ?? 10;
97
+ const timeoutSeconds = normalizePositiveInteger(options.timeoutSeconds ?? options.timeout, "timeout-seconds") ?? 10;
98
+ const cat = normalizeTimelineCat(options.cat);
99
+ const data = await client.listTimelineEvents({
100
+ mode,
101
+ cat,
102
+ limit,
103
+ timeoutMs: timeoutSeconds * 1000,
104
+ });
105
+
106
+ return {
107
+ resource: "timeline-events",
108
+ filters: {
109
+ mode,
110
+ cat,
111
+ limit,
112
+ timeoutSeconds,
113
+ },
114
+ data,
115
+ };
116
+ }
117
+
118
+ export async function executeTimelineUserCommand(args) {
119
+ const options = parseFlags(args);
120
+ const client = new BangumiClient(getConfig());
121
+ const username = await resolveUsernameOrMe(client, firstPositional(options));
122
+
123
+ const limit = normalizeTimelineLimit(options.limit);
124
+ const until = normalizeNonNegativeInteger(options.until, "until");
125
+ const data = await client.listUserTimeline(username, {
126
+ limit,
127
+ until,
128
+ });
129
+
130
+ return {
131
+ resource: "timeline-user-list",
132
+ filters: {
133
+ user: String(username),
134
+ limit,
135
+ until,
136
+ },
137
+ data,
138
+ };
139
+ }
140
+
141
+ export async function executeTimelineRepliesCommand(args) {
142
+ const options = parseFlags(args);
143
+ const client = new BangumiClient(getConfig());
144
+ const timelineId = firstPositional(options);
145
+ if (!timelineId) {
146
+ throw new CommandError("Usage: bgm timeline replies <timeline_id>");
147
+ }
148
+
149
+ const data = await client.listTimelineReplies(timelineId);
150
+ return {
151
+ resource: "timeline-replies",
152
+ timelineId: Number(timelineId),
153
+ data,
154
+ };
155
+ }
156
+
157
+ export async function executeTimelineSayCommand(args, context = {}) {
158
+ const options = parseFlags(args);
159
+ const client = new BangumiClient(getConfig());
160
+ const content = firstPositional(options) ?? options.content;
161
+ if (!content) {
162
+ throw new CommandError("Usage: bgm timeline say <content> [--turnstile-token <token>] [--manual] [--listen-host <host>] [--port <n>] [--public-origin <url>] [--timeout-seconds <n>]");
163
+ }
164
+
165
+ const turnstileToken = await resolveTurnstileTokenForMutation(options, {
166
+ actionLabel: "post a timeline status",
167
+ context,
168
+ });
169
+
170
+ const result = await client.createTimeline({
171
+ content,
172
+ turnstileToken,
173
+ });
174
+
175
+ return {
176
+ resource: "timeline-mutation",
177
+ action: "say",
178
+ timelineId: result.id,
179
+ };
180
+ }
181
+
182
+ export async function executeTimelineReplyCommand(args, context = {}) {
183
+ const options = parseFlags(args);
184
+ const client = new BangumiClient(getConfig());
185
+ const timelineId = firstPositional(options);
186
+ const content = getPositional(options, 1) ?? options.content;
187
+ const replyTo = normalizeNonNegativeInteger(options.replyTo, "reply-to") ?? 0;
188
+
189
+ if (!timelineId || !content) {
190
+ throw new CommandError("Usage: bgm timeline reply <timeline_id> <content> [--reply-to <comment_id>] [--turnstile-token <token>] [--manual] [--listen-host <host>] [--port <n>] [--public-origin <url>] [--timeout-seconds <n>]");
191
+ }
192
+
193
+ const turnstileToken = await resolveTurnstileTokenForMutation(options, {
194
+ actionLabel: "reply to a timeline entry",
195
+ context,
196
+ });
197
+
198
+ const result = await client.createTimelineReply(timelineId, {
199
+ content,
200
+ replyTo,
201
+ turnstileToken,
202
+ });
203
+
204
+ return {
205
+ resource: "timeline-mutation",
206
+ action: "reply",
207
+ timelineId: Number(timelineId),
208
+ commentId: result.id,
209
+ replyTo,
210
+ };
211
+ }
212
+
213
+ export async function executeTimelineDeleteCommand(args) {
214
+ const options = parseFlags(args);
215
+ const client = new BangumiClient(getConfig());
216
+ const timelineId = firstPositional(options);
217
+ if (!timelineId) {
218
+ throw new CommandError("Usage: bgm timeline delete <timeline_id>");
219
+ }
220
+
221
+ await client.deleteTimeline(timelineId);
222
+ return {
223
+ resource: "timeline-mutation",
224
+ action: "delete",
225
+ timelineId: Number(timelineId),
226
+ };
227
+ }
228
+
229
+ export async function executeTimelineLikeCommand(args) {
230
+ const options = parseFlags(args);
231
+ const client = new BangumiClient(getConfig());
232
+ const timelineId = firstPositional(options);
233
+ const value = normalizeBangumiReactionValue(getPositional(options, 1) ?? options.value, "timeline");
234
+ if (!timelineId || value === undefined) {
235
+ throw new CommandError("Usage: bgm timeline like <timeline_id> <value>");
236
+ }
237
+
238
+ await client.likeTimeline(timelineId, value);
239
+ return {
240
+ resource: "timeline-mutation",
241
+ action: "like",
242
+ timelineId: Number(timelineId),
243
+ value,
244
+ };
245
+ }
246
+
247
+ export async function executeTimelineUnlikeCommand(args) {
248
+ const options = parseFlags(args);
249
+ const client = new BangumiClient(getConfig());
250
+ const timelineId = firstPositional(options);
251
+ if (!timelineId) {
252
+ throw new CommandError("Usage: bgm timeline unlike <timeline_id>");
253
+ }
254
+
255
+ await client.unlikeTimeline(timelineId);
256
+ return {
257
+ resource: "timeline-mutation",
258
+ action: "unlike",
259
+ timelineId: Number(timelineId),
260
+ };
261
+ }
262
+
263
+ function normalizeTimelineCat(value) {
264
+ if (value === undefined || value === null || value === "") {
265
+ return undefined;
266
+ }
267
+
268
+ if (/^\d+$/.test(String(value))) {
269
+ return Number(value);
270
+ }
271
+
272
+ const normalized = String(value).toLowerCase().replace(/[-_\s]+/g, "_");
273
+ const mapped = {
274
+ daily: 1,
275
+ wiki: 2,
276
+ subject: 3,
277
+ progress: 4,
278
+ status: 5,
279
+ blog: 6,
280
+ index: 7,
281
+ mono: 8,
282
+ doujin: 9,
283
+ }[normalized];
284
+ if (mapped === undefined) {
285
+ throw new CommandError(`Unsupported timeline cat: ${value}`);
286
+ }
287
+ return mapped;
288
+ }
@@ -0,0 +1,58 @@
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
+ normalizePageSize,
8
+ } from "../utils/helpers.js";
9
+ import { normalizeSubjectType } from "../utils/validators.js";
10
+
11
+ export async function runTrendingCommand(command, args, context) {
12
+ switch (command) {
13
+ case "subjects": {
14
+ const result = await executeTrendingSubjectsCommand(args);
15
+ printResult(result, context);
16
+ return;
17
+ }
18
+ case "subject-topics":
19
+ case "topics": {
20
+ const result = await executeTrendingSubjectTopicsCommand(args);
21
+ printResult(result, context);
22
+ return;
23
+ }
24
+ default:
25
+ throw new CommandError("Usage: bgm trending <subjects|subject-topics> ...");
26
+ }
27
+ }
28
+
29
+ async function executeTrendingSubjectsCommand(args) {
30
+ const options = parseFlags(args);
31
+ const type = normalizeSubjectType(options.type);
32
+ if (!type) {
33
+ throw new CommandError("Usage: bgm trending subjects --type <book|anime|music|game|real> [--limit n] [--offset n]");
34
+ }
35
+
36
+ const limit = normalizePageSize(options.limit);
37
+ const offset = normalizeNonNegativeInteger(options.offset, "offset");
38
+ const result = await new BangumiClient(getConfig()).listTrendingSubjects({ type, limit, offset });
39
+ return {
40
+ ...result,
41
+ resource: "trending-subjects",
42
+ title: "Trending subjects",
43
+ filters: { type, limit, offset },
44
+ };
45
+ }
46
+
47
+ async function executeTrendingSubjectTopicsCommand(args) {
48
+ const options = parseFlags(args);
49
+ const limit = normalizePageSize(options.limit);
50
+ const offset = normalizeNonNegativeInteger(options.offset, "offset");
51
+ const result = await new BangumiClient(getConfig()).listTrendingSubjectTopics({ limit, offset });
52
+ return {
53
+ ...result,
54
+ resource: "trending-subject-topics",
55
+ title: "Trending subject topics",
56
+ filters: { limit, offset },
57
+ };
58
+ }