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.
171 lines
6.9 KiB
TypeScript
171 lines
6.9 KiB
TypeScript
import { AuthRequiredError, CommandExecutionError } from '@jackwener/opencli/errors';
|
|
import { cli, Strategy } from '@jackwener/opencli/registry';
|
|
import { resolveTwitterQueryId } from './shared.js';
|
|
|
|
const TWEET_RESULT_BY_REST_ID_QUERY_ID = '7xflPyRiUxGVbJd4uWmbfg';
|
|
|
|
cli({
|
|
site: 'twitter',
|
|
name: 'article',
|
|
description: 'Fetch a Twitter Article (long-form content) and export as Markdown',
|
|
domain: 'x.com',
|
|
strategy: Strategy.COOKIE,
|
|
browser: true,
|
|
args: [
|
|
{ name: 'tweet-id', type: 'string', positional: true, required: true, help: 'Tweet ID or URL containing the article' },
|
|
],
|
|
columns: ['title', 'author', 'content', 'url'],
|
|
func: async (page, kwargs) => {
|
|
// Extract tweet ID from URL if needed.
|
|
// Article URLs (x.com/i/article/{articleId}) use a different ID than
|
|
// tweet status URLs — the GraphQL endpoint needs the parent tweet ID.
|
|
let tweetId = kwargs['tweet-id'];
|
|
const isArticleUrl = /\/article\/\d+/.test(tweetId);
|
|
const urlMatch = tweetId.match(/\/(?:status|article)\/(\d+)/);
|
|
if (urlMatch) tweetId = urlMatch[1];
|
|
|
|
if (isArticleUrl) {
|
|
// Navigate to the article page and resolve the parent tweet ID from DOM
|
|
await page.goto(`https://x.com/i/article/${tweetId}`);
|
|
await page.wait(3);
|
|
const resolvedId = await page.evaluate(`
|
|
(function() {
|
|
var links = document.querySelectorAll('a[href*="/status/"]');
|
|
for (var i = 0; i < links.length; i++) {
|
|
var m = links[i].href.match(/\\/status\\/(\\d+)/);
|
|
if (m) return m[1];
|
|
}
|
|
var og = document.querySelector('meta[property="og:url"]');
|
|
if (og && og.content) {
|
|
var m2 = og.content.match(/\\/status\\/(\\d+)/);
|
|
if (m2) return m2[1];
|
|
}
|
|
return null;
|
|
})()
|
|
`);
|
|
if (!resolvedId || typeof resolvedId !== 'string') {
|
|
throw new CommandExecutionError(
|
|
`Could not resolve article ${tweetId} to a tweet ID. The article page may not contain a linked tweet.`,
|
|
);
|
|
}
|
|
tweetId = resolvedId;
|
|
}
|
|
|
|
// Navigate to the tweet page for cookie context
|
|
await page.goto(`https://x.com/i/status/${tweetId}`);
|
|
await page.wait(3);
|
|
const queryId = await resolveTwitterQueryId(page, 'TweetResultByRestId', TWEET_RESULT_BY_REST_ID_QUERY_ID);
|
|
|
|
const result = await page.evaluate(`
|
|
async () => {
|
|
const tweetId = "${tweetId}";
|
|
const ct0 = document.cookie.split(';').map(c=>c.trim()).find(c=>c.startsWith('ct0='))?.split('=')[1];
|
|
if (!ct0) return {error: 'No ct0 cookie — not logged into x.com'};
|
|
|
|
const bearer = 'AAAAAAAAAAAAAAAAAAAAANRILgAAAAAAnNwIzUejRCOuH5E6I8xnZz4puTs%3D1Zv7ttfk8LF81IUq16cHjhLTvJu4FA33AGWWjCpTnA';
|
|
const headers = {
|
|
'Authorization': 'Bearer ' + decodeURIComponent(bearer),
|
|
'X-Csrf-Token': ct0,
|
|
'X-Twitter-Auth-Type': 'OAuth2Session',
|
|
'X-Twitter-Active-User': 'yes'
|
|
};
|
|
|
|
const variables = JSON.stringify({
|
|
tweetId: tweetId,
|
|
withCommunity: false,
|
|
includePromotedContent: false,
|
|
withVoice: false,
|
|
});
|
|
const features = JSON.stringify({
|
|
longform_notetweets_consumption_enabled: true,
|
|
responsive_web_twitter_article_tweet_consumption_enabled: true,
|
|
longform_notetweets_rich_text_read_enabled: true,
|
|
longform_notetweets_inline_media_enabled: true,
|
|
articles_preview_enabled: true,
|
|
responsive_web_graphql_exclude_directive_enabled: true,
|
|
verified_phone_label_enabled: false,
|
|
});
|
|
const fieldToggles = JSON.stringify({
|
|
withArticleRichContentState: true,
|
|
withArticlePlainText: true,
|
|
});
|
|
|
|
const url = '/i/api/graphql/' + ${JSON.stringify(queryId)} + '/TweetResultByRestId?variables='
|
|
+ encodeURIComponent(variables)
|
|
+ '&features=' + encodeURIComponent(features)
|
|
+ '&fieldToggles=' + encodeURIComponent(fieldToggles);
|
|
|
|
const resp = await fetch(url, {headers, credentials: 'include'});
|
|
if (!resp.ok) return {error: 'HTTP ' + resp.status, hint: 'Tweet may not exist or queryId expired'};
|
|
const d = await resp.json();
|
|
|
|
const result = d.data?.tweetResult?.result;
|
|
if (!result) return {error: 'Article not found'};
|
|
|
|
// Unwrap TweetWithVisibilityResults
|
|
const tw = result.tweet || result;
|
|
const legacy = tw.legacy || {};
|
|
const user = tw.core?.user_results?.result;
|
|
const screenName = user?.legacy?.screen_name || user?.core?.screen_name || 'unknown';
|
|
|
|
// Extract article content
|
|
const articleResults = tw.article?.article_results?.result;
|
|
if (!articleResults) {
|
|
// Fallback: return note_tweet text if present
|
|
const noteText = tw.note_tweet?.note_tweet_results?.result?.text;
|
|
if (noteText) {
|
|
return [{
|
|
title: '(Note Tweet)',
|
|
author: screenName,
|
|
content: noteText,
|
|
url: 'https://x.com/' + screenName + '/status/' + tweetId,
|
|
}];
|
|
}
|
|
return {error: 'Tweet ' + tweetId + ' has no article content'};
|
|
}
|
|
|
|
const title = articleResults.title || '(Untitled)';
|
|
const contentState = articleResults.content_state || {};
|
|
const blocks = contentState.blocks || [];
|
|
|
|
// Convert draft.js blocks to Markdown
|
|
const parts = [];
|
|
let orderedCounter = 0;
|
|
for (const block of blocks) {
|
|
const blockType = block.type || 'unstyled';
|
|
if (blockType === 'atomic') continue;
|
|
const text = block.text || '';
|
|
if (!text) continue;
|
|
if (blockType !== 'ordered-list-item') orderedCounter = 0;
|
|
|
|
if (blockType === 'header-one') parts.push('# ' + text);
|
|
else if (blockType === 'header-two') parts.push('## ' + text);
|
|
else if (blockType === 'header-three') parts.push('### ' + text);
|
|
else if (blockType === 'blockquote') parts.push('> ' + text);
|
|
else if (blockType === 'unordered-list-item') parts.push('- ' + text);
|
|
else if (blockType === 'ordered-list-item') {
|
|
orderedCounter++;
|
|
parts.push(orderedCounter + '. ' + text);
|
|
}
|
|
else if (blockType === 'code-block') parts.push('\`\`\`\\n' + text + '\\n\`\`\`');
|
|
else parts.push(text);
|
|
}
|
|
|
|
return [{
|
|
title,
|
|
author: screenName,
|
|
content: parts.join('\\n\\n') || legacy.full_text || '',
|
|
url: 'https://x.com/' + screenName + '/status/' + tweetId,
|
|
}];
|
|
}
|
|
`);
|
|
|
|
if (result?.error) {
|
|
if (String(result.error).includes('No ct0 cookie')) throw new AuthRequiredError('x.com', result.error);
|
|
throw new CommandExecutionError(result.error + (result.hint ? ` (${result.hint})` : ''));
|
|
}
|
|
|
|
return result || [];
|
|
}
|
|
});
|