mirror of
https://github.com/jackwener/OpenCLI.git
synced 2026-09-14 18:25:42 +08:00
64ac362a40
* feat(rednote): add rednote.com adapter mirroring xiaohongshu read commands (#1136) Implements rednote.com support as discussed in issue #1136. The mainland xiaohongshu adapter stays in place; international users redirected to www.rednote.com now have a CLI without a copy-pasted adapter. Issue #1136 documents that xiaohongshu and rednote share DOM selectors, URL paths, API paths, response schema, cookies, and the xsec_token auth mechanism. The only material differences: Layer xiaohongshu rednote Web host www.xiaohongshu.com www.rednote.com API host edith.xiaohongshu.com webapi.rednote.com Security host fe-static.xhscdn.com as.rednote.com Cookie root .xiaohongshu.com .rednote.com Search gate Inline text Full-screen modal + text ## Architecture (minimal) `clis/xiaohongshu/*` keep all selector / regex / extraction logic. Each command file is touched minimally to export the IIFE or pipeline so the sibling adapter can reuse it: search.js + export const buildSearchExtractJs(webHost) + export const command = cli({...}) note.js + export const NOTE_EXTRACT_JS + export const command = cli({...}) comments.js + export function buildCommentsExtractJs(withReplies) + export parseCommentLimit + export const command = cli({...}) download.js + export function buildDownloadExtractJs(noteId) (CDN allowlist now includes rednote alongside xhscdn) + export const command = cli({...}) user.js + export const USER_SNAPSHOT_JS + export const command = cli({...}) feed.js + export function buildFeedPipeline(webHost) + export const command = cli({...}) notifications.js + export function buildNotificationsPipeline(webHost) + export const command = cli({...}) note-helpers.js buildNoteUrl now accepts `cookieRoot` + `signedUrlHint` options (defaults preserved so xhs callers and tests are unchanged) user-helpers.js buildXhsNoteUrl / extractXhsUserNotes accept an optional `webHost` argument (default xhs) The `export const command = cli({...})` pattern matches twitter/lists.js and clis/discord-app/*; without it the build-manifest scanner attributes xhs's command to whichever rednote sibling triggered the transitive import first. ## clis/rednote/ — thin shims Each rednote command file imports the relevant builder / constant from its xiaohongshu sibling and calls `cli()` with the rednote host triple. No selectors, regexes, or extraction logic are duplicated. search.js imports buildSearchExtractJs + noteIdToDate declares its own WAIT_FOR_CONTENT_JS (modal + text login-gate variants — the one xhs behaviour that genuinely differs) note.js imports NOTE_EXTRACT_JS + buildNoteUrl + parseNoteId comments.js imports buildCommentsExtractJs + parseCommentLimit + buildNoteUrl + parseNoteId download.js imports buildDownloadExtractJs + buildNoteUrl + parseNoteId user.js imports USER_SNAPSHOT_JS + extractXhsUserNotes + normalizeXhsUserId ## Scope (initial) Ships the five commands verified live against the user's logged-in rednote.com session: search / note / comments / user / download. `feed` and `notifications` are intentionally left out. Both rely on intercepting the xiaohongshu Pinia store at the `homefeed` / `you` capture pattern; live verification on rednote returns `tap → dict (error)` for the feed step, so shipping them would surface a broken contract. The mainland xiaohongshu commands continue to work. Adding the rednote-side feed / notifications is straightforward follow-up work once someone with rednote access maps the network surface. Creator-center commands (publish, creator-*) have no rednote counterpart and stay xiaohongshu-only, per the reporter's note in #1136. ## Verification - clis/xiaohongshu/ + clis/rednote/: 103/103 tests green - npx tsc --noEmit: clean - npm run build: 807 manifest entries (xhs 13 + rednote 5 + everything else preserved) - silent-column-drop / typed-error-lint: 103 / 189 baseline entries, no new violations - Live verify against the user's rednote.com session: rednote search "travel" --limit 1 → real note row rednote note <signed-url> → 7 field/value rows rednote comments <signed-url> --limit 3 → 3 top-level rows rednote user 5b21f6564eacab3b38f05c39 --limit 2 → 2 profile notes Spaced 15–30s between runs per the xhs/rednote rate-limit guidance; no write commands invoked. Regression check: xiaohongshu/feed on the existing mainland session still returns the standard 6-field rows after the refactor. Closes #1136 * fix(rednote): tighten adapter failure boundaries --------- Co-authored-by: jackwener <jakevingoo@gmail.com>
140 lines
6.2 KiB
JavaScript
140 lines
6.2 KiB
JavaScript
/**
|
|
* Rednote notifications — calls `notification.getNotification(type)` and reads
|
|
* `notification.activeTabMessageList` from the Pinia store.
|
|
*
|
|
* Differs from xiaohongshu/notifications because the rednote intercept tap
|
|
* does not see a fresh `/you/` request after `getNotification`. The store is
|
|
* populated directly, so a `func`-mode read is more reliable. Field names
|
|
* accept both snake_case (`user_info.nickname`) and camelCase
|
|
* (`userInfo.nickName`) to absorb the same SSR client-transform diff that
|
|
* `feed` hits on rednote.
|
|
*/
|
|
import { cli, Strategy } from '@jackwener/opencli/registry';
|
|
import { ArgumentError, CommandExecutionError } from '@jackwener/opencli/errors';
|
|
|
|
const NOTIFICATION_TYPES = new Set(['mentions', 'likes', 'connections']);
|
|
|
|
function parseNotificationType(raw) {
|
|
const type = String(raw ?? 'mentions');
|
|
if (!NOTIFICATION_TYPES.has(type)) {
|
|
throw new ArgumentError(`--type must be one of mentions, likes, or connections, got ${JSON.stringify(raw)}`);
|
|
}
|
|
return type;
|
|
}
|
|
|
|
function parseLimit(raw) {
|
|
const parsed = Number(raw ?? 20);
|
|
if (!Number.isFinite(parsed) || !Number.isInteger(parsed)) {
|
|
throw new ArgumentError(`--limit must be a positive integer, got ${JSON.stringify(raw)}`);
|
|
}
|
|
if (parsed < 1) {
|
|
throw new ArgumentError(`--limit must be a positive integer, got ${parsed}`);
|
|
}
|
|
return parsed;
|
|
}
|
|
|
|
const READ_NOTIFICATIONS_JS = `
|
|
(async (type) => {
|
|
let pinia = null;
|
|
const probe = (el) => el?.__vue_app__?.config?.globalProperties?.$pinia ?? null;
|
|
pinia = probe(document.querySelector('#app'));
|
|
if (!pinia) {
|
|
for (const el of document.querySelectorAll('*')) {
|
|
pinia = probe(el);
|
|
if (pinia) break;
|
|
}
|
|
}
|
|
if (!pinia || !pinia._s) return { error: 'no_pinia' };
|
|
const store = pinia._s.get('notification');
|
|
if (!store) return { error: 'no_notification_store' };
|
|
if (typeof store.getNotification !== 'function') return { error: 'no_getNotification_action' };
|
|
try { await store.getNotification(type); } catch (e) { return { error: 'action_failed', detail: e?.message }; }
|
|
// Read messages from whichever store path is populated. rednote keeps the
|
|
// current tab in activeTabMessageList but may instead drop the list into
|
|
// notificationMap[type] (or notificationMap[type].messages) depending on
|
|
// the build, so check both before timing out.
|
|
const readMessages = () => {
|
|
if (Array.isArray(store.activeTabMessageList) && store.activeTabMessageList.length > 0) return store.activeTabMessageList;
|
|
const tab = store.notificationMap?.[type];
|
|
if (Array.isArray(tab) && tab.length > 0) return tab;
|
|
if (Array.isArray(tab?.messages) && tab.messages.length > 0) return tab.messages;
|
|
if (Array.isArray(tab?.messageList) && tab.messageList.length > 0) return tab.messageList;
|
|
return null;
|
|
};
|
|
let messages = null;
|
|
for (let i = 0; i < 16; i++) {
|
|
messages = readMessages();
|
|
if (messages) break;
|
|
await new Promise(r => setTimeout(r, 500));
|
|
}
|
|
const arr = messages ?? (Array.isArray(store.activeTabMessageList) ? store.activeTabMessageList : []);
|
|
const pick = (item, snake, camel) => item?.[snake] ?? item?.[camel];
|
|
// Try the leaf as written, plus its snake→camel and camel→snake variants.
|
|
// Needed because rednote ships e.g. \`userInfo.nickName\` while xhs returns
|
|
// \`user_info.nickname\` (or \`user_info.nick_name\`); the field name varies
|
|
// independently of the wrapping object name.
|
|
const leafVariants = (leaf) => {
|
|
const camel = leaf.replace(/_([a-z])/g, (_, c) => c.toUpperCase());
|
|
const snake = leaf.replace(/([A-Z])/g, (_, c) => '_' + c.toLowerCase());
|
|
const capCamel = leaf.charAt(0) + leaf.slice(1).replace(/([a-z])([A-Z])/g, '$1$2').replace(/(^|_)([a-z])/g, (_, sep, c) => (sep ? c.toUpperCase() : c));
|
|
return [...new Set([leaf, camel, snake, capCamel])];
|
|
};
|
|
const nested = (item, snake, camel, ...leafCandidates) => {
|
|
const a = pick(item, snake, camel);
|
|
if (!a || typeof a !== 'object') return '';
|
|
for (const candidate of leafCandidates) {
|
|
for (const variant of leafVariants(candidate)) {
|
|
if (a[variant] != null && a[variant] !== '') return a[variant];
|
|
}
|
|
}
|
|
return '';
|
|
};
|
|
return {
|
|
items: arr.map(item => ({
|
|
user: nested(item, 'user_info', 'userInfo', 'nickname', 'nickName'),
|
|
action: item?.title ?? item?.actionTitle ?? '',
|
|
content: nested(item, 'comment_info', 'commentInfo', 'content'),
|
|
note: nested(item, 'item_info', 'itemInfo', 'content'),
|
|
time: item?.time ?? item?.timestamp ?? '',
|
|
})),
|
|
};
|
|
})(${JSON.stringify('PLACEHOLDER_TYPE')})
|
|
`;
|
|
|
|
export const command = cli({
|
|
site: 'rednote',
|
|
name: 'notifications',
|
|
access: 'read',
|
|
description: 'Rednote notifications (mentions/likes/connections)',
|
|
domain: 'www.rednote.com',
|
|
strategy: Strategy.COOKIE,
|
|
browser: true,
|
|
navigateBefore: false,
|
|
args: [
|
|
{
|
|
name: 'type',
|
|
default: 'mentions',
|
|
help: 'Notification type: mentions, likes, or connections',
|
|
},
|
|
{ name: 'limit', type: 'int', default: 20, help: 'Number of notifications to return' },
|
|
],
|
|
columns: ['rank', 'user', 'action', 'content', 'note', 'time'],
|
|
func: async (page, kwargs) => {
|
|
const type = parseNotificationType(kwargs.type);
|
|
const limit = parseLimit(kwargs.limit);
|
|
await page.goto('https://www.rednote.com/notification');
|
|
await page.wait({ time: 2 });
|
|
const script = READ_NOTIFICATIONS_JS.replace(JSON.stringify('PLACEHOLDER_TYPE'), JSON.stringify(type));
|
|
const data = await page.evaluate(script);
|
|
if (!data || typeof data !== 'object') {
|
|
throw new CommandExecutionError('rednote notifications: unexpected evaluate response');
|
|
}
|
|
if (data.error) {
|
|
throw new CommandExecutionError(`rednote notifications: ${data.error}${data.detail ? ' (' + data.detail + ')' : ''}`, 'The rednote SPA may still be hydrating; reload www.rednote.com/notification and retry.');
|
|
}
|
|
return (data.items || [])
|
|
.slice(0, limit)
|
|
.map((row, i) => ({ rank: i + 1, ...row }));
|
|
},
|
|
});
|