@jackwener/opencli 1.0.0 → 1.0.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.
- package/.github/workflows/build-extension.yml +62 -0
- package/.github/workflows/ci.yml +6 -6
- package/.github/workflows/e2e-headed.yml +2 -2
- package/.github/workflows/pkg-pr-new.yml +2 -2
- package/.github/workflows/release.yml +2 -5
- package/.github/workflows/security.yml +2 -2
- package/CDP.md +1 -1
- package/CDP.zh-CN.md +1 -1
- package/README.md +35 -8
- package/README.zh-CN.md +35 -8
- package/SKILL.md +3 -5
- package/dist/browser/cdp.d.ts +27 -0
- package/dist/browser/cdp.js +295 -0
- package/dist/browser/daemon-client.d.ts +1 -1
- package/dist/browser/index.d.ts +4 -2
- package/dist/browser/index.js +5 -5
- package/dist/browser/mcp.d.ts +5 -8
- package/dist/browser/mcp.js +9 -10
- package/dist/browser/page.d.ts +8 -1
- package/dist/browser/page.js +25 -40
- package/dist/browser/utils.d.ts +10 -0
- package/dist/browser/utils.js +27 -0
- package/dist/browser.test.js +48 -7
- package/dist/chaoxing.d.ts +58 -0
- package/dist/chaoxing.js +225 -0
- package/dist/chaoxing.test.d.ts +1 -0
- package/dist/chaoxing.test.js +38 -0
- package/dist/cli-manifest.json +597 -14
- package/dist/cli.d.ts +1 -0
- package/dist/cli.js +197 -0
- package/dist/clis/apple-podcasts/episodes.d.ts +1 -0
- package/dist/clis/apple-podcasts/episodes.js +28 -0
- package/dist/clis/apple-podcasts/search.d.ts +1 -0
- package/dist/clis/apple-podcasts/search.js +29 -0
- package/dist/clis/apple-podcasts/top.d.ts +1 -0
- package/dist/clis/apple-podcasts/top.js +34 -0
- package/dist/clis/apple-podcasts/utils.d.ts +11 -0
- package/dist/clis/apple-podcasts/utils.js +30 -0
- package/dist/clis/apple-podcasts/utils.test.d.ts +1 -0
- package/dist/clis/apple-podcasts/utils.test.js +57 -0
- package/dist/clis/boss/chatlist.d.ts +1 -0
- package/dist/clis/boss/chatlist.js +50 -0
- package/dist/clis/boss/chatmsg.d.ts +1 -0
- package/dist/clis/boss/chatmsg.js +73 -0
- package/dist/clis/boss/send.d.ts +1 -0
- package/dist/clis/boss/send.js +176 -0
- package/dist/clis/chaoxing/assignments.d.ts +1 -0
- package/dist/clis/chaoxing/assignments.js +74 -0
- package/dist/clis/chaoxing/exams.d.ts +1 -0
- package/dist/clis/chaoxing/exams.js +74 -0
- package/dist/clis/chatgpt/ask.js +15 -14
- package/dist/clis/chatgpt/ax.d.ts +1 -0
- package/dist/clis/chatgpt/ax.js +78 -0
- package/dist/clis/chatgpt/read.js +5 -6
- package/dist/clis/chatwise/history.js +18 -1
- package/dist/clis/discord-app/channels.js +33 -21
- package/dist/clis/twitter/accept.d.ts +1 -0
- package/dist/clis/twitter/accept.js +202 -0
- package/dist/clis/twitter/followers.js +30 -22
- package/dist/clis/twitter/following.js +19 -14
- package/dist/clis/twitter/notifications.js +29 -22
- package/dist/clis/twitter/post.js +9 -2
- package/dist/clis/twitter/reply-dm.d.ts +1 -0
- package/dist/clis/twitter/reply-dm.js +181 -0
- package/dist/clis/twitter/search.js +30 -11
- package/dist/clis/weread/book.d.ts +1 -0
- package/dist/clis/weread/book.js +26 -0
- package/dist/clis/weread/highlights.d.ts +1 -0
- package/dist/clis/weread/highlights.js +23 -0
- package/dist/clis/weread/notebooks.d.ts +1 -0
- package/dist/clis/weread/notebooks.js +21 -0
- package/dist/clis/weread/notes.d.ts +1 -0
- package/dist/clis/weread/notes.js +29 -0
- package/dist/clis/weread/ranking.d.ts +1 -0
- package/dist/clis/weread/ranking.js +28 -0
- package/dist/clis/weread/search.d.ts +1 -0
- package/dist/clis/weread/search.js +25 -0
- package/dist/clis/weread/shelf.d.ts +1 -0
- package/dist/clis/weread/shelf.js +24 -0
- package/dist/clis/weread/utils.d.ts +20 -0
- package/dist/clis/weread/utils.js +72 -0
- package/dist/clis/weread/utils.test.d.ts +1 -0
- package/dist/clis/weread/utils.test.js +85 -0
- package/dist/clis/xiaohongshu/download.d.ts +1 -1
- package/dist/clis/xiaohongshu/download.js +1 -1
- package/dist/daemon.js +2 -2
- package/dist/doctor.d.ts +0 -21
- package/dist/doctor.js +2 -24
- package/dist/engine.js +24 -13
- package/dist/explore.js +46 -101
- package/dist/main.js +4 -203
- package/dist/output.d.ts +1 -1
- package/dist/registry.d.ts +3 -3
- package/dist/runtime.d.ts +1 -4
- package/dist/runtime.js +1 -4
- package/dist/scripts/framework.d.ts +4 -0
- package/dist/scripts/framework.js +21 -0
- package/dist/scripts/interact.d.ts +4 -0
- package/dist/scripts/interact.js +20 -0
- package/dist/scripts/store.d.ts +9 -0
- package/dist/scripts/store.js +44 -0
- package/dist/setup.js +2 -2
- package/dist/synthesize.js +1 -1
- package/extension/dist/background.js +392 -0
- package/extension/manifest.json +3 -3
- package/extension/package.json +1 -1
- package/extension/src/background.ts +101 -24
- package/extension/src/protocol.ts +1 -1
- package/package.json +1 -1
- package/src/browser/cdp.ts +295 -0
- package/src/browser/daemon-client.ts +1 -1
- package/src/browser/index.ts +5 -6
- package/src/browser/mcp.ts +14 -15
- package/src/browser/page.ts +25 -41
- package/src/browser/utils.ts +27 -0
- package/src/browser.test.ts +52 -6
- package/src/chaoxing.test.ts +45 -0
- package/src/chaoxing.ts +268 -0
- package/src/cli.ts +185 -0
- package/src/clis/antigravity/SKILL.md +5 -0
- package/src/clis/apple-podcasts/episodes.ts +28 -0
- package/src/clis/apple-podcasts/search.ts +29 -0
- package/src/clis/apple-podcasts/top.ts +34 -0
- package/src/clis/apple-podcasts/utils.test.ts +72 -0
- package/src/clis/apple-podcasts/utils.ts +37 -0
- package/src/clis/boss/chatlist.ts +50 -0
- package/src/clis/boss/chatmsg.ts +70 -0
- package/src/clis/boss/send.ts +193 -0
- package/src/clis/chaoxing/README.md +36 -0
- package/src/clis/chaoxing/README.zh-CN.md +35 -0
- package/src/clis/chaoxing/assignments.ts +88 -0
- package/src/clis/chaoxing/exams.ts +88 -0
- package/src/clis/chatgpt/ask.ts +14 -15
- package/src/clis/chatgpt/ax.ts +81 -0
- package/src/clis/chatgpt/read.ts +5 -7
- package/src/clis/chatwise/history.ts +15 -1
- package/src/clis/discord-app/channels.ts +33 -21
- package/src/clis/twitter/accept.ts +213 -0
- package/src/clis/twitter/followers.ts +36 -29
- package/src/clis/twitter/following.ts +25 -20
- package/src/clis/twitter/notifications.ts +34 -27
- package/src/clis/twitter/post.ts +9 -2
- package/src/clis/twitter/reply-dm.ts +193 -0
- package/src/clis/twitter/search.ts +34 -12
- package/src/clis/weread/book.ts +28 -0
- package/src/clis/weread/highlights.ts +25 -0
- package/src/clis/weread/notebooks.ts +23 -0
- package/src/clis/weread/notes.ts +31 -0
- package/src/clis/weread/ranking.ts +29 -0
- package/src/clis/weread/search.ts +26 -0
- package/src/clis/weread/shelf.ts +26 -0
- package/src/clis/weread/utils.test.ts +104 -0
- package/src/clis/weread/utils.ts +74 -0
- package/src/clis/xiaohongshu/download.ts +1 -1
- package/src/daemon.ts +2 -2
- package/src/doctor.ts +2 -19
- package/src/engine.ts +20 -13
- package/src/explore.ts +51 -100
- package/src/main.ts +4 -186
- package/src/output.ts +12 -12
- package/src/registry.ts +3 -3
- package/src/runtime.ts +2 -6
- package/src/scripts/framework.ts +20 -0
- package/src/scripts/interact.ts +22 -0
- package/src/scripts/store.ts +40 -0
- package/src/setup.ts +2 -2
- package/src/synthesize.ts +1 -1
- package/tests/e2e/public-commands.test.ts +68 -1
- package/dist/clis/grok/debug.d.ts +0 -1
- package/dist/clis/grok/debug.js +0 -45
- package/src/clis/grok/debug.ts +0 -49
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
import { cli, Strategy } from '../../registry.js';
|
|
2
|
+
cli({
|
|
3
|
+
site: 'twitter',
|
|
4
|
+
name: 'reply-dm',
|
|
5
|
+
description: 'Send a message to recent DM conversations',
|
|
6
|
+
domain: 'x.com',
|
|
7
|
+
strategy: Strategy.UI,
|
|
8
|
+
browser: true,
|
|
9
|
+
timeoutSeconds: 600, // 10 min — batch operation
|
|
10
|
+
args: [
|
|
11
|
+
{ name: 'text', type: 'string', required: true, help: 'Message text to send (e.g. "我的微信 wxkabi")' },
|
|
12
|
+
{ name: 'max', type: 'int', required: false, default: 20, help: 'Maximum number of conversations to reply to (default: 20)' },
|
|
13
|
+
{ name: 'skip-replied', type: 'boolean', required: false, default: true, help: 'Skip conversations where you already sent the same text (default: true)' },
|
|
14
|
+
],
|
|
15
|
+
columns: ['index', 'status', 'user', 'message'],
|
|
16
|
+
func: async (page, kwargs) => {
|
|
17
|
+
if (!page)
|
|
18
|
+
throw new Error('Requires browser');
|
|
19
|
+
const messageText = kwargs.text;
|
|
20
|
+
const maxSend = kwargs.max ?? 20;
|
|
21
|
+
const skipReplied = kwargs['skip-replied'] !== false;
|
|
22
|
+
const results = [];
|
|
23
|
+
let sentCount = 0;
|
|
24
|
+
// Step 1: Navigate to messages to get conversation list
|
|
25
|
+
await page.goto('https://x.com/messages');
|
|
26
|
+
await page.wait(5);
|
|
27
|
+
// Step 2: Collect conversations with scroll-to-load
|
|
28
|
+
const needed = maxSend + 10; // extra buffer for skips
|
|
29
|
+
const convList = await page.evaluate(`(async () => {
|
|
30
|
+
try {
|
|
31
|
+
// Wait for initial items
|
|
32
|
+
let attempts = 0;
|
|
33
|
+
while (attempts < 10) {
|
|
34
|
+
const items = document.querySelectorAll('[data-testid^="dm-conversation-item-"], [data-testid="conversation"]');
|
|
35
|
+
if (items.length > 0) break;
|
|
36
|
+
await new Promise(r => setTimeout(r, 1000));
|
|
37
|
+
attempts++;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// Scroll to load more conversations
|
|
41
|
+
const needed = ${needed};
|
|
42
|
+
const seenIds = new Set();
|
|
43
|
+
let noNewCount = 0;
|
|
44
|
+
|
|
45
|
+
for (let scroll = 0; scroll < 30; scroll++) {
|
|
46
|
+
const items = Array.from(document.querySelectorAll('[data-testid^="dm-conversation-item-"], [data-testid="conversation"]'));
|
|
47
|
+
items.forEach(el => seenIds.add(el.getAttribute('data-testid')));
|
|
48
|
+
|
|
49
|
+
if (seenIds.size >= needed) break;
|
|
50
|
+
|
|
51
|
+
// Find the scrollable container and scroll it
|
|
52
|
+
const scrollContainer = document.querySelector('[data-testid="dm-inbox-panel"]') ||
|
|
53
|
+
items[items.length - 1]?.closest('[class*="scroll"]') ||
|
|
54
|
+
items[items.length - 1]?.parentElement;
|
|
55
|
+
if (scrollContainer) {
|
|
56
|
+
scrollContainer.scrollTop = scrollContainer.scrollHeight;
|
|
57
|
+
}
|
|
58
|
+
// Also try scrolling the last item into view
|
|
59
|
+
if (items.length > 0) {
|
|
60
|
+
items[items.length - 1].scrollIntoView({ behavior: 'instant', block: 'end' });
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
await new Promise(r => setTimeout(r, 1500));
|
|
64
|
+
|
|
65
|
+
// Check if new items appeared
|
|
66
|
+
const newItems = Array.from(document.querySelectorAll('[data-testid^="dm-conversation-item-"], [data-testid="conversation"]'));
|
|
67
|
+
const newIds = new Set(newItems.map(el => el.getAttribute('data-testid')));
|
|
68
|
+
if (newIds.size <= seenIds.size) {
|
|
69
|
+
noNewCount++;
|
|
70
|
+
if (noNewCount >= 3) break; // No more loading after 3 tries
|
|
71
|
+
} else {
|
|
72
|
+
noNewCount = 0;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// Collect all visible conversations
|
|
77
|
+
const finalItems = Array.from(document.querySelectorAll('[data-testid^="dm-conversation-item-"], [data-testid="conversation"]'));
|
|
78
|
+
const conversations = finalItems.map((item, idx) => {
|
|
79
|
+
const testId = item.getAttribute('data-testid') || '';
|
|
80
|
+
const text = item.innerText || '';
|
|
81
|
+
const lines = text.split('\\n').filter(l => l.trim());
|
|
82
|
+
const user = lines[0] || 'Unknown';
|
|
83
|
+
const match = testId.match(/dm-conversation-item-(.+)/);
|
|
84
|
+
const convId = match ? match[1].replace(':', '-') : '';
|
|
85
|
+
const link = item.querySelector('a[href*="/messages/"]');
|
|
86
|
+
const href = link ? link.href : '';
|
|
87
|
+
return { idx, user, convId, href, preview: text.substring(0, 100) };
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
return { ok: true, conversations, total: conversations.length };
|
|
91
|
+
} catch(e) {
|
|
92
|
+
return { ok: false, error: String(e), conversations: [], total: 0 };
|
|
93
|
+
}
|
|
94
|
+
})()`);
|
|
95
|
+
if (!convList?.ok || !convList.conversations?.length) {
|
|
96
|
+
return [{ index: 1, status: 'info', user: 'System', message: 'No conversations found' }];
|
|
97
|
+
}
|
|
98
|
+
const conversations = convList.conversations;
|
|
99
|
+
// Step 3: Iterate through conversations and send message
|
|
100
|
+
for (const conv of conversations) {
|
|
101
|
+
if (sentCount >= maxSend)
|
|
102
|
+
break;
|
|
103
|
+
const convUrl = conv.convId
|
|
104
|
+
? `https://x.com/messages/${conv.convId}`
|
|
105
|
+
: conv.href;
|
|
106
|
+
if (!convUrl)
|
|
107
|
+
continue;
|
|
108
|
+
await page.goto(convUrl);
|
|
109
|
+
await page.wait(3);
|
|
110
|
+
const sendResult = await page.evaluate(`(async () => {
|
|
111
|
+
try {
|
|
112
|
+
const messageText = ${JSON.stringify(messageText)};
|
|
113
|
+
const skipReplied = ${skipReplied};
|
|
114
|
+
|
|
115
|
+
// Get username from conversation
|
|
116
|
+
const dmHeader = document.querySelector('[data-testid="DmActivityContainer"] [dir="ltr"] span') ||
|
|
117
|
+
document.querySelector('[data-testid="conversation-header"]') ||
|
|
118
|
+
document.querySelector('[data-testid="DmActivityContainer"] h2');
|
|
119
|
+
const username = dmHeader ? dmHeader.innerText.trim().split('\\\\n')[0] : '${conv.user}';
|
|
120
|
+
|
|
121
|
+
// Check if we already sent this message
|
|
122
|
+
if (skipReplied) {
|
|
123
|
+
const chatArea = document.querySelector('[data-testid="DmScrollerContainer"]') ||
|
|
124
|
+
document.querySelector('main');
|
|
125
|
+
const chatText = chatArea ? chatArea.innerText : '';
|
|
126
|
+
if (chatText.includes(messageText)) {
|
|
127
|
+
return { status: 'skipped', user: username, message: 'Already sent this message' };
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// Find the text input
|
|
132
|
+
const input = document.querySelector('[data-testid="dmComposerTextInput"]');
|
|
133
|
+
if (!input) {
|
|
134
|
+
return { status: 'error', user: username, message: 'No message input found' };
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// Focus and type into the DraftEditor
|
|
138
|
+
input.focus();
|
|
139
|
+
await new Promise(r => setTimeout(r, 300));
|
|
140
|
+
document.execCommand('insertText', false, messageText);
|
|
141
|
+
await new Promise(r => setTimeout(r, 500));
|
|
142
|
+
|
|
143
|
+
// Click send button
|
|
144
|
+
const sendBtn = document.querySelector('[data-testid="dmComposerSendButton"]');
|
|
145
|
+
if (!sendBtn) {
|
|
146
|
+
return { status: 'error', user: username, message: 'No send button found' };
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
sendBtn.click();
|
|
150
|
+
await new Promise(r => setTimeout(r, 1500));
|
|
151
|
+
|
|
152
|
+
return { status: 'sent', user: username, message: 'Message sent: ' + messageText };
|
|
153
|
+
} catch(e) {
|
|
154
|
+
return { status: 'error', user: 'system', message: String(e) };
|
|
155
|
+
}
|
|
156
|
+
})()`);
|
|
157
|
+
if (sendResult?.status === 'sent') {
|
|
158
|
+
sentCount++;
|
|
159
|
+
results.push({
|
|
160
|
+
index: sentCount,
|
|
161
|
+
status: 'sent',
|
|
162
|
+
user: sendResult.user || conv.user,
|
|
163
|
+
message: sendResult.message,
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
else if (sendResult?.status === 'skipped') {
|
|
167
|
+
results.push({
|
|
168
|
+
index: results.length + 1,
|
|
169
|
+
status: 'skipped',
|
|
170
|
+
user: sendResult.user || conv.user,
|
|
171
|
+
message: sendResult.message,
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
await page.wait(1);
|
|
175
|
+
}
|
|
176
|
+
if (results.length === 0) {
|
|
177
|
+
results.push({ index: 0, status: 'info', user: 'System', message: 'No conversations processed' });
|
|
178
|
+
}
|
|
179
|
+
return results;
|
|
180
|
+
}
|
|
181
|
+
});
|
|
@@ -12,18 +12,35 @@ cli({
|
|
|
12
12
|
],
|
|
13
13
|
columns: ['id', 'author', 'text', 'likes', 'views', 'url'],
|
|
14
14
|
func: async (page, kwargs) => {
|
|
15
|
-
|
|
16
|
-
//
|
|
17
|
-
await page.goto('https://x.com');
|
|
18
|
-
await page.wait(
|
|
15
|
+
const query = kwargs.query;
|
|
16
|
+
// 1. Navigate to x.com/explore (has a search input at the top)
|
|
17
|
+
await page.goto('https://x.com/explore');
|
|
18
|
+
await page.wait(3);
|
|
19
|
+
// 2. Install interceptor BEFORE triggering search.
|
|
20
|
+
// SPA navigation preserves the JS context, so the monkey-patched
|
|
21
|
+
// fetch will capture the SearchTimeline API call.
|
|
19
22
|
await page.installInterceptor('SearchTimeline');
|
|
20
|
-
//
|
|
21
|
-
|
|
22
|
-
|
|
23
|
+
// 3. Trigger SPA navigation to search results via history API.
|
|
24
|
+
// pushState + popstate triggers React Router's listener without
|
|
25
|
+
// a full page reload, so the interceptor stays alive.
|
|
26
|
+
// Note: the previous approach (nativeSetter + Enter keydown on the
|
|
27
|
+
// search input) does not reliably trigger Twitter's form submission.
|
|
28
|
+
const searchUrl = JSON.stringify(`/search?q=${encodeURIComponent(query)}&f=top`);
|
|
29
|
+
await page.evaluate(`
|
|
30
|
+
(() => {
|
|
31
|
+
window.history.pushState({}, '', ${searchUrl});
|
|
32
|
+
window.dispatchEvent(new PopStateEvent('popstate', { state: {} }));
|
|
33
|
+
})()
|
|
34
|
+
`);
|
|
23
35
|
await page.wait(5);
|
|
24
|
-
//
|
|
36
|
+
// Verify SPA navigation succeeded
|
|
37
|
+
const currentPath = await page.evaluate('() => window.location.pathname');
|
|
38
|
+
if (!currentPath?.startsWith('/search')) {
|
|
39
|
+
throw new Error('SPA navigation to /search failed. Twitter may have changed its routing.');
|
|
40
|
+
}
|
|
41
|
+
// 4. Scroll to trigger additional pagination
|
|
25
42
|
await page.autoScroll({ times: 3, delayMs: 2000 });
|
|
26
|
-
//
|
|
43
|
+
// 6. Retrieve captured data
|
|
27
44
|
const requests = await page.getInterceptedRequests();
|
|
28
45
|
if (!requests || requests.length === 0)
|
|
29
46
|
return [];
|
|
@@ -31,7 +48,7 @@ cli({
|
|
|
31
48
|
const seen = new Set();
|
|
32
49
|
for (const req of requests) {
|
|
33
50
|
try {
|
|
34
|
-
const insts = req
|
|
51
|
+
const insts = req?.data?.search_by_raw_query?.search_timeline?.timeline?.instructions || [];
|
|
35
52
|
const addEntries = insts.find((i) => i.type === 'TimelineAddEntries')
|
|
36
53
|
|| insts.find((i) => i.entries && Array.isArray(i.entries));
|
|
37
54
|
if (!addEntries?.entries)
|
|
@@ -49,9 +66,11 @@ cli({
|
|
|
49
66
|
if (!tweet.rest_id || seen.has(tweet.rest_id))
|
|
50
67
|
continue;
|
|
51
68
|
seen.add(tweet.rest_id);
|
|
69
|
+
// Twitter moved screen_name from legacy to core
|
|
70
|
+
const tweetUser = tweet.core?.user_results?.result;
|
|
52
71
|
results.push({
|
|
53
72
|
id: tweet.rest_id,
|
|
54
|
-
author:
|
|
73
|
+
author: tweetUser?.core?.screen_name || tweetUser?.legacy?.screen_name || 'unknown',
|
|
55
74
|
text: tweet.note_tweet?.note_tweet_results?.result?.text || tweet.legacy?.full_text || '',
|
|
56
75
|
likes: tweet.legacy?.favorite_count || 0,
|
|
57
76
|
views: tweet.views?.count || '0',
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { cli, Strategy } from '../../registry.js';
|
|
2
|
+
import { fetchWithPage } from './utils.js';
|
|
3
|
+
cli({
|
|
4
|
+
site: 'weread',
|
|
5
|
+
name: 'book',
|
|
6
|
+
description: 'View book details on WeRead',
|
|
7
|
+
domain: 'weread.qq.com',
|
|
8
|
+
strategy: Strategy.COOKIE,
|
|
9
|
+
args: [
|
|
10
|
+
{ name: 'bookId', positional: true, required: true, help: 'Book ID (numeric, from search or shelf results)' },
|
|
11
|
+
],
|
|
12
|
+
columns: ['title', 'author', 'publisher', 'intro', 'category', 'rating'],
|
|
13
|
+
func: async (page, args) => {
|
|
14
|
+
const data = await fetchWithPage(page, '/book/info', { bookId: args.bookId });
|
|
15
|
+
// newRating is 0-1000 scale per community docs; needs runtime verification
|
|
16
|
+
const rating = data.newRating ? `${(data.newRating / 10).toFixed(1)}%` : '-';
|
|
17
|
+
return [{
|
|
18
|
+
title: data.title ?? '',
|
|
19
|
+
author: data.author ?? '',
|
|
20
|
+
publisher: data.publisher ?? '',
|
|
21
|
+
intro: data.intro ?? '',
|
|
22
|
+
category: data.category ?? '',
|
|
23
|
+
rating,
|
|
24
|
+
}];
|
|
25
|
+
},
|
|
26
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { cli, Strategy } from '../../registry.js';
|
|
2
|
+
import { fetchWithPage, formatDate } from './utils.js';
|
|
3
|
+
cli({
|
|
4
|
+
site: 'weread',
|
|
5
|
+
name: 'highlights',
|
|
6
|
+
description: 'List your highlights (underlines) in a book',
|
|
7
|
+
domain: 'weread.qq.com',
|
|
8
|
+
strategy: Strategy.COOKIE,
|
|
9
|
+
args: [
|
|
10
|
+
{ name: 'bookId', positional: true, required: true, help: 'Book ID (from shelf or search results)' },
|
|
11
|
+
{ name: 'limit', type: 'int', default: 20, help: 'Max results' },
|
|
12
|
+
],
|
|
13
|
+
columns: ['chapter', 'text', 'createTime'],
|
|
14
|
+
func: async (page, args) => {
|
|
15
|
+
const data = await fetchWithPage(page, '/book/bookmarklist', { bookId: args.bookId });
|
|
16
|
+
const items = data?.updated ?? [];
|
|
17
|
+
return items.slice(0, Number(args.limit)).map((item) => ({
|
|
18
|
+
chapter: item.chapterName ?? '',
|
|
19
|
+
text: item.markText ?? '',
|
|
20
|
+
createTime: formatDate(item.createTime),
|
|
21
|
+
}));
|
|
22
|
+
},
|
|
23
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { cli, Strategy } from '../../registry.js';
|
|
2
|
+
import { fetchWithPage } from './utils.js';
|
|
3
|
+
cli({
|
|
4
|
+
site: 'weread',
|
|
5
|
+
name: 'notebooks',
|
|
6
|
+
description: 'List books that have highlights or notes',
|
|
7
|
+
domain: 'weread.qq.com',
|
|
8
|
+
strategy: Strategy.COOKIE,
|
|
9
|
+
columns: ['title', 'author', 'noteCount', 'bookId'],
|
|
10
|
+
func: async (page, _args) => {
|
|
11
|
+
const data = await fetchWithPage(page, '/user/notebooks');
|
|
12
|
+
const books = data?.books ?? [];
|
|
13
|
+
return books.map((item) => ({
|
|
14
|
+
title: item.book?.title ?? '',
|
|
15
|
+
author: item.book?.author ?? '',
|
|
16
|
+
// TODO: bookmarkCount/reviewCount field names from community docs, verify with real API
|
|
17
|
+
noteCount: (item.bookmarkCount ?? 0) + (item.reviewCount ?? 0),
|
|
18
|
+
bookId: item.bookId ?? '',
|
|
19
|
+
}));
|
|
20
|
+
},
|
|
21
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { cli, Strategy } from '../../registry.js';
|
|
2
|
+
import { fetchWithPage, formatDate } from './utils.js';
|
|
3
|
+
cli({
|
|
4
|
+
site: 'weread',
|
|
5
|
+
name: 'notes',
|
|
6
|
+
description: 'List your notes (thoughts) on a book',
|
|
7
|
+
domain: 'weread.qq.com',
|
|
8
|
+
strategy: Strategy.COOKIE,
|
|
9
|
+
args: [
|
|
10
|
+
{ name: 'bookId', positional: true, required: true, help: 'Book ID (from shelf or search results)' },
|
|
11
|
+
{ name: 'limit', type: 'int', default: 20, help: 'Max results' },
|
|
12
|
+
],
|
|
13
|
+
columns: ['chapter', 'text', 'review', 'createTime'],
|
|
14
|
+
func: async (page, args) => {
|
|
15
|
+
const data = await fetchWithPage(page, '/review/list', {
|
|
16
|
+
bookId: args.bookId,
|
|
17
|
+
listType: '11',
|
|
18
|
+
mine: '1',
|
|
19
|
+
synckey: '0',
|
|
20
|
+
});
|
|
21
|
+
const items = data?.reviews ?? [];
|
|
22
|
+
return items.slice(0, Number(args.limit)).map((item) => ({
|
|
23
|
+
chapter: item.review?.chapterName ?? '',
|
|
24
|
+
text: item.review?.abstract ?? '',
|
|
25
|
+
review: item.review?.content ?? '',
|
|
26
|
+
createTime: formatDate(item.review?.createTime),
|
|
27
|
+
}));
|
|
28
|
+
},
|
|
29
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { cli, Strategy } from '../../registry.js';
|
|
2
|
+
import { fetchWebApi } from './utils.js';
|
|
3
|
+
cli({
|
|
4
|
+
site: 'weread',
|
|
5
|
+
name: 'ranking',
|
|
6
|
+
description: 'WeRead book rankings by category',
|
|
7
|
+
domain: 'weread.qq.com',
|
|
8
|
+
strategy: Strategy.PUBLIC,
|
|
9
|
+
browser: false,
|
|
10
|
+
args: [
|
|
11
|
+
{ name: 'category', positional: true, default: 'all', help: 'Category: all (default), rising, or numeric category ID' },
|
|
12
|
+
{ name: 'limit', type: 'int', default: 20, help: 'Max results' },
|
|
13
|
+
],
|
|
14
|
+
columns: ['rank', 'title', 'author', 'category', 'readingCount', 'bookId'],
|
|
15
|
+
func: async (_page, args) => {
|
|
16
|
+
const cat = encodeURIComponent(args.category ?? 'all');
|
|
17
|
+
const data = await fetchWebApi(`/bookListInCategory/${cat}`, { rank: '1' });
|
|
18
|
+
const books = data?.books ?? [];
|
|
19
|
+
return books.slice(0, Number(args.limit)).map((item, i) => ({
|
|
20
|
+
rank: i + 1,
|
|
21
|
+
title: item.bookInfo?.title ?? '',
|
|
22
|
+
author: item.bookInfo?.author ?? '',
|
|
23
|
+
category: item.bookInfo?.category ?? '',
|
|
24
|
+
readingCount: item.readingCount ?? 0,
|
|
25
|
+
bookId: item.bookInfo?.bookId ?? '',
|
|
26
|
+
}));
|
|
27
|
+
},
|
|
28
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { cli, Strategy } from '../../registry.js';
|
|
2
|
+
import { fetchWebApi } from './utils.js';
|
|
3
|
+
cli({
|
|
4
|
+
site: 'weread',
|
|
5
|
+
name: 'search',
|
|
6
|
+
description: 'Search books on WeRead',
|
|
7
|
+
domain: 'weread.qq.com',
|
|
8
|
+
strategy: Strategy.PUBLIC,
|
|
9
|
+
browser: false,
|
|
10
|
+
args: [
|
|
11
|
+
{ name: 'keyword', positional: true, required: true, help: 'Search keyword' },
|
|
12
|
+
{ name: 'limit', type: 'int', default: 10, help: 'Max results' },
|
|
13
|
+
],
|
|
14
|
+
columns: ['rank', 'title', 'author', 'bookId'],
|
|
15
|
+
func: async (_page, args) => {
|
|
16
|
+
const data = await fetchWebApi('/search/global', { keyword: args.keyword });
|
|
17
|
+
const books = data?.books ?? [];
|
|
18
|
+
return books.slice(0, Number(args.limit)).map((item, i) => ({
|
|
19
|
+
rank: i + 1,
|
|
20
|
+
title: item.bookInfo?.title ?? '',
|
|
21
|
+
author: item.bookInfo?.author ?? '',
|
|
22
|
+
bookId: item.bookInfo?.bookId ?? '',
|
|
23
|
+
}));
|
|
24
|
+
},
|
|
25
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { cli, Strategy } from '../../registry.js';
|
|
2
|
+
import { fetchWithPage } from './utils.js';
|
|
3
|
+
cli({
|
|
4
|
+
site: 'weread',
|
|
5
|
+
name: 'shelf',
|
|
6
|
+
description: 'List books on your WeRead bookshelf',
|
|
7
|
+
domain: 'weread.qq.com',
|
|
8
|
+
strategy: Strategy.COOKIE,
|
|
9
|
+
args: [
|
|
10
|
+
{ name: 'limit', type: 'int', default: 20, help: 'Max results' },
|
|
11
|
+
],
|
|
12
|
+
columns: ['title', 'author', 'progress', 'bookId'],
|
|
13
|
+
func: async (page, args) => {
|
|
14
|
+
const data = await fetchWithPage(page, '/shelf/sync', { synckey: '0', lectureSynckey: '0' });
|
|
15
|
+
const books = data?.books ?? [];
|
|
16
|
+
return books.slice(0, Number(args.limit)).map((item) => ({
|
|
17
|
+
title: item.bookInfo?.title ?? item.title ?? '',
|
|
18
|
+
author: item.bookInfo?.author ?? item.author ?? '',
|
|
19
|
+
// TODO: readingProgress field name from community docs, verify with real API response
|
|
20
|
+
progress: item.readingProgress != null ? `${item.readingProgress}%` : '-',
|
|
21
|
+
bookId: item.bookId ?? item.bookInfo?.bookId ?? '',
|
|
22
|
+
}));
|
|
23
|
+
},
|
|
24
|
+
});
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WeRead shared helpers: fetch wrappers and formatting.
|
|
3
|
+
*
|
|
4
|
+
* Two API domains:
|
|
5
|
+
* - WEB_API (weread.qq.com/web/*): public, Node.js fetch
|
|
6
|
+
* - API (i.weread.qq.com/*): private, browser page.evaluate with cookies
|
|
7
|
+
*/
|
|
8
|
+
import type { IPage } from '../../types.js';
|
|
9
|
+
/**
|
|
10
|
+
* Fetch a public WeRead web endpoint (Node.js direct fetch).
|
|
11
|
+
* Used by search and ranking commands (browser: false).
|
|
12
|
+
*/
|
|
13
|
+
export declare function fetchWebApi(path: string, params?: Record<string, string>): Promise<any>;
|
|
14
|
+
/**
|
|
15
|
+
* Fetch a private WeRead API endpoint via browser page.evaluate.
|
|
16
|
+
* Automatically carries cookies for authenticated requests.
|
|
17
|
+
*/
|
|
18
|
+
export declare function fetchWithPage(page: IPage, path: string, params?: Record<string, string>): Promise<any>;
|
|
19
|
+
/** Format a Unix timestamp (seconds) to YYYY-MM-DD in UTC+8. Returns '-' for invalid input. */
|
|
20
|
+
export declare function formatDate(ts: number | undefined | null): string;
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WeRead shared helpers: fetch wrappers and formatting.
|
|
3
|
+
*
|
|
4
|
+
* Two API domains:
|
|
5
|
+
* - WEB_API (weread.qq.com/web/*): public, Node.js fetch
|
|
6
|
+
* - API (i.weread.qq.com/*): private, browser page.evaluate with cookies
|
|
7
|
+
*/
|
|
8
|
+
import { CliError } from '../../errors.js';
|
|
9
|
+
const WEB_API = 'https://weread.qq.com/web';
|
|
10
|
+
const API = 'https://i.weread.qq.com';
|
|
11
|
+
const UA = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36';
|
|
12
|
+
/**
|
|
13
|
+
* Fetch a public WeRead web endpoint (Node.js direct fetch).
|
|
14
|
+
* Used by search and ranking commands (browser: false).
|
|
15
|
+
*/
|
|
16
|
+
export async function fetchWebApi(path, params) {
|
|
17
|
+
const url = new URL(`${WEB_API}${path}`);
|
|
18
|
+
if (params) {
|
|
19
|
+
for (const [k, v] of Object.entries(params))
|
|
20
|
+
url.searchParams.set(k, v);
|
|
21
|
+
}
|
|
22
|
+
const resp = await fetch(url.toString(), {
|
|
23
|
+
headers: { 'User-Agent': UA },
|
|
24
|
+
});
|
|
25
|
+
if (!resp.ok) {
|
|
26
|
+
throw new CliError('FETCH_ERROR', `HTTP ${resp.status} for ${path}`, 'WeRead API may be temporarily unavailable');
|
|
27
|
+
}
|
|
28
|
+
try {
|
|
29
|
+
return await resp.json();
|
|
30
|
+
}
|
|
31
|
+
catch {
|
|
32
|
+
throw new CliError('PARSE_ERROR', `Invalid JSON response for ${path}`, 'WeRead may have returned an HTML error page');
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Fetch a private WeRead API endpoint via browser page.evaluate.
|
|
37
|
+
* Automatically carries cookies for authenticated requests.
|
|
38
|
+
*/
|
|
39
|
+
export async function fetchWithPage(page, path, params) {
|
|
40
|
+
const url = new URL(`${API}${path}`);
|
|
41
|
+
if (params) {
|
|
42
|
+
for (const [k, v] of Object.entries(params))
|
|
43
|
+
url.searchParams.set(k, v);
|
|
44
|
+
}
|
|
45
|
+
const urlStr = url.toString();
|
|
46
|
+
const data = await page.evaluate(`
|
|
47
|
+
async () => {
|
|
48
|
+
const res = await fetch(${JSON.stringify(urlStr)}, { credentials: "include" });
|
|
49
|
+
if (!res.ok) return { _httpError: String(res.status) };
|
|
50
|
+
try { return await res.json(); }
|
|
51
|
+
catch { return { _httpError: 'JSON parse error (status ' + res.status + ')' }; }
|
|
52
|
+
}
|
|
53
|
+
`);
|
|
54
|
+
if (data?._httpError) {
|
|
55
|
+
throw new CliError('FETCH_ERROR', `HTTP ${data._httpError} for ${path}`, 'WeRead API may be temporarily unavailable');
|
|
56
|
+
}
|
|
57
|
+
if (data?.errcode === -2010) {
|
|
58
|
+
throw new CliError('AUTH_REQUIRED', 'Not logged in to WeRead', 'Please log in to weread.qq.com in Chrome first');
|
|
59
|
+
}
|
|
60
|
+
if (data?.errcode != null && data.errcode !== 0) {
|
|
61
|
+
throw new CliError('API_ERROR', data.errmsg ?? `WeRead API error ${data.errcode}`);
|
|
62
|
+
}
|
|
63
|
+
return data;
|
|
64
|
+
}
|
|
65
|
+
/** Format a Unix timestamp (seconds) to YYYY-MM-DD in UTC+8. Returns '-' for invalid input. */
|
|
66
|
+
export function formatDate(ts) {
|
|
67
|
+
if (!Number.isFinite(ts) || ts <= 0)
|
|
68
|
+
return '-';
|
|
69
|
+
// WeRead timestamps are China-centric; offset to UTC+8 to avoid off-by-one near midnight
|
|
70
|
+
const d = new Date(ts * 1000 + 8 * 3600_000);
|
|
71
|
+
return d.toISOString().slice(0, 10);
|
|
72
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
2
|
+
import { formatDate, fetchWebApi, fetchWithPage } from './utils.js';
|
|
3
|
+
describe('formatDate', () => {
|
|
4
|
+
it('formats a typical Unix timestamp in UTC+8', () => {
|
|
5
|
+
// 1705276800 = 2024-01-15 00:00:00 UTC = 2024-01-15 08:00:00 Beijing
|
|
6
|
+
expect(formatDate(1705276800)).toBe('2024-01-15');
|
|
7
|
+
});
|
|
8
|
+
it('handles UTC midnight edge case with UTC+8 offset', () => {
|
|
9
|
+
// 1705190399 = 2024-01-13 23:59:59 UTC = 2024-01-14 07:59:59 Beijing
|
|
10
|
+
expect(formatDate(1705190399)).toBe('2024-01-14');
|
|
11
|
+
});
|
|
12
|
+
it('returns dash for zero', () => {
|
|
13
|
+
expect(formatDate(0)).toBe('-');
|
|
14
|
+
});
|
|
15
|
+
it('returns dash for negative', () => {
|
|
16
|
+
expect(formatDate(-1)).toBe('-');
|
|
17
|
+
});
|
|
18
|
+
it('returns dash for NaN', () => {
|
|
19
|
+
expect(formatDate(NaN)).toBe('-');
|
|
20
|
+
});
|
|
21
|
+
it('returns dash for Infinity', () => {
|
|
22
|
+
expect(formatDate(Infinity)).toBe('-');
|
|
23
|
+
});
|
|
24
|
+
it('returns dash for undefined', () => {
|
|
25
|
+
expect(formatDate(undefined)).toBe('-');
|
|
26
|
+
});
|
|
27
|
+
it('returns dash for null', () => {
|
|
28
|
+
expect(formatDate(null)).toBe('-');
|
|
29
|
+
});
|
|
30
|
+
});
|
|
31
|
+
describe('fetchWebApi', () => {
|
|
32
|
+
beforeEach(() => {
|
|
33
|
+
vi.restoreAllMocks();
|
|
34
|
+
});
|
|
35
|
+
it('returns parsed JSON for successful response', async () => {
|
|
36
|
+
vi.stubGlobal('fetch', vi.fn().mockResolvedValue({
|
|
37
|
+
ok: true,
|
|
38
|
+
json: () => Promise.resolve({ books: [{ title: 'Test' }] }),
|
|
39
|
+
}));
|
|
40
|
+
const result = await fetchWebApi('/search/global', { keyword: 'test' });
|
|
41
|
+
expect(result).toEqual({ books: [{ title: 'Test' }] });
|
|
42
|
+
});
|
|
43
|
+
it('throws CliError on HTTP error', async () => {
|
|
44
|
+
vi.stubGlobal('fetch', vi.fn().mockResolvedValue({
|
|
45
|
+
ok: false,
|
|
46
|
+
status: 403,
|
|
47
|
+
json: () => Promise.resolve({}),
|
|
48
|
+
}));
|
|
49
|
+
await expect(fetchWebApi('/search/global')).rejects.toThrow('HTTP 403');
|
|
50
|
+
});
|
|
51
|
+
it('throws PARSE_ERROR on non-JSON response', async () => {
|
|
52
|
+
vi.stubGlobal('fetch', vi.fn().mockResolvedValue({
|
|
53
|
+
ok: true,
|
|
54
|
+
json: () => Promise.reject(new SyntaxError('Unexpected token <')),
|
|
55
|
+
}));
|
|
56
|
+
await expect(fetchWebApi('/search/global')).rejects.toThrow('Invalid JSON');
|
|
57
|
+
});
|
|
58
|
+
});
|
|
59
|
+
describe('fetchWithPage', () => {
|
|
60
|
+
it('throws AUTH_REQUIRED on errcode -2010', async () => {
|
|
61
|
+
const mockPage = {
|
|
62
|
+
evaluate: vi.fn().mockResolvedValue({ errcode: -2010, errmsg: '用户不存在' }),
|
|
63
|
+
};
|
|
64
|
+
await expect(fetchWithPage(mockPage, '/book/info')).rejects.toThrow('Not logged in');
|
|
65
|
+
});
|
|
66
|
+
it('throws API_ERROR on unknown errcode', async () => {
|
|
67
|
+
const mockPage = {
|
|
68
|
+
evaluate: vi.fn().mockResolvedValue({ errcode: -1, errmsg: 'unknown error' }),
|
|
69
|
+
};
|
|
70
|
+
await expect(fetchWithPage(mockPage, '/book/info')).rejects.toThrow('unknown error');
|
|
71
|
+
});
|
|
72
|
+
it('returns data on success (errcode 0 or absent)', async () => {
|
|
73
|
+
const mockPage = {
|
|
74
|
+
evaluate: vi.fn().mockResolvedValue({ title: 'Test Book', errcode: 0 }),
|
|
75
|
+
};
|
|
76
|
+
const result = await fetchWithPage(mockPage, '/book/info');
|
|
77
|
+
expect(result.title).toBe('Test Book');
|
|
78
|
+
});
|
|
79
|
+
it('throws FETCH_ERROR on HTTP error', async () => {
|
|
80
|
+
const mockPage = {
|
|
81
|
+
evaluate: vi.fn().mockResolvedValue({ _httpError: '403' }),
|
|
82
|
+
};
|
|
83
|
+
await expect(fetchWithPage(mockPage, '/book/info')).rejects.toThrow('HTTP 403');
|
|
84
|
+
});
|
|
85
|
+
});
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* Xiaohongshu download — download images and videos from a note.
|
|
3
3
|
*
|
|
4
4
|
* Usage:
|
|
5
|
-
* opencli xiaohongshu download --
|
|
5
|
+
* opencli xiaohongshu download --note_id abc123 --output ./xhs
|
|
6
6
|
*/
|
|
7
7
|
import * as fs from 'node:fs';
|
|
8
8
|
import * as path from 'node:path';
|