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.
98 lines
4.3 KiB
TypeScript
98 lines
4.3 KiB
TypeScript
/**
|
|
* YouTube comments — get video comments via InnerTube API.
|
|
*/
|
|
import { cli, Strategy } from '@jackwener/opencli/registry';
|
|
import { CommandExecutionError } from '@jackwener/opencli/errors';
|
|
import { parseVideoId } from './utils.js';
|
|
|
|
cli({
|
|
site: 'youtube',
|
|
name: 'comments',
|
|
description: 'Get YouTube video comments',
|
|
domain: 'www.youtube.com',
|
|
strategy: Strategy.COOKIE,
|
|
args: [
|
|
{ name: 'url', required: true, positional: true, help: 'YouTube video URL or video ID' },
|
|
{ name: 'limit', type: 'int', default: 20, help: 'Max comments (max 100)' },
|
|
],
|
|
columns: ['rank', 'author', 'text', 'likes', 'replies', 'time'],
|
|
func: async (page, kwargs) => {
|
|
const videoId = parseVideoId(kwargs.url);
|
|
const limit = Math.min(kwargs.limit || 20, 100);
|
|
await page.goto(`https://www.youtube.com/watch?v=${videoId}`);
|
|
await page.wait(3);
|
|
|
|
const data = await page.evaluate(`
|
|
(async () => {
|
|
const videoId = ${JSON.stringify(videoId)};
|
|
const limit = ${limit};
|
|
const cfg = window.ytcfg?.data_ || {};
|
|
const apiKey = cfg.INNERTUBE_API_KEY;
|
|
const context = cfg.INNERTUBE_CONTEXT;
|
|
if (!apiKey || !context) return {error: 'YouTube config not found'};
|
|
|
|
// Step 1: Get comment continuation token
|
|
let continuationToken = null;
|
|
|
|
// Try from current page ytInitialData
|
|
if (window.ytInitialData) {
|
|
const results = window.ytInitialData.contents?.twoColumnWatchNextResults?.results?.results?.contents || [];
|
|
const commentSection = results.find(i => i.itemSectionRenderer?.targetId === 'comments-section');
|
|
continuationToken = commentSection?.itemSectionRenderer?.contents?.[0]?.continuationItemRenderer?.continuationEndpoint?.continuationCommand?.token;
|
|
}
|
|
|
|
// Fallback: fetch via next API
|
|
if (!continuationToken) {
|
|
const nextResp = await fetch('/youtubei/v1/next?key=' + apiKey + '&prettyPrint=false', {
|
|
method: 'POST', credentials: 'include',
|
|
headers: {'Content-Type': 'application/json'},
|
|
body: JSON.stringify({context, videoId})
|
|
});
|
|
if (!nextResp.ok) return {error: 'Failed to get video data: HTTP ' + nextResp.status};
|
|
const nextData = await nextResp.json();
|
|
const results = nextData.contents?.twoColumnWatchNextResults?.results?.results?.contents || [];
|
|
const commentSection = results.find(i => i.itemSectionRenderer?.targetId === 'comments-section');
|
|
continuationToken = commentSection?.itemSectionRenderer?.contents?.[0]?.continuationItemRenderer?.continuationEndpoint?.continuationCommand?.token;
|
|
}
|
|
|
|
if (!continuationToken) return {error: 'No comment section found — comments may be disabled'};
|
|
|
|
// Step 2: Fetch comments
|
|
const commentResp = await fetch('/youtubei/v1/next?key=' + apiKey + '&prettyPrint=false', {
|
|
method: 'POST', credentials: 'include',
|
|
headers: {'Content-Type': 'application/json'},
|
|
body: JSON.stringify({context, continuation: continuationToken})
|
|
});
|
|
if (!commentResp.ok) return {error: 'Failed to fetch comments: HTTP ' + commentResp.status};
|
|
const commentData = await commentResp.json();
|
|
|
|
// Parse from frameworkUpdates (new ViewModel format)
|
|
const mutations = commentData.frameworkUpdates?.entityBatchUpdate?.mutations || [];
|
|
const commentEntities = mutations.filter(m => m.payload?.commentEntityPayload);
|
|
|
|
return commentEntities.slice(0, limit).map((m, i) => {
|
|
const p = m.payload.commentEntityPayload;
|
|
const props = p.properties || {};
|
|
const author = p.author || {};
|
|
const toolbar = p.toolbar || {};
|
|
return {
|
|
rank: i + 1,
|
|
author: author.displayName || '',
|
|
text: (props.content?.content || '').substring(0, 300),
|
|
likes: toolbar.likeCountNotliked || '0',
|
|
replies: toolbar.replyCount || '0',
|
|
time: props.publishedTime || '',
|
|
};
|
|
});
|
|
})()
|
|
`);
|
|
|
|
if (!Array.isArray(data)) {
|
|
const errMsg = data && typeof data === 'object' ? String((data as Record<string, unknown>).error || '') : '';
|
|
if (errMsg) throw new CommandExecutionError(errMsg);
|
|
return [];
|
|
}
|
|
return data;
|
|
},
|
|
});
|