@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,157 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* TUI and display formatting utilities.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
const TUI_PAGE_SIZE = 7;
|
|
6
|
+
|
|
7
|
+
export function formatCollectionSnapshotSummary(snapshot) {
|
|
8
|
+
if (!snapshot) {
|
|
9
|
+
return "Current collection: none";
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
return [
|
|
13
|
+
"Current collection",
|
|
14
|
+
` Status: ${formatCollectionStatusLabel(snapshot.type)}`,
|
|
15
|
+
` Rating: ${snapshot.rate ?? 0}`,
|
|
16
|
+
` Comment: ${snapshot.comment || "-"}`,
|
|
17
|
+
].join("\n");
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function formatCriteriaSummary(criteria) {
|
|
21
|
+
const entries = Object.entries(criteria).filter(([, value]) => value !== undefined && value !== "");
|
|
22
|
+
if (entries.length === 0) {
|
|
23
|
+
return "";
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const lines = ["Criteria"];
|
|
27
|
+
for (const [key, value] of entries) {
|
|
28
|
+
lines.push(` ${key}: ${value}`);
|
|
29
|
+
}
|
|
30
|
+
return lines.join("\n");
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function formatPageSummary(total, pageIndex, pageCount) {
|
|
34
|
+
return `Results: ${total} total Page: ${pageIndex + 1}/${pageCount}`;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function buildPagedMenu(items, pageIndex, formatter) {
|
|
38
|
+
const pageCount = Math.max(1, Math.ceil(items.length / TUI_PAGE_SIZE));
|
|
39
|
+
const normalizedPageIndex = Math.min(Math.max(0, pageIndex), pageCount - 1);
|
|
40
|
+
const startIndex = normalizedPageIndex * TUI_PAGE_SIZE;
|
|
41
|
+
const pageItems = items.slice(startIndex, startIndex + TUI_PAGE_SIZE);
|
|
42
|
+
|
|
43
|
+
return {
|
|
44
|
+
items: pageItems.map((item) => ({ ...item, __label: formatter(item) })),
|
|
45
|
+
startIndex,
|
|
46
|
+
pageCount,
|
|
47
|
+
hasPrevious: normalizedPageIndex > 0,
|
|
48
|
+
hasNext: normalizedPageIndex < pageCount - 1,
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export function formatSubjectMenuLabel(subject) {
|
|
53
|
+
const parts = [
|
|
54
|
+
`#${subject?.id ?? "-"}`,
|
|
55
|
+
`[${formatSubjectTypeLabel(subject?.type)}]`,
|
|
56
|
+
subject?.name_cn || subject?.name || "-",
|
|
57
|
+
];
|
|
58
|
+
|
|
59
|
+
if (subject?.rating?.rank) {
|
|
60
|
+
parts.push(`#${subject.rating.rank}`);
|
|
61
|
+
} else {
|
|
62
|
+
parts.push("unranked");
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
if (subject?.rating?.score !== undefined) {
|
|
66
|
+
parts.push(`score ${subject.rating.score}`);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
return parts.join(" ");
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export function formatCollectionMenuLabel(item) {
|
|
73
|
+
const subject = item?.subject ?? {};
|
|
74
|
+
const parts = [
|
|
75
|
+
`#${item?.subject_id ?? subject?.id ?? "-"}`,
|
|
76
|
+
`[${formatCollectionStatusLabel(item?.type)}]`,
|
|
77
|
+
`[${formatSubjectTypeLabel(item?.subject_type ?? subject?.type)}]`,
|
|
78
|
+
subject?.name_cn || subject?.name || "-",
|
|
79
|
+
];
|
|
80
|
+
|
|
81
|
+
if (subject?.rank) {
|
|
82
|
+
parts.push(`#${subject.rank}`);
|
|
83
|
+
} else {
|
|
84
|
+
parts.push("unranked");
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
if (item?.rate) {
|
|
88
|
+
parts.push(`my ${item.rate}`);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
return parts.join(" ");
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export function formatGroupMenuLabel(group) {
|
|
95
|
+
const parts = [
|
|
96
|
+
`#${group?.id ?? "-"}`,
|
|
97
|
+
group?.title || "-",
|
|
98
|
+
];
|
|
99
|
+
|
|
100
|
+
if (group?.name) {
|
|
101
|
+
parts.push(`(${group.name})`);
|
|
102
|
+
}
|
|
103
|
+
if (group?.members !== undefined) {
|
|
104
|
+
parts.push(`${group.members} members`);
|
|
105
|
+
}
|
|
106
|
+
if (group?.topics !== undefined) {
|
|
107
|
+
parts.push(`${group.topics} topics`);
|
|
108
|
+
} else if (group?.topicCount !== undefined) {
|
|
109
|
+
parts.push(`${group.topicCount} active`);
|
|
110
|
+
}
|
|
111
|
+
if (group?.hotScore !== undefined) {
|
|
112
|
+
parts.push(`hot ${Number(group.hotScore).toFixed(4)}`);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
return parts.join(" ");
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
export function formatGroupTopicMenuLabel(topic) {
|
|
119
|
+
const parts = [
|
|
120
|
+
`#${topic?.id ?? "-"}`,
|
|
121
|
+
topic?.title || "-",
|
|
122
|
+
];
|
|
123
|
+
|
|
124
|
+
if (topic?.group?.title) {
|
|
125
|
+
parts.push(`[${topic.group.title}]`);
|
|
126
|
+
}
|
|
127
|
+
if (topic?.replyCount !== undefined) {
|
|
128
|
+
parts.push(`${topic.replyCount} replies`);
|
|
129
|
+
}
|
|
130
|
+
if (topic?.hotScore !== undefined) {
|
|
131
|
+
parts.push(`hot ${Number(topic.hotScore).toFixed(4)}`);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
return parts.join(" ");
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
export function formatSubjectTypeLabel(type) {
|
|
138
|
+
const map = {
|
|
139
|
+
1: "Book",
|
|
140
|
+
2: "Anime",
|
|
141
|
+
3: "Music",
|
|
142
|
+
4: "Game",
|
|
143
|
+
6: "Real",
|
|
144
|
+
};
|
|
145
|
+
return map[type] ?? String(type ?? "-");
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
export function formatCollectionStatusLabel(type) {
|
|
149
|
+
const map = {
|
|
150
|
+
1: "Wish",
|
|
151
|
+
2: "Collect",
|
|
152
|
+
3: "Doing",
|
|
153
|
+
4: "On hold",
|
|
154
|
+
5: "Dropped",
|
|
155
|
+
};
|
|
156
|
+
return map[type] ?? String(type ?? "-");
|
|
157
|
+
}
|
|
@@ -0,0 +1,283 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* General helper utilities.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { spawnSync } from "node:child_process";
|
|
6
|
+
import { chmodSync } from "node:fs";
|
|
7
|
+
import path from "node:path";
|
|
8
|
+
import process from "node:process";
|
|
9
|
+
import {
|
|
10
|
+
getConfig,
|
|
11
|
+
getConfigFilePath,
|
|
12
|
+
getConfigSourceFilePath,
|
|
13
|
+
} from "../core/config.js";
|
|
14
|
+
import { CommandError } from "../core/output.js";
|
|
15
|
+
import { DEFAULT_TURNSTILE_TIMEOUT_MS } from "../core/turnstile.js";
|
|
16
|
+
import { fallbackUserAgent } from "./auth.js";
|
|
17
|
+
|
|
18
|
+
export function delayMs(ms) {
|
|
19
|
+
return new Promise((resolve) => {
|
|
20
|
+
setTimeout(resolve, ms);
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function sleep(ms) {
|
|
25
|
+
return new Promise((resolve) => {
|
|
26
|
+
setTimeout(resolve, ms);
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function escapeHtml(value) {
|
|
31
|
+
return String(value)
|
|
32
|
+
.replaceAll("&", "&")
|
|
33
|
+
.replaceAll("<", "<")
|
|
34
|
+
.replaceAll(">", ">")
|
|
35
|
+
.replaceAll('"', """)
|
|
36
|
+
.replaceAll("'", "'");
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function normalizeConfigKey(key) {
|
|
40
|
+
const aliasMap = {
|
|
41
|
+
clientid: "clientId",
|
|
42
|
+
clientsecret: "clientSecret",
|
|
43
|
+
redirecturi: "redirectUri",
|
|
44
|
+
oauthserverbaseurl: "oauthServerBaseUrl",
|
|
45
|
+
accesstoken: "accessToken",
|
|
46
|
+
refreshtoken: "refreshToken",
|
|
47
|
+
tokentype: "tokenType",
|
|
48
|
+
useragent: "userAgent",
|
|
49
|
+
timezone: "timezone",
|
|
50
|
+
proxy: "proxy",
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
const condensed = String(key).replace(/[-_]/g, "").toLowerCase();
|
|
54
|
+
const normalized = aliasMap[condensed];
|
|
55
|
+
if (!normalized) {
|
|
56
|
+
throw new CommandError(`Unsupported config key: ${key}`);
|
|
57
|
+
}
|
|
58
|
+
return normalized;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export function previewToken(token) {
|
|
62
|
+
const value = String(token);
|
|
63
|
+
if (value.length <= 10) {
|
|
64
|
+
return value;
|
|
65
|
+
}
|
|
66
|
+
return `${value.slice(0, 6)}...${value.slice(-4)}`;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export function formatPlatformName(platform) {
|
|
70
|
+
switch (platform) {
|
|
71
|
+
case "darwin":
|
|
72
|
+
return "macOS";
|
|
73
|
+
case "win32":
|
|
74
|
+
return "Windows";
|
|
75
|
+
default:
|
|
76
|
+
return "Linux";
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export function pathsEqual(left, right) {
|
|
81
|
+
if (process.platform === "win32") {
|
|
82
|
+
return path.resolve(left).toLowerCase() === path.resolve(right).toLowerCase();
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
return path.resolve(left) === path.resolve(right);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export function ensureExecutable(filePath) {
|
|
89
|
+
try {
|
|
90
|
+
chmodSync(filePath, 0o755);
|
|
91
|
+
} catch {
|
|
92
|
+
// Best effort only. If chmod fails, the installer may still succeed on systems
|
|
93
|
+
// where executable bits are already correct.
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export function hasSavedConfigValue(value) {
|
|
98
|
+
return typeof value === "string" ? value.trim().length > 0 : Boolean(value);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export function writeProgress(context, message) {
|
|
102
|
+
const output = context?.json ? console.error : console.log;
|
|
103
|
+
output(message);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export function tryOpenExternalUrl(url) {
|
|
107
|
+
const platform = process.platform;
|
|
108
|
+
const command = platform === "darwin"
|
|
109
|
+
? "open"
|
|
110
|
+
: platform === "win32"
|
|
111
|
+
? "cmd"
|
|
112
|
+
: "xdg-open";
|
|
113
|
+
const commandArgs = platform === "win32"
|
|
114
|
+
? ["/c", "start", "", url]
|
|
115
|
+
: [url];
|
|
116
|
+
|
|
117
|
+
const result = spawnSync(command, commandArgs, {
|
|
118
|
+
stdio: "ignore",
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
return !result.error && result.status === 0;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
export function compareStrings(left, right) {
|
|
125
|
+
return String(left).localeCompare(String(right), "zh-Hans-CN");
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
export function parseOptionalInteger(value) {
|
|
129
|
+
if (value === undefined || value === null || value === "") {
|
|
130
|
+
return undefined;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
const parsed = Number.parseInt(String(value), 10);
|
|
134
|
+
if (Number.isNaN(parsed)) {
|
|
135
|
+
throw new CommandError(`Expected integer, received: ${value}`);
|
|
136
|
+
}
|
|
137
|
+
return parsed;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
export function normalizeNonNegativeInteger(value, label) {
|
|
141
|
+
const parsed = parseOptionalInteger(value);
|
|
142
|
+
if (parsed === undefined) {
|
|
143
|
+
return undefined;
|
|
144
|
+
}
|
|
145
|
+
if (parsed < 0) {
|
|
146
|
+
throw new CommandError(`Expected ${label} to be >= 0, received: ${value}`);
|
|
147
|
+
}
|
|
148
|
+
return parsed;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
export function normalizePositiveInteger(value, label) {
|
|
152
|
+
const parsed = parseOptionalInteger(value);
|
|
153
|
+
if (parsed === undefined) {
|
|
154
|
+
return undefined;
|
|
155
|
+
}
|
|
156
|
+
if (parsed <= 0) {
|
|
157
|
+
throw new CommandError(`Expected ${label} to be > 0, received: ${value}`);
|
|
158
|
+
}
|
|
159
|
+
return parsed;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
export function normalizePageSize(value) {
|
|
163
|
+
const parsed = normalizeNonNegativeInteger(value, "limit");
|
|
164
|
+
if (parsed === undefined) {
|
|
165
|
+
return undefined;
|
|
166
|
+
}
|
|
167
|
+
if (parsed > 100) {
|
|
168
|
+
throw new CommandError(`Expected limit to be <= 100, received: ${value}`);
|
|
169
|
+
}
|
|
170
|
+
return parsed;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
export function normalizeEpisodePageSize(value) {
|
|
174
|
+
const parsed = normalizeNonNegativeInteger(value, "limit");
|
|
175
|
+
if (parsed === undefined) {
|
|
176
|
+
return undefined;
|
|
177
|
+
}
|
|
178
|
+
if (parsed === 0 || parsed > 200) {
|
|
179
|
+
throw new CommandError(`Expected limit to be between 1 and 200, received: ${value}`);
|
|
180
|
+
}
|
|
181
|
+
return parsed;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
export function normalizePositiveNumber(value, label) {
|
|
185
|
+
if (value === undefined || value === null || value === "") {
|
|
186
|
+
return undefined;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
const parsed = Number(value);
|
|
190
|
+
if (Number.isNaN(parsed) || parsed <= 0) {
|
|
191
|
+
throw new CommandError(`Expected ${label} to be > 0, received: ${value}`);
|
|
192
|
+
}
|
|
193
|
+
return parsed;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
export function normalizeRateValue(value) {
|
|
197
|
+
const parsed = parseOptionalInteger(value);
|
|
198
|
+
if (parsed === undefined || parsed < 0 || parsed > 10) {
|
|
199
|
+
throw new CommandError(`Expected rate to be between 0 and 10, received: ${value}`);
|
|
200
|
+
}
|
|
201
|
+
return parsed;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
export function parseOptionalBoolean(value) {
|
|
205
|
+
if (value === undefined || value === null || value === "") {
|
|
206
|
+
return undefined;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
if (typeof value === "boolean") {
|
|
210
|
+
return value;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
const normalized = String(value).toLowerCase();
|
|
214
|
+
if (["true", "1", "yes", "y"].includes(normalized)) {
|
|
215
|
+
return true;
|
|
216
|
+
}
|
|
217
|
+
if (["false", "0", "no", "n"].includes(normalized)) {
|
|
218
|
+
return false;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
throw new CommandError(`Expected boolean, received: ${value}`);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
export async function resolveUsernameOrMe(client, username) {
|
|
225
|
+
if (typeof username === "string" && username.trim()) {
|
|
226
|
+
return username.trim();
|
|
227
|
+
}
|
|
228
|
+
if (username !== undefined && username !== null && String(username).trim()) {
|
|
229
|
+
return String(username).trim();
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
const me = await client.getMe();
|
|
233
|
+
const resolvedUsername = typeof me?.username === "string" ? me.username.trim() : "";
|
|
234
|
+
if (!resolvedUsername) {
|
|
235
|
+
throw new CommandError("Unable to resolve the current username. Log in first or pass a username explicitly.");
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
return resolvedUsername;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
export function toBoolean(value, fallback) {
|
|
242
|
+
if (value === undefined) {
|
|
243
|
+
return fallback;
|
|
244
|
+
}
|
|
245
|
+
return parseOptionalBoolean(value);
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
export function normalizeTurnstileTimeoutMs(value) {
|
|
249
|
+
const seconds = normalizePositiveInteger(value, "timeout-seconds");
|
|
250
|
+
if (seconds === undefined) {
|
|
251
|
+
return DEFAULT_TURNSTILE_TIMEOUT_MS;
|
|
252
|
+
}
|
|
253
|
+
return seconds * 1000;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
export function inferConfigScope(configFile, repoRoot) {
|
|
257
|
+
return configFile.startsWith(`${repoRoot}${path.sep}`) ? "project" : "global";
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
export function buildVersionStatusPayload(repoRoot) {
|
|
261
|
+
const config = getConfig();
|
|
262
|
+
const configFile = getConfigFilePath();
|
|
263
|
+
const configSourceFile = getConfigSourceFilePath();
|
|
264
|
+
|
|
265
|
+
return {
|
|
266
|
+
resource: "version-status",
|
|
267
|
+
name: config.appName ?? "bgm-cli",
|
|
268
|
+
version: config.appVersion ?? "1.0.0",
|
|
269
|
+
configScope: inferConfigScope(configFile, repoRoot),
|
|
270
|
+
configFile,
|
|
271
|
+
configSourceFile,
|
|
272
|
+
accessTokenSaved: hasSavedConfigValue(config.accessToken),
|
|
273
|
+
refreshTokenSaved: hasSavedConfigValue(config.refreshToken),
|
|
274
|
+
privateSessionSaved: hasSavedConfigValue(config.privateSessionId),
|
|
275
|
+
oauthAppConfigured:
|
|
276
|
+
hasSavedConfigValue(config.clientId) &&
|
|
277
|
+
hasSavedConfigValue(config.clientSecret) &&
|
|
278
|
+
hasSavedConfigValue(config.redirectUri),
|
|
279
|
+
oauthServerBaseUrl: config.oauthServerBaseUrl ?? null,
|
|
280
|
+
timezone: config.timezone ?? null,
|
|
281
|
+
userAgent: config.userAgent ?? fallbackUserAgent(config),
|
|
282
|
+
};
|
|
283
|
+
}
|
package/src/utils/hot.js
ADDED
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Hot topic/group ranking utilities.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { GROUP_HOT_WINDOWS } from "./validators.js";
|
|
6
|
+
|
|
7
|
+
export async function fetchTopicsForHotWindow(client, { window, mode, scan }) {
|
|
8
|
+
const cutoff = computeHotCutoffTimestamp(window);
|
|
9
|
+
const pageSize = 100;
|
|
10
|
+
const collected = [];
|
|
11
|
+
|
|
12
|
+
for (let offset = 0; offset < scan; offset += pageSize) {
|
|
13
|
+
const page = await client.listRecentGroupTopics({
|
|
14
|
+
mode,
|
|
15
|
+
limit: Math.min(pageSize, scan - offset),
|
|
16
|
+
offset,
|
|
17
|
+
});
|
|
18
|
+
const topics = Array.isArray(page?.data) ? page.data : [];
|
|
19
|
+
if (topics.length === 0) {
|
|
20
|
+
break;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
let seenOlderTopic = false;
|
|
24
|
+
for (const topic of topics) {
|
|
25
|
+
const activityTimestamp = getTopicActivityTimestamp(topic);
|
|
26
|
+
if (activityTimestamp >= cutoff) {
|
|
27
|
+
collected.push(topic);
|
|
28
|
+
} else {
|
|
29
|
+
seenOlderTopic = true;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
if (seenOlderTopic || topics.length < pageSize) {
|
|
34
|
+
break;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
return collected;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export async function fetchRecentRepliedTopics(client, { mode, limit, scan }) {
|
|
42
|
+
const pageSize = 100;
|
|
43
|
+
const collected = [];
|
|
44
|
+
|
|
45
|
+
for (let offset = 0; offset < scan && collected.length < limit; offset += pageSize) {
|
|
46
|
+
const page = await client.listRecentGroupTopics({
|
|
47
|
+
mode,
|
|
48
|
+
limit: Math.min(pageSize, scan - offset),
|
|
49
|
+
offset,
|
|
50
|
+
});
|
|
51
|
+
const topics = Array.isArray(page?.data) ? page.data : [];
|
|
52
|
+
if (topics.length === 0) {
|
|
53
|
+
break;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
for (const topic of topics) {
|
|
57
|
+
if (isRepliedTopic(topic)) {
|
|
58
|
+
collected.push(topic);
|
|
59
|
+
if (collected.length >= limit) {
|
|
60
|
+
break;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
if (topics.length < pageSize) {
|
|
66
|
+
break;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
return collected;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export function rankHotTopics(topics, window) {
|
|
74
|
+
return [...topics]
|
|
75
|
+
.map((topic) => buildHotTopicEntry(topic, window))
|
|
76
|
+
.sort((left, right) => {
|
|
77
|
+
if (right.hotScore !== left.hotScore) {
|
|
78
|
+
return right.hotScore - left.hotScore;
|
|
79
|
+
}
|
|
80
|
+
return getTopicActivityTimestamp(right) - getTopicActivityTimestamp(left);
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export function buildHotTopicEntry(topic, window) {
|
|
85
|
+
const hotScore = computeTopicHotScore(topic, window);
|
|
86
|
+
return {
|
|
87
|
+
...topic,
|
|
88
|
+
hotScore,
|
|
89
|
+
ageHours: computeAgeHours(getTopicActivityTimestamp(topic)),
|
|
90
|
+
window,
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export function aggregateHotGroups(topics, window) {
|
|
95
|
+
const grouped = new Map();
|
|
96
|
+
|
|
97
|
+
for (const topic of topics) {
|
|
98
|
+
const groupKey = String(topic?.group?.name ?? topic?.group?.id ?? topic?.parentID ?? "");
|
|
99
|
+
if (!groupKey) {
|
|
100
|
+
continue;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
if (!grouped.has(groupKey)) {
|
|
104
|
+
grouped.set(groupKey, {
|
|
105
|
+
id: topic?.group?.id,
|
|
106
|
+
name: topic?.group?.name,
|
|
107
|
+
title: topic?.group?.title,
|
|
108
|
+
members: topic?.group?.members ?? 0,
|
|
109
|
+
accessible: topic?.group?.accessible,
|
|
110
|
+
createdAt: topic?.group?.createdAt,
|
|
111
|
+
hotScore: 0,
|
|
112
|
+
topicCount: 0,
|
|
113
|
+
replyCount: 0,
|
|
114
|
+
latestActivityAt: 0,
|
|
115
|
+
topTopics: [],
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
const entry = grouped.get(groupKey);
|
|
120
|
+
entry.hotScore += Number(topic.hotScore ?? 0);
|
|
121
|
+
entry.topicCount += 1;
|
|
122
|
+
entry.replyCount += Number(topic.replyCount ?? 0);
|
|
123
|
+
entry.latestActivityAt = Math.max(entry.latestActivityAt, getTopicActivityTimestamp(topic));
|
|
124
|
+
entry.topTopics.push({
|
|
125
|
+
id: topic.id,
|
|
126
|
+
title: topic.title,
|
|
127
|
+
replyCount: topic.replyCount ?? 0,
|
|
128
|
+
hotScore: topic.hotScore,
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
return [...grouped.values()]
|
|
133
|
+
.map((entry) => ({
|
|
134
|
+
...entry,
|
|
135
|
+
hotScore: applyGroupRecencyBonus(entry.hotScore, entry.latestActivityAt, window),
|
|
136
|
+
topTopics: entry.topTopics
|
|
137
|
+
.sort((left, right) => right.hotScore - left.hotScore)
|
|
138
|
+
.slice(0, 3),
|
|
139
|
+
ageHours: computeAgeHours(entry.latestActivityAt),
|
|
140
|
+
}))
|
|
141
|
+
.sort((left, right) => {
|
|
142
|
+
if (right.hotScore !== left.hotScore) {
|
|
143
|
+
return right.hotScore - left.hotScore;
|
|
144
|
+
}
|
|
145
|
+
if (right.topicCount !== left.topicCount) {
|
|
146
|
+
return right.topicCount - left.topicCount;
|
|
147
|
+
}
|
|
148
|
+
return right.replyCount - left.replyCount;
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
export function computeTopicHotScore(topic, window) {
|
|
153
|
+
const config = GROUP_HOT_WINDOWS[window];
|
|
154
|
+
const ageHours = computeAgeHours(getTopicActivityTimestamp(topic));
|
|
155
|
+
const replyCount = Number(topic?.replyCount ?? 0);
|
|
156
|
+
return Math.log1p(replyCount + 1) / ((ageHours + 2) ** config.gravity);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
export function applyGroupRecencyBonus(score, latestActivityAt, window) {
|
|
160
|
+
const config = GROUP_HOT_WINDOWS[window];
|
|
161
|
+
const ageHours = computeAgeHours(latestActivityAt);
|
|
162
|
+
const recencyBonus = Math.exp(-ageHours / config.groupDecayHours);
|
|
163
|
+
return score + recencyBonus;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
export function computeAgeHours(timestampSeconds) {
|
|
167
|
+
const seconds = Math.max(0, Math.floor(Date.now() / 1000) - Number(timestampSeconds ?? 0));
|
|
168
|
+
return seconds / 3600;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
export function getTopicActivityTimestamp(topic) {
|
|
172
|
+
return Number(topic?.updatedAt ?? topic?.createdAt ?? 0);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
export function isRepliedTopic(topic) {
|
|
176
|
+
return Number(topic?.replyCount ?? 0) > 0 && Number(topic?.updatedAt ?? 0) > Number(topic?.createdAt ?? 0);
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
export function computeHotCutoffTimestamp(window) {
|
|
180
|
+
const config = GROUP_HOT_WINDOWS[window];
|
|
181
|
+
return Math.floor(Date.now() / 1000) - config.hours * 3600;
|
|
182
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Installation path and shell hint utilities.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import os from "node:os";
|
|
6
|
+
import path from "node:path";
|
|
7
|
+
import process from "node:process";
|
|
8
|
+
|
|
9
|
+
export function getManagedInstallDir() {
|
|
10
|
+
if (process.platform === "win32") {
|
|
11
|
+
return process.env.LOCALAPPDATA && process.env.LOCALAPPDATA.trim() !== ""
|
|
12
|
+
? path.join(process.env.LOCALAPPDATA, "Programs", "bgm-cli")
|
|
13
|
+
: path.join(os.homedir(), "AppData", "Local", "Programs", "bgm-cli");
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
return path.join(os.homedir(), ".local", "share", "bgm-cli");
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function getShellReloadHint() {
|
|
20
|
+
if (process.platform === "win32") {
|
|
21
|
+
return "Restart PowerShell or CMD, then run `bgm --help`.";
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const shell = process.env.SHELL ?? "";
|
|
25
|
+
if (shell.includes("zsh")) {
|
|
26
|
+
return "Run `source ~/.zshrc`, then run `bgm --help`.";
|
|
27
|
+
}
|
|
28
|
+
if (shell.includes("bash")) {
|
|
29
|
+
return "Run `source ~/.bashrc`, then run `bgm --help`.";
|
|
30
|
+
}
|
|
31
|
+
return "Reload your shell configuration, then run `bgm --help`.";
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function getUpdateShellHint() {
|
|
35
|
+
if (process.platform === "win32") {
|
|
36
|
+
return "Restart PowerShell or CMD if the old process is still open, then run `bgm --help`.";
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
return "Open a new shell if the current process still holds the old script, then run `bgm --help`.";
|
|
40
|
+
}
|