@jackwener/opencli 1.7.1 → 1.7.3

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 (122) hide show
  1. package/README.md +5 -2
  2. package/README.zh-CN.md +6 -3
  3. package/cli-manifest.json +1085 -73
  4. package/clis/barchart/flow.js +1 -1
  5. package/clis/barchart/greeks.js +2 -2
  6. package/clis/barchart/options.js +2 -2
  7. package/clis/barchart/quote.js +1 -1
  8. package/clis/bilibili/feed.js +202 -48
  9. package/clis/binance/asks.js +21 -0
  10. package/clis/binance/commands.test.js +70 -0
  11. package/clis/binance/depth.js +21 -0
  12. package/clis/binance/gainers.js +22 -0
  13. package/clis/binance/klines.js +21 -0
  14. package/clis/binance/losers.js +22 -0
  15. package/clis/binance/pairs.js +21 -0
  16. package/clis/binance/price.js +18 -0
  17. package/clis/binance/prices.js +19 -0
  18. package/clis/binance/ticker.js +21 -0
  19. package/clis/binance/top.js +21 -0
  20. package/clis/binance/trades.js +20 -0
  21. package/clis/boss/utils.js +2 -1
  22. package/clis/chatgpt/image.js +97 -0
  23. package/clis/chatgpt/utils.js +297 -0
  24. package/clis/{chatgpt → chatgpt-app}/ask.js +1 -1
  25. package/clis/{chatgpt → chatgpt-app}/model.js +1 -1
  26. package/clis/{chatgpt → chatgpt-app}/new.js +1 -1
  27. package/clis/{chatgpt → chatgpt-app}/read.js +1 -1
  28. package/clis/{chatgpt → chatgpt-app}/send.js +1 -1
  29. package/clis/{chatgpt → chatgpt-app}/status.js +1 -1
  30. package/clis/discord-app/delete.js +114 -0
  31. package/clis/douban/utils.js +29 -2
  32. package/clis/douban/utils.test.js +121 -1
  33. package/clis/ke/chengjiao.js +77 -0
  34. package/clis/ke/ershoufang.js +100 -0
  35. package/clis/ke/utils.js +104 -0
  36. package/clis/ke/xiaoqu.js +77 -0
  37. package/clis/ke/zufang.js +94 -0
  38. package/clis/maimai/search-talents.js +172 -0
  39. package/clis/mubu/doc.js +40 -0
  40. package/clis/mubu/docs.js +43 -0
  41. package/clis/mubu/notes.js +244 -0
  42. package/clis/mubu/recent.js +27 -0
  43. package/clis/mubu/search.js +62 -0
  44. package/clis/mubu/utils.js +304 -0
  45. package/clis/reuters/search.js +1 -1
  46. package/clis/twitter/lists-parser.js +77 -0
  47. package/clis/twitter/lists.d.ts +5 -0
  48. package/clis/twitter/lists.js +62 -0
  49. package/clis/twitter/lists.test.js +50 -0
  50. package/clis/weibo/feed.js +18 -5
  51. package/clis/xiaohongshu/comments.js +18 -6
  52. package/clis/xiaohongshu/comments.test.js +36 -0
  53. package/clis/xiaohongshu/creator-note-detail.js +2 -0
  54. package/clis/xiaohongshu/creator-note-detail.test.js +32 -0
  55. package/clis/xiaohongshu/creator-notes-summary.js +4 -0
  56. package/clis/xiaohongshu/creator-notes-summary.test.js +39 -1
  57. package/clis/xiaohongshu/creator-notes.js +1 -0
  58. package/clis/xiaohongshu/creator-profile.js +1 -0
  59. package/clis/xiaohongshu/creator-stats.js +1 -0
  60. package/clis/xiaohongshu/download.js +12 -0
  61. package/clis/xiaohongshu/download.test.js +30 -0
  62. package/clis/xiaohongshu/navigation.test.js +34 -0
  63. package/clis/xiaohongshu/note.js +14 -5
  64. package/clis/xiaohongshu/note.test.js +28 -0
  65. package/clis/xiaohongshu/publish.js +1 -0
  66. package/clis/xiaohongshu/search.js +1 -0
  67. package/clis/xiaohongshu/user.js +1 -0
  68. package/clis/yahoo-finance/quote.js +1 -1
  69. package/clis/zsxq/topic.js +5 -3
  70. package/clis/zsxq/topic.test.js +4 -3
  71. package/clis/zsxq/utils.js +1 -1
  72. package/dist/src/browser/base-page.d.ts +9 -0
  73. package/dist/src/browser/base-page.js +19 -0
  74. package/dist/src/browser/cdp.js +10 -2
  75. package/dist/src/browser/daemon-client.d.ts +1 -0
  76. package/dist/src/cli.js +112 -2
  77. package/dist/src/daemon.js +5 -0
  78. package/dist/src/discovery.d.ts +5 -2
  79. package/dist/src/discovery.js +7 -35
  80. package/dist/src/doctor.d.ts +1 -0
  81. package/dist/src/doctor.js +51 -2
  82. package/dist/src/electron-apps.js +1 -1
  83. package/dist/src/engine.test.js +29 -1
  84. package/dist/src/errors.d.ts +1 -0
  85. package/dist/src/errors.js +13 -0
  86. package/dist/src/execution.js +36 -9
  87. package/dist/src/execution.test.js +23 -0
  88. package/dist/src/logger.d.ts +2 -2
  89. package/dist/src/logger.js +4 -9
  90. package/dist/src/main.js +6 -5
  91. package/dist/src/registry.js +3 -4
  92. package/dist/src/types.d.ts +2 -0
  93. package/dist/src/update-check.d.ts +14 -0
  94. package/dist/src/update-check.js +48 -3
  95. package/dist/src/update-check.test.js +31 -0
  96. package/package.json +3 -3
  97. package/scripts/fetch-adapters.js +92 -34
  98. package/dist/src/clis/binance/asks.js +0 -20
  99. package/dist/src/clis/binance/commands.test.d.ts +0 -3
  100. package/dist/src/clis/binance/commands.test.js +0 -58
  101. package/dist/src/clis/binance/depth.d.ts +0 -1
  102. package/dist/src/clis/binance/depth.js +0 -20
  103. package/dist/src/clis/binance/gainers.d.ts +0 -1
  104. package/dist/src/clis/binance/gainers.js +0 -21
  105. package/dist/src/clis/binance/klines.d.ts +0 -1
  106. package/dist/src/clis/binance/klines.js +0 -20
  107. package/dist/src/clis/binance/losers.d.ts +0 -1
  108. package/dist/src/clis/binance/losers.js +0 -21
  109. package/dist/src/clis/binance/pairs.d.ts +0 -1
  110. package/dist/src/clis/binance/pairs.js +0 -20
  111. package/dist/src/clis/binance/price.d.ts +0 -1
  112. package/dist/src/clis/binance/price.js +0 -17
  113. package/dist/src/clis/binance/prices.d.ts +0 -1
  114. package/dist/src/clis/binance/prices.js +0 -18
  115. package/dist/src/clis/binance/ticker.d.ts +0 -1
  116. package/dist/src/clis/binance/ticker.js +0 -20
  117. package/dist/src/clis/binance/top.d.ts +0 -1
  118. package/dist/src/clis/binance/top.js +0 -20
  119. package/dist/src/clis/binance/trades.d.ts +0 -1
  120. package/dist/src/clis/binance/trades.js +0 -19
  121. /package/clis/{chatgpt → chatgpt-app}/ax.js +0 -0
  122. /package/dist/src/{clis/binance/asks.d.ts → update-check.test.d.ts} +0 -0
