@jackwener/opencli 1.8.0 → 1.8.1
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/README.md +8 -49
- package/README.zh-CN.md +8 -52
- package/cli-manifest.json +1796 -191
- package/clis/_atlassian/shared.js +577 -0
- package/clis/_atlassian/shared.test.js +170 -0
- package/clis/bilibili/comment.js +125 -0
- package/clis/bilibili/comment.test.js +153 -0
- package/clis/bilibili/comments.js +116 -21
- package/clis/bilibili/comments.test.js +77 -18
- package/clis/bilibili/subtitle.js +76 -31
- package/clis/bilibili/subtitle.test.js +156 -9
- package/clis/bilibili/utils.js +63 -5
- package/clis/bilibili/utils.test.js +45 -1
- package/clis/chess/analyze.js +35 -0
- package/clis/chess/analyze.test.js +79 -0
- package/clis/chess/game.js +114 -0
- package/clis/chess/game.test.js +178 -0
- package/clis/chess/games.js +67 -0
- package/clis/chess/games.test.js +164 -0
- package/clis/chess/stats.js +32 -0
- package/clis/chess/stats.test.js +79 -0
- package/clis/chess/utils.js +170 -0
- package/clis/chess/utils.test.js +230 -0
- package/clis/confluence/commands.test.js +195 -0
- package/clis/confluence/create.js +39 -0
- package/clis/confluence/page.js +23 -0
- package/clis/confluence/search.js +34 -0
- package/clis/confluence/shared.js +173 -0
- package/clis/confluence/update.js +38 -0
- package/clis/douyin/hashtag.js +84 -23
- package/clis/douyin/hashtag.test.js +113 -0
- package/clis/geogebra/add-circle.js +46 -0
- package/clis/geogebra/add-line.js +35 -0
- package/clis/geogebra/add-point.js +27 -0
- package/clis/geogebra/add-polygon.js +25 -0
- package/clis/geogebra/eval.js +35 -0
- package/clis/geogebra/geogebra.test.js +175 -0
- package/clis/geogebra/hexagon.js +62 -0
- package/clis/geogebra/info.js +72 -0
- package/clis/geogebra/list.js +35 -0
- package/clis/geogebra/triangle.js +60 -0
- package/clis/geogebra/utils.js +271 -0
- package/clis/jira/attachments.js +28 -0
- package/clis/jira/commands.test.js +287 -0
- package/clis/jira/comments.js +28 -0
- package/clis/jira/issue.js +28 -0
- package/clis/jira/links.js +28 -0
- package/clis/jira/search.js +47 -0
- package/clis/jira/shared.js +256 -0
- package/clis/linkedin/job-detail.js +167 -0
- package/clis/linkedin/job-detail.test.js +38 -0
- package/clis/linkedin/jobs-preferences.js +113 -0
- package/clis/linkedin/jobs-preferences.test.js +43 -0
- package/clis/linkedin/post-analytics.js +74 -0
- package/clis/linkedin/post-analytics.test.js +40 -0
- package/clis/linkedin/posts-core.js +241 -0
- package/clis/linkedin/posts.js +22 -0
- package/clis/linkedin/posts.test.js +40 -0
- package/clis/linkedin/profile-analytics.js +104 -0
- package/clis/linkedin/profile-analytics.test.js +67 -0
- package/clis/linkedin/profile-experience.js +671 -0
- package/clis/linkedin/profile-experience.test.js +152 -0
- package/clis/linkedin/profile-projects.js +311 -0
- package/clis/linkedin/profile-projects.test.js +111 -0
- package/clis/linkedin/profile-read.js +148 -0
- package/clis/linkedin/profile-read.test.js +77 -0
- package/clis/linkedin/services-read.js +213 -0
- package/clis/linkedin/services-read.test.js +105 -0
- package/clis/linkedin/shared.js +124 -0
- package/clis/linkedin/timeline.js +14 -7
- package/clis/notebooklm/add-source.js +269 -0
- package/clis/notebooklm/add-source.test.js +97 -0
- package/clis/notebooklm/create.js +76 -0
- package/clis/notebooklm/create.test.js +58 -0
- package/clis/notebooklm/generate-audio.js +91 -0
- package/clis/notebooklm/generate-audio.test.js +63 -0
- package/clis/notebooklm/generate-slides.js +106 -0
- package/clis/notebooklm/generate-slides.test.js +75 -0
- package/clis/notebooklm/open.test.js +10 -10
- package/clis/notebooklm/rpc.js +20 -6
- package/clis/notebooklm/rpc.test.js +27 -1
- package/clis/notebooklm/utils.js +100 -24
- package/clis/notebooklm/utils.test.js +60 -1
- package/clis/notebooklm/write-note.js +103 -0
- package/clis/notebooklm/write-note.test.js +70 -0
- package/clis/pixiv/detail.js +41 -34
- package/clis/pixiv/detail.test.js +93 -0
- package/clis/pixiv/user.js +36 -31
- package/clis/pixiv/user.test.js +100 -0
- package/clis/pixiv/utils.js +56 -7
- package/clis/suno/generate.js +5 -0
- package/clis/suno/generate.test.js +9 -0
- package/clis/suno/status.js +3 -2
- package/clis/suno/utils.js +33 -24
- package/clis/suno/utils.test.js +106 -0
- package/clis/twitter/followers.js +6 -2
- package/clis/twitter/followers.test.js +19 -1
- package/clis/twitter/following.js +14 -5
- package/clis/twitter/following.test.js +29 -0
- package/clis/twitter/likes.js +12 -4
- package/clis/twitter/likes.test.js +26 -1
- package/clis/twitter/list-add.js +1 -1
- package/clis/twitter/list-remove.js +1 -1
- package/clis/twitter/notifications.js +4 -4
- package/clis/twitter/post.js +62 -4
- package/clis/twitter/post.test.js +35 -3
- package/clis/twitter/profile.js +81 -28
- package/clis/twitter/profile.test.js +113 -2
- package/clis/twitter/quote.js +9 -4
- package/clis/twitter/reply.js +13 -10
- package/clis/twitter/reply.test.js +41 -0
- package/clis/twitter/search.js +1 -1
- package/clis/twitter/search.test.js +35 -0
- package/clis/twitter/shared.js +11 -0
- package/clis/twitter/shared.test.js +37 -1
- package/clis/twitter/utils.js +53 -16
- package/clis/upwork/detail.js +132 -0
- package/clis/upwork/feed.js +109 -0
- package/clis/upwork/search.js +115 -0
- package/clis/upwork/upwork.test.js +566 -0
- package/clis/upwork/utils.js +323 -0
- package/clis/weread/book-search.js +438 -0
- package/clis/weread/book-search.test.js +242 -0
- package/clis/weread/search-regression.test.js +80 -0
- package/clis/weread/search.js +17 -2
- package/clis/xiaohongshu/creator-note-detail.js +165 -28
- package/clis/xiaohongshu/creator-note-detail.test.js +186 -37
- package/clis/xiaohongshu/creator-notes.js +251 -2
- package/clis/xiaohongshu/creator-notes.test.js +79 -2
- package/clis/xiaohongshu/download.js +97 -39
- package/clis/xiaohongshu/download.test.js +201 -0
- package/clis/zhihu/answer-comments.js +2 -21
- package/clis/zhihu/answer-detail.js +2 -31
- package/clis/zhihu/collection.js +2 -14
- package/clis/zhihu/collection.test.js +4 -3
- package/clis/zhihu/question.js +1 -9
- package/clis/zhihu/question.test.js +2 -2
- package/clis/zhihu/search.js +1 -12
- package/clis/zhihu/search.test.js +2 -2
- package/clis/zhihu/text.js +29 -0
- package/clis/zhihu/text.test.js +24 -0
- package/dist/src/browser/network-cache.js +13 -1
- package/dist/src/browser/network-cache.test.js +17 -0
- package/dist/src/download/index.js +13 -1
- package/dist/src/download/index.test.js +23 -1
- package/dist/src/download/media-download.test.js +3 -1
- package/dist/src/download/progress.js +2 -2
- package/dist/src/download/progress.test.js +12 -1
- package/dist/src/output.js +11 -1
- package/dist/src/output.test.js +6 -0
- package/dist/src/registry.js +1 -0
- package/dist/src/registry.test.js +11 -0
- package/package.json +1 -1
|
@@ -0,0 +1,438 @@
|
|
|
1
|
+
import { cli, Strategy } from '@jackwener/opencli/registry';
|
|
2
|
+
import { ArgumentError, CommandExecutionError, EmptyResultError } from '@jackwener/opencli/errors';
|
|
3
|
+
import { WEREAD_UA, WEREAD_WEB_ORIGIN } from './utils.js';
|
|
4
|
+
|
|
5
|
+
const MAX_LIMIT = 100;
|
|
6
|
+
const MAX_FRAGMENT_SIZE = 500;
|
|
7
|
+
const SEARCH_PAGE_SIZE = 50;
|
|
8
|
+
|
|
9
|
+
function decodeHtmlText(value) {
|
|
10
|
+
return String(value || '')
|
|
11
|
+
.replace(/<[^>]+>/g, '')
|
|
12
|
+
.replace(/&#x([0-9a-fA-F]+);/gi, (_, n) => String.fromCharCode(parseInt(n, 16)))
|
|
13
|
+
.replace(/&#(\d+);/g, (_, n) => String.fromCharCode(Number(n)))
|
|
14
|
+
.replace(/ /g, ' ')
|
|
15
|
+
.replace(/&/g, '&')
|
|
16
|
+
.replace(/"/g, '"')
|
|
17
|
+
.trim();
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function normalizeSearchText(value) {
|
|
21
|
+
return String(value || '').replace(/\s+/g, ' ').trim();
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function normalizePositiveInteger(value, defaultValue, label, maxValue) {
|
|
25
|
+
const raw = value ?? defaultValue;
|
|
26
|
+
const n = Number(raw);
|
|
27
|
+
if (!Number.isInteger(n) || n <= 0) {
|
|
28
|
+
throw new ArgumentError(`${label} must be a positive integer`);
|
|
29
|
+
}
|
|
30
|
+
if (maxValue != null && n > maxValue) {
|
|
31
|
+
throw new ArgumentError(`${label} must be <= ${maxValue}`);
|
|
32
|
+
}
|
|
33
|
+
return n;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function parseOptionalFiniteNumber(value) {
|
|
37
|
+
if (value == null || value === '')
|
|
38
|
+
return null;
|
|
39
|
+
const n = Number(value);
|
|
40
|
+
return Number.isFinite(n) ? n : null;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function parseHasMore(value) {
|
|
44
|
+
if (value === true || value === 1 || value === '1')
|
|
45
|
+
return true;
|
|
46
|
+
if (value === false || value === 0 || value === '0')
|
|
47
|
+
return false;
|
|
48
|
+
return null;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function normalizeRequiredString(value, label) {
|
|
52
|
+
const text = normalizeSearchText(value);
|
|
53
|
+
if (!text) {
|
|
54
|
+
throw new ArgumentError(`${label} is required`);
|
|
55
|
+
}
|
|
56
|
+
return text;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function parseWereadReaderUrl(value) {
|
|
60
|
+
let url;
|
|
61
|
+
try {
|
|
62
|
+
url = new URL(String(value || ''), WEREAD_WEB_ORIGIN);
|
|
63
|
+
}
|
|
64
|
+
catch {
|
|
65
|
+
return '';
|
|
66
|
+
}
|
|
67
|
+
const pathParts = url.pathname.split('/').filter(Boolean);
|
|
68
|
+
if (url.protocol !== 'https:' || url.hostname !== 'weread.qq.com' || pathParts[0] !== 'web' || pathParts[1] !== 'reader' || !pathParts[2]) {
|
|
69
|
+
return '';
|
|
70
|
+
}
|
|
71
|
+
if (pathParts.length !== 3) {
|
|
72
|
+
return '';
|
|
73
|
+
}
|
|
74
|
+
return url.toString();
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
async function fetchJson(url, label) {
|
|
78
|
+
let resp;
|
|
79
|
+
try {
|
|
80
|
+
resp = await fetch(url.toString(), {
|
|
81
|
+
headers: { 'User-Agent': WEREAD_UA },
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
catch (error) {
|
|
85
|
+
throw new CommandExecutionError(`${label} request failed: ${error instanceof Error ? error.message : String(error)}`);
|
|
86
|
+
}
|
|
87
|
+
if (!resp.ok) {
|
|
88
|
+
throw new CommandExecutionError(`${label} request failed: HTTP ${resp.status}`);
|
|
89
|
+
}
|
|
90
|
+
try {
|
|
91
|
+
return await resp.json();
|
|
92
|
+
}
|
|
93
|
+
catch {
|
|
94
|
+
throw new CommandExecutionError(`${label} returned invalid JSON`);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
async function fetchText(url, label) {
|
|
99
|
+
let resp;
|
|
100
|
+
try {
|
|
101
|
+
resp = await fetch(url.toString(), {
|
|
102
|
+
headers: { 'User-Agent': WEREAD_UA },
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
catch (error) {
|
|
106
|
+
throw new CommandExecutionError(`${label} request failed: ${error instanceof Error ? error.message : String(error)}`);
|
|
107
|
+
}
|
|
108
|
+
if (!resp.ok) {
|
|
109
|
+
throw new CommandExecutionError(`${label} request failed: HTTP ${resp.status}`);
|
|
110
|
+
}
|
|
111
|
+
return resp.text();
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function buildReaderUrlFromInfoId(infoId) {
|
|
115
|
+
const text = normalizeSearchText(infoId);
|
|
116
|
+
return text ? `${WEREAD_WEB_ORIGIN}/web/reader/${text}` : '';
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
function extractReaderInitialState(html) {
|
|
120
|
+
const marker = 'window.__INITIAL_STATE__=';
|
|
121
|
+
const start = html.indexOf(marker);
|
|
122
|
+
if (start < 0)
|
|
123
|
+
return null;
|
|
124
|
+
const jsonStart = start + marker.length;
|
|
125
|
+
const cleanupStart = html.indexOf(';(function(){var s;', jsonStart);
|
|
126
|
+
const scriptEnd = html.indexOf('</script>', jsonStart);
|
|
127
|
+
const jsonEnd = cleanupStart >= 0 ? cleanupStart : scriptEnd;
|
|
128
|
+
if (jsonEnd < 0)
|
|
129
|
+
return null;
|
|
130
|
+
try {
|
|
131
|
+
return JSON.parse(html.slice(jsonStart, jsonEnd));
|
|
132
|
+
}
|
|
133
|
+
catch {
|
|
134
|
+
return null;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
function extractJsonLdBookInfo(html) {
|
|
139
|
+
const match = html.match(/<script[^>]*type=["']application\/ld\+json["'][^>]*>([\s\S]*?)<\/script>/i);
|
|
140
|
+
if (!match)
|
|
141
|
+
return {};
|
|
142
|
+
try {
|
|
143
|
+
const data = JSON.parse(match[1]);
|
|
144
|
+
const info = {};
|
|
145
|
+
info.bookId = normalizeSearchText(data?.['@Id']);
|
|
146
|
+
info.title = normalizeSearchText(data?.name);
|
|
147
|
+
info.author = normalizeSearchText(data?.author?.name);
|
|
148
|
+
info.readerUrl = normalizeSearchText(data?.url);
|
|
149
|
+
return info;
|
|
150
|
+
}
|
|
151
|
+
catch {
|
|
152
|
+
return {};
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
function parseReaderMetadata(html, readerUrl) {
|
|
157
|
+
const state = extractReaderInitialState(html);
|
|
158
|
+
const reader = state?.reader ?? {};
|
|
159
|
+
const info = reader.bookInfo ?? {};
|
|
160
|
+
const jsonLd = extractJsonLdBookInfo(html);
|
|
161
|
+
const chapters = Array.isArray(reader.chapterInfos) ? reader.chapterInfos : [];
|
|
162
|
+
const infoId = normalizeSearchText(reader.infoId) || normalizeSearchText(info.encodeId);
|
|
163
|
+
const metadata = {};
|
|
164
|
+
metadata.bookId = normalizeSearchText(info.bookId) || normalizeSearchText(reader.bookId) || jsonLd.bookId;
|
|
165
|
+
metadata.title = normalizeSearchText(info.title) || jsonLd.title;
|
|
166
|
+
metadata.author = normalizeSearchText(info.author) || jsonLd.author;
|
|
167
|
+
metadata.readerUrl = normalizeSearchText(readerUrl) || buildReaderUrlFromInfoId(infoId) || jsonLd.readerUrl;
|
|
168
|
+
metadata.chapters = chapters;
|
|
169
|
+
return metadata;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
async function loadReaderMetadata(readerUrl) {
|
|
173
|
+
if (!readerUrl)
|
|
174
|
+
return null;
|
|
175
|
+
const html = await fetchText(readerUrl, 'WeRead reader page');
|
|
176
|
+
const metadata = parseReaderMetadata(html, readerUrl);
|
|
177
|
+
return metadata.bookId ? metadata : null;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
function parseSearchHtmlEntries(html) {
|
|
181
|
+
const items = Array.from(html.matchAll(/<li[^>]*class="wr_bookList_item"[^>]*>([\s\S]*?)<\/li>/g));
|
|
182
|
+
return items.map((match) => {
|
|
183
|
+
const chunk = match[1];
|
|
184
|
+
const hrefMatch = chunk.match(/<a[^>]*href="([^"]+)"[^>]*class="wr_bookList_item_link"[^>]*>|<a[^>]*class="wr_bookList_item_link"[^>]*href="([^"]+)"[^>]*>/);
|
|
185
|
+
const titleMatch = chunk.match(/<p[^>]*class="wr_bookList_item_title"[^>]*>([\s\S]*?)<\/p>/);
|
|
186
|
+
const authorMatch = chunk.match(/<p[^>]*class="wr_bookList_item_author"[^>]*>([\s\S]*?)<\/p>/);
|
|
187
|
+
const href = hrefMatch?.[1] || hrefMatch?.[2] || '';
|
|
188
|
+
const entry = {};
|
|
189
|
+
entry.title = decodeHtmlText(titleMatch?.[1] || '');
|
|
190
|
+
entry.author = decodeHtmlText(authorMatch?.[1] || '');
|
|
191
|
+
entry.readerUrl = href ? parseWereadReaderUrl(href) : '';
|
|
192
|
+
return entry;
|
|
193
|
+
}).filter((entry) => entry.title && entry.readerUrl);
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
async function loadSearchHtmlEntries(bookQuery) {
|
|
197
|
+
const url = new URL('/web/search/books', WEREAD_WEB_ORIGIN);
|
|
198
|
+
url.searchParams.set('keyword', bookQuery);
|
|
199
|
+
return parseSearchHtmlEntries(await fetchText(url, 'WeRead search page'));
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
function resolveReaderUrlForBook(book, htmlEntries) {
|
|
203
|
+
const title = normalizeSearchText(book.title);
|
|
204
|
+
const author = normalizeSearchText(book.author);
|
|
205
|
+
if (!title)
|
|
206
|
+
return '';
|
|
207
|
+
if (author) {
|
|
208
|
+
const exact = htmlEntries.filter((entry) => normalizeSearchText(entry.title) === title && normalizeSearchText(entry.author) === author);
|
|
209
|
+
if (exact.length === 1)
|
|
210
|
+
return exact[0].readerUrl;
|
|
211
|
+
}
|
|
212
|
+
const sameTitle = htmlEntries.filter((entry) => normalizeSearchText(entry.title) === title);
|
|
213
|
+
return sameTitle.length === 1 ? sameTitle[0].readerUrl : '';
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
async function searchBookByQuery(bookQuery, bookRank) {
|
|
217
|
+
const url = new URL('/web/search/global', `${WEREAD_WEB_ORIGIN}/web`);
|
|
218
|
+
url.searchParams.set('keyword', bookQuery);
|
|
219
|
+
const data = await fetchJson(url, 'WeRead book search');
|
|
220
|
+
if (!Array.isArray(data?.books)) {
|
|
221
|
+
throw new CommandExecutionError('WeRead book search returned malformed books');
|
|
222
|
+
}
|
|
223
|
+
const books = data.books;
|
|
224
|
+
if (books.length === 0) {
|
|
225
|
+
throw new EmptyResultError('weread book-search', `No WeRead books found for "${bookQuery}"`);
|
|
226
|
+
}
|
|
227
|
+
if (bookRank > books.length) {
|
|
228
|
+
throw new ArgumentError(`book-rank must be <= ${books.length}`, `Only ${books.length} book search result(s) were returned for "${bookQuery}"`);
|
|
229
|
+
}
|
|
230
|
+
const bookInfo = books[bookRank - 1]?.bookInfo ?? {};
|
|
231
|
+
const selected = {
|
|
232
|
+
bookId: normalizeSearchText(bookInfo.bookId),
|
|
233
|
+
title: normalizeSearchText(bookInfo.title),
|
|
234
|
+
author: normalizeSearchText(bookInfo.author),
|
|
235
|
+
readerUrl: '',
|
|
236
|
+
chapters: [],
|
|
237
|
+
};
|
|
238
|
+
if (!selected.bookId) {
|
|
239
|
+
throw new CommandExecutionError(`WeRead book search result ${bookRank} is missing bookId`);
|
|
240
|
+
}
|
|
241
|
+
const htmlEntries = await loadSearchHtmlEntries(bookQuery);
|
|
242
|
+
selected.readerUrl = resolveReaderUrlForBook(selected, htmlEntries);
|
|
243
|
+
const readerMetadata = await loadReaderMetadata(selected.readerUrl);
|
|
244
|
+
return {
|
|
245
|
+
...selected,
|
|
246
|
+
...Object.fromEntries(Object.entries(readerMetadata ?? {}).filter(([, value]) => value != null && value !== '' && !(Array.isArray(value) && value.length === 0))),
|
|
247
|
+
};
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
async function resolveBookTarget(target, bookRank) {
|
|
251
|
+
if (/^https?:\/\//i.test(target)) {
|
|
252
|
+
const readerUrl = parseWereadReaderUrl(target);
|
|
253
|
+
if (!readerUrl) {
|
|
254
|
+
throw new ArgumentError('book URL must be a https://weread.qq.com/web/reader/<id> URL');
|
|
255
|
+
}
|
|
256
|
+
const metadata = await loadReaderMetadata(readerUrl);
|
|
257
|
+
if (!metadata?.bookId) {
|
|
258
|
+
throw new CommandExecutionError('Could not parse a bookId from the reader URL');
|
|
259
|
+
}
|
|
260
|
+
return metadata;
|
|
261
|
+
}
|
|
262
|
+
if (/^\d+$/.test(target)) {
|
|
263
|
+
const metadata = {};
|
|
264
|
+
metadata.bookId = target;
|
|
265
|
+
metadata.title = '';
|
|
266
|
+
metadata.author = '';
|
|
267
|
+
metadata.readerUrl = '';
|
|
268
|
+
metadata.chapters = [];
|
|
269
|
+
return metadata;
|
|
270
|
+
}
|
|
271
|
+
return searchBookByQuery(target, bookRank);
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
async function searchWithinBook(bookId, query, limit, fragmentSize) {
|
|
275
|
+
const rows = [];
|
|
276
|
+
let maxIdx = 0;
|
|
277
|
+
while (rows.length < limit) {
|
|
278
|
+
const remaining = limit - rows.length;
|
|
279
|
+
const pageSize = remaining < SEARCH_PAGE_SIZE ? remaining : SEARCH_PAGE_SIZE;
|
|
280
|
+
const url = new URL('/web/book/search', WEREAD_WEB_ORIGIN);
|
|
281
|
+
url.searchParams.set('bookId', bookId);
|
|
282
|
+
url.searchParams.set('keyword', query);
|
|
283
|
+
url.searchParams.set('maxIdx', String(maxIdx));
|
|
284
|
+
url.searchParams.set('count', String(pageSize));
|
|
285
|
+
url.searchParams.set('fragmentSize', String(fragmentSize));
|
|
286
|
+
url.searchParams.set('onlyCount', '0');
|
|
287
|
+
const data = await fetchJson(url, 'WeRead in-book search');
|
|
288
|
+
if (!Array.isArray(data?.result)) {
|
|
289
|
+
throw new CommandExecutionError('WeRead in-book search returned malformed result');
|
|
290
|
+
}
|
|
291
|
+
const result = data.result.map((item) => {
|
|
292
|
+
if (!item || typeof item !== 'object') {
|
|
293
|
+
throw new CommandExecutionError('WeRead in-book search returned malformed match');
|
|
294
|
+
}
|
|
295
|
+
const snippet = normalizeSearchText(item.abstract);
|
|
296
|
+
const searchIdx = parseOptionalFiniteNumber(item.searchIdx);
|
|
297
|
+
if (!snippet || searchIdx == null || searchIdx <= 0) {
|
|
298
|
+
throw new CommandExecutionError('WeRead in-book search returned malformed match');
|
|
299
|
+
}
|
|
300
|
+
return {
|
|
301
|
+
...item,
|
|
302
|
+
abstract: snippet,
|
|
303
|
+
chapterIdx: parseOptionalFiniteNumber(item.chapterIdx),
|
|
304
|
+
chapterUid: parseOptionalFiniteNumber(item.chapterUid),
|
|
305
|
+
searchIdx,
|
|
306
|
+
};
|
|
307
|
+
});
|
|
308
|
+
if (result.length === 0)
|
|
309
|
+
break;
|
|
310
|
+
rows.push(...result);
|
|
311
|
+
const lastSearchIdx = result[result.length - 1].searchIdx;
|
|
312
|
+
if (lastSearchIdx <= maxIdx)
|
|
313
|
+
throw new CommandExecutionError('WeRead in-book search returned non-advancing searchIdx');
|
|
314
|
+
maxIdx = lastSearchIdx;
|
|
315
|
+
if (rows.length >= limit)
|
|
316
|
+
break;
|
|
317
|
+
const hasMore = parseHasMore(data?.hasMore);
|
|
318
|
+
if (hasMore == null) {
|
|
319
|
+
if (result.length < pageSize)
|
|
320
|
+
break;
|
|
321
|
+
throw new CommandExecutionError('WeRead in-book search returned malformed pagination state');
|
|
322
|
+
}
|
|
323
|
+
if (!hasMore)
|
|
324
|
+
break;
|
|
325
|
+
}
|
|
326
|
+
if (rows.length === 0) {
|
|
327
|
+
throw new EmptyResultError('weread book-search', `No matches for "${query}" in book ${bookId}`);
|
|
328
|
+
}
|
|
329
|
+
return rows.slice(0, limit);
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
function buildChapterMap(chapters) {
|
|
333
|
+
const map = new Map();
|
|
334
|
+
for (const chapter of chapters) {
|
|
335
|
+
const chapterUid = parseOptionalFiniteNumber(chapter?.chapterUid);
|
|
336
|
+
if (chapterUid == null)
|
|
337
|
+
continue;
|
|
338
|
+
map.set(chapterUid, {
|
|
339
|
+
chapterIdx: parseOptionalFiniteNumber(chapter?.chapterIdx),
|
|
340
|
+
chapterTitle: normalizeSearchText(chapter?.title),
|
|
341
|
+
});
|
|
342
|
+
}
|
|
343
|
+
return map;
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
function buildRows(book, matches) {
|
|
347
|
+
const chapterMap = buildChapterMap(book.chapters ?? []);
|
|
348
|
+
return matches.map((item, index) => {
|
|
349
|
+
const chapterUid = parseOptionalFiniteNumber(item?.chapterUid);
|
|
350
|
+
const chapter = chapterMap.get(chapterUid) ?? {};
|
|
351
|
+
const chapterIdx = chapter.chapterIdx ?? parseOptionalFiniteNumber(item?.chapterIdx);
|
|
352
|
+
return {
|
|
353
|
+
rank: index + 1,
|
|
354
|
+
book_title: book.title || null,
|
|
355
|
+
author: book.author || null,
|
|
356
|
+
chapter_idx: chapterIdx,
|
|
357
|
+
chapter_title: chapter.chapterTitle || null,
|
|
358
|
+
snippet: normalizeSearchText(item?.abstract),
|
|
359
|
+
search_idx: item.searchIdx,
|
|
360
|
+
chapter_uid: chapterUid,
|
|
361
|
+
book_id: book.bookId,
|
|
362
|
+
url: book.readerUrl || null,
|
|
363
|
+
};
|
|
364
|
+
});
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
function formatMarkdownResults(book, query, rows) {
|
|
368
|
+
const title = book.title || `WeRead book ${book.bookId}`;
|
|
369
|
+
const lines = [`# ${title}`];
|
|
370
|
+
if (book.author)
|
|
371
|
+
lines.push(`- author: ${book.author}`);
|
|
372
|
+
lines.push(`- book_id: \`${book.bookId}\``);
|
|
373
|
+
lines.push(`- query: \`${query}\``);
|
|
374
|
+
lines.push(`- matches: ${rows.length}`);
|
|
375
|
+
if (book.readerUrl)
|
|
376
|
+
lines.push(`- url: ${book.readerUrl}`);
|
|
377
|
+
lines.push('');
|
|
378
|
+
for (const row of rows) {
|
|
379
|
+
const chapterLabel = row.chapter_title || `chapter ${row.chapter_uid ?? ''}`.trim();
|
|
380
|
+
lines.push(`## ${row.rank}. ${chapterLabel}`);
|
|
381
|
+
const details = [];
|
|
382
|
+
if (row.chapter_idx !== null)
|
|
383
|
+
details.push(`chapter_idx: ${row.chapter_idx}`);
|
|
384
|
+
if (row.chapter_uid !== null)
|
|
385
|
+
details.push(`chapter_uid: ${row.chapter_uid}`);
|
|
386
|
+
details.push(`search_idx: ${row.search_idx}`);
|
|
387
|
+
lines.push('');
|
|
388
|
+
lines.push(`> ${row.snippet}`);
|
|
389
|
+
lines.push('');
|
|
390
|
+
for (const detail of details) {
|
|
391
|
+
lines.push(`- ${detail}`);
|
|
392
|
+
}
|
|
393
|
+
if (row.rank < rows.length)
|
|
394
|
+
lines.push('');
|
|
395
|
+
}
|
|
396
|
+
return lines.join('\n');
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
cli({
|
|
400
|
+
site: 'weread',
|
|
401
|
+
name: 'book-search',
|
|
402
|
+
access: 'read',
|
|
403
|
+
description: 'Search within a WeRead book after resolving it by title',
|
|
404
|
+
domain: 'weread.qq.com',
|
|
405
|
+
strategy: Strategy.PUBLIC,
|
|
406
|
+
browser: false,
|
|
407
|
+
defaultFormat: 'md',
|
|
408
|
+
args: [
|
|
409
|
+
{ name: 'book', positional: true, required: true, help: 'Book title keyword, numeric bookId, or reader URL' },
|
|
410
|
+
{ name: 'query', positional: true, required: true, help: 'Keyword to search inside the selected book' },
|
|
411
|
+
{ name: 'book-rank', type: 'int', default: 1, help: 'Which book search result to use when book is a title keyword' },
|
|
412
|
+
{ name: 'limit', type: 'int', default: 20, help: 'Max in-book matches to return (1-100)' },
|
|
413
|
+
{ name: 'fragment-size', type: 'int', default: 150, help: 'Snippet length around each match (1-500)' },
|
|
414
|
+
{ name: 'raw', type: 'boolean', default: false, help: 'Output structured rows instead of markdown text' },
|
|
415
|
+
],
|
|
416
|
+
func: async (args) => {
|
|
417
|
+
const bookTarget = normalizeRequiredString(args.book, 'book');
|
|
418
|
+
const query = normalizeRequiredString(args.query, 'query');
|
|
419
|
+
const bookRank = normalizePositiveInteger(args['book-rank'], 1, 'book-rank');
|
|
420
|
+
const limit = normalizePositiveInteger(args.limit, 20, 'limit', MAX_LIMIT);
|
|
421
|
+
const fragmentSize = normalizePositiveInteger(args['fragment-size'], 150, 'fragment-size', MAX_FRAGMENT_SIZE);
|
|
422
|
+
const book = await resolveBookTarget(bookTarget, bookRank);
|
|
423
|
+
const matches = await searchWithinBook(book.bookId, query, limit, fragmentSize);
|
|
424
|
+
const rows = buildRows(book, matches);
|
|
425
|
+
if (Boolean(args.raw))
|
|
426
|
+
return rows;
|
|
427
|
+
return [{ markdown: formatMarkdownResults(book, query, rows) }];
|
|
428
|
+
},
|
|
429
|
+
});
|
|
430
|
+
|
|
431
|
+
export const __test__ = {
|
|
432
|
+
buildRows,
|
|
433
|
+
extractReaderInitialState,
|
|
434
|
+
formatMarkdownResults,
|
|
435
|
+
parseReaderMetadata,
|
|
436
|
+
parseSearchHtmlEntries,
|
|
437
|
+
resolveReaderUrlForBook,
|
|
438
|
+
};
|
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
|
2
|
+
import { getRegistry } from '@jackwener/opencli/registry';
|
|
3
|
+
import './book-search.js';
|
|
4
|
+
|
|
5
|
+
function jsonResponse(body, status = 200) {
|
|
6
|
+
return {
|
|
7
|
+
ok: status >= 200 && status < 300,
|
|
8
|
+
status,
|
|
9
|
+
json: () => Promise.resolve(body),
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function textResponse(body, status = 200) {
|
|
14
|
+
return {
|
|
15
|
+
ok: status >= 200 && status < 300,
|
|
16
|
+
status,
|
|
17
|
+
text: () => Promise.resolve(body),
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function readerHtml({ bookId = 'book-1', title = '史记', author = '司马迁', chapters = [] } = {}) {
|
|
22
|
+
const state = {
|
|
23
|
+
reader: {
|
|
24
|
+
infoId: 'reader-1',
|
|
25
|
+
bookId,
|
|
26
|
+
bookInfo: { bookId, title, author, encodeId: 'reader-1' },
|
|
27
|
+
chapterInfos: chapters,
|
|
28
|
+
},
|
|
29
|
+
};
|
|
30
|
+
return `
|
|
31
|
+
<html>
|
|
32
|
+
<head>
|
|
33
|
+
<script type="application/ld+json">{"@Id":"${bookId}","name":"${title}","author":{"name":"${author}"},"url":"https://weread.qq.com/web/reader/reader-1"}</script>
|
|
34
|
+
</head>
|
|
35
|
+
<body>
|
|
36
|
+
<script>window.__INITIAL_STATE__=${JSON.stringify(state)};(function(){var s;(s=document.currentScript).parentNode.removeChild(s);}());</script>
|
|
37
|
+
</body>
|
|
38
|
+
</html>
|
|
39
|
+
`;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
describe('weread/book-search', () => {
|
|
43
|
+
const command = getRegistry().get('weread/book-search');
|
|
44
|
+
|
|
45
|
+
beforeEach(() => {
|
|
46
|
+
vi.restoreAllMocks();
|
|
47
|
+
vi.unstubAllGlobals();
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it('registers book-search with markdown default output', () => {
|
|
51
|
+
expect(command?.defaultFormat).toBe('md');
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it('resolves a book by title, reads reader chapter metadata, and searches inside the book', async () => {
|
|
55
|
+
expect(command?.func).toBeTypeOf('function');
|
|
56
|
+
const fetchMock = vi.fn()
|
|
57
|
+
.mockResolvedValueOnce(jsonResponse({
|
|
58
|
+
books: [
|
|
59
|
+
{
|
|
60
|
+
bookInfo: {
|
|
61
|
+
title: '史记',
|
|
62
|
+
author: '司马迁',
|
|
63
|
+
bookId: 'book-1',
|
|
64
|
+
},
|
|
65
|
+
},
|
|
66
|
+
],
|
|
67
|
+
}))
|
|
68
|
+
.mockResolvedValueOnce(textResponse(`
|
|
69
|
+
<ul class="search_bookDetail_list">
|
|
70
|
+
<li class="wr_bookList_item">
|
|
71
|
+
<a class="wr_bookList_item_link" href="/web/reader/reader-1"></a>
|
|
72
|
+
<p class="wr_bookList_item_title">史记</p>
|
|
73
|
+
<p class="wr_bookList_item_author">司马迁</p>
|
|
74
|
+
</li>
|
|
75
|
+
</ul>
|
|
76
|
+
`))
|
|
77
|
+
.mockResolvedValueOnce(textResponse(readerHtml({
|
|
78
|
+
bookId: 'book-1',
|
|
79
|
+
title: '史记',
|
|
80
|
+
author: '司马迁',
|
|
81
|
+
chapters: [
|
|
82
|
+
{ chapterUid: 171, chapterIdx: 9, title: '卷一 五帝本纪第一' },
|
|
83
|
+
],
|
|
84
|
+
})))
|
|
85
|
+
.mockResolvedValueOnce(jsonResponse({
|
|
86
|
+
result: [
|
|
87
|
+
{
|
|
88
|
+
chapterUid: 171,
|
|
89
|
+
abstract: '尧、舜二帝举贤任能,天下大治。',
|
|
90
|
+
absStart: 100,
|
|
91
|
+
absEnd: 118,
|
|
92
|
+
keyword: ['舜'],
|
|
93
|
+
searchIdx: 1,
|
|
94
|
+
},
|
|
95
|
+
],
|
|
96
|
+
hasMore: 0,
|
|
97
|
+
}));
|
|
98
|
+
vi.stubGlobal('fetch', fetchMock);
|
|
99
|
+
|
|
100
|
+
const result = await command.func({ book: '史记', query: '舜', limit: 5, 'book-rank': 1 });
|
|
101
|
+
|
|
102
|
+
expect(String(fetchMock.mock.calls[0][0])).toContain('/web/search/global?keyword=');
|
|
103
|
+
expect(String(fetchMock.mock.calls[1][0])).toContain('/web/search/books?keyword=');
|
|
104
|
+
expect(fetchMock.mock.calls[2][0]).toBe('https://weread.qq.com/web/reader/reader-1');
|
|
105
|
+
expect(String(fetchMock.mock.calls[3][0])).toContain('/web/book/search?bookId=book-1');
|
|
106
|
+
expect(String(fetchMock.mock.calls[3][0])).toContain('keyword=%E8%88%9C');
|
|
107
|
+
expect(result).toEqual([
|
|
108
|
+
{
|
|
109
|
+
markdown: [
|
|
110
|
+
'# 史记',
|
|
111
|
+
'- author: 司马迁',
|
|
112
|
+
'- book_id: `book-1`',
|
|
113
|
+
'- query: `舜`',
|
|
114
|
+
'- matches: 1',
|
|
115
|
+
'- url: https://weread.qq.com/web/reader/reader-1',
|
|
116
|
+
'',
|
|
117
|
+
'## 1. 卷一 五帝本纪第一',
|
|
118
|
+
'',
|
|
119
|
+
'> 尧、舜二帝举贤任能,天下大治。',
|
|
120
|
+
'',
|
|
121
|
+
'- chapter_idx: 9',
|
|
122
|
+
'- chapter_uid: 171',
|
|
123
|
+
'- search_idx: 1',
|
|
124
|
+
].join('\n'),
|
|
125
|
+
},
|
|
126
|
+
]);
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
it('uses book-rank to choose among search results and paginates by searchIdx', async () => {
|
|
130
|
+
expect(command?.func).toBeTypeOf('function');
|
|
131
|
+
const fetchMock = vi.fn()
|
|
132
|
+
.mockResolvedValueOnce(jsonResponse({
|
|
133
|
+
books: [
|
|
134
|
+
{ bookInfo: { title: '文明', author: '作者甲', bookId: 'wrong-book' } },
|
|
135
|
+
{ bookInfo: { title: '文明', author: '作者乙', bookId: 'right-book' } },
|
|
136
|
+
],
|
|
137
|
+
}))
|
|
138
|
+
.mockResolvedValueOnce(textResponse('<html></html>'))
|
|
139
|
+
.mockResolvedValueOnce(jsonResponse({
|
|
140
|
+
result: [
|
|
141
|
+
{ chapterUid: 1, abstract: 'first', searchIdx: 1 },
|
|
142
|
+
{ chapterUid: 1, abstract: 'second', searchIdx: 2 },
|
|
143
|
+
],
|
|
144
|
+
hasMore: 1,
|
|
145
|
+
}))
|
|
146
|
+
.mockResolvedValueOnce(jsonResponse({
|
|
147
|
+
result: [
|
|
148
|
+
{ chapterUid: 2, abstract: 'third', searchIdx: 3 },
|
|
149
|
+
],
|
|
150
|
+
hasMore: 0,
|
|
151
|
+
}));
|
|
152
|
+
vi.stubGlobal('fetch', fetchMock);
|
|
153
|
+
|
|
154
|
+
const result = await command.func({ book: '文明', query: '德', 'book-rank': 2, limit: 3, raw: true });
|
|
155
|
+
|
|
156
|
+
expect(String(fetchMock.mock.calls[2][0])).toContain('bookId=right-book');
|
|
157
|
+
expect(String(fetchMock.mock.calls[2][0])).toContain('maxIdx=0');
|
|
158
|
+
expect(String(fetchMock.mock.calls[3][0])).toContain('maxIdx=2');
|
|
159
|
+
expect(result.map((row) => row.snippet)).toEqual(['first', 'second', 'third']);
|
|
160
|
+
expect(result.every((row) => row.book_id === 'right-book')).toBe(true);
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
it('throws EMPTY_RESULT when no book matches the title query', async () => {
|
|
164
|
+
expect(command?.func).toBeTypeOf('function');
|
|
165
|
+
vi.stubGlobal('fetch', vi.fn().mockResolvedValue(jsonResponse({ books: [] })));
|
|
166
|
+
|
|
167
|
+
await expect(command.func({ book: 'not-a-book', query: 'x' })).rejects.toMatchObject({
|
|
168
|
+
code: 'EMPTY_RESULT',
|
|
169
|
+
});
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
it('fails closed when the book search payload is malformed', async () => {
|
|
173
|
+
expect(command?.func).toBeTypeOf('function');
|
|
174
|
+
vi.stubGlobal('fetch', vi.fn().mockResolvedValue(jsonResponse({ result: [] })));
|
|
175
|
+
|
|
176
|
+
await expect(command.func({ book: '史记', query: '舜' })).rejects.toMatchObject({
|
|
177
|
+
code: 'COMMAND_EXEC',
|
|
178
|
+
message: 'WeRead book search returned malformed books',
|
|
179
|
+
});
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
it('rejects off-domain reader URLs instead of fetching arbitrary pages', async () => {
|
|
183
|
+
expect(command?.func).toBeTypeOf('function');
|
|
184
|
+
|
|
185
|
+
await expect(command.func({ book: 'https://example.com/web/reader/reader-1', query: '舜' })).rejects.toMatchObject({
|
|
186
|
+
code: 'ARGUMENT',
|
|
187
|
+
message: 'book URL must be a https://weread.qq.com/web/reader/<id> URL',
|
|
188
|
+
});
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
it('fails closed when in-book search results are malformed', async () => {
|
|
192
|
+
expect(command?.func).toBeTypeOf('function');
|
|
193
|
+
vi.stubGlobal('fetch', vi.fn().mockResolvedValue(jsonResponse({ result: [{ searchIdx: 1 }], hasMore: 0 })));
|
|
194
|
+
|
|
195
|
+
await expect(command.func({ book: '123', query: '舜', raw: true })).rejects.toMatchObject({
|
|
196
|
+
code: 'COMMAND_EXEC',
|
|
197
|
+
message: 'WeRead in-book search returned malformed match',
|
|
198
|
+
});
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
it('fails closed when in-book pagination does not advance', async () => {
|
|
202
|
+
expect(command?.func).toBeTypeOf('function');
|
|
203
|
+
vi.stubGlobal('fetch', vi.fn()
|
|
204
|
+
.mockResolvedValueOnce(jsonResponse({
|
|
205
|
+
result: [{ chapterUid: 1, abstract: 'first', searchIdx: 1 }],
|
|
206
|
+
hasMore: 1,
|
|
207
|
+
}))
|
|
208
|
+
.mockResolvedValueOnce(jsonResponse({
|
|
209
|
+
result: [{ chapterUid: 1, abstract: 'second', searchIdx: 1 }],
|
|
210
|
+
hasMore: 1,
|
|
211
|
+
})));
|
|
212
|
+
|
|
213
|
+
await expect(command.func({ book: '123', query: '舜', limit: 2, raw: true })).rejects.toMatchObject({
|
|
214
|
+
code: 'COMMAND_EXEC',
|
|
215
|
+
message: 'WeRead in-book search returned non-advancing searchIdx',
|
|
216
|
+
});
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
it('fails closed when a full in-book search page has no pagination state', async () => {
|
|
220
|
+
expect(command?.func).toBeTypeOf('function');
|
|
221
|
+
vi.stubGlobal('fetch', vi.fn().mockResolvedValueOnce(jsonResponse({
|
|
222
|
+
result: Array.from({ length: 50 }, (_, index) => ({
|
|
223
|
+
chapterUid: 1,
|
|
224
|
+
abstract: `match ${index + 1}`,
|
|
225
|
+
searchIdx: index + 1,
|
|
226
|
+
})),
|
|
227
|
+
})));
|
|
228
|
+
|
|
229
|
+
await expect(command.func({ book: '123', query: '舜', limit: 51, raw: true })).rejects.toMatchObject({
|
|
230
|
+
code: 'COMMAND_EXEC',
|
|
231
|
+
message: 'WeRead in-book search returned malformed pagination state',
|
|
232
|
+
});
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
it('validates numeric arguments instead of silently clamping', async () => {
|
|
236
|
+
expect(command?.func).toBeTypeOf('function');
|
|
237
|
+
await expect(command.func({ book: '史记', query: '舜', limit: 101 })).rejects.toMatchObject({
|
|
238
|
+
code: 'ARGUMENT',
|
|
239
|
+
message: 'limit must be <= 100',
|
|
240
|
+
});
|
|
241
|
+
});
|
|
242
|
+
});
|