@sovovs/bycli 2.1.21 → 2.1.23

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/cli-manifest.json CHANGED
@@ -28307,6 +28307,50 @@
28307
28307
  "sourceFile": "weixin/save-articles.js",
28308
28308
  "navigateBefore": "https://mp.weixin.qq.com"
28309
28309
  },
28310
+ {
28311
+ "site": "weixin",
28312
+ "name": "sougousearch",
28313
+ "description": "使用搜狗微信搜索公众号文章;如需导出正文 Markdown,请使用 weixin download 处理公众号文章链接",
28314
+ "access": "read",
28315
+ "domain": "weixin.sogou.com",
28316
+ "strategy": "public",
28317
+ "browser": true,
28318
+ "args": [
28319
+ {
28320
+ "name": "query",
28321
+ "type": "str",
28322
+ "required": true,
28323
+ "positional": true,
28324
+ "help": "搜索关键词;如需正文 Markdown,请使用 weixin download 处理公众号文章链接"
28325
+ },
28326
+ {
28327
+ "name": "page",
28328
+ "type": "int",
28329
+ "default": 1,
28330
+ "required": false,
28331
+ "help": "结果页码,从 1 开始"
28332
+ },
28333
+ {
28334
+ "name": "limit",
28335
+ "type": "int",
28336
+ "default": 10,
28337
+ "required": false,
28338
+ "help": "返回条数,最大 10"
28339
+ }
28340
+ ],
28341
+ "columns": [
28342
+ "rank",
28343
+ "page",
28344
+ "title",
28345
+ "account",
28346
+ "url",
28347
+ "summary",
28348
+ "publish_time"
28349
+ ],
28350
+ "type": "js",
28351
+ "modulePath": "weixin/search.js",
28352
+ "sourceFile": "weixin/search.js"
28353
+ },
28310
28354
  {
28311
28355
  "site": "weread",
28312
28356
  "name": "ai-outline",
@@ -0,0 +1,163 @@
1
+ import { ArgumentError, CommandExecutionError, EmptyResultError } from '@sovovs/bycli/errors';
2
+ import { cli, Strategy } from '@sovovs/bycli/registry';
3
+
4
+ const SOGOU_WEIXIN_DOMAIN = 'weixin.sogou.com';
5
+ const DEFAULT_PAGE = 1;
6
+ const DEFAULT_LIMIT = 10;
7
+ const MAX_LIMIT = 10;
8
+
9
+ function normalizePositiveInteger(value, name, defaultValue, maxValue) {
10
+ if (value === undefined || value === null) return defaultValue;
11
+ const text = String(value).trim();
12
+ if (!/^\d+$/.test(text)) {
13
+ throw new ArgumentError(
14
+ `weixin sougousearch --${name} must be a positive integer`,
15
+ `Pass --${name} as a whole number${maxValue ? ` from 1 to ${maxValue}` : ' greater than 0'}.`,
16
+ );
17
+ }
18
+ const parsed = Number(text);
19
+ if (!Number.isSafeInteger(parsed) || parsed < 1 || (maxValue && parsed > maxValue)) {
20
+ throw new ArgumentError(
21
+ `weixin sougousearch --${name} is out of range`,
22
+ `Pass --${name} as a whole number${maxValue ? ` from 1 to ${maxValue}` : ' greater than 0'}.`,
23
+ );
24
+ }
25
+ return parsed;
26
+ }
27
+
28
+ function normalizePage(page) {
29
+ return normalizePositiveInteger(page, 'page', DEFAULT_PAGE);
30
+ }
31
+
32
+ function normalizeLimit(limit) {
33
+ return normalizePositiveInteger(limit, 'limit', DEFAULT_LIMIT, MAX_LIMIT);
34
+ }
35
+
36
+ function buildSearchUrl(query, pageNo) {
37
+ const searchUrl = new URL('https://weixin.sogou.com/weixin');
38
+ searchUrl.searchParams.set('query', query);
39
+ searchUrl.searchParams.set('type', '2');
40
+ searchUrl.searchParams.set('page', String(pageNo));
41
+ searchUrl.searchParams.set('ie', 'utf8');
42
+ return searchUrl.toString();
43
+ }
44
+
45
+ function buildExtractSearchResultsEvaluate() {
46
+ return String.raw`(() => {
47
+ const clean = (value) => {
48
+ return (value || '')
49
+ .replace(/\s+/g, ' ')
50
+ .replace(/<!--red_beg-->|<!--red_end-->/g, '')
51
+ .replace(/document\.write\(timeConvert\('\d+'\)\)/g, '')
52
+ .trim();
53
+ };
54
+
55
+ const absolutize = (href) => {
56
+ if (!href) return '';
57
+ try {
58
+ return new URL(href, window.location.origin).toString();
59
+ } catch {
60
+ return href;
61
+ }
62
+ };
63
+
64
+ const bodyText = clean(document.body && document.body.innerText);
65
+ const blocked = /验证码|安全验证|异常访问|访问过于频繁|请输入验证码/.test(bodyText);
66
+ const empty = /没有找到相关的微信文章|未找到相关|暂无相关|没有找到/.test(bodyText)
67
+ || Boolean(document.querySelector('.no-result, .no_result, .s-noresult'));
68
+ const cards = Array.from(document.querySelectorAll('.news-list li'));
69
+ const extracted = cards.map((item) => {
70
+ const linkEl = item.querySelector('h3 a[href]');
71
+ const summaryEl = item.querySelector('p.txt-info');
72
+ const accountEl = item.querySelector('.s-p .all-time-y2');
73
+ const timeEl = item.querySelector('.s-p .s2');
74
+ return {
75
+ title: clean(linkEl && linkEl.textContent),
76
+ account: clean(accountEl && accountEl.textContent),
77
+ url: absolutize(linkEl && linkEl.getAttribute('href')),
78
+ summary: clean(summaryEl && summaryEl.textContent),
79
+ publish_time: clean(timeEl && timeEl.textContent),
80
+ };
81
+ });
82
+ const rows = extracted.filter((row) => row.title && row.url);
83
+
84
+ return {
85
+ blocked,
86
+ empty,
87
+ invalidCount: extracted.length - rows.length,
88
+ rows,
89
+ };
90
+ })()`;
91
+ }
92
+
93
+ export const weixinSearchCommand = cli({
94
+ site: 'weixin',
95
+ name: 'sougousearch',
96
+ access: 'read',
97
+ description: '使用搜狗微信搜索公众号文章;如需导出正文 Markdown,请使用 weixin download 处理公众号文章链接',
98
+ domain: SOGOU_WEIXIN_DOMAIN,
99
+ strategy: Strategy.PUBLIC,
100
+ browser: true,
101
+ args: [
102
+ { name: 'query', positional: true, required: true, help: '搜索关键词;如需正文 Markdown,请使用 weixin download 处理公众号文章链接' },
103
+ { name: 'page', type: 'int', default: 1, help: '结果页码,从 1 开始' },
104
+ { name: 'limit', type: 'int', default: 10, help: '返回条数,最大 10' },
105
+ ],
106
+ columns: ['rank', 'page', 'title', 'account', 'url', 'summary', 'publish_time'],
107
+ func: async (page, kwargs) => {
108
+ const query = String(kwargs.query ?? '').trim();
109
+ if (!query) {
110
+ throw new ArgumentError('A search query is required.', 'Pass a non-empty keyword to search Weixin articles via Sogou.');
111
+ }
112
+
113
+ const pageNo = normalizePage(kwargs.page);
114
+ const limit = normalizeLimit(kwargs.limit);
115
+ const searchUrl = buildSearchUrl(query, pageNo);
116
+
117
+ let payload;
118
+ try {
119
+ await page.goto(searchUrl);
120
+ await page.wait(2);
121
+ payload = await page.evaluate(buildExtractSearchResultsEvaluate());
122
+ } catch (error) {
123
+ const detail = error instanceof Error ? error.message : String(error);
124
+ throw new CommandExecutionError('weixin sougousearch failed while loading Sogou results', detail);
125
+ }
126
+
127
+ if (!payload || typeof payload !== 'object' || !Array.isArray(payload.rows)) {
128
+ throw new CommandExecutionError('weixin sougousearch returned an unreadable browser payload', 'Sogou Weixin may have changed its result page structure.');
129
+ }
130
+ if (payload.blocked) {
131
+ throw new CommandExecutionError('Sogou Weixin blocked this search request', 'Open weixin.sogou.com in Chrome and complete any verification before retrying.');
132
+ }
133
+ if (payload.invalidCount > 0) {
134
+ throw new CommandExecutionError('Sogou Weixin returned article cards without required title or URL', 'The result page structure may have changed; refusing to return a partial result set.');
135
+ }
136
+
137
+ const rows = payload.rows;
138
+ if (rows.length === 0 && payload.empty) {
139
+ throw new EmptyResultError('weixin sougousearch', 'Try a different keyword or a different page number.');
140
+ }
141
+ if (rows.length === 0) {
142
+ throw new CommandExecutionError('weixin sougousearch did not expose article result cards', 'Sogou Weixin may have changed its selectors or returned a transient shell page.');
143
+ }
144
+
145
+ return rows.slice(0, limit).map((row, index) => ({
146
+ rank: (pageNo - 1) * 10 + index + 1,
147
+ page: pageNo,
148
+ title: row.title,
149
+ account: row.account,
150
+ url: row.url,
151
+ summary: row.summary,
152
+ publish_time: row.publish_time,
153
+ }));
154
+ },
155
+ });
156
+
157
+ export const __test__ = {
158
+ MAX_LIMIT,
159
+ normalizePage,
160
+ normalizeLimit,
161
+ buildSearchUrl,
162
+ buildExtractSearchResultsEvaluate,
163
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sovovs/bycli",
3
- "version": "2.1.21",
3
+ "version": "2.1.23",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },