@aronnaxlin/bgm-cli 1.1.2 → 1.1.4

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aronnaxlin/bgm-cli",
3
- "version": "1.1.2",
3
+ "version": "1.1.4",
4
4
  "description": "Command-line client for Bangumi.",
5
5
  "license": "AGPL-3.0-only",
6
6
  "author": "aronnaxlin",
@@ -59,7 +59,7 @@ export async function runSearchCommand(command, args, context) {
59
59
  }
60
60
  case "reply":
61
61
  case "replies": {
62
- const result = await executeEntitySearch("replies", args, searchReplies);
62
+ const result = await executeEntitySearch("replies", args, searchReplies, { allowSource: true });
63
63
  printResult(result, context);
64
64
  return;
65
65
  }
@@ -82,11 +82,17 @@ export async function runSearchCommand(command, args, context) {
82
82
  }
83
83
  }
84
84
 
85
- async function executeEntitySearch(resource, args, searchFn) {
85
+ const REPLY_SOURCES = new Set([
86
+ "all", "group", "subject", "episode", "ep", "character", "crt", "person", "prsn", "blog",
87
+ ]);
88
+
89
+ async function executeEntitySearch(resource, args, searchFn, { allowSource = false } = {}) {
86
90
  const options = parseFlags(args);
87
91
  const keyword = firstPositional(options);
88
92
  if (!keyword) {
89
- throw new CommandError(`Usage: bgm search ${resource} <keyword> [--limit n] [--offset n] [--sort <sort>]`);
93
+ throw new CommandError(
94
+ `Usage: bgm search ${resource} <keyword> [--limit n] [--offset n] [--sort <sort>]${allowSource ? " [--source <source>]" : ""}`,
95
+ );
90
96
  }
91
97
 
92
98
  const query = {
@@ -98,6 +104,16 @@ async function executeEntitySearch(resource, args, searchFn) {
98
104
  query.sort = options.sort;
99
105
  }
100
106
 
107
+ if (allowSource && options.source) {
108
+ const source = String(options.source).toLowerCase();
109
+ if (!REPLY_SOURCES.has(source)) {
110
+ throw new CommandError(
111
+ "Invalid --source. Use one of: all, group, subject, episode (ep), character (crt), person (prsn), blog.",
112
+ );
113
+ }
114
+ query.source = source;
115
+ }
116
+
101
117
  const result = await searchFn(query);
102
118
 
103
119
  return {
@@ -109,6 +125,7 @@ async function executeEntitySearch(resource, args, searchFn) {
109
125
  limit: query.limit,
110
126
  offset: query.offset,
111
127
  sort: query.sort,
128
+ ...(query.source ? { source: query.source } : {}),
112
129
  },
113
130
  };
114
131
  }
@@ -49,6 +49,12 @@ async function communityRequest(path, options = {}) {
49
49
  return adaptEnvelope(raw);
50
50
  } catch (error) {
51
51
  if (error instanceof BangumiApiError) {
52
+ if (isCloudflareChallenge(error)) {
53
+ throw new BangumiCommunityApiError(
54
+ `SearchEncore (bgmdb.ry.mk) rejected the request with a Cloudflare bot challenge (HTTP ${error.status}).`,
55
+ { status: error.status, details: { url, path } },
56
+ );
57
+ }
52
58
  throw new BangumiCommunityApiError(error.message, {
53
59
  status: error.status,
54
60
  details: error.details,
@@ -63,6 +69,13 @@ async function communityRequest(path, options = {}) {
63
69
  }
64
70
  }
65
71
 
72
+ function isCloudflareChallenge(error) {
73
+ return (
74
+ typeof error.details === "string" &&
75
+ error.details.includes("cdn-cgi/challenge-platform")
76
+ );
77
+ }
78
+
66
79
  /**
67
80
  * 将第三方的 envelope 格式适配为官方风格:
68
81
  * 原始: { data, pagination: { total, limit, offset }, meta: { executionMs } }
@@ -110,6 +123,7 @@ function buildSearchQuery(params = {}) {
110
123
  if (params.limit !== undefined) q.limit = params.limit;
111
124
  if (params.offset !== undefined) q.offset = params.offset;
112
125
  if (params.sort !== undefined) q.sort = params.sort;
126
+ if (params.source !== undefined) q.source = params.source;
113
127
  return q;
114
128
  }
115
129
 
@@ -208,20 +222,6 @@ export async function listGroupTopics(groupKey, query = {}) {
208
222
  return communityRequest(`/groups/${encode(groupKey)}/topics`, { query });
209
223
  }
210
224
 
211
- /**
212
- * 获取指定用户的所有小组话题。
213
- */
214
- export async function listUserGroupTopics(userKey, query = {}) {
215
- return communityRequest(`/users/${encode(userKey)}/group-topics`, { query });
216
- }
217
-
218
- /**
219
- * 获取最新的条目话题列表。
220
- */
221
- export async function listRecentSubjectTopics(query = {}) {
222
- return communityRequest("/subjects/-/topics", { query });
223
- }
224
-
225
225
  /**
226
226
  * 获取指定条目的话题列表。
227
227
  */
@@ -229,27 +229,6 @@ export async function listSubjectTopics(subjectId, query = {}) {
229
229
  return communityRequest(`/subjects/${encode(subjectId)}/topics`, { query });
230
230
  }
231
231
 
232
- /**
233
- * 获取指定用户的所有条目话题。
234
- */
235
- export async function listUserSubjectTopics(userKey, query = {}) {
236
- return communityRequest(`/users/${encode(userKey)}/subject-topics`, { query });
237
- }
238
-
239
- /**
240
- * 获取指定用户的目录列表。
241
- */
242
- export async function listUserIndexes(userKey, query = {}) {
243
- return communityRequest(`/users/${encode(userKey)}/indexes`, { query });
244
- }
245
-
246
- /**
247
- * 获取指定用户的日志列表。
248
- */
249
- export async function listUserBlogs(userKey, query = {}) {
250
- return communityRequest(`/users/${encode(userKey)}/blogs`, { query });
251
- }
252
-
253
232
  /* ------------------------------------------------------------------ */
254
233
  /* 详情接口 */
255
234
  /* ------------------------------------------------------------------ */
@@ -264,16 +243,6 @@ export async function getUser(userKey) {
264
243
  return communityRequest(`/users/${encode(userKey)}`);
265
244
  }
266
245
 
267
- /**
268
- * 获取用户头像历史记录。
269
- */
270
- export async function getUserAvatarHistory(userKey) {
271
- if (!userKey) {
272
- throw new BangumiCommunityApiError("Missing userKey.");
273
- }
274
- return communityRequest(`/users/${encode(userKey)}/avatars`);
275
- }
276
-
277
246
  /**
278
247
  * 获取单个小组信息。
279
248
  */
@@ -3,6 +3,7 @@ import path from "node:path";
3
3
  import { fileURLToPath } from "node:url";
4
4
  import { existsSync, readFileSync } from "node:fs";
5
5
  import { mkdir, readFile, writeFile } from "node:fs/promises";
6
+ import { APP_VERSION } from "../utils/version.js";
6
7
 
7
8
  export class ConfigError extends Error {
8
9
  constructor(message) {
@@ -23,7 +24,7 @@ const DEV_ENV_FILE = path.join(REPO_ROOT, "bgm-dev.env");
23
24
 
24
25
  const DEFAULT_CONFIG = {
25
26
  appName: "bgm-cli",
26
- appVersion: "1.0.2",
27
+ appVersion: APP_VERSION,
27
28
  homepageLink: "https://github.com/aronnaxlin/bgm-cli",
28
29
  developerId: "aronnaxlin",
29
30
  oauthServerBaseUrl: "https://oauth-backend-jet.vercel.app",
@@ -494,7 +495,7 @@ function normalizeUserAgent(config) {
494
495
  }
495
496
 
496
497
  const appName = config.appName ?? "bgm-cli";
497
- const version = config.appVersion ?? "1.0.0";
498
+ const version = config.appVersion ?? APP_VERSION;
498
499
  const genericValues = new Set([
499
500
  `${appName}/${version}`,
500
501
  `${appName}/0.1.0`,
@@ -504,17 +505,33 @@ function normalizeUserAgent(config) {
504
505
  "yourname/bgm-cli/0.1.0",
505
506
  ]);
506
507
 
507
- if ((genericValues.has(current) || current === legacyRecommended) && recommended) {
508
+ if ((genericValues.has(current) || current === legacyRecommended || isManagedUserAgent(current, config)) && recommended) {
508
509
  return recommended;
509
510
  }
510
511
 
511
512
  return current;
512
513
  }
513
514
 
515
+ // A User-Agent in the recommended format but carrying an older release's
516
+ // version is ours to refresh; anything else was customised by the user.
517
+ function isManagedUserAgent(current, config) {
518
+ const developerId = config.developerId ?? extractGithubUsername(config.homepageLink);
519
+ const appName = config.appName ?? "bgm-cli";
520
+ const prefix = developerId ? `${developerId}/${appName}/` : `${appName}/`;
521
+ const suffix = config.homepageLink ? ` (${config.homepageLink})` : "";
522
+
523
+ if (!current.startsWith(prefix) || !current.endsWith(suffix)) {
524
+ return false;
525
+ }
526
+
527
+ const version = current.slice(prefix.length, current.length - suffix.length);
528
+ return /^\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?$/.test(version);
529
+ }
530
+
514
531
  function buildRecommendedUserAgent(config) {
515
532
  const developerId = config.developerId ?? extractGithubUsername(config.homepageLink);
516
533
  const appName = config.appName ?? "bgm-cli";
517
- const version = config.appVersion ?? "1.0.0";
534
+ const version = config.appVersion ?? APP_VERSION;
518
535
  const homepageLink = config.homepageLink;
519
536
 
520
537
  let userAgent = developerId ? `${developerId}/${appName}/${version}` : `${appName}/${version}`;
@@ -181,7 +181,7 @@ function buildUsageText(target) {
181
181
  ["bgm [--json] search group <keyword> [--limit n] [--offset n] [--sort <sort>]", "Search groups via SearchEncore."],
182
182
  ["bgm [--json] search topic <keyword> [--limit n] [--offset n] [--sort <sort>]", "Search group topics via SearchEncore."],
183
183
  ["bgm [--json] search subject-topic <keyword> [--limit n] [--offset n] [--sort <sort>]", "Search subject topics via SearchEncore."],
184
- ["bgm [--json] search reply <keyword> [--limit n] [--offset n] [--sort <sort>]", "Search replies via SearchEncore."],
184
+ ["bgm [--json] search reply <keyword> [--limit n] [--offset n] [--sort newest|oldest] [--source <source>]", "Search replies via SearchEncore. Source: all|group|subject|episode|character|person|blog."],
185
185
  ["bgm [--json] search index <keyword> [--limit n] [--offset n] [--sort <sort>]", "Search indexes via SearchEncore."],
186
186
  ["bgm [--json] search blog <keyword> [--limit n] [--offset n] [--sort <sort>]", "Search blogs via SearchEncore."],
187
187
  ], [
@@ -190,6 +190,10 @@ function buildUsageText(target) {
190
190
  " (bgmdb.ry.mk). Data is crawled/aggregated, NOT from bangumi.tv",
191
191
  " official sources. Every response is tagged with",
192
192
  " _meta.isSearchEncore = true for downstream identification.",
193
+ "",
194
+ " --sort: newest|oldest|popular (aliases new|old|hot); anything else ranks by relevance.",
195
+ " Keywords accept inline directives: user:<name|uid> group:<slug>",
196
+ " type:<anime|book|music|game|real> exact:true include:nsfw exclude:nsfw include:blocked",
193
197
  ]);
194
198
  case "notify":
195
199
  return buildGroupUsage("Notify", [
@@ -1,18 +1,18 @@
1
1
  import { CommandError } from "./output.js";
2
2
 
3
- const TOPIC_POST_REACTION_VALUES = Object.freeze([54, 62, 79, 80, 85, 88, 90, 104, 122, 140]);
4
- const SUBJECT_COLLECT_REACTION_VALUES = Object.freeze([54]);
3
+ const TOPIC_POST_REACTION_VALUES = Object.freeze([0, 54, 62, 79, 80, 85, 88, 90, 104, 122, 140, 141]);
4
+ const SUBJECT_COLLECT_REACTION_VALUES = Object.freeze([0, 54, 80, 88, 90, 104, 122, 140]);
5
5
 
6
6
  export const BANGUMI_REACTION_TARGETS = Object.freeze({
7
7
  subjectPost: Object.freeze({
8
8
  label: "subject discussion reply",
9
9
  allowedValues: TOPIC_POST_REACTION_VALUES,
10
- note: "Subject and group topic replies share the p1 topic-post reaction shape; values were probed on a group topic post.",
10
+ note: "Subject and group topic replies share the p1 topic-post reaction shape; values mirror ALLOWED_COMMON_REACTIONS in bangumi/server-private lib/like.ts.",
11
11
  }),
12
12
  groupPost: Object.freeze({
13
13
  label: "group topic reply",
14
14
  allowedValues: TOPIC_POST_REACTION_VALUES,
15
- note: "Values probed live on a group topic post.",
15
+ note: "Values mirror ALLOWED_COMMON_REACTIONS in bangumi/server-private lib/like.ts.",
16
16
  }),
17
17
  episodeComment: Object.freeze({
18
18
  label: "episode comment",
@@ -22,7 +22,7 @@ export const BANGUMI_REACTION_TARGETS = Object.freeze({
22
22
  subjectCollect: Object.freeze({
23
23
  label: "subject collection comment",
24
24
  allowedValues: SUBJECT_COLLECT_REACTION_VALUES,
25
- note: "Value 54 is probed valid; 79 is probed invalid for this target.",
25
+ note: "Values mirror ALLOWED_SUBJECT_COLLECT_REACTIONS in bangumi/server-private lib/like.ts; 79 and 141 are not accepted here.",
26
26
  }),
27
27
  timeline: Object.freeze({
28
28
  label: "timeline entry",
@@ -40,8 +40,8 @@ export function normalizeBangumiReactionValue(value, targetKey) {
40
40
  if (Number.isNaN(parsed)) {
41
41
  throw new CommandError(`Expected reaction value to be an integer, received: ${value}`);
42
42
  }
43
- if (parsed <= 0) {
44
- throw new CommandError(`Expected reaction value to be > 0, received: ${value}`);
43
+ if (parsed < 0) {
44
+ throw new CommandError(`Expected reaction value to be >= 0, received: ${value}`);
45
45
  }
46
46
 
47
47
  const target = BANGUMI_REACTION_TARGETS[targetKey];
@@ -299,12 +299,12 @@
299
299
  "description": "Bangumi p1 反应贴贴白名单。不是所有表情都能用于 like/unlike。",
300
300
  "verifiedTargets": {
301
301
  "groupPost": {
302
- "values": [54, 62, 79, 80, 85, 88, 90, 104, 122, 140],
303
- "source": "Live probe on 2026-05-24."
302
+ "values": [0, 54, 62, 79, 80, 85, 88, 90, 104, 122, 140, 141],
303
+ "source": "ALLOWED_COMMON_REACTIONS in bangumi/server-private lib/like.ts (2026-09)."
304
304
  },
305
305
  "subjectCollect": {
306
- "values": [54],
307
- "source": "Live probe on 2026-05-24."
306
+ "values": [0, 54, 80, 88, 90, 104, 122, 140],
307
+ "source": "ALLOWED_SUBJECT_COLLECT_REACTIONS in bangumi/server-private lib/like.ts (2026-09)."
308
308
  }
309
309
  }
310
310
  }
package/src/utils/auth.js CHANGED
@@ -3,6 +3,7 @@
3
3
  */
4
4
 
5
5
  import { CommandError } from "../core/output.js";
6
+ import { APP_VERSION } from "./version.js";
6
7
 
7
8
  export function createState() {
8
9
  return `bgm-cli-${Date.now().toString(36)}`;
@@ -11,7 +12,7 @@ export function createState() {
11
12
  export function fallbackUserAgent(config) {
12
13
  const developerId = deriveDeveloperId(config);
13
14
  const appName = config.appName ?? "bgm-cli";
14
- const version = config.appVersion ?? "1.0.0";
15
+ const version = config.appVersion ?? APP_VERSION;
15
16
  const homepageLink = config.homepageLink;
16
17
 
17
18
  let userAgent = developerId
@@ -14,6 +14,7 @@ import {
14
14
  import { CommandError } from "../core/output.js";
15
15
  import { DEFAULT_TURNSTILE_TIMEOUT_MS } from "../core/turnstile.js";
16
16
  import { fallbackUserAgent } from "./auth.js";
17
+ import { APP_VERSION } from "./version.js";
17
18
 
18
19
  export function delayMs(ms) {
19
20
  return new Promise((resolve) => {
@@ -265,7 +266,7 @@ export function buildVersionStatusPayload(repoRoot) {
265
266
  return {
266
267
  resource: "version-status",
267
268
  name: config.appName ?? "bgm-cli",
268
- version: config.appVersion ?? "1.0.0",
269
+ version: config.appVersion ?? APP_VERSION,
269
270
  configScope: inferConfigScope(configFile, repoRoot),
270
271
  configFile,
271
272
  configSourceFile,
@@ -0,0 +1,24 @@
1
+ /**
2
+ * Single source of truth for the CLI version at runtime.
3
+ *
4
+ * The released version lives in git tags (vX.Y.Z). The release workflow stamps
5
+ * it into package.json right before `npm publish`, so a published package
6
+ * always reports its real version. A source checkout reports the placeholder
7
+ * committed to package.json, like a dev build.
8
+ */
9
+
10
+ import { readFileSync } from "node:fs";
11
+
12
+ export const DEV_VERSION = "0.0.0-dev";
13
+
14
+ function readPackageVersion() {
15
+ try {
16
+ const raw = readFileSync(new URL("../../package.json", import.meta.url), "utf8");
17
+ const version = JSON.parse(raw).version;
18
+ return typeof version === "string" && version ? version : DEV_VERSION;
19
+ } catch {
20
+ return DEV_VERSION;
21
+ }
22
+ }
23
+
24
+ export const APP_VERSION = readPackageVersion();