Files
jackwener__opencli/clis/twitter/article.ts
jakevin 80eef46b4e refactor: monorepo adapter separation (clis/ at root) (#782)
* refactor: move adapters from src/clis/ to root clis/ for monorepo separation

Separates CLI adapters from the core runtime to prepare for independent
adapter distribution via postinstall fetch.

Key changes:
- Move src/clis/ → clis/ (adapters at repo root)
- Change tsconfig rootDir from "src" to "." so tsc compiles both
- Create root-level shim files (registry.ts, errors.ts, etc.) so adapter
  relative imports (../../registry.js) resolve correctly
- Update build-manifest.ts, main.ts paths for new dist/src/ structure
- Expand ensureUserCliCompatShims() to cover all adapter import targets
  (types, utils, logger, launcher, browser/*, download/*, pipeline/*)
- Add scripts/fetch-adapters.js postinstall for ~/.opencli/clis/ sync
- Update vitest.config.ts adapter test paths
- Add package.json files field to exclude adapters from npm package

Official adapter files are unconditionally overwritten on update;
user-created files not in the manifest are preserved.

* fix: add dist/clis/ and cli-manifest.json to npm files, harden fetch-adapters

- Add dist/clis/ and dist/cli-manifest.json to package.json files field
  so built-in adapters and manifest ship with the npm package
- Replace execSync with execFileSync to prevent command injection
- Add version check to skip redundant adapter fetches
- Track tmpRoot explicitly for reliable cleanup

* fix: address review blockers — manifest-based updates, global-only fetch, first-run fallback

1. Manifest-based update strategy:
   - Read old manifest to identify previously-official files
   - Clean up files removed upstream (in old manifest but not new)
   - User-created files (never in any manifest) remain untouched

2. Only run fetch-adapters on global install (npm_config_global=true)
   or explicit OPENCLI_FETCH=1, preventing heavy side effects for
   local/dev installs

3. First-run fallback in discovery.ts:
   - ensureUserAdapters() checks for adapter-manifest.json
   - If missing and ~/.opencli/clis/ is empty, spawns fetch-adapters.js
   - Guarantees adapters are available even with --ignore-scripts

* fix: remove OPENCLI_FETCH env var, use internal _OPENCLI_FIRST_RUN instead

* feat: also support OPENCLI_FETCH=1 for explicit adapter fetch trigger

* simplify: replace git clone with local copy from dist/clis/

Adapters already ship in the npm package (dist/clis/), so there's no
need to clone from GitHub. Copy directly from the installed package:

- Eliminates git, curl, tar dependencies
- No network calls in postinstall
- No timeout/offline issues
- Version always matches the installed CLI
- ~65 lines of clone/download code replaced by one cpSync loop
2026-04-05 01:46:36 +08:00

171 lines
6.9 KiB
TypeScript

import { AuthRequiredError, CommandExecutionError } from '../../errors.js';
import { cli, Strategy } from '../../registry.js';
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 || [];
}
});