mirror of
https://github.com/jackwener/OpenCLI.git
synced 2026-09-14 18:25:42 +08:00
4fe9a73ebc
* refactor: migrate adapter imports to package exports Replace all relative imports (../../src/registry.js, ../../browser/cdp.js, etc.) with package exports (@jackwener/opencli/registry, @jackwener/opencli/errors, etc.) across all 484 adapter files. This decouples adapter import resolution from directory structure: - User CLIs in ~/.opencli/clis/ resolve via node_modules symlink - Internal adapters resolve via Node.js self-referencing - No more shim files needed for import resolution Changes: - package.json: add sub-path exports for all public modules - clis/**: replace relative imports with @jackwener/opencli/... - discovery.ts: simplify ensureUserCliCompatShims to symlink-only - registry-api.ts: export CommandArgs type - Remove root-level shim directories (browser/, download/, pipeline/) - Remove shim entries from tsconfig.json include and package.json files * test: add regression tests for package exports Prevents regressions like #788/#791 by: 1. Scanning all adapter files for forbidden relative imports (../../src/, ../../browser/, etc.) — fails if any remain 2. Verifying every package.json export maps to an existing source file 18 new test cases. * fix: use junction on Windows + broaden test patterns - discovery.ts: use 'junction' symlink type on Windows (no admin required) - package-exports.test.ts: generalize forbidden patterns to catch any depth of ../ traversal (not just ../../ and ../../../) * fix: update stale vi.mock/importActual paths in adapter tests Test files still used old relative paths for vi.mock() and vi.importActual() calls. Updated 5 test files to use package exports. Also broadened regression test patterns to catch mock/importActual paths. * fix: use rm instead of unlink for symlink cleanup, add warn on failure Addresses review feedback from Astro-Han: - rm() handles both symlinks and stale directories (unlink fails on dirs) - Log a warning when symlink creation fails instead of silent catch * docs: update import examples to use package exports Update all documentation, contributing guides, and skills to use @jackwener/opencli/registry instead of ../../src/registry.js. Without this, users following the docs would write adapters with broken imports since the old shim files are no longer created.
101 lines
3.7 KiB
TypeScript
101 lines
3.7 KiB
TypeScript
/**
|
|
* Xiaohongshu note — read full note content from a public note page.
|
|
*
|
|
* Extracts title, author, description text, and engagement metrics
|
|
* (likes, collects, comment count) via DOM extraction.
|
|
*
|
|
* Supports both bare note IDs and full URLs (with xsec_token).
|
|
* Bare IDs now use /search_result/<id> which works without xsec_token
|
|
* when the user is logged in via cookies.
|
|
*/
|
|
|
|
import { cli, Strategy } from '@jackwener/opencli/registry';
|
|
import { AuthRequiredError, EmptyResultError } from '@jackwener/opencli/errors';
|
|
import { parseNoteId, buildNoteUrl } from './note-helpers.js';
|
|
|
|
cli({
|
|
site: 'xiaohongshu',
|
|
name: 'note',
|
|
description: '获取小红书笔记正文和互动数据',
|
|
domain: 'www.xiaohongshu.com',
|
|
strategy: Strategy.COOKIE,
|
|
args: [
|
|
{ name: 'note-id', required: true, positional: true, help: 'Note ID or full URL (preserves xsec_token for access)' },
|
|
],
|
|
columns: ['field', 'value'],
|
|
func: async (page, kwargs) => {
|
|
const raw = String(kwargs['note-id']);
|
|
const noteId = parseNoteId(raw);
|
|
const url = buildNoteUrl(raw);
|
|
|
|
await page.goto(url);
|
|
await page.wait(3);
|
|
|
|
const data = await page.evaluate(`
|
|
(() => {
|
|
const loginWall = /登录后查看|请登录/.test(document.body.innerText || '')
|
|
const notFound = /页面不见了|笔记不存在|无法浏览/.test(document.body.innerText || '')
|
|
|
|
const clean = (el) => (el?.textContent || '').replace(/\\s+/g, ' ').trim()
|
|
|
|
const title = clean(document.querySelector('#detail-title, .title'))
|
|
const desc = clean(document.querySelector('#detail-desc, .desc, .note-text'))
|
|
const author = clean(document.querySelector('.username, .author-wrapper .name'))
|
|
const likes = clean(document.querySelector('.like-wrapper .count'))
|
|
const collects = clean(document.querySelector('.collect-wrapper .count'))
|
|
const comments = clean(document.querySelector('.chat-wrapper .count'))
|
|
|
|
// Try to extract tags/topics
|
|
const tags = []
|
|
document.querySelectorAll('#detail-desc a.tag, #detail-desc a[href*="search_result"]').forEach(el => {
|
|
const t = (el.textContent || '').trim()
|
|
if (t) tags.push(t)
|
|
})
|
|
|
|
return { loginWall, notFound, title, desc, author, likes, collects, comments, tags }
|
|
})()
|
|
`);
|
|
|
|
if (!data || typeof data !== 'object') {
|
|
throw new EmptyResultError('xiaohongshu/note', 'Unexpected evaluate response');
|
|
}
|
|
|
|
if ((data as any).loginWall) {
|
|
throw new AuthRequiredError('www.xiaohongshu.com', 'Note content requires login');
|
|
}
|
|
|
|
if ((data as any).notFound) {
|
|
throw new EmptyResultError('xiaohongshu/note', `Note ${noteId} not found or unavailable — it may have been deleted or restricted`);
|
|
}
|
|
|
|
const d = data as any;
|
|
// XHS renders placeholder text like "赞"/"收藏"/"评论" when count is 0;
|
|
// normalize to '0' unless the value looks numeric.
|
|
const numOrZero = (v: string) => /^\d+/.test(v) ? v : '0';
|
|
|
|
// Title + author are always present on a real note page.
|
|
// If both are missing, the page likely failed to load properly.
|
|
if (!d.title && !d.author) {
|
|
throw new EmptyResultError(
|
|
'xiaohongshu/note',
|
|
'The note page loaded without visible content. The note may be deleted or restricted.',
|
|
);
|
|
}
|
|
|
|
const rows = [
|
|
{ field: 'title', value: d.title || '' },
|
|
{ field: 'author', value: d.author || '' },
|
|
{ field: 'content', value: d.desc || '' },
|
|
{ field: 'likes', value: numOrZero(d.likes || '') },
|
|
{ field: 'collects', value: numOrZero(d.collects || '') },
|
|
{ field: 'comments', value: numOrZero(d.comments || '') },
|
|
];
|
|
|
|
if (d.tags?.length) {
|
|
rows.push({ field: 'tags', value: d.tags.join(', ') });
|
|
}
|
|
|
|
return rows;
|
|
},
|
|
});
|