@jackwener/opencli 0.6.3 → 0.7.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (81) hide show
  1. package/LICENSE +190 -28
  2. package/README.md +4 -4
  3. package/README.zh-CN.md +4 -4
  4. package/SKILL.md +22 -6
  5. package/dist/browser.js +2 -3
  6. package/dist/build-manifest.js +2 -0
  7. package/dist/cli-manifest.json +604 -24
  8. package/dist/clis/reddit/comment.d.ts +1 -0
  9. package/dist/clis/reddit/comment.js +57 -0
  10. package/dist/clis/reddit/popular.yaml +40 -0
  11. package/dist/clis/reddit/read.yaml +76 -0
  12. package/dist/clis/reddit/save.d.ts +1 -0
  13. package/dist/clis/reddit/save.js +51 -0
  14. package/dist/clis/reddit/saved.d.ts +1 -0
  15. package/dist/clis/reddit/saved.js +46 -0
  16. package/dist/clis/reddit/search.yaml +37 -11
  17. package/dist/clis/reddit/subreddit.yaml +14 -4
  18. package/dist/clis/reddit/subscribe.d.ts +1 -0
  19. package/dist/clis/reddit/subscribe.js +50 -0
  20. package/dist/clis/reddit/upvote.d.ts +1 -0
  21. package/dist/clis/reddit/upvote.js +64 -0
  22. package/dist/clis/reddit/upvoted.d.ts +1 -0
  23. package/dist/clis/reddit/upvoted.js +46 -0
  24. package/dist/clis/reddit/user-comments.yaml +45 -0
  25. package/dist/clis/reddit/user-posts.yaml +43 -0
  26. package/dist/clis/reddit/user.yaml +39 -0
  27. package/dist/clis/twitter/article.d.ts +1 -0
  28. package/dist/clis/twitter/article.js +157 -0
  29. package/dist/clis/twitter/bookmark.d.ts +1 -0
  30. package/dist/clis/twitter/bookmark.js +63 -0
  31. package/dist/clis/twitter/follow.d.ts +1 -0
  32. package/dist/clis/twitter/follow.js +65 -0
  33. package/dist/clis/twitter/profile.js +110 -42
  34. package/dist/clis/twitter/thread.d.ts +1 -0
  35. package/dist/clis/twitter/thread.js +150 -0
  36. package/dist/clis/twitter/unbookmark.d.ts +1 -0
  37. package/dist/clis/twitter/unbookmark.js +62 -0
  38. package/dist/clis/twitter/unfollow.d.ts +1 -0
  39. package/dist/clis/twitter/unfollow.js +71 -0
  40. package/dist/engine.js +2 -1
  41. package/dist/main.js +41 -10
  42. package/dist/output.js +2 -1
  43. package/dist/registry.d.ts +2 -8
  44. package/dist/snapshotFormatter.d.ts +9 -0
  45. package/dist/snapshotFormatter.js +352 -15
  46. package/dist/snapshotFormatter.test.d.ts +7 -0
  47. package/dist/snapshotFormatter.test.js +521 -0
  48. package/dist/validate.d.ts +14 -2
  49. package/dist/verify.d.ts +14 -2
  50. package/package.json +2 -2
  51. package/src/browser.ts +2 -4
  52. package/src/build-manifest.ts +3 -0
  53. package/src/clis/reddit/comment.ts +60 -0
  54. package/src/clis/reddit/popular.yaml +40 -0
  55. package/src/clis/reddit/read.yaml +76 -0
  56. package/src/clis/reddit/save.ts +54 -0
  57. package/src/clis/reddit/saved.ts +48 -0
  58. package/src/clis/reddit/search.yaml +37 -11
  59. package/src/clis/reddit/subreddit.yaml +14 -4
  60. package/src/clis/reddit/subscribe.ts +53 -0
  61. package/src/clis/reddit/upvote.ts +67 -0
  62. package/src/clis/reddit/upvoted.ts +48 -0
  63. package/src/clis/reddit/user-comments.yaml +45 -0
  64. package/src/clis/reddit/user-posts.yaml +43 -0
  65. package/src/clis/reddit/user.yaml +39 -0
  66. package/src/clis/twitter/article.ts +161 -0
  67. package/src/clis/twitter/bookmark.ts +67 -0
  68. package/src/clis/twitter/follow.ts +69 -0
  69. package/src/clis/twitter/profile.ts +113 -45
  70. package/src/clis/twitter/thread.ts +181 -0
  71. package/src/clis/twitter/unbookmark.ts +66 -0
  72. package/src/clis/twitter/unfollow.ts +75 -0
  73. package/src/engine.ts +4 -1
  74. package/src/main.ts +34 -7
  75. package/src/output.ts +2 -1
  76. package/src/registry.ts +2 -8
  77. package/src/snapshotFormatter.test.ts +579 -0
  78. package/src/snapshotFormatter.ts +399 -13
  79. package/src/validate.ts +19 -4
  80. package/src/verify.ts +17 -3
  81. package/vitest.config.ts +15 -1
@@ -0,0 +1,39 @@
1
+ site: reddit
2
+ name: user
3
+ description: View a Reddit user profile
4
+ domain: reddit.com
5
+ strategy: cookie
6
+ browser: true
7
+
8
+ args:
9
+ username:
10
+ type: string
11
+ required: true
12
+
13
+ columns: [field, value]
14
+
15
+ pipeline:
16
+ - navigate: https://www.reddit.com
17
+ - evaluate: |
18
+ (async () => {
19
+ const username = ${{ args.username | json }};
20
+ const name = username.startsWith('u/') ? username.slice(2) : username;
21
+ const res = await fetch('/user/' + name + '/about.json?raw_json=1', {
22
+ credentials: 'include'
23
+ });
24
+ const d = await res.json();
25
+ const u = d?.data || d || {};
26
+ const created = u.created_utc ? new Date(u.created_utc * 1000).toISOString().split('T')[0] : '-';
27
+ return [
28
+ { field: 'Username', value: 'u/' + (u.name || name) },
29
+ { field: 'Post Karma', value: String(u.link_karma || 0) },
30
+ { field: 'Comment Karma', value: String(u.comment_karma || 0) },
31
+ { field: 'Total Karma', value: String(u.total_karma || (u.link_karma||0) + (u.comment_karma||0)) },
32
+ { field: 'Account Created', value: created },
33
+ { field: 'Gold', value: u.is_gold ? '⭐ Yes' : 'No' },
34
+ { field: 'Verified', value: u.verified ? '✅ Yes' : 'No' },
35
+ ];
36
+ })()
37
+ - map:
38
+ field: ${{ item.field }}
39
+ value: ${{ item.value }}
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,157 @@
1
+ import { cli, Strategy } from '../../registry.js';
2
+ cli({
3
+ site: 'twitter',
4
+ name: 'article',
5
+ description: 'Fetch a Twitter Article (long-form content) and export as Markdown',
6
+ domain: 'x.com',
7
+ strategy: Strategy.COOKIE,
8
+ browser: true,
9
+ args: [
10
+ { name: 'tweet_id', type: 'string', positional: true, required: true, help: 'Tweet ID or URL containing the article' },
11
+ ],
12
+ columns: ['title', 'author', 'content', 'url'],
13
+ func: async (page, kwargs) => {
14
+ // Extract tweet ID from URL if needed
15
+ let tweetId = kwargs.tweet_id;
16
+ const urlMatch = tweetId.match(/\/(?:status|article)\/(\d+)/);
17
+ if (urlMatch)
18
+ tweetId = urlMatch[1];
19
+ // Navigate to the tweet page for cookie context
20
+ await page.goto(`https://x.com/i/status/${tweetId}`);
21
+ await page.wait(3);
22
+ const result = await page.evaluate(`
23
+ async () => {
24
+ const tweetId = "${tweetId}";
25
+ const ct0 = document.cookie.split(';').map(c=>c.trim()).find(c=>c.startsWith('ct0='))?.split('=')[1];
26
+ if (!ct0) return {error: 'No ct0 cookie — not logged into x.com'};
27
+
28
+ const bearer = 'AAAAAAAAAAAAAAAAAAAAANRILgAAAAAAnNwIzUejRCOuH5E6I8xnZz4puTs%3D1Zv7ttfk8LF81IUq16cHjhLTvJu4FA33AGWWjCpTnA';
29
+ const headers = {
30
+ 'Authorization': 'Bearer ' + decodeURIComponent(bearer),
31
+ 'X-Csrf-Token': ct0,
32
+ 'X-Twitter-Auth-Type': 'OAuth2Session',
33
+ 'X-Twitter-Active-User': 'yes'
34
+ };
35
+
36
+ const variables = JSON.stringify({
37
+ tweetId: tweetId,
38
+ withCommunity: false,
39
+ includePromotedContent: false,
40
+ withVoice: false,
41
+ });
42
+ const features = JSON.stringify({
43
+ longform_notetweets_consumption_enabled: true,
44
+ responsive_web_twitter_article_tweet_consumption_enabled: true,
45
+ longform_notetweets_rich_text_read_enabled: true,
46
+ longform_notetweets_inline_media_enabled: true,
47
+ articles_preview_enabled: true,
48
+ responsive_web_graphql_exclude_directive_enabled: true,
49
+ verified_phone_label_enabled: false,
50
+ });
51
+ const fieldToggles = JSON.stringify({
52
+ withArticleRichContentState: true,
53
+ withArticlePlainText: true,
54
+ });
55
+
56
+ // Dynamically resolve queryId: GitHub community source → JS bundle scan → hardcoded fallback
57
+ async function resolveQueryId(operationName, fallbackId) {
58
+ try {
59
+ const ghResp = await fetch('https://raw.githubusercontent.com/fa0311/twitter-openapi/refs/heads/main/src/config/placeholder.json');
60
+ if (ghResp.ok) {
61
+ const data = await ghResp.json();
62
+ const entry = data[operationName];
63
+ if (entry && entry.queryId) return entry.queryId;
64
+ }
65
+ } catch {}
66
+ try {
67
+ const scripts = performance.getEntriesByType('resource')
68
+ .filter(r => r.name.includes('client-web') && r.name.endsWith('.js'))
69
+ .map(r => r.name);
70
+ for (const scriptUrl of scripts.slice(0, 15)) {
71
+ try {
72
+ const text = await (await fetch(scriptUrl)).text();
73
+ const re = new RegExp('queryId:"([A-Za-z0-9_-]+)"[^}]{0,200}operationName:"' + operationName + '"');
74
+ const m = text.match(re);
75
+ if (m) return m[1];
76
+ } catch {}
77
+ }
78
+ } catch {}
79
+ return fallbackId;
80
+ }
81
+
82
+ const queryId = await resolveQueryId('TweetResultByRestId', '7xflPyRiUxGVbJd4uWmbfg');
83
+ const url = '/i/api/graphql/' + queryId + '/TweetResultByRestId?variables='
84
+ + encodeURIComponent(variables)
85
+ + '&features=' + encodeURIComponent(features)
86
+ + '&fieldToggles=' + encodeURIComponent(fieldToggles);
87
+
88
+ const resp = await fetch(url, {headers, credentials: 'include'});
89
+ if (!resp.ok) return {error: 'HTTP ' + resp.status, hint: 'Tweet may not exist or queryId expired'};
90
+ const d = await resp.json();
91
+
92
+ const result = d.data?.tweetResult?.result;
93
+ if (!result) return {error: 'Article not found'};
94
+
95
+ // Unwrap TweetWithVisibilityResults
96
+ const tw = result.tweet || result;
97
+ const legacy = tw.legacy || {};
98
+ const user = tw.core?.user_results?.result;
99
+ const screenName = user?.legacy?.screen_name || user?.core?.screen_name || 'unknown';
100
+
101
+ // Extract article content
102
+ const articleResults = tw.article?.article_results?.result;
103
+ if (!articleResults) {
104
+ // Fallback: return note_tweet text if present
105
+ const noteText = tw.note_tweet?.note_tweet_results?.result?.text;
106
+ if (noteText) {
107
+ return [{
108
+ title: '(Note Tweet)',
109
+ author: screenName,
110
+ content: noteText,
111
+ url: 'https://x.com/' + screenName + '/status/' + tweetId,
112
+ }];
113
+ }
114
+ return {error: 'Tweet ' + tweetId + ' has no article content'};
115
+ }
116
+
117
+ const title = articleResults.title || '(Untitled)';
118
+ const contentState = articleResults.content_state || {};
119
+ const blocks = contentState.blocks || [];
120
+
121
+ // Convert draft.js blocks to Markdown
122
+ const parts = [];
123
+ let orderedCounter = 0;
124
+ for (const block of blocks) {
125
+ const blockType = block.type || 'unstyled';
126
+ if (blockType === 'atomic') continue;
127
+ const text = block.text || '';
128
+ if (!text) continue;
129
+ if (blockType !== 'ordered-list-item') orderedCounter = 0;
130
+
131
+ if (blockType === 'header-one') parts.push('# ' + text);
132
+ else if (blockType === 'header-two') parts.push('## ' + text);
133
+ else if (blockType === 'header-three') parts.push('### ' + text);
134
+ else if (blockType === 'blockquote') parts.push('> ' + text);
135
+ else if (blockType === 'unordered-list-item') parts.push('- ' + text);
136
+ else if (blockType === 'ordered-list-item') {
137
+ orderedCounter++;
138
+ parts.push(orderedCounter + '. ' + text);
139
+ }
140
+ else if (blockType === 'code-block') parts.push('\`\`\`\\n' + text + '\\n\`\`\`');
141
+ else parts.push(text);
142
+ }
143
+
144
+ return [{
145
+ title,
146
+ author: screenName,
147
+ content: parts.join('\\n\\n') || legacy.full_text || '',
148
+ url: 'https://x.com/' + screenName + '/status/' + tweetId,
149
+ }];
150
+ }
151
+ `);
152
+ if (result?.error) {
153
+ throw new Error(result.error + (result.hint ? ` (${result.hint})` : ''));
154
+ }
155
+ return result || [];
156
+ }
157
+ });
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,63 @@
1
+ import { cli, Strategy } from '../../registry.js';
2
+ cli({
3
+ site: 'twitter',
4
+ name: 'bookmark',
5
+ description: 'Bookmark a tweet',
6
+ domain: 'x.com',
7
+ strategy: Strategy.UI,
8
+ browser: true,
9
+ args: [
10
+ { name: 'url', type: 'string', positional: true, required: true, help: 'Tweet URL to bookmark' },
11
+ ],
12
+ columns: ['status', 'message'],
13
+ func: async (page, kwargs) => {
14
+ if (!page)
15
+ throw new Error('Requires browser');
16
+ await page.goto(kwargs.url);
17
+ await page.wait(5);
18
+ const result = await page.evaluate(`(async () => {
19
+ try {
20
+ let attempts = 0;
21
+ let bookmarkBtn = null;
22
+ let removeBtn = null;
23
+
24
+ while (attempts < 20) {
25
+ // Check if already bookmarked
26
+ removeBtn = document.querySelector('[data-testid="removeBookmark"]');
27
+ if (removeBtn) {
28
+ return { ok: true, message: 'Tweet is already bookmarked.' };
29
+ }
30
+
31
+ bookmarkBtn = document.querySelector('[data-testid="bookmark"]');
32
+ if (bookmarkBtn) break;
33
+
34
+ await new Promise(r => setTimeout(r, 500));
35
+ attempts++;
36
+ }
37
+
38
+ if (!bookmarkBtn) {
39
+ return { ok: false, message: 'Could not find Bookmark button. Are you logged in?' };
40
+ }
41
+
42
+ bookmarkBtn.click();
43
+ await new Promise(r => setTimeout(r, 1000));
44
+
45
+ // Verify
46
+ const verify = document.querySelector('[data-testid="removeBookmark"]');
47
+ if (verify) {
48
+ return { ok: true, message: 'Tweet successfully bookmarked.' };
49
+ } else {
50
+ return { ok: false, message: 'Bookmark action initiated but UI did not update.' };
51
+ }
52
+ } catch (e) {
53
+ return { ok: false, message: e.toString() };
54
+ }
55
+ })()`);
56
+ if (result.ok)
57
+ await page.wait(2);
58
+ return [{
59
+ status: result.ok ? 'success' : 'failed',
60
+ message: result.message
61
+ }];
62
+ }
63
+ });
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,65 @@
1
+ import { cli, Strategy } from '../../registry.js';
2
+ cli({
3
+ site: 'twitter',
4
+ name: 'follow',
5
+ description: 'Follow a Twitter user',
6
+ domain: 'x.com',
7
+ strategy: Strategy.UI,
8
+ browser: true,
9
+ args: [
10
+ { name: 'username', type: 'string', positional: true, required: true, help: 'Twitter screen name (without @)' },
11
+ ],
12
+ columns: ['status', 'message'],
13
+ func: async (page, kwargs) => {
14
+ if (!page)
15
+ throw new Error('Requires browser');
16
+ const username = kwargs.username.replace(/^@/, '');
17
+ await page.goto(`https://x.com/${username}`);
18
+ await page.wait(5);
19
+ const result = await page.evaluate(`(async () => {
20
+ try {
21
+ let attempts = 0;
22
+ let followBtn = null;
23
+ let unfollowTestId = null;
24
+
25
+ while (attempts < 20) {
26
+ // Check if already following (button shows screen_name-unfollow)
27
+ unfollowTestId = document.querySelector('[data-testid$="-unfollow"]');
28
+ if (unfollowTestId) {
29
+ return { ok: true, message: 'Already following @${username}.' };
30
+ }
31
+
32
+ // Look for the Follow button
33
+ followBtn = document.querySelector('[data-testid$="-follow"]');
34
+ if (followBtn) break;
35
+
36
+ await new Promise(r => setTimeout(r, 500));
37
+ attempts++;
38
+ }
39
+
40
+ if (!followBtn) {
41
+ return { ok: false, message: 'Could not find Follow button. Are you logged in?' };
42
+ }
43
+
44
+ followBtn.click();
45
+ await new Promise(r => setTimeout(r, 1500));
46
+
47
+ // Verify
48
+ const verify = document.querySelector('[data-testid$="-unfollow"]');
49
+ if (verify) {
50
+ return { ok: true, message: 'Successfully followed @${username}.' };
51
+ } else {
52
+ return { ok: false, message: 'Follow action initiated but UI did not update.' };
53
+ }
54
+ } catch (e) {
55
+ return { ok: false, message: e.toString() };
56
+ }
57
+ })()`);
58
+ if (result.ok)
59
+ await page.wait(2);
60
+ return [{
61
+ status: result.ok ? 'success' : 'failed',
62
+ message: result.message
63
+ }];
64
+ }
65
+ });
@@ -2,55 +2,123 @@ import { cli, Strategy } from '../../registry.js';
2
2
  cli({
3
3
  site: 'twitter',
4
4
  name: 'profile',
5
- description: 'Fetch tweets from a user profile',
5
+ description: 'Fetch a Twitter user profile (bio, stats, etc.)',
6
6
  domain: 'x.com',
7
- strategy: Strategy.INTERCEPT,
7
+ strategy: Strategy.COOKIE,
8
8
  browser: true,
9
9
  args: [
10
- { name: 'username', type: 'string', required: true },
11
- { name: 'limit', type: 'int', default: 15 },
10
+ { name: 'username', type: 'string', positional: true, help: 'Twitter screen name (without @). Defaults to logged-in user.' },
12
11
  ],
13
- columns: ['id', 'text', 'likes', 'views', 'url'],
12
+ columns: ['screen_name', 'name', 'bio', 'location', 'url', 'followers', 'following', 'tweets', 'likes', 'verified', 'created_at'],
14
13
  func: async (page, kwargs) => {
15
- // Navigate to user profile via search for reliability
16
- await page.goto(`https://x.com/search?q=from:${kwargs.username}&f=live`);
17
- await page.wait(5);
18
- // Inject XHR interceptor
19
- await page.installInterceptor('SearchTimeline');
20
- // Trigger API by scrolling
21
- await page.autoScroll({ times: 3, delayMs: 2000 });
22
- // Retrieve data
23
- const requests = await page.getInterceptedRequests();
24
- if (!requests || requests.length === 0)
25
- return [];
26
- let results = [];
27
- for (const req of requests) {
28
- try {
29
- const insts = req.data.data.search_by_raw_query.search_timeline.timeline.instructions;
30
- const addEntries = insts.find((i) => i.type === 'TimelineAddEntries');
31
- if (!addEntries)
32
- continue;
33
- for (const entry of addEntries.entries) {
34
- if (!entry.entryId.startsWith('tweet-'))
35
- continue;
36
- let tweet = entry.content?.itemContent?.tweet_results?.result;
37
- if (!tweet)
38
- continue;
39
- if (tweet.__typename === 'TweetWithVisibilityResults' && tweet.tweet) {
40
- tweet = tweet.tweet;
41
- }
42
- results.push({
43
- id: tweet.rest_id,
44
- text: tweet.legacy?.full_text || '',
45
- likes: tweet.legacy?.favorite_count || 0,
46
- views: tweet.views?.count || '0',
47
- url: `https://x.com/i/status/${tweet.rest_id}`
48
- });
49
- }
14
+ let username = (kwargs.username || '').replace(/^@/, '');
15
+ // If no username, detect the logged-in user
16
+ if (!username) {
17
+ await page.goto('https://x.com/home');
18
+ await page.wait(5);
19
+ const href = await page.evaluate(`() => {
20
+ const link = document.querySelector('a[data-testid="AppTabBar_Profile_Link"]');
21
+ return link ? link.getAttribute('href') : null;
22
+ }`);
23
+ if (!href)
24
+ throw new Error('Could not detect logged-in user. Are you logged in?');
25
+ username = href.replace('/', '');
26
+ }
27
+ // Navigate directly to the user's profile page (gives us cookie context)
28
+ await page.goto(`https://x.com/${username}`);
29
+ await page.wait(3);
30
+ const result = await page.evaluate(`
31
+ async () => {
32
+ const screenName = "${username}";
33
+ const ct0 = document.cookie.split(';').map(c=>c.trim()).find(c=>c.startsWith('ct0='))?.split('=')[1];
34
+ if (!ct0) return {error: 'No ct0 cookie — not logged into x.com'};
35
+
36
+ const bearer = 'AAAAAAAAAAAAAAAAAAAAANRILgAAAAAAnNwIzUejRCOuH5E6I8xnZz4puTs%3D1Zv7ttfk8LF81IUq16cHjhLTvJu4FA33AGWWjCpTnA';
37
+ const headers = {
38
+ 'Authorization': 'Bearer ' + decodeURIComponent(bearer),
39
+ 'X-Csrf-Token': ct0,
40
+ 'X-Twitter-Auth-Type': 'OAuth2Session',
41
+ 'X-Twitter-Active-User': 'yes'
42
+ };
43
+
44
+ const variables = JSON.stringify({
45
+ screen_name: screenName,
46
+ withSafetyModeUserFields: true,
47
+ });
48
+ const features = JSON.stringify({
49
+ hidden_profile_subscriptions_enabled: true,
50
+ rweb_tipjar_consumption_enabled: true,
51
+ responsive_web_graphql_exclude_directive_enabled: true,
52
+ verified_phone_label_enabled: false,
53
+ subscriptions_verification_info_is_identity_verified_enabled: true,
54
+ subscriptions_verification_info_verified_since_enabled: true,
55
+ highlights_tweets_tab_ui_enabled: true,
56
+ responsive_web_twitter_article_notes_tab_enabled: true,
57
+ subscriptions_feature_can_gift_premium: true,
58
+ creator_subscriptions_tweet_preview_api_enabled: true,
59
+ responsive_web_graphql_skip_user_profile_image_extensions_enabled: false,
60
+ responsive_web_graphql_timeline_navigation_enabled: true,
61
+ });
62
+
63
+ // Dynamically resolve queryId: GitHub community source → JS bundle scan → hardcoded fallback
64
+ async function resolveQueryId(operationName, fallbackId) {
65
+ try {
66
+ const ghResp = await fetch('https://raw.githubusercontent.com/fa0311/twitter-openapi/refs/heads/main/src/config/placeholder.json');
67
+ if (ghResp.ok) {
68
+ const data = await ghResp.json();
69
+ const entry = data[operationName];
70
+ if (entry && entry.queryId) return entry.queryId;
50
71
  }
51
- catch (e) {
72
+ } catch {}
73
+ try {
74
+ const scripts = performance.getEntriesByType('resource')
75
+ .filter(r => r.name.includes('client-web') && r.name.endsWith('.js'))
76
+ .map(r => r.name);
77
+ for (const scriptUrl of scripts.slice(0, 15)) {
78
+ try {
79
+ const text = await (await fetch(scriptUrl)).text();
80
+ const re = new RegExp('queryId:"([A-Za-z0-9_-]+)"[^}]{0,200}operationName:"' + operationName + '"');
81
+ const m = text.match(re);
82
+ if (m) return m[1];
83
+ } catch {}
52
84
  }
85
+ } catch {}
86
+ return fallbackId;
87
+ }
88
+
89
+ const queryId = await resolveQueryId('UserByScreenName', 'qRednkZG-rn1P6b48NINmQ');
90
+ const url = '/i/api/graphql/' + queryId + '/UserByScreenName?variables='
91
+ + encodeURIComponent(variables)
92
+ + '&features=' + encodeURIComponent(features);
93
+
94
+ const resp = await fetch(url, {headers, credentials: 'include'});
95
+ if (!resp.ok) return {error: 'HTTP ' + resp.status, hint: 'User may not exist or queryId expired'};
96
+ const d = await resp.json();
97
+
98
+ const result = d.data?.user?.result;
99
+ if (!result) return {error: 'User @' + screenName + ' not found'};
100
+
101
+ const legacy = result.legacy || {};
102
+ const expandedUrl = legacy.entities?.url?.urls?.[0]?.expanded_url || '';
103
+
104
+ return [{
105
+ screen_name: legacy.screen_name || screenName,
106
+ name: legacy.name || '',
107
+ bio: legacy.description || '',
108
+ location: legacy.location || '',
109
+ url: expandedUrl,
110
+ followers: legacy.followers_count || 0,
111
+ following: legacy.friends_count || 0,
112
+ tweets: legacy.statuses_count || 0,
113
+ likes: legacy.favourites_count || 0,
114
+ verified: result.is_blue_verified || legacy.verified || false,
115
+ created_at: legacy.created_at || '',
116
+ }];
117
+ }
118
+ `);
119
+ if (result?.error) {
120
+ throw new Error(result.error + (result.hint ? ` (${result.hint})` : ''));
53
121
  }
54
- return results.slice(0, kwargs.limit);
122
+ return result || [];
55
123
  }
56
124
  });
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,150 @@
1
+ import { cli, Strategy } from '../../registry.js';
2
+ // ── Twitter GraphQL constants ──────────────────────────────────────────
3
+ const BEARER_TOKEN = 'AAAAAAAAAAAAAAAAAAAAANRILgAAAAAAnNwIzUejRCOuH5E6I8xnZz4puTs%3D1Zv7ttfk8LF81IUq16cHjhLTvJu4FA33AGWWjCpTnA';
4
+ const TWEET_DETAIL_QUERY_ID = 'nBS-WpgA6ZG0CyNHD517JQ';
5
+ const FEATURES = {
6
+ responsive_web_graphql_exclude_directive_enabled: true,
7
+ verified_phone_label_enabled: false,
8
+ creator_subscriptions_tweet_preview_api_enabled: true,
9
+ responsive_web_graphql_timeline_navigation_enabled: true,
10
+ responsive_web_graphql_skip_user_profile_image_extensions_enabled: false,
11
+ longform_notetweets_consumption_enabled: true,
12
+ longform_notetweets_rich_text_read_enabled: true,
13
+ longform_notetweets_inline_media_enabled: true,
14
+ freedom_of_speech_not_reach_fetch_enabled: true,
15
+ };
16
+ const FIELD_TOGGLES = { withArticleRichContentState: true, withArticlePlainText: false };
17
+ function buildTweetDetailUrl(tweetId, cursor) {
18
+ const vars = {
19
+ focalTweetId: tweetId,
20
+ referrer: 'tweet',
21
+ with_rux_injections: false,
22
+ includePromotedContent: false,
23
+ rankingMode: 'Recency',
24
+ withCommunity: true,
25
+ withQuickPromoteEligibilityTweetFields: true,
26
+ withBirdwatchNotes: true,
27
+ withVoice: true,
28
+ };
29
+ if (cursor)
30
+ vars.cursor = cursor;
31
+ return `/i/api/graphql/${TWEET_DETAIL_QUERY_ID}/TweetDetail`
32
+ + `?variables=${encodeURIComponent(JSON.stringify(vars))}`
33
+ + `&features=${encodeURIComponent(JSON.stringify(FEATURES))}`
34
+ + `&fieldToggles=${encodeURIComponent(JSON.stringify(FIELD_TOGGLES))}`;
35
+ }
36
+ function extractTweet(r, seen) {
37
+ if (!r)
38
+ return null;
39
+ const tw = r.tweet || r;
40
+ const l = tw.legacy || {};
41
+ if (!tw.rest_id || seen.has(tw.rest_id))
42
+ return null;
43
+ seen.add(tw.rest_id);
44
+ const u = tw.core?.user_results?.result;
45
+ const noteText = tw.note_tweet?.note_tweet_results?.result?.text;
46
+ const screenName = u?.legacy?.screen_name || u?.core?.screen_name || 'unknown';
47
+ return {
48
+ id: tw.rest_id,
49
+ author: screenName,
50
+ text: noteText || l.full_text || '',
51
+ likes: l.favorite_count || 0,
52
+ retweets: l.retweet_count || 0,
53
+ in_reply_to: l.in_reply_to_status_id_str || undefined,
54
+ created_at: l.created_at,
55
+ url: `https://x.com/${screenName}/status/${tw.rest_id}`,
56
+ };
57
+ }
58
+ function parseTweetDetail(data, seen) {
59
+ const tweets = [];
60
+ let nextCursor = null;
61
+ const instructions = data?.data?.threaded_conversation_with_injections_v2?.instructions
62
+ || data?.data?.tweetResult?.result?.timeline?.instructions
63
+ || [];
64
+ for (const inst of instructions) {
65
+ for (const entry of inst.entries || []) {
66
+ // Cursor entries
67
+ const c = entry.content;
68
+ if (c?.entryType === 'TimelineTimelineCursor' || c?.__typename === 'TimelineTimelineCursor') {
69
+ if (c.cursorType === 'Bottom' || c.cursorType === 'ShowMore')
70
+ nextCursor = c.value;
71
+ continue;
72
+ }
73
+ if (entry.entryId?.startsWith('cursor-bottom-') || entry.entryId?.startsWith('cursor-showMore-')) {
74
+ nextCursor = c?.itemContent?.value || c?.value || nextCursor;
75
+ continue;
76
+ }
77
+ // Direct tweet entry
78
+ const tw = extractTweet(c?.itemContent?.tweet_results?.result, seen);
79
+ if (tw)
80
+ tweets.push(tw);
81
+ // Conversation module (nested replies)
82
+ for (const item of c?.items || []) {
83
+ const nested = extractTweet(item.item?.itemContent?.tweet_results?.result, seen);
84
+ if (nested)
85
+ tweets.push(nested);
86
+ }
87
+ }
88
+ }
89
+ return { tweets, nextCursor };
90
+ }
91
+ // ── CLI definition ────────────────────────────────────────────────────
92
+ cli({
93
+ site: 'twitter',
94
+ name: 'thread',
95
+ description: 'Get a tweet thread (original + all replies)',
96
+ domain: 'x.com',
97
+ strategy: Strategy.COOKIE,
98
+ browser: true,
99
+ args: [
100
+ { name: 'tweet_id', type: 'string', required: true },
101
+ { name: 'limit', type: 'int', default: 50 },
102
+ ],
103
+ columns: ['id', 'author', 'text', 'likes', 'retweets', 'url'],
104
+ func: async (page, kwargs) => {
105
+ let tweetId = kwargs.tweet_id;
106
+ const urlMatch = tweetId.match(/\/status\/(\d+)/);
107
+ if (urlMatch)
108
+ tweetId = urlMatch[1];
109
+ // Navigate to x.com for cookie context
110
+ await page.goto('https://x.com');
111
+ await page.wait(3);
112
+ // Extract CSRF token — the only thing we need from the browser
113
+ const ct0 = await page.evaluate(`() => {
114
+ return document.cookie.split(';').map(c=>c.trim()).find(c=>c.startsWith('ct0='))?.split('=')[1] || null;
115
+ }`);
116
+ if (!ct0)
117
+ throw new Error('Not logged into x.com (no ct0 cookie)');
118
+ // Build auth headers in TypeScript
119
+ const headers = JSON.stringify({
120
+ 'Authorization': `Bearer ${decodeURIComponent(BEARER_TOKEN)}`,
121
+ 'X-Csrf-Token': ct0,
122
+ 'X-Twitter-Auth-Type': 'OAuth2Session',
123
+ 'X-Twitter-Active-User': 'yes',
124
+ });
125
+ // Paginate — fetch in browser, parse in TypeScript
126
+ const allTweets = [];
127
+ const seen = new Set();
128
+ let cursor = null;
129
+ for (let i = 0; i < 5; i++) {
130
+ const apiUrl = buildTweetDetailUrl(tweetId, cursor);
131
+ // Browser-side: just fetch + return JSON (3 lines)
132
+ const data = await page.evaluate(`async () => {
133
+ const r = await fetch("${apiUrl}", { headers: ${headers}, credentials: 'include' });
134
+ return r.ok ? await r.json() : { error: r.status };
135
+ }`);
136
+ if (data?.error) {
137
+ if (allTweets.length === 0)
138
+ throw new Error(`HTTP ${data.error}: Tweet not found or queryId expired`);
139
+ break;
140
+ }
141
+ // TypeScript-side: type-safe parsing + cursor extraction
142
+ const { tweets, nextCursor } = parseTweetDetail(data, seen);
143
+ allTweets.push(...tweets);
144
+ if (!nextCursor || nextCursor === cursor)
145
+ break;
146
+ cursor = nextCursor;
147
+ }
148
+ return allTweets.slice(0, kwargs.limit);
149
+ },
150
+ });
@@ -0,0 +1 @@
1
+ export {};