@@ -0,0 +1,304 @@
1
+ import { AuthRequiredError, CommandExecutionError } from '@jackwener/opencli/errors';
2
+
3
+ export const API_BASE = 'https://api2.mubu.com/v3/api';
4
+ const MUBU_DOMAIN = 'mubu.com';
5
+ const AUTH_HINT = 'Mubu requires a logged-in browser session at mubu.com';
6
+
7
+ function isAuthFailure(code, message) {
8
+ if (code === 401 || code === 403) return true;
9
+ if (!message) return false;
10
+ return /not logged in|login required|unauthorized|未登录|请先登录|需要登录|login expired/i.test(message);
11
+ }
12
+
13
+ /**
14
+ * 在浏览器页面上下文里用 XHR 发 POST 请求(参考 zsxq 适配器模式)。
15
+ * mubu app 自身也是这个机制:从 localStorage 读 Jwt-Token,通过同名 header 发到 api2.mubu.com。
16
+ * 不经过 Node.js 进程发网络请求,避免 CORS 问题和 extension fetch 拦截。
17
+ */
18
+ export async function mubuPost(page, path, body) {
19
+ const url = `${API_BASE}${path}`;
20
+
21
+ const result = await page.evaluate(`
22
+ (async () => {
23
+ const token = localStorage.getItem('Jwt-Token');
24
+ if (!token) return { ok: false, status: 0, data: null, error: 'no token' };
25
+ return await new Promise((resolve) => {
26
+ const xhr = new XMLHttpRequest();
27
+ xhr.open('POST', ${JSON.stringify(url)}, true);
28
+ xhr.setRequestHeader('Content-Type', 'application/json');
29
+ xhr.setRequestHeader('Jwt-Token', token);
30
+ xhr.onload = () => {
31
+ let data = null;
32
+ try { data = JSON.parse(xhr.responseText); } catch {}
33
+ resolve({ ok: xhr.status >= 200 && xhr.status < 300, status: xhr.status, data });
34
+ };
35
+ xhr.onerror = () => resolve({ ok: false, status: 0, data: null, error: 'network error' });
36
+ xhr.send(${JSON.stringify(JSON.stringify(body))});
37
+ });
38
+ })()
39
+ `);
40
+
41
+ if (!result || result.error === 'no token') {
42
+ throw new AuthRequiredError(MUBU_DOMAIN, AUTH_HINT);
43
+ }
44
+ if (!result.ok || !result.data) {
45
+ throw new CommandExecutionError(`mubu: ${path}: HTTP ${result.status} ${result.error ?? ''}`);
46
+ }
47
+
48
+ const { data } = result;
49
+ if (data.code !== 0) {
50
+ if (isAuthFailure(data.code, data.message)) {
51
+ throw new AuthRequiredError(MUBU_DOMAIN, AUTH_HINT);
52
+ }
53
+ throw new CommandExecutionError(`mubu: ${path}: code=${data.code} ${data.message ?? ''}`);
54
+ }
55
+
56
+ return data.data;
57
+ }
58
+
59
+ export function formatDate(ts) {
60
+ if (!ts) return '';
61
+ const d = new Date(ts);
62
+ const pad = (n) => String(n).padStart(2, '0');
63
+ return `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())} ${pad(d.getHours())}:${pad(d.getMinutes())}`;
64
+ }
65
+
66
+ const NAMED_ENTITIES = { amp: '&', lt: '<', gt: '>', quot: '"', apos: "'", nbsp: ' ' };
67
+
68
+ function decodeHtmlEntities(s) {
69
+ return s
70
+ .replace(/&#x([0-9a-f]+);/gi, (_, h) => String.fromCodePoint(parseInt(h, 16)))
71
+ .replace(/&#(\d+);/g, (_, n) => String.fromCodePoint(parseInt(n, 10)))
72
+ .replace(/&(amp|lt|gt|quot|apos|nbsp);/g, (_, n) => NAMED_ENTITIES[n]);
73
+ }
74
+
75
+ /** 解析幕布 HTML 表格为行列二维数组(保留内部 HTML) */
76
+ function parseTableRows(tableHtml) {
77
+ const rows = [];
78
+ const rowMatches = tableHtml.match(/<tr[^>]*>[\s\S]*?<\/tr>/gi) ?? [];
79
+ for (const row of rowMatches) {
80
+ const cells = [];
81
+ const cellMatches = row.match(/<(?:td|th)[^>]*>[\s\S]*?<\/(?:td|th)>/gi) ?? [];
82
+ for (const cell of cellMatches) {
83
+ // 只剥离最外层的 <td> 或 <th>,保留内部的加粗、链接和 <br>
84
+ let innerHtml = cell.replace(/^<(?:td|th)[^>]*>|<\/(?:td|th)>$/gi, '');
85
+ cells.push(innerHtml.trim());
86
+ }
87
+ rows.push(cells);
88
+ }
89
+ return rows;
90
+ }
91
+
92
+ /** 将幕布 HTML text 转为纯文本 */
93
+ export function htmlToText(html) {
94
+ let text = html;
95
+ // 表格 → 纯文本(tab 分隔);用 </table>\s*</div> 作结束锚,跳过 th/td 内部嵌套 div
96
+ text = text.replace(/<div class="table-container">[\s\S]*?<\/table>\s*<\/div>/g, (m) => {
97
+ return parseTableRows(m).map((r) => r.map(c => {
98
+ // 纯文本环境:<br> 换空格,清空所有标签
99
+ let plainCell = c.replace(/<br\s*\/?>/gi, ' ').replace(/<[^>]+>/g, '');
100
+ return decodeHtmlEntities(plainCell).trim();
101
+ }).join('\t')).join('\n');
102
+ });
103
+ text = text
104
+ .replace(/<br\s*\/?>/gi, '\n')
105
+ .replace(/<[^>]+>/g, '');
106
+ return decodeHtmlEntities(text).trim();
107
+ }
108
+
109
+ /** 将幕布 HTML 表格转为 Markdown 表格 */
110
+ function tableToMarkdown(tableHtml) {
111
+ const rows = parseTableRows(tableHtml);
112
+ if (rows.length === 0) return '';
113
+
114
+ const processRow = (row) => row.map(cellHtml => {
115
+ // 把表格内的 <br> 换成占位符,防止稍后被全局替换为 \n 导致表格断裂
116
+ return cellHtml.replace(/<br\s*\/?>/gi, '[[BR]]');
117
+ }).join(' | ');
118
+
119
+ const lines = [`| ${processRow(rows[0])} |`, `| ${rows[0].map(() => '---').join(' | ')} |`];
120
+ for (let i = 1; i < rows.length; i++) {
121
+ lines.push(`| ${processRow(rows[i])} |`);
122
+ }
123
+ return lines.join('\n');
124
+ }
125
+
126
+ /** 将幕布 HTML text 转为 Markdown inline 标记 */
127
+ export function htmlToMarkdown(html) {
128
+ let md = html;
129
+
130
+ // 1. 表格 → Markdown 表格
131
+ md = md.replace(/<div class="table-container">[\s\S]*?<\/table>\s*<\/div>/g, (m) => tableToMarkdown(m));
132
+
133
+ // 2. <br> → 换行
134
+ md = md.replace(/<br\s*\/?>/gi, '\n');
135
+
136
+ // 3. 统一处理样式标签,支持多 class 组合
137
+ md = md.replace(/<span class="([^"]+)"[^>]*>([\s\S]*?)<\/span>/gi, (match, classes, inner) => {
138
+ // 允许正常处理超链接内部的 bold 和 italic 样式
139
+ if (classes.includes('node-mention')) {
140
+ return match;
141
+ }
142
+ let res = inner;
143
+ if (/\bbold\b/.test(classes)) res = `**${res}**`;
144
+ if (/\bitalic\b/.test(classes)) res = `*${res}*`;
145
+ if (/\bstrikethrough\b/.test(classes)) res = `~~${res}~~`;
146
+ if (/\bunderline\b/.test(classes)) res = `\uFFFEU_OPEN\uFFFE${res}\uFFFEU_CLOSE\uFFFE`;
147
+ return res;
148
+ });
149
+
150
+ // 4. node-mention(主题链接 → Markdown 链接,支持组合 class 并继承自身样式)
151
+ md = md.replace(
152
+ /<span([^>]*\bclass="[^"]*\bnode-mention\b[^"]*"[^>]*)>([\s\S]*?)<\/span>/gi,
153
+ (match, attrs, inner) => {
154
+ const docMatch = attrs.match(/\bdata-doc="([^"]+)"/i);
155
+ const docId = docMatch ? docMatch[1] : '';
156
+ if (!docId) return match;
157
+
158
+ const classMatch = attrs.match(/\bclass="([^"]+)"/i);
159
+ const classes = classMatch ? classMatch[1] : '';
160
+
161
+ let res = inner.replace(/<[^>]+>/g, '').trim();
162
+
163
+ // 继承标签自身的样式
164
+ if (/\bbold\b/.test(classes)) res = `**${res}**`;
165
+ if (/\bitalic\b/.test(classes)) res = `*${res}*`;
166
+ if (/\bstrikethrough\b/.test(classes)) res = `~~${res}~~`;
167
+ if (/\bunderline\b/.test(classes)) res = `\uFFFEU_OPEN\uFFFE${res}\uFFFEU_CLOSE\uFFFE`;
168
+
169
+ return `[${res}](https://mubu.com/app/edit/${docId})`;
170
+ }
171
+ );
172
+
173
+ // 5. links (外部链接 → Markdown 链接,继承 a 标签自身样式)
174
+ md = md.replace(/<a([^>]*)>([\s\S]*?)<\/a>/gi, (match, attrs, inner) => {
175
+ const hrefMatch = attrs.match(/\bhref="([^"]+)"/i);
176
+ if (!hrefMatch) return match;
177
+ const href = hrefMatch[1];
178
+
179
+ const classMatch = attrs.match(/\bclass="([^"]+)"/i);
180
+ const classes = classMatch ? classMatch[1] : '';
181
+
182
+ let res = inner;
183
+
184
+ // 继承 a 标签自身的样式
185
+ if (/\bbold\b/.test(classes)) res = `**${res}**`;
186
+ if (/\bitalic\b/.test(classes)) res = `*${res}*`;
187
+ if (/\bstrikethrough\b/.test(classes)) res = `~~${res}~~`;
188
+ if (/\bunderline\b/.test(classes)) res = `\uFFFEU_OPEN\uFFFE${res}\uFFFEU_CLOSE\uFFFE`;
189
+
190
+ return `[${res}](${href})`;
191
+ });
192
+
193
+ // 6. 普通 span
194
+ md = md.replace(/<span[^>]*>([\s\S]*?)<\/span>/gi, '$1');
195
+
196
+ // 7. 清理其余标签
197
+ md = md.replace(/<[^>]+>/g, '');
198
+
199
+ // 8. HTML 实体解码
200
+ md = decodeHtmlEntities(md);
201
+
202
+ // 9. 还原 underline 占位符
203
+ md = md.replace(/\uFFFEU_OPEN\uFFFE/g, '<u>').replace(/\uFFFEU_CLOSE\uFFFE/g, '</u>');
204
+
205
+ // 10. 还原表格内的换行符
206
+ md = md.replace(/\[\[BR\]\]/g, '<br>');
207
+
208
+ return md.trim();
209
+ }
210
+
211
+ const IMAGE_BASE = 'https://api2.mubu.com/v3';
212
+
213
+ function imageUrl(uri) {
214
+ return uri.startsWith('http') ? uri : `${IMAGE_BASE}/${uri}`;
215
+ }
216
+
217
+ function taskPrefix(node) {
218
+ if (!node.taskStatus) return '';
219
+ return node.taskStatus === 2 ? '[x] ' : '[ ] ';
220
+ }
221
+
222
+ function taskMeta(node) {
223
+ const parts = [];
224
+ if (node.deadline) {
225
+ const ts = formatDate(node.deadline * 1000);
226
+ parts.push(`📅 ${node.deadlineType === 'date' ? ts.slice(0, 10) : ts}`);
227
+ }
228
+ if (node.remindAt) parts.push(`⏰ ${formatDate(node.remindAt * 1000)}`);
229
+ return parts.length ? ' ' + parts.join(' ') : '';
230
+ }
231
+
232
+ /** 递归将节点树渲染为缩进纯文本 */
233
+ export function nodesToText(nodes, depth = 0) {
234
+ const lines = [];
235
+ for (const node of nodes) {
236
+ const indent = ' '.repeat(depth);
237
+ const emoji = node.emoji ? node.emoji + ' ' : '';
238
+ const text = htmlToText(node.text);
239
+ const prefix = taskPrefix(node);
240
+ const meta = taskMeta(node);
241
+ if (text || emoji || prefix) {
242
+ if (text.includes('\n')) {
243
+ const [first, ...rest] = text.split('\n');
244
+ lines.push(indent + prefix + emoji + first);
245
+ for (const line of rest) lines.push(indent + ' ' + line);
246
+ if (meta) lines.push(indent + ' ' + meta.trim());
247
+ } else {
248
+ lines.push(indent + prefix + emoji + text + meta);
249
+ }
250
+ }
251
+ if (node.note) {
252
+ const noteText = htmlToText(node.note);
253
+ for (const line of noteText.split('\n')) lines.push(indent + ' ' + line);
254
+ }
255
+ if (node.images?.length) {
256
+ for (const img of node.images) {
257
+ lines.push(indent + `[图片: ${imageUrl(img.uri)}]`);
258
+ }
259
+ }
260
+ if (node.children?.length) {
261
+ lines.push(nodesToText(node.children, depth + 1));
262
+ }
263
+ }
264
+ return lines.filter(Boolean).join('\n');
265
+ }
266
+
267
+ /** 递归将节点树渲染为 Markdown(大纲 = 缩进列表,不映射为标题) */
268
+ export function nodesToMarkdown(nodes, depth = 0) {
269
+ const lines = [];
270
+ for (const node of nodes) {
271
+ const text = htmlToMarkdown(node.text);
272
+ if (!text && !node.images?.length && !node.note && !node.emoji) continue;
273
+
274
+ const indent = ' '.repeat(depth);
275
+ const emoji = node.emoji ? node.emoji + ' ' : '';
276
+ const prefix = taskPrefix(node);
277
+ const meta = taskMeta(node);
278
+ if (text || emoji || prefix) {
279
+ if (text.includes('\n')) {
280
+ const [first, ...rest] = text.split('\n');
281
+ lines.push(indent + '- ' + prefix + emoji + first);
282
+ const continuation = indent + ' ';
283
+ for (const line of rest) lines.push(continuation + line);
284
+ if (meta) lines.push(continuation + meta.trim());
285
+ } else {
286
+ lines.push(indent + '- ' + prefix + emoji + text + meta);
287
+ }
288
+ }
289
+ if (node.note) {
290
+ const noteLines = htmlToMarkdown(node.note).split('\n');
291
+ for (const line of noteLines) lines.push(indent + ' > ' + line);
292
+ }
293
+ if (node.images?.length) {
294
+ for (const img of node.images) {
295
+ lines.push(indent + ` ![image](${imageUrl(img.uri)})`);
296
+ }
297
+ }
298
+
299
+ if (node.children?.length) {
300
+ lines.push(nodesToMarkdown(node.children, depth + 1));
301
+ }
302
+ }
303
+ return lines.filter(Boolean).join('\n');
304
+ }
@@ -21,7 +21,7 @@ cli({
21
21
  (async () => {
22
22
  const count = ${count};
23
23
  const apiQuery = JSON.stringify({
24
- keyword: '${kwargs.query.replace(/'/g, "\\'")}',
24
+ keyword: ${JSON.stringify(kwargs.query)},
25
25
  offset: 0, orderby: 'display_date:desc', size: count, website: 'reuters'
26
26
  });
27
27
  const apiUrl = 'https://www.reuters.com/pf/api/v3/content/fetch/articles-by-search-v2?query=' + encodeURIComponent(apiQuery);
@@ -0,0 +1,77 @@
1
+ const MEMBER_PATTERNS = [
2
+ /([\d.,]+(?:\s?[KMB千萬万亿])?)\s*members?/i,
3
+ /([\d.,]+(?:\s?[KMB千萬万亿])?)\s*位成员/,
4
+ ];
5
+ const FOLLOWER_PATTERNS = [
6
+ /([\d.,]+(?:\s?[KMB千萬万亿])?)\s*followers?/i,
7
+ /([\d.,]+(?:\s?[KMB千萬万亿])?)\s*位关注者/,
8
+ ];
9
+ const PRIVATE_PATTERNS = [/\bprivate\b/i, /锁定列表/];
10
+ const EMPTY_STATE_PATTERNS = [
11
+ /hasn't created any lists/i,
12
+ /has not created any lists/i,
13
+ /no lists yet/i,
14
+ /没有创建任何列表/,
15
+ /还没有创建任何列表/,
16
+ ];
17
+ function normalizeText(text) {
18
+ return String(text || '').replace(/\s+/g, ' ').trim();
19
+ }
20
+ function matchMetric(text, patterns) {
21
+ for (const pattern of patterns) {
22
+ const match = text.match(pattern);
23
+ if (match)
24
+ return normalizeText(match[1]);
25
+ }
26
+ return '0';
27
+ }
28
+ function looksLikeMetadata(line) {
29
+ const text = normalizeText(line);
30
+ if (!text)
31
+ return true;
32
+ if (text.startsWith('@'))
33
+ return true;
34
+ if (MEMBER_PATTERNS.some((pattern) => pattern.test(text)))
35
+ return true;
36
+ if (FOLLOWER_PATTERNS.some((pattern) => pattern.test(text)))
37
+ return true;
38
+ if (PRIVATE_PATTERNS.some((pattern) => pattern.test(text)))
39
+ return true;
40
+ if (/^(public|pinned)$/i.test(text))
41
+ return true;
42
+ if (/^(lists?|你的列表)$/i.test(text))
43
+ return true;
44
+ return false;
45
+ }
46
+ export function parseListCards(cards) {
47
+ const seen = new Set();
48
+ const results = [];
49
+ for (const card of cards || []) {
50
+ const href = normalizeText(card?.href);
51
+ const rawText = String(card?.text || '');
52
+ if (!href || seen.has(href))
53
+ continue;
54
+ seen.add(href);
55
+ const text = normalizeText(rawText);
56
+ if (!text)
57
+ continue;
58
+ const lines = rawText
59
+ .split('\n')
60
+ .map((line) => normalizeText(line))
61
+ .filter(Boolean);
62
+ const name = lines.find((line) => !looksLikeMetadata(line));
63
+ if (!name)
64
+ continue;
65
+ results.push({
66
+ name,
67
+ members: matchMetric(text, MEMBER_PATTERNS),
68
+ followers: matchMetric(text, FOLLOWER_PATTERNS),
69
+ mode: PRIVATE_PATTERNS.some((pattern) => pattern.test(text)) ? 'private' : 'public',
70
+ });
71
+ }
72
+ return results;
73
+ }
74
+ export function isEmptyListsState(text) {
75
+ const normalized = normalizeText(text);
76
+ return EMPTY_STATE_PATTERNS.some((pattern) => pattern.test(normalized));
77
+ }
@@ -0,0 +1,5 @@
1
+ import { Argument, Column } from '@jackwener/opencli/types';
2
+ declare const args: Argument[];
3
+ declare const columns: Column[];
4
+ export { args, columns };
5
+ export default {};
@@ -0,0 +1,62 @@
1
+ import { AuthRequiredError, SelectorError } from '@jackwener/opencli/errors';
2
+ import { cli, Strategy } from '@jackwener/opencli/registry';
3
+ import { isEmptyListsState, parseListCards } from './lists-parser.js';
4
+
5
+ cli({
6
+ site: 'twitter',
7
+ name: 'lists',
8
+ description: 'Get Twitter/X lists for a user',
9
+ domain: 'x.com',
10
+ strategy: Strategy.COOKIE,
11
+ browser: true,
12
+ args: [
13
+ { name: 'user', positional: true, type: 'string', required: false },
14
+ { name: 'limit', type: 'int', default: 50 },
15
+ ],
16
+ columns: ['name', 'members', 'followers', 'mode'],
17
+ func: async (page, kwargs) => {
18
+ let targetUser = kwargs.user;
19
+ if (!targetUser) {
20
+ await page.goto('https://x.com/home');
21
+ await page.wait({ selector: '[data-testid="primaryColumn"]' });
22
+ const href = await page.evaluate(`() => {
23
+ const link = document.querySelector('a[data-testid="AppTabBar_Profile_Link"]');
24
+ return link ? link.getAttribute('href') : null;
25
+ }`);
26
+ if (!href) {
27
+ throw new AuthRequiredError('x.com', 'Could not find logged-in user profile link. Are you logged in?');
28
+ }
29
+ targetUser = href.replace('/', '');
30
+ }
31
+ await page.goto(`https://x.com/${targetUser}/lists`);
32
+ await page.wait(3);
33
+ const pageData = await page.evaluate(`() => {
34
+ const cards = [];
35
+ const seen = new Set();
36
+ for (const anchor of Array.from(document.querySelectorAll('a[href*="/i/lists/"]'))) {
37
+ const href = anchor.getAttribute('href') || '';
38
+ if (!/\\/i\\/lists\\/\\d+/.test(href) || seen.has(href)) continue;
39
+ seen.add(href);
40
+ const container = anchor.closest('[data-testid="cellInnerDiv"]') || anchor;
41
+ const text = (container.innerText || anchor.innerText || '').trim();
42
+ if (!text) continue;
43
+ cards.push({ href, text });
44
+ }
45
+ return {
46
+ cards,
47
+ pageText: document.body.innerText || '',
48
+ };
49
+ }`);
50
+ if (!pageData?.pageText) {
51
+ throw new SelectorError('Twitter lists', 'Empty page text');
52
+ }
53
+ const results = parseListCards(pageData.cards);
54
+ if (results.length === 0) {
55
+ if (isEmptyListsState(pageData.pageText)) {
56
+ return [];
57
+ }
58
+ throw new SelectorError('Twitter lists', `Could not parse list data`);
59
+ }
60
+ return results.slice(0, kwargs.limit);
61
+ }
62
+ });
@@ -0,0 +1,50 @@
1
+ import { describe, expect, it } from 'vitest';
2
+ import { isEmptyListsState, parseListCards } from './lists-parser.js';
3
+
4
+ describe('twitter lists parser', () => {
5
+ it('parses english list cards without relying on page locale', () => {
6
+ const result = parseListCards([
7
+ {
8
+ href: '/i/lists/123',
9
+ text: `AI Researchers
10
+ @jack
11
+ 124 Members 3.4K Followers
12
+ Private`,
13
+ },
14
+ ]);
15
+ expect(result).toEqual([
16
+ {
17
+ name: 'AI Researchers',
18
+ members: '124',
19
+ followers: '3.4K',
20
+ mode: 'private',
21
+ },
22
+ ]);
23
+ });
24
+
25
+ it('parses chinese list cards without scanning document.body.innerText', () => {
26
+ const result = parseListCards([
27
+ {
28
+ href: '/i/lists/456',
29
+ text: `AI观察
30
+ @jack
31
+ 321 位成员 8.8K 位关注者
32
+ 锁定列表`,
33
+ },
34
+ ]);
35
+ expect(result).toEqual([
36
+ {
37
+ name: 'AI观察',
38
+ members: '321',
39
+ followers: '8.8K',
40
+ mode: 'private',
41
+ },
42
+ ]);
43
+ });
44
+
45
+ it('detects empty state text in english and chinese', () => {
46
+ expect(isEmptyListsState(`@jack hasn't created any Lists yet`)).toBe(true);
47
+ expect(isEmptyListsState('这个账号还没有创建任何列表')).toBe(true);
48
+ expect(isEmptyListsState('AI Researchers 124 Members')).toBe(false);
49
+ });
50
+ });
@@ -1,20 +1,32 @@
1
1
  /**
2
- * Weibo feed — home timeline from followed users.
2
+ * Weibo feed — for-you or following timeline.
3
3
  */
4
4
  import { cli, Strategy } from '@jackwener/opencli/registry';
5
5
  import { getSelfUid } from './utils.js';
6
+ const TIMELINE_ENDPOINTS = {
7
+ 'for-you': 'unreadfriendstimeline',
8
+ following: 'friendstimeline',
9
+ };
6
10
  cli({
7
11
  site: 'weibo',
8
12
  name: 'feed',
9
- description: 'Weibo home timeline (posts from followed users)',
13
+ description: 'Fetch Weibo timeline (for-you or following)',
10
14
  domain: 'weibo.com',
11
15
  strategy: Strategy.COOKIE,
12
16
  args: [
17
+ {
18
+ name: 'type',
19
+ default: 'for-you',
20
+ choices: ['for-you', 'following'],
21
+ help: 'Timeline type: for-you (algorithmic) or following (chronological)',
22
+ },
13
23
  { name: 'limit', type: 'int', default: 15, help: 'Number of posts (max 50)' },
14
24
  ],
15
25
  columns: ['author', 'text', 'reposts', 'comments', 'likes', 'time', 'url'],
16
26
  func: async (page, kwargs) => {
17
27
  const count = Math.min(kwargs.limit || 15, 50);
28
+ const timelineType = kwargs.type === 'following' ? 'following' : 'for-you';
29
+ const endpoint = TIMELINE_ENDPOINTS[timelineType];
18
30
  await page.goto('https://weibo.com');
19
31
  await page.wait(2);
20
32
  const uid = await getSelfUid(page);
@@ -22,13 +34,14 @@ cli({
22
34
  (async () => {
23
35
  const uid = ${JSON.stringify(uid)};
24
36
  const count = ${count};
37
+ const endpoint = ${JSON.stringify(endpoint)};
25
38
  const listId = '10001' + uid;
26
39
  const strip = (html) => (html || '').replace(/<[^>]+>/g, '').replace(/&nbsp;/g, ' ').replace(/&lt;/g, '<').replace(/&gt;/g, '>').replace(/&amp;/g, '&').trim();
27
40
 
28
- const resp = await fetch('/ajax/feed/unreadfriendstimeline?list_id=' + listId + '&refresh=4&since_id=0&count=' + count, {credentials: 'include'});
29
- if (!resp.ok) return {error: 'HTTP ' + resp.status};
41
+ const resp = await fetch('/ajax/feed/' + endpoint + '?list_id=' + listId + '&refresh=4&since_id=0&count=' + count, { credentials: 'include' });
42
+ if (!resp.ok) return { error: 'HTTP ' + resp.status };
30
43
  const data = await resp.json();
31
- if (!data.ok) return {error: 'API error: ' + (data.msg || 'unknown')};
44
+ if (!data.ok) return { error: 'API error: ' + (data.msg || 'unknown') };
32
45
 
33
46
  return (data.statuses || []).slice(0, count).map(s => {
34
47
  const u = s.user || {};
@@ -6,7 +6,7 @@
6
6
  * the --with-replies flag.
7
7
  */
8
8
  import { cli, Strategy } from '@jackwener/opencli/registry';
9
- import { AuthRequiredError, EmptyResultError } from '@jackwener/opencli/errors';
9
+ import { AuthRequiredError, CliError, EmptyResultError } from '@jackwener/opencli/errors';
10
10
  import { parseNoteId, buildNoteUrl } from './note-helpers.js';
11
11
  function parseCommentLimit(raw, fallback = 20) {
12
12
  const n = Number(raw);
@@ -20,6 +20,7 @@ cli({
20
20
  description: '获取小红书笔记评论(支持楼中楼子回复)',
21
21
  domain: 'www.xiaohongshu.com',
22
22
  strategy: Strategy.COOKIE,
23
+ navigateBefore: false,
23
24
  args: [
24
25
  { name: 'note-id', required: true, positional: true, help: 'Note ID or full URL (preserves xsec_token for access)' },
25
26
  { name: 'limit', type: 'int', default: 20, help: 'Number of top-level comments (max 50)' },
@@ -32,21 +33,27 @@ cli({
32
33
  const raw = String(kwargs['note-id']);
33
34
  const noteId = parseNoteId(raw);
34
35
  await page.goto(buildNoteUrl(raw));
35
- await page.wait(3);
36
+ await page.wait({ time: 2 + Math.random() * 3 });
36
37
  const data = await page.evaluate(`
37
38
  (async () => {
38
39
  const wait = (ms) => new Promise(r => setTimeout(r, ms))
39
40
  const withReplies = ${withReplies}
40
41
 
41
42
  // Check login state
42
- const loginWall = /登录后查看|请登录/.test(document.body.innerText || '')
43
+ const bodyText = document.body?.innerText || ''
44
+ const loginWall = /登录后查看|请登录/.test(bodyText)
45
+ const securityBlock = /安全限制|访问链接异常/.test(bodyText)
46
+ || /website-login\\/error|error_code=300017|error_code=300031/.test(location.href)
43
47
 
44
48
  // Scroll the note container to trigger comment loading
45
49
  const scroller = document.querySelector('.note-scroller') || document.querySelector('.container')
46
50
  if (scroller) {
47
51
  for (let i = 0; i < 3; i++) {
52
+ const beforeCount = scroller.querySelectorAll('.parent-comment').length
48
53
  scroller.scrollTo(0, scroller.scrollHeight)
49
- await wait(1000)
54
+ await wait(800 + Math.random() * 1200)
55
+ const afterCount = scroller.querySelectorAll('.parent-comment').length
56
+ if (afterCount <= beforeCount) break
50
57
  }
51
58
  }
52
59
 
@@ -72,7 +79,7 @@ cli({
72
79
  const text = clean(el)
73
80
  el.click()
74
81
  clickedTexts.add(text)
75
- await wait(300)
82
+ await wait(200 + Math.random() * 300)
76
83
  }
77
84
  }
78
85
  }
@@ -105,12 +112,17 @@ cli({
105
112
  }
106
113
  }
107
114
 
108
- return { loginWall, results }
115
+ return { pageUrl: location.href, securityBlock, loginWall, results }
109
116
  })()
110
117
  `);
111
118
  if (!data || typeof data !== 'object') {
112
119
  throw new EmptyResultError('xiaohongshu/comments', 'Unexpected evaluate response');
113
120
  }
121
+ if (data.securityBlock) {
122
+ throw new CliError('SECURITY_BLOCK', 'Xiaohongshu security block: the note detail page was blocked by risk control.', /^https?:\/\//.test(raw)
123
+ ? 'The page may be temporarily restricted. Try again later or from a different session.'
124
+ : 'Try using a full URL from search results (with xsec_token) instead of a bare note ID.');
125
+ }
114
126
  if (data.loginWall) {
115
127
  throw new AuthRequiredError('www.xiaohongshu.com', 'Note comments require login');
116
128
  }