mirror of
https://github.com/jackwener/OpenCLI.git
synced 2026-09-14 18:25:42 +08:00
314bc6f231
1. Remove root-level shim files (errors.ts, logger.ts, registry.ts, types.ts, utils.ts, launcher.ts) — update all ~840 adapter imports to reference src/ directly 2. Consolidate interceptor: reuse shared DISGUISE_FN in tap interceptor instead of reimplementing 3. Unify error classification: single ClassifiedError type with icon/exitCode/hint lookup table, eliminating duplicated pattern matching between resolveExitCode and renderError 4. Simplify cascade probe: replace repetitive switch cases with PROBE_OPTIONS lookup map 12. Clean up synthesize.ts: remove deprecated snake_case field aliases (recommended_args, recommended_columns, recommendedColumnsLegacy) and unnecessary constant aliases Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
98 lines
4.2 KiB
TypeScript
98 lines
4.2 KiB
TypeScript
/**
|
|
* YouTube comments — get video comments via InnerTube API.
|
|
*/
|
|
import { cli, Strategy } from '../../src/registry.js';
|
|
import { CommandExecutionError } from '../../src/errors.js';
|
|
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;
|
|
},
|
|
});
|