@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,213 @@
|
|
|
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
|
+
resolveUsernameOrMe,
|
|
9
|
+
} from "../utils/helpers.js";
|
|
10
|
+
import { resolveTurnstileTokenForMutation } from "../utils/turnstile-flow.js";
|
|
11
|
+
|
|
12
|
+
export async function runBlogCommand(command, args, context) {
|
|
13
|
+
switch (command) {
|
|
14
|
+
case "list": {
|
|
15
|
+
const result = await executeBlogListCommand(args);
|
|
16
|
+
printResult(result, context);
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
case "get": {
|
|
20
|
+
const result = await executeBlogGetCommand(args);
|
|
21
|
+
printResult(result, context);
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
case "comments": {
|
|
25
|
+
const result = await executeBlogCommentsCommand(args);
|
|
26
|
+
printResult(result, context);
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
case "reply": {
|
|
30
|
+
const result = await executeBlogReplyCommand(args, context);
|
|
31
|
+
printResult(result, context);
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
case "edit-comment": {
|
|
35
|
+
const result = await executeBlogEditCommentCommand(args);
|
|
36
|
+
printResult(result, context);
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
case "delete-comment": {
|
|
40
|
+
const result = await executeBlogDeleteCommentCommand(args);
|
|
41
|
+
printResult(result, context);
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
case "photos": {
|
|
45
|
+
const result = await executeBlogPhotosCommand(args);
|
|
46
|
+
printResult(result, context);
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
case "subjects": {
|
|
50
|
+
const result = await executeBlogSubjectsCommand(args);
|
|
51
|
+
printResult(result, context);
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
default:
|
|
55
|
+
throw new CommandError("Usage: bgm blog <list|get|comments|reply|edit-comment|delete-comment|photos|subjects> ...");
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export async function executeBlogListCommand(args) {
|
|
60
|
+
const options = parseFlags(args);
|
|
61
|
+
const client = new BangumiClient(getConfig());
|
|
62
|
+
const username = await resolveUsernameOrMe(client, options.user);
|
|
63
|
+
const limit = normalizePageSize(options.limit);
|
|
64
|
+
const offset = normalizeNonNegativeInteger(options.offset, "offset");
|
|
65
|
+
const result = await client.listUserBlogs(username, {
|
|
66
|
+
limit,
|
|
67
|
+
offset,
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
return {
|
|
71
|
+
...result,
|
|
72
|
+
resource: "blog-list",
|
|
73
|
+
filters: {
|
|
74
|
+
user: username,
|
|
75
|
+
limit,
|
|
76
|
+
offset,
|
|
77
|
+
},
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export async function executeBlogGetCommand(args) {
|
|
82
|
+
const options = parseFlags(args);
|
|
83
|
+
const client = new BangumiClient(getConfig());
|
|
84
|
+
const entryId = firstPositional(options);
|
|
85
|
+
if (!entryId) {
|
|
86
|
+
throw new CommandError("Usage: bgm blog get <blog_id>");
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
return client.getBlogEntry(entryId);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export async function executeBlogCommentsCommand(args) {
|
|
93
|
+
const options = parseFlags(args);
|
|
94
|
+
const client = new BangumiClient(getConfig());
|
|
95
|
+
const entryId = firstPositional(options);
|
|
96
|
+
if (!entryId) {
|
|
97
|
+
throw new CommandError("Usage: bgm blog comments <blog_id>");
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
const data = await client.listBlogComments(entryId);
|
|
101
|
+
return {
|
|
102
|
+
resource: "blog-comments",
|
|
103
|
+
entryId: Number(entryId),
|
|
104
|
+
data,
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
export async function executeBlogReplyCommand(args, context = {}) {
|
|
109
|
+
const options = parseFlags(args);
|
|
110
|
+
const client = new BangumiClient(getConfig());
|
|
111
|
+
const entryId = firstPositional(options);
|
|
112
|
+
const content = getPositional(options, 1) ?? options.content;
|
|
113
|
+
const replyTo = normalizeNonNegativeInteger(options.replyTo, "reply-to") ?? 0;
|
|
114
|
+
|
|
115
|
+
if (!entryId || !content) {
|
|
116
|
+
throw new CommandError("Usage: bgm blog reply <blog_id> <content> [--reply-to <comment_id>] [--turnstile-token <token>] [--manual] [--listen-host <host>] [--port <n>] [--public-origin <url>] [--timeout-seconds <n>]");
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
const turnstileToken = await resolveTurnstileTokenForMutation(options, {
|
|
120
|
+
actionLabel: "reply to a blog",
|
|
121
|
+
context,
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
const result = await client.createBlogComment(entryId, {
|
|
125
|
+
content,
|
|
126
|
+
replyTo,
|
|
127
|
+
turnstileToken,
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
return {
|
|
131
|
+
resource: "blog-comment-mutation",
|
|
132
|
+
action: "reply",
|
|
133
|
+
entryId: Number(entryId),
|
|
134
|
+
commentId: result.id,
|
|
135
|
+
replyTo,
|
|
136
|
+
url: `https://bgm.tv/blog/${entryId}`,
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
export async function executeBlogEditCommentCommand(args) {
|
|
141
|
+
const options = parseFlags(args);
|
|
142
|
+
const client = new BangumiClient(getConfig());
|
|
143
|
+
const commentId = firstPositional(options);
|
|
144
|
+
const content = getPositional(options, 1) ?? options.content;
|
|
145
|
+
if (!commentId || !content) {
|
|
146
|
+
throw new CommandError("Usage: bgm blog edit-comment <comment_id> <content>");
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
await client.updateBlogComment(commentId, { content });
|
|
150
|
+
return {
|
|
151
|
+
resource: "blog-comment-mutation",
|
|
152
|
+
action: "edit",
|
|
153
|
+
commentId: Number(commentId),
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
export async function executeBlogDeleteCommentCommand(args) {
|
|
158
|
+
const options = parseFlags(args);
|
|
159
|
+
const client = new BangumiClient(getConfig());
|
|
160
|
+
const commentId = firstPositional(options);
|
|
161
|
+
if (!commentId) {
|
|
162
|
+
throw new CommandError("Usage: bgm blog delete-comment <comment_id>");
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
await client.deleteBlogComment(commentId);
|
|
166
|
+
return {
|
|
167
|
+
resource: "blog-comment-mutation",
|
|
168
|
+
action: "delete",
|
|
169
|
+
commentId: Number(commentId),
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
export async function executeBlogPhotosCommand(args) {
|
|
174
|
+
const options = parseFlags(args);
|
|
175
|
+
const client = new BangumiClient(getConfig());
|
|
176
|
+
const entryId = firstPositional(options);
|
|
177
|
+
if (!entryId) {
|
|
178
|
+
throw new CommandError("Usage: bgm blog photos <blog_id> [--limit n] [--offset n]");
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
const limit = normalizePageSize(options.limit);
|
|
182
|
+
const offset = normalizeNonNegativeInteger(options.offset, "offset");
|
|
183
|
+
const result = await client.listBlogPhotos(entryId, {
|
|
184
|
+
limit,
|
|
185
|
+
offset,
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
return {
|
|
189
|
+
...result,
|
|
190
|
+
resource: "blog-photos",
|
|
191
|
+
entryId: Number(entryId),
|
|
192
|
+
filters: {
|
|
193
|
+
limit,
|
|
194
|
+
offset,
|
|
195
|
+
},
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
export async function executeBlogSubjectsCommand(args) {
|
|
200
|
+
const options = parseFlags(args);
|
|
201
|
+
const client = new BangumiClient(getConfig());
|
|
202
|
+
const entryId = firstPositional(options);
|
|
203
|
+
if (!entryId) {
|
|
204
|
+
throw new CommandError("Usage: bgm blog subjects <blog_id>");
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
const data = await client.listBlogSubjects(entryId);
|
|
208
|
+
return {
|
|
209
|
+
resource: "blog-subjects",
|
|
210
|
+
entryId: Number(entryId),
|
|
211
|
+
data,
|
|
212
|
+
};
|
|
213
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { BangumiClient } from "../core/client.js";
|
|
2
|
+
import { getConfig } from "../core/config.js";
|
|
3
|
+
import { printResult } from "../core/output.js";
|
|
4
|
+
import { resolveWeekdaySubcommand, todayWeekdayId } from "../utils/calendar.js";
|
|
5
|
+
|
|
6
|
+
export async function runCalendarCommand(command, args, context) {
|
|
7
|
+
const client = new BangumiClient(getConfig());
|
|
8
|
+
const data = await client.getCalendar();
|
|
9
|
+
|
|
10
|
+
const subcommand = (command && !String(command).startsWith("--"))
|
|
11
|
+
? command
|
|
12
|
+
: null;
|
|
13
|
+
|
|
14
|
+
if (subcommand === "all") {
|
|
15
|
+
printResult({ resource: "calendar", data }, context);
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const weekdayId = subcommand
|
|
20
|
+
? resolveWeekdaySubcommand(subcommand)
|
|
21
|
+
: null;
|
|
22
|
+
|
|
23
|
+
if (weekdayId !== null) {
|
|
24
|
+
const filtered = data.filter((d) => d.weekday.id === weekdayId);
|
|
25
|
+
printResult({ resource: "calendar", data: filtered }, context);
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const todayId = todayWeekdayId();
|
|
30
|
+
const filtered = data.filter((d) => d.weekday.id === todayId);
|
|
31
|
+
printResult({ resource: "calendar", data: filtered }, context);
|
|
32
|
+
}
|
|
@@ -0,0 +1,291 @@
|
|
|
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
|
+
parseOptionalBoolean,
|
|
9
|
+
parseOptionalInteger,
|
|
10
|
+
} from "../utils/helpers.js";
|
|
11
|
+
import { normalizeSubjectType } from "../utils/validators.js";
|
|
12
|
+
import { resolveTurnstileTokenForMutation } from "../utils/turnstile-flow.js";
|
|
13
|
+
|
|
14
|
+
export async function runCharacterCommand(command, args, context) {
|
|
15
|
+
const executor = {
|
|
16
|
+
get: executeCharacterGetCommand,
|
|
17
|
+
search: executeCharacterSearchCommand,
|
|
18
|
+
casts: executeCharacterCastsCommand,
|
|
19
|
+
collects: executeCharacterCollectsCommand,
|
|
20
|
+
comments: executeCharacterCommentsCommand,
|
|
21
|
+
indexes: executeCharacterIndexesCommand,
|
|
22
|
+
photos: executeCharacterPhotosCommand,
|
|
23
|
+
"photos-preview": executeCharacterPhotoPreviewCommand,
|
|
24
|
+
photo: executeCharacterPhotoCommand,
|
|
25
|
+
"photo-comments": executeCharacterPhotoCommentsCommand,
|
|
26
|
+
relations: executeCharacterRelationsCommand,
|
|
27
|
+
comment: executeCharacterCommentCommand,
|
|
28
|
+
"edit-comment": executeCharacterEditCommentCommand,
|
|
29
|
+
"delete-comment": executeCharacterDeleteCommentCommand,
|
|
30
|
+
}[command];
|
|
31
|
+
|
|
32
|
+
if (!executor) {
|
|
33
|
+
throw new CommandError("Usage: bgm character <get|search|casts|collects|comments|indexes|photos|photos-preview|photo|photo-comments|relations|comment|edit-comment|delete-comment> ...");
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const result = await executor(args, context);
|
|
37
|
+
printResult(result, context);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
async function executeCharacterGetCommand(args) {
|
|
41
|
+
const options = parseFlags(args);
|
|
42
|
+
const characterId = firstPositional(options);
|
|
43
|
+
if (!characterId) {
|
|
44
|
+
throw new CommandError("Usage: bgm character get <character_id>");
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
return {
|
|
48
|
+
resource: "character",
|
|
49
|
+
data: await new BangumiClient(getConfig()).getCharacter(characterId),
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
async function executeCharacterSearchCommand(args) {
|
|
54
|
+
const options = parseFlags(args);
|
|
55
|
+
const keyword = firstPositional(options);
|
|
56
|
+
if (!keyword) {
|
|
57
|
+
throw new CommandError("Usage: bgm character search <keyword> [--nsfw <true|false>] [--limit n] [--offset n]");
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const limit = normalizePageSize(options.limit);
|
|
61
|
+
const offset = normalizeNonNegativeInteger(options.offset, "offset");
|
|
62
|
+
const filter = {};
|
|
63
|
+
if (options.nsfw !== undefined) {
|
|
64
|
+
filter.nsfw = parseOptionalBoolean(options.nsfw);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const result = await new BangumiClient(getConfig()).searchCharacters({
|
|
68
|
+
keyword,
|
|
69
|
+
limit,
|
|
70
|
+
offset,
|
|
71
|
+
filter,
|
|
72
|
+
});
|
|
73
|
+
return wrapList(result, "character-search", "Characters", { keyword, limit, offset, nsfw: filter.nsfw });
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
async function executeCharacterCastsCommand(args) {
|
|
77
|
+
const { client, id, query } = parseCharacterListArgs(args, "casts", "[--type <main|support|guest|n>] [--subject-type <book|anime|music|game|real>]");
|
|
78
|
+
const result = await client.listCharacterCasts(id, {
|
|
79
|
+
...query,
|
|
80
|
+
type: normalizeCastType(query.type),
|
|
81
|
+
subjectType: normalizeSubjectType(query.subjectType),
|
|
82
|
+
});
|
|
83
|
+
return wrapList(result, "character-casts", "Character casts", { characterId: Number(id), ...query });
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
async function executeCharacterCollectsCommand(args) {
|
|
87
|
+
const { client, id, query } = parseCharacterListArgs(args, "collects");
|
|
88
|
+
const result = await client.listCharacterCollects(id, query);
|
|
89
|
+
return wrapList(result, "character-collects", "Character collectors", { characterId: Number(id), ...query });
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
async function executeCharacterCommentsCommand(args) {
|
|
93
|
+
const { client, id, query } = parseCharacterListArgs(args, "comments");
|
|
94
|
+
const result = await client.listCharacterComments(id, query);
|
|
95
|
+
return wrapList(result, "character-comments", "Character comments", { characterId: Number(id), ...query });
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
async function executeCharacterIndexesCommand(args) {
|
|
99
|
+
const { client, id, query } = parseCharacterListArgs(args, "indexes");
|
|
100
|
+
const result = await client.listCharacterIndexes(id, query);
|
|
101
|
+
return wrapList(result, "character-indexes", "Character indexes", { characterId: Number(id), ...query });
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
async function executeCharacterPhotosCommand(args) {
|
|
105
|
+
const { client, id, query } = parseCharacterListArgs(args, "photos");
|
|
106
|
+
const result = await client.listCharacterPhotos(id, query);
|
|
107
|
+
return wrapList(result, "character-photos", "Character photos", { characterId: Number(id), ...query });
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
async function executeCharacterPhotoPreviewCommand(args) {
|
|
111
|
+
const options = parseFlags(args);
|
|
112
|
+
const characterId = firstPositional(options);
|
|
113
|
+
if (!characterId) {
|
|
114
|
+
throw new CommandError("Usage: bgm character photos-preview <character_id> [--limit n]");
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
const limit = normalizePhotoPreviewLimit(options.limit);
|
|
118
|
+
const result = await new BangumiClient(getConfig()).listCharacterPhotoPreview(characterId, { limit });
|
|
119
|
+
return wrapList(result, "character-photos-preview", "Character photo preview", { characterId: Number(characterId), limit });
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
async function executeCharacterPhotoCommand(args) {
|
|
123
|
+
const options = parseFlags(args);
|
|
124
|
+
const characterId = firstPositional(options);
|
|
125
|
+
const photoId = getPositional(options, 1);
|
|
126
|
+
if (!characterId || !photoId) {
|
|
127
|
+
throw new CommandError("Usage: bgm character photo <character_id> <photo_id>");
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
return {
|
|
131
|
+
resource: "mono-photo",
|
|
132
|
+
scope: "character",
|
|
133
|
+
targetId: Number(characterId),
|
|
134
|
+
photoId: Number(photoId),
|
|
135
|
+
data: await new BangumiClient(getConfig()).getCharacterPhoto(characterId, photoId),
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
async function executeCharacterPhotoCommentsCommand(args) {
|
|
140
|
+
const options = parseFlags(args);
|
|
141
|
+
const characterId = firstPositional(options);
|
|
142
|
+
const photoId = getPositional(options, 1);
|
|
143
|
+
if (!characterId || !photoId) {
|
|
144
|
+
throw new CommandError("Usage: bgm character photo-comments <character_id> <photo_id>");
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
const data = await new BangumiClient(getConfig()).listCharacterPhotoComments(characterId, photoId);
|
|
148
|
+
return {
|
|
149
|
+
resource: "mono-photo-comments",
|
|
150
|
+
scope: "character",
|
|
151
|
+
targetId: Number(characterId),
|
|
152
|
+
photoId: Number(photoId),
|
|
153
|
+
data,
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
async function executeCharacterRelationsCommand(args) {
|
|
158
|
+
const { client, id, query } = parseCharacterListArgs(args, "relations");
|
|
159
|
+
const result = await client.listCharacterRelations(id, query);
|
|
160
|
+
return wrapList(result, "character-relations", "Character relations", { characterId: Number(id), ...query });
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
async function executeCharacterCommentCommand(args, context = {}) {
|
|
164
|
+
const options = parseFlags(args);
|
|
165
|
+
const characterId = firstPositional(options);
|
|
166
|
+
const content = getPositional(options, 1) ?? options.content;
|
|
167
|
+
const replyTo = normalizeNonNegativeInteger(options.replyTo, "reply-to") ?? 0;
|
|
168
|
+
if (!characterId || !content) {
|
|
169
|
+
throw new CommandError("Usage: bgm character comment <character_id> <content> [--reply-to <comment_id>] [--turnstile-token <token>] [--manual]");
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
const turnstileToken = await resolveTurnstileTokenForMutation(options, {
|
|
173
|
+
actionLabel: "create a character comment",
|
|
174
|
+
context,
|
|
175
|
+
});
|
|
176
|
+
const result = await new BangumiClient(getConfig()).createCharacterComment(characterId, {
|
|
177
|
+
content,
|
|
178
|
+
replyTo,
|
|
179
|
+
turnstileToken,
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
return buildCommentMutationResult("character", "create", {
|
|
183
|
+
targetId: characterId,
|
|
184
|
+
commentId: result.id,
|
|
185
|
+
replyTo,
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
async function executeCharacterEditCommentCommand(args) {
|
|
190
|
+
const options = parseFlags(args);
|
|
191
|
+
const commentId = firstPositional(options);
|
|
192
|
+
const content = getPositional(options, 1) ?? options.content;
|
|
193
|
+
if (!commentId || !content) {
|
|
194
|
+
throw new CommandError("Usage: bgm character edit-comment <comment_id> <content>");
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
await new BangumiClient(getConfig()).updateCharacterComment(commentId, { content });
|
|
198
|
+
return buildCommentMutationResult("character", "edit", { commentId });
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
async function executeCharacterDeleteCommentCommand(args) {
|
|
202
|
+
const options = parseFlags(args);
|
|
203
|
+
const commentId = firstPositional(options);
|
|
204
|
+
if (!commentId) {
|
|
205
|
+
throw new CommandError("Usage: bgm character delete-comment <comment_id>");
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
await new BangumiClient(getConfig()).deleteCharacterComment(commentId);
|
|
209
|
+
return buildCommentMutationResult("character", "delete", { commentId });
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
function parseCharacterListArgs(args, subcommand, suffix = "") {
|
|
213
|
+
const options = parseFlags(args);
|
|
214
|
+
const id = firstPositional(options);
|
|
215
|
+
if (!id) {
|
|
216
|
+
throw new CommandError(`Usage: bgm character ${subcommand} <character_id> ${suffix} [--limit n] [--offset n]`.trim());
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
return {
|
|
220
|
+
client: new BangumiClient(getConfig()),
|
|
221
|
+
id,
|
|
222
|
+
query: {
|
|
223
|
+
limit: normalizePageSize(options.limit),
|
|
224
|
+
offset: normalizeNonNegativeInteger(options.offset, "offset"),
|
|
225
|
+
type: options.type,
|
|
226
|
+
subjectType: options.subjectType,
|
|
227
|
+
},
|
|
228
|
+
};
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
function normalizeCastType(value) {
|
|
232
|
+
if (value === undefined || value === null || value === "") {
|
|
233
|
+
return undefined;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
const mapped = {
|
|
237
|
+
main: 1,
|
|
238
|
+
major: 1,
|
|
239
|
+
support: 2,
|
|
240
|
+
supporting: 2,
|
|
241
|
+
guest: 3,
|
|
242
|
+
}[String(value).toLowerCase()];
|
|
243
|
+
return mapped ?? parseOptionalInteger(value);
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
function normalizePhotoPreviewLimit(value) {
|
|
247
|
+
const parsed = parseOptionalInteger(value);
|
|
248
|
+
if (parsed === undefined) {
|
|
249
|
+
return undefined;
|
|
250
|
+
}
|
|
251
|
+
if (parsed < 1 || parsed > 20) {
|
|
252
|
+
throw new CommandError(`Expected limit to be between 1 and 20, received: ${value}`);
|
|
253
|
+
}
|
|
254
|
+
return parsed;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
function wrapList(result, resource, title, filters) {
|
|
258
|
+
const normalized = normalizeListResult(result);
|
|
259
|
+
return {
|
|
260
|
+
...normalized,
|
|
261
|
+
resource,
|
|
262
|
+
title,
|
|
263
|
+
filters,
|
|
264
|
+
};
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
function normalizeListResult(result) {
|
|
268
|
+
if (Array.isArray(result)) {
|
|
269
|
+
return {
|
|
270
|
+
data: result,
|
|
271
|
+
total: result.length,
|
|
272
|
+
};
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
return {
|
|
276
|
+
...result,
|
|
277
|
+
data: Array.isArray(result?.data) ? result.data : [],
|
|
278
|
+
total: result?.total ?? (Array.isArray(result?.data) ? result.data.length : 0),
|
|
279
|
+
};
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
function buildCommentMutationResult(scope, action, details) {
|
|
283
|
+
return {
|
|
284
|
+
resource: "mono-comment-mutation",
|
|
285
|
+
scope,
|
|
286
|
+
action,
|
|
287
|
+
targetId: details.targetId !== undefined ? Number(details.targetId) : undefined,
|
|
288
|
+
commentId: details.commentId !== undefined ? Number(details.commentId) : undefined,
|
|
289
|
+
replyTo: details.replyTo !== undefined ? Number(details.replyTo) : undefined,
|
|
290
|
+
};
|
|
291
|
+
}
|