dp-worker 0.1.24 → 0.1.26
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.
- package/dist/bird/sidecar-entry.js +16 -2
- package/dist/bird/sidecar-entry.js.map +1 -1
- package/dist/chrome-extension/omni-flow/manifest.json +1 -1
- package/node_modules/dp-shared/dist/bird-contracts.d.ts +94 -0
- package/node_modules/dp-shared/dist/bird-contracts.d.ts.map +1 -1
- package/node_modules/dp-shared/dist/bird-contracts.js +108 -0
- package/node_modules/dp-shared/dist/bird-contracts.js.map +1 -1
- package/node_modules/dp-shared/dist/task-history-contracts.d.ts +2 -2
- package/package.json +4 -2
- package/vendor/bird/LICENSE +21 -0
- package/vendor/bird/README.md +116 -0
- package/vendor/bird/dist/cli/pagination.d.ts +35 -0
- package/vendor/bird/dist/cli/pagination.js +43 -0
- package/vendor/bird/dist/cli/program.d.ts +5 -0
- package/vendor/bird/dist/cli/program.js +113 -0
- package/vendor/bird/dist/cli/shared.d.ts +77 -0
- package/vendor/bird/dist/cli/shared.js +327 -0
- package/vendor/bird/dist/cli.d.ts +12 -0
- package/vendor/bird/dist/cli.js +29 -0
- package/vendor/bird/dist/commands/bookmarks.d.ts +4 -0
- package/vendor/bird/dist/commands/bookmarks.js +189 -0
- package/vendor/bird/dist/commands/check.d.ts +4 -0
- package/vendor/bird/dist/commands/check.js +43 -0
- package/vendor/bird/dist/commands/follow.d.ts +4 -0
- package/vendor/bird/dist/commands/follow.js +91 -0
- package/vendor/bird/dist/commands/help.d.ts +4 -0
- package/vendor/bird/dist/commands/help.js +19 -0
- package/vendor/bird/dist/commands/home.d.ts +4 -0
- package/vendor/bird/dist/commands/home.js +43 -0
- package/vendor/bird/dist/commands/lists.d.ts +4 -0
- package/vendor/bird/dist/commands/lists.js +125 -0
- package/vendor/bird/dist/commands/news.d.ts +4 -0
- package/vendor/bird/dist/commands/news.js +131 -0
- package/vendor/bird/dist/commands/post.d.ts +4 -0
- package/vendor/bird/dist/commands/post.js +101 -0
- package/vendor/bird/dist/commands/query-ids.d.ts +4 -0
- package/vendor/bird/dist/commands/query-ids.js +80 -0
- package/vendor/bird/dist/commands/read.d.ts +4 -0
- package/vendor/bird/dist/commands/read.js +152 -0
- package/vendor/bird/dist/commands/search.d.ts +4 -0
- package/vendor/bird/dist/commands/search.js +115 -0
- package/vendor/bird/dist/commands/unbookmark.d.ts +4 -0
- package/vendor/bird/dist/commands/unbookmark.js +36 -0
- package/vendor/bird/dist/commands/user-tweets.d.ts +4 -0
- package/vendor/bird/dist/commands/user-tweets.js +109 -0
- package/vendor/bird/dist/commands/users.d.ts +4 -0
- package/vendor/bird/dist/commands/users.js +295 -0
- package/vendor/bird/dist/index.d.ts +2 -0
- package/vendor/bird/dist/index.js +2 -0
- package/vendor/bird/dist/lib/cli-args.d.ts +7 -0
- package/vendor/bird/dist/lib/cli-args.js +25 -0
- package/vendor/bird/dist/lib/cookies.d.ts +31 -0
- package/vendor/bird/dist/lib/cookies.js +81 -0
- package/vendor/bird/dist/lib/extract-bookmark-folder-id.d.ts +2 -0
- package/vendor/bird/dist/lib/extract-bookmark-folder-id.js +20 -0
- package/vendor/bird/dist/lib/extract-list-id.d.ts +2 -0
- package/vendor/bird/dist/lib/extract-list-id.js +19 -0
- package/vendor/bird/dist/lib/extract-tweet-id.d.ts +2 -0
- package/vendor/bird/dist/lib/extract-tweet-id.js +14 -0
- package/vendor/bird/dist/lib/features.json +17 -0
- package/vendor/bird/dist/lib/index.d.ts +10 -0
- package/vendor/bird/dist/lib/index.js +4 -0
- package/vendor/bird/dist/lib/normalize-handle.d.ts +6 -0
- package/vendor/bird/dist/lib/normalize-handle.js +31 -0
- package/vendor/bird/dist/lib/output.d.ts +29 -0
- package/vendor/bird/dist/lib/output.js +88 -0
- package/vendor/bird/dist/lib/paginate-cursor.d.ts +27 -0
- package/vendor/bird/dist/lib/paginate-cursor.js +37 -0
- package/vendor/bird/dist/lib/query-ids.json +20 -0
- package/vendor/bird/dist/lib/runtime-features.d.ts +19 -0
- package/vendor/bird/dist/lib/runtime-features.js +151 -0
- package/vendor/bird/dist/lib/runtime-query-ids.d.ts +33 -0
- package/vendor/bird/dist/lib/runtime-query-ids.js +264 -0
- package/vendor/bird/dist/lib/thread-filters.d.ts +8 -0
- package/vendor/bird/dist/lib/thread-filters.js +124 -0
- package/vendor/bird/dist/lib/twitter-client-base.d.ts +38 -0
- package/vendor/bird/dist/lib/twitter-client-base.js +129 -0
- package/vendor/bird/dist/lib/twitter-client-bookmarks.d.ts +7 -0
- package/vendor/bird/dist/lib/twitter-client-bookmarks.js +61 -0
- package/vendor/bird/dist/lib/twitter-client-constants.d.ts +43 -0
- package/vendor/bird/dist/lib/twitter-client-constants.js +50 -0
- package/vendor/bird/dist/lib/twitter-client-engagement.d.ts +16 -0
- package/vendor/bird/dist/lib/twitter-client-engagement.js +81 -0
- package/vendor/bird/dist/lib/twitter-client-features.d.ts +14 -0
- package/vendor/bird/dist/lib/twitter-client-features.js +347 -0
- package/vendor/bird/dist/lib/twitter-client-follow.d.ts +8 -0
- package/vendor/bird/dist/lib/twitter-client-follow.js +178 -0
- package/vendor/bird/dist/lib/twitter-client-home.d.ts +13 -0
- package/vendor/bird/dist/lib/twitter-client-home.js +138 -0
- package/vendor/bird/dist/lib/twitter-client-lists.d.ts +11 -0
- package/vendor/bird/dist/lib/twitter-client-lists.js +321 -0
- package/vendor/bird/dist/lib/twitter-client-media.d.ts +11 -0
- package/vendor/bird/dist/lib/twitter-client-media.js +136 -0
- package/vendor/bird/dist/lib/twitter-client-news.d.ts +47 -0
- package/vendor/bird/dist/lib/twitter-client-news.js +283 -0
- package/vendor/bird/dist/lib/twitter-client-posting.d.ts +8 -0
- package/vendor/bird/dist/lib/twitter-client-posting.js +218 -0
- package/vendor/bird/dist/lib/twitter-client-search.d.ts +19 -0
- package/vendor/bird/dist/lib/twitter-client-search.js +157 -0
- package/vendor/bird/dist/lib/twitter-client-timelines.d.ts +23 -0
- package/vendor/bird/dist/lib/twitter-client-timelines.js +471 -0
- package/vendor/bird/dist/lib/twitter-client-tweet-detail.d.ts +25 -0
- package/vendor/bird/dist/lib/twitter-client-tweet-detail.js +295 -0
- package/vendor/bird/dist/lib/twitter-client-types.d.ts +407 -0
- package/vendor/bird/dist/lib/twitter-client-types.js +2 -0
- package/vendor/bird/dist/lib/twitter-client-user-lookup.d.ts +16 -0
- package/vendor/bird/dist/lib/twitter-client-user-lookup.js +224 -0
- package/vendor/bird/dist/lib/twitter-client-user-tweets.d.ts +22 -0
- package/vendor/bird/dist/lib/twitter-client-user-tweets.js +154 -0
- package/vendor/bird/dist/lib/twitter-client-users.d.ts +9 -0
- package/vendor/bird/dist/lib/twitter-client-users.js +358 -0
- package/vendor/bird/dist/lib/twitter-client-utils.d.ts +173 -0
- package/vendor/bird/dist/lib/twitter-client-utils.js +511 -0
- package/vendor/bird/dist/lib/twitter-client.d.ts +23 -0
- package/vendor/bird/dist/lib/twitter-client.js +21 -0
- package/vendor/bird/dist/lib/version.d.ts +6 -0
- package/vendor/bird/dist/lib/version.js +174 -0
|
@@ -0,0 +1,283 @@
|
|
|
1
|
+
import { TWITTER_API_BASE } from './twitter-client-constants.js';
|
|
2
|
+
import { buildExploreFeatures } from './twitter-client-features.js';
|
|
3
|
+
const POST_COUNT_REGEX = /[\d.]+[KMB]?\s*posts?/i;
|
|
4
|
+
const POST_COUNT_MATCH_REGEX = /([\d.]+)([KMB]?)\s*posts?/i;
|
|
5
|
+
// Timeline IDs for different Explore tabs
|
|
6
|
+
const TIMELINE_IDS = {
|
|
7
|
+
forYou: 'VGltZWxpbmU6DAC2CwABAAAAB2Zvcl95b3UAAA==',
|
|
8
|
+
trending: 'VGltZWxpbmU6DAC2CwABAAAACHRyZW5kaW5nAAA=',
|
|
9
|
+
news: 'VGltZWxpbmU6DAC2CwABAAAABG5ld3MAAA==',
|
|
10
|
+
sports: 'VGltZWxpbmU6DAC2CwABAAAABnNwb3J0cwAA',
|
|
11
|
+
entertainment: 'VGltZWxpbmU6DAC2CwABAAAADWVudGVydGFpbm1lbnQAAA==',
|
|
12
|
+
};
|
|
13
|
+
export function withNews(Base) {
|
|
14
|
+
class TwitterClientNews extends Base {
|
|
15
|
+
// biome-ignore lint/complexity/noUselessConstructor lint/suspicious/noExplicitAny: TS mixin constructor requirement.
|
|
16
|
+
constructor(...args) {
|
|
17
|
+
super(...args);
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Fetch news and trending topics from Twitter's Explore page tabs
|
|
21
|
+
*/
|
|
22
|
+
async getNews(count = 10, options = {}) {
|
|
23
|
+
const { includeRaw = false, withTweets = false, tweetsPerItem = 5, aiOnly = false, tabs = ['forYou', 'news', 'sports', 'entertainment'], } = options;
|
|
24
|
+
const debug = process.env.BIRD_DEBUG === '1';
|
|
25
|
+
if (debug) {
|
|
26
|
+
console.error(`[getNews] Fetching from tabs: ${tabs.join(', ')}`);
|
|
27
|
+
}
|
|
28
|
+
const allItems = [];
|
|
29
|
+
const seenHeadlines = new Set();
|
|
30
|
+
// Fetch from each tab
|
|
31
|
+
for (const tab of tabs) {
|
|
32
|
+
const timelineId = TIMELINE_IDS[tab];
|
|
33
|
+
if (!timelineId) {
|
|
34
|
+
continue;
|
|
35
|
+
}
|
|
36
|
+
try {
|
|
37
|
+
const tabItems = await this.fetchTimelineTab(tab, timelineId, count, aiOnly, includeRaw);
|
|
38
|
+
// Deduplicate across tabs
|
|
39
|
+
for (const item of tabItems) {
|
|
40
|
+
if (!seenHeadlines.has(item.headline)) {
|
|
41
|
+
seenHeadlines.add(item.headline);
|
|
42
|
+
allItems.push(item);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
if (debug) {
|
|
46
|
+
console.error(`[getNews] Tab ${tab}: found ${tabItems.length} items, total unique: ${allItems.length}`);
|
|
47
|
+
}
|
|
48
|
+
// Stop early if we have enough
|
|
49
|
+
if (allItems.length >= count) {
|
|
50
|
+
break;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
catch (error) {
|
|
54
|
+
if (debug) {
|
|
55
|
+
console.error(`[getNews] Error fetching tab ${tab}:`, error);
|
|
56
|
+
}
|
|
57
|
+
// Continue with other tabs
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
if (allItems.length === 0) {
|
|
61
|
+
return { success: false, error: 'No news items found' };
|
|
62
|
+
}
|
|
63
|
+
// Limit to requested count
|
|
64
|
+
const items = allItems.slice(0, count);
|
|
65
|
+
if (withTweets) {
|
|
66
|
+
await this.enrichWithTweets(items, tweetsPerItem, includeRaw);
|
|
67
|
+
}
|
|
68
|
+
return { success: true, items };
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Fetch a specific timeline tab using GenericTimelineById
|
|
72
|
+
*/
|
|
73
|
+
async fetchTimelineTab(tabName, timelineId, maxCount, aiOnly, includeRaw) {
|
|
74
|
+
const queryId = await this.getQueryId('GenericTimelineById');
|
|
75
|
+
const features = buildExploreFeatures();
|
|
76
|
+
const variables = {
|
|
77
|
+
timelineId: timelineId,
|
|
78
|
+
count: maxCount * 2, // Fetch more to account for filtering
|
|
79
|
+
includePromotedContent: false,
|
|
80
|
+
};
|
|
81
|
+
const params = new URLSearchParams({
|
|
82
|
+
variables: JSON.stringify(variables),
|
|
83
|
+
features: JSON.stringify(features),
|
|
84
|
+
});
|
|
85
|
+
const url = `${TWITTER_API_BASE}/${queryId}/GenericTimelineById?${params.toString()}`;
|
|
86
|
+
const response = await this.fetchWithTimeout(url, {
|
|
87
|
+
method: 'GET',
|
|
88
|
+
headers: this.getHeaders(),
|
|
89
|
+
});
|
|
90
|
+
if (!response.ok) {
|
|
91
|
+
const text = await response.text();
|
|
92
|
+
throw new Error(`HTTP ${response.status}: ${text.slice(0, 200)}`);
|
|
93
|
+
}
|
|
94
|
+
const data = (await response.json());
|
|
95
|
+
// Debug: save response if BIRD_DEBUG_JSON is set
|
|
96
|
+
if (process.env.BIRD_DEBUG_JSON) {
|
|
97
|
+
const fs = await import('node:fs/promises');
|
|
98
|
+
const debugPath = process.env.BIRD_DEBUG_JSON.replace('.json', `-${tabName}.json`);
|
|
99
|
+
await fs.writeFile(debugPath, JSON.stringify(data, null, 2)).catch(() => { });
|
|
100
|
+
}
|
|
101
|
+
if (data.errors && data.errors.length > 0) {
|
|
102
|
+
throw new Error(data.errors.map((e) => e.message).join('; '));
|
|
103
|
+
}
|
|
104
|
+
// Parse timeline response
|
|
105
|
+
return this.parseTimelineTabItems(data, tabName, maxCount, aiOnly, includeRaw);
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Parse items from a GenericTimelineById response
|
|
109
|
+
*/
|
|
110
|
+
parseTimelineTabItems(
|
|
111
|
+
// biome-ignore lint/suspicious/noExplicitAny: API response structure is complex
|
|
112
|
+
data, source, maxCount, aiOnly, includeRaw) {
|
|
113
|
+
const items = [];
|
|
114
|
+
const seenHeadlines = new Set();
|
|
115
|
+
// Navigate to timeline instructions
|
|
116
|
+
const timeline = data?.data?.timeline?.timeline;
|
|
117
|
+
if (!timeline) {
|
|
118
|
+
return [];
|
|
119
|
+
}
|
|
120
|
+
const instructions = timeline.instructions || [];
|
|
121
|
+
for (const instruction of instructions) {
|
|
122
|
+
const entries = instruction.entries ?? (instruction.entry ? [instruction.entry] : []);
|
|
123
|
+
if (!entries || entries.length === 0) {
|
|
124
|
+
continue;
|
|
125
|
+
}
|
|
126
|
+
for (const entry of entries) {
|
|
127
|
+
if (items.length >= maxCount) {
|
|
128
|
+
break;
|
|
129
|
+
}
|
|
130
|
+
const content = entry.content;
|
|
131
|
+
if (!content) {
|
|
132
|
+
continue;
|
|
133
|
+
}
|
|
134
|
+
// Handle TimelineTimelineItem (single trend item)
|
|
135
|
+
if (content.itemContent) {
|
|
136
|
+
const newsItem = this.parseNewsItemFromContent(content.itemContent, entry.entryId, source, seenHeadlines, aiOnly, includeRaw);
|
|
137
|
+
if (newsItem) {
|
|
138
|
+
items.push(newsItem);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
// Handle TimelineTimelineModule (multiple items)
|
|
142
|
+
const itemsArray = content?.items || [];
|
|
143
|
+
for (const data of itemsArray) {
|
|
144
|
+
if (items.length >= maxCount) {
|
|
145
|
+
break;
|
|
146
|
+
}
|
|
147
|
+
// Structure can be data.itemContent OR data.item.itemContent
|
|
148
|
+
const itemContent = data?.itemContent || data?.item?.itemContent;
|
|
149
|
+
if (!itemContent) {
|
|
150
|
+
continue;
|
|
151
|
+
}
|
|
152
|
+
const newsItem = this.parseNewsItemFromContent(itemContent, entry.entryId, source, seenHeadlines, aiOnly, includeRaw);
|
|
153
|
+
if (newsItem) {
|
|
154
|
+
items.push(newsItem);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
return items;
|
|
160
|
+
}
|
|
161
|
+
parseNewsItemFromContent(
|
|
162
|
+
// biome-ignore lint/suspicious/noExplicitAny: API response structure is complex
|
|
163
|
+
itemContent, entryId, source, seenHeadlines, aiOnly, includeRaw) {
|
|
164
|
+
const headline = itemContent.name || itemContent.title;
|
|
165
|
+
if (!headline) {
|
|
166
|
+
return null;
|
|
167
|
+
}
|
|
168
|
+
const trendMetadata = itemContent?.trend_metadata;
|
|
169
|
+
const trendUrl = itemContent.trend_url?.url || trendMetadata?.url?.url;
|
|
170
|
+
// Detect AI news by characteristics:
|
|
171
|
+
// 1. Full sentence headlines (contains spaces and is longer)
|
|
172
|
+
// 2. Has social_context with "News" category
|
|
173
|
+
// 3. Or explicitly marked as is_ai_trend
|
|
174
|
+
const socialContext = itemContent?.social_context?.text || '';
|
|
175
|
+
const hasNewsCategory = socialContext.includes('News') || socialContext.includes('hours ago');
|
|
176
|
+
const isFullSentence = headline.split(' ').length >= 5; // AI news are full sentences
|
|
177
|
+
const isExplicitlyAiTrend = itemContent.is_ai_trend === true;
|
|
178
|
+
const isAiNews = isExplicitlyAiTrend || (isFullSentence && hasNewsCategory);
|
|
179
|
+
// Filter AI trends if aiOnly is enabled
|
|
180
|
+
if (aiOnly && !isAiNews) {
|
|
181
|
+
return null;
|
|
182
|
+
}
|
|
183
|
+
if (seenHeadlines.has(headline)) {
|
|
184
|
+
return null;
|
|
185
|
+
}
|
|
186
|
+
seenHeadlines.add(headline);
|
|
187
|
+
let postCount;
|
|
188
|
+
let timeAgo;
|
|
189
|
+
let category = 'Trending';
|
|
190
|
+
// Parse social context for metadata
|
|
191
|
+
const socialCtx = itemContent?.social_context;
|
|
192
|
+
if (socialCtx?.text) {
|
|
193
|
+
const socialContextText = socialCtx.text;
|
|
194
|
+
const parts = socialContextText.split('·').map((s) => s.trim());
|
|
195
|
+
for (const part of parts) {
|
|
196
|
+
if (part.includes('ago')) {
|
|
197
|
+
timeAgo = part;
|
|
198
|
+
}
|
|
199
|
+
else if (part.match(POST_COUNT_REGEX)) {
|
|
200
|
+
const match = part.match(POST_COUNT_MATCH_REGEX);
|
|
201
|
+
if (match) {
|
|
202
|
+
let num = Number.parseFloat(match[1]);
|
|
203
|
+
const suffix = match[2]?.toUpperCase();
|
|
204
|
+
if (suffix === 'K') {
|
|
205
|
+
num *= 1000;
|
|
206
|
+
}
|
|
207
|
+
else if (suffix === 'M') {
|
|
208
|
+
num *= 1_000_000;
|
|
209
|
+
}
|
|
210
|
+
else if (suffix === 'B') {
|
|
211
|
+
num *= 1_000_000_000;
|
|
212
|
+
}
|
|
213
|
+
postCount = Math.round(num);
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
else {
|
|
217
|
+
category = part;
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
// Parse trend metadata
|
|
222
|
+
if (trendMetadata?.meta_description) {
|
|
223
|
+
const metaDesc = trendMetadata.meta_description;
|
|
224
|
+
const postMatch = metaDesc.match(POST_COUNT_MATCH_REGEX);
|
|
225
|
+
if (postMatch) {
|
|
226
|
+
let num = Number.parseFloat(postMatch[1]);
|
|
227
|
+
const suffix = postMatch[2]?.toUpperCase();
|
|
228
|
+
if (suffix === 'K') {
|
|
229
|
+
num *= 1000;
|
|
230
|
+
}
|
|
231
|
+
else if (suffix === 'M') {
|
|
232
|
+
num *= 1_000_000;
|
|
233
|
+
}
|
|
234
|
+
else if (suffix === 'B') {
|
|
235
|
+
num *= 1_000_000_000;
|
|
236
|
+
}
|
|
237
|
+
postCount = Math.round(num);
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
if (trendMetadata?.domain_context && (category === 'Trending' || category === 'News')) {
|
|
241
|
+
category = trendMetadata.domain_context;
|
|
242
|
+
}
|
|
243
|
+
const item = {
|
|
244
|
+
id: trendUrl ?? (entryId ? `${entryId}-${headline}` : `${source}-${headline}`),
|
|
245
|
+
headline,
|
|
246
|
+
category: isAiNews ? `AI · ${category}` : category,
|
|
247
|
+
timeAgo,
|
|
248
|
+
postCount,
|
|
249
|
+
description: itemContent.description,
|
|
250
|
+
url: trendUrl,
|
|
251
|
+
};
|
|
252
|
+
if (includeRaw) {
|
|
253
|
+
item._raw = itemContent;
|
|
254
|
+
}
|
|
255
|
+
return item;
|
|
256
|
+
}
|
|
257
|
+
async enrichWithTweets(items, tweetsPerItem, includeRaw) {
|
|
258
|
+
const debug = process.env.BIRD_DEBUG === '1';
|
|
259
|
+
for (const item of items) {
|
|
260
|
+
try {
|
|
261
|
+
const searchQuery = item.headline;
|
|
262
|
+
if (!searchQuery) {
|
|
263
|
+
continue;
|
|
264
|
+
}
|
|
265
|
+
// Use the search method if available (requires search mixin)
|
|
266
|
+
if ('search' in this && typeof this.search === 'function') {
|
|
267
|
+
const result = (await this.search(searchQuery, tweetsPerItem, { includeRaw }));
|
|
268
|
+
if (result.success && result.tweets) {
|
|
269
|
+
item.tweets = result.tweets;
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
catch {
|
|
274
|
+
if (debug) {
|
|
275
|
+
console.error('[getNews] Failed to enrich item with tweets:', item.headline);
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
return TwitterClientNews;
|
|
282
|
+
}
|
|
283
|
+
//# sourceMappingURL=twitter-client-news.js.map
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { AbstractConstructor, Mixin, TwitterClientBase } from './twitter-client-base.js';
|
|
2
|
+
import type { TweetResult } from './twitter-client-types.js';
|
|
3
|
+
export interface TwitterClientPostingMethods {
|
|
4
|
+
tweet(text: string, mediaIds?: string[]): Promise<TweetResult>;
|
|
5
|
+
reply(text: string, replyToTweetId: string, mediaIds?: string[]): Promise<TweetResult>;
|
|
6
|
+
}
|
|
7
|
+
export declare function withPosting<TBase extends AbstractConstructor<TwitterClientBase>>(Base: TBase): Mixin<TBase, TwitterClientPostingMethods>;
|
|
8
|
+
//# sourceMappingURL=twitter-client-posting.d.ts.map
|
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
import { TWITTER_API_BASE, TWITTER_GRAPHQL_POST_URL, TWITTER_STATUS_UPDATE_URL } from './twitter-client-constants.js';
|
|
2
|
+
import { buildTweetCreateFeatures } from './twitter-client-features.js';
|
|
3
|
+
export function withPosting(Base) {
|
|
4
|
+
class TwitterClientPosting extends Base {
|
|
5
|
+
// biome-ignore lint/complexity/noUselessConstructor lint/suspicious/noExplicitAny: TS mixin constructor requirement.
|
|
6
|
+
constructor(...args) {
|
|
7
|
+
super(...args);
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Post a new tweet
|
|
11
|
+
*/
|
|
12
|
+
async tweet(text, mediaIds) {
|
|
13
|
+
const variables = {
|
|
14
|
+
tweet_text: text,
|
|
15
|
+
dark_request: false,
|
|
16
|
+
media: {
|
|
17
|
+
media_entities: (mediaIds ?? []).map((id) => ({ media_id: id, tagged_users: [] })),
|
|
18
|
+
possibly_sensitive: false,
|
|
19
|
+
},
|
|
20
|
+
semantic_annotation_ids: [],
|
|
21
|
+
};
|
|
22
|
+
const features = buildTweetCreateFeatures();
|
|
23
|
+
return this.createTweet(variables, features);
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Reply to an existing tweet
|
|
27
|
+
*/
|
|
28
|
+
async reply(text, replyToTweetId, mediaIds) {
|
|
29
|
+
const variables = {
|
|
30
|
+
tweet_text: text,
|
|
31
|
+
reply: {
|
|
32
|
+
in_reply_to_tweet_id: replyToTweetId,
|
|
33
|
+
exclude_reply_user_ids: [],
|
|
34
|
+
},
|
|
35
|
+
dark_request: false,
|
|
36
|
+
media: {
|
|
37
|
+
media_entities: (mediaIds ?? []).map((id) => ({ media_id: id, tagged_users: [] })),
|
|
38
|
+
possibly_sensitive: false,
|
|
39
|
+
},
|
|
40
|
+
semantic_annotation_ids: [],
|
|
41
|
+
};
|
|
42
|
+
const features = buildTweetCreateFeatures();
|
|
43
|
+
return this.createTweet(variables, features);
|
|
44
|
+
}
|
|
45
|
+
async createTweet(variables, features) {
|
|
46
|
+
await this.ensureClientUserId();
|
|
47
|
+
let queryId = await this.getQueryId('CreateTweet');
|
|
48
|
+
let urlWithOperation = `${TWITTER_API_BASE}/${queryId}/CreateTweet`;
|
|
49
|
+
const buildBody = () => JSON.stringify({ variables, features, queryId });
|
|
50
|
+
let body = buildBody();
|
|
51
|
+
try {
|
|
52
|
+
const headers = { ...this.getHeaders(), referer: 'https://x.com/compose/post' };
|
|
53
|
+
let response = await this.fetchWithTimeout(urlWithOperation, {
|
|
54
|
+
method: 'POST',
|
|
55
|
+
headers,
|
|
56
|
+
body,
|
|
57
|
+
});
|
|
58
|
+
// Twitter increasingly prefers POST to /i/api/graphql with queryId in the payload.
|
|
59
|
+
// If the operation URL 404s, retry the generic endpoint.
|
|
60
|
+
if (response.status === 404) {
|
|
61
|
+
await this.refreshQueryIds();
|
|
62
|
+
queryId = await this.getQueryId('CreateTweet');
|
|
63
|
+
urlWithOperation = `${TWITTER_API_BASE}/${queryId}/CreateTweet`;
|
|
64
|
+
body = buildBody();
|
|
65
|
+
response = await this.fetchWithTimeout(urlWithOperation, {
|
|
66
|
+
method: 'POST',
|
|
67
|
+
headers: { ...this.getHeaders(), referer: 'https://x.com/compose/post' },
|
|
68
|
+
body,
|
|
69
|
+
});
|
|
70
|
+
if (response.status === 404) {
|
|
71
|
+
const retry = await this.fetchWithTimeout(TWITTER_GRAPHQL_POST_URL, {
|
|
72
|
+
method: 'POST',
|
|
73
|
+
headers: { ...this.getHeaders(), referer: 'https://x.com/compose/post' },
|
|
74
|
+
body,
|
|
75
|
+
});
|
|
76
|
+
if (!retry.ok) {
|
|
77
|
+
const text = await retry.text();
|
|
78
|
+
return { success: false, error: `HTTP ${retry.status}: ${text.slice(0, 200)}` };
|
|
79
|
+
}
|
|
80
|
+
const data = (await retry.json());
|
|
81
|
+
if (data.errors && data.errors.length > 0) {
|
|
82
|
+
const fallback = await this.tryStatusUpdateFallback(data.errors, variables);
|
|
83
|
+
if (fallback) {
|
|
84
|
+
return fallback;
|
|
85
|
+
}
|
|
86
|
+
return { success: false, error: this.formatErrors(data.errors) };
|
|
87
|
+
}
|
|
88
|
+
const tweetId = data.data?.create_tweet?.tweet_results?.result?.rest_id;
|
|
89
|
+
if (tweetId) {
|
|
90
|
+
return { success: true, tweetId };
|
|
91
|
+
}
|
|
92
|
+
return { success: false, error: 'Tweet created but no ID returned' };
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
if (!response.ok) {
|
|
96
|
+
const text = await response.text();
|
|
97
|
+
return {
|
|
98
|
+
success: false,
|
|
99
|
+
error: `HTTP ${response.status}: ${text.slice(0, 200)}`,
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
const data = (await response.json());
|
|
103
|
+
if (data.errors && data.errors.length > 0) {
|
|
104
|
+
const fallback = await this.tryStatusUpdateFallback(data.errors, variables);
|
|
105
|
+
if (fallback) {
|
|
106
|
+
return fallback;
|
|
107
|
+
}
|
|
108
|
+
return {
|
|
109
|
+
success: false,
|
|
110
|
+
error: this.formatErrors(data.errors),
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
const tweetId = data.data?.create_tweet?.tweet_results?.result?.rest_id;
|
|
114
|
+
if (tweetId) {
|
|
115
|
+
return {
|
|
116
|
+
success: true,
|
|
117
|
+
tweetId,
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
return {
|
|
121
|
+
success: false,
|
|
122
|
+
error: 'Tweet created but no ID returned',
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
catch (error) {
|
|
126
|
+
return {
|
|
127
|
+
success: false,
|
|
128
|
+
error: error instanceof Error ? error.message : String(error),
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
formatErrors(errors) {
|
|
133
|
+
return errors
|
|
134
|
+
.map((error) => (typeof error.code === 'number' ? `${error.message} (${error.code})` : error.message))
|
|
135
|
+
.join(', ');
|
|
136
|
+
}
|
|
137
|
+
statusUpdateInputFromCreateTweetVariables(variables) {
|
|
138
|
+
const text = typeof variables.tweet_text === 'string' ? variables.tweet_text : null;
|
|
139
|
+
if (!text) {
|
|
140
|
+
return null;
|
|
141
|
+
}
|
|
142
|
+
const reply = variables.reply;
|
|
143
|
+
const inReplyToTweetId = reply &&
|
|
144
|
+
typeof reply === 'object' &&
|
|
145
|
+
typeof reply.in_reply_to_tweet_id === 'string'
|
|
146
|
+
? reply.in_reply_to_tweet_id
|
|
147
|
+
: undefined;
|
|
148
|
+
const media = variables.media;
|
|
149
|
+
const mediaEntities = media && typeof media === 'object' ? media.media_entities : undefined;
|
|
150
|
+
const mediaIds = Array.isArray(mediaEntities)
|
|
151
|
+
? mediaEntities
|
|
152
|
+
.map((entity) => entity && typeof entity === 'object' && 'media_id' in entity
|
|
153
|
+
? entity.media_id
|
|
154
|
+
: undefined)
|
|
155
|
+
.filter((value) => typeof value === 'string' || typeof value === 'number')
|
|
156
|
+
.map((value) => String(value))
|
|
157
|
+
: undefined;
|
|
158
|
+
return { text, inReplyToTweetId, mediaIds: mediaIds && mediaIds.length > 0 ? mediaIds : undefined };
|
|
159
|
+
}
|
|
160
|
+
async postStatusUpdate(input) {
|
|
161
|
+
const params = new URLSearchParams();
|
|
162
|
+
params.set('status', input.text);
|
|
163
|
+
if (input.inReplyToTweetId) {
|
|
164
|
+
params.set('in_reply_to_status_id', input.inReplyToTweetId);
|
|
165
|
+
params.set('auto_populate_reply_metadata', 'true');
|
|
166
|
+
}
|
|
167
|
+
if (input.mediaIds && input.mediaIds.length > 0) {
|
|
168
|
+
params.set('media_ids', input.mediaIds.join(','));
|
|
169
|
+
}
|
|
170
|
+
try {
|
|
171
|
+
const response = await this.fetchWithTimeout(TWITTER_STATUS_UPDATE_URL, {
|
|
172
|
+
method: 'POST',
|
|
173
|
+
headers: {
|
|
174
|
+
...this.getBaseHeaders(),
|
|
175
|
+
'content-type': 'application/x-www-form-urlencoded',
|
|
176
|
+
referer: 'https://x.com/compose/post',
|
|
177
|
+
},
|
|
178
|
+
body: params.toString(),
|
|
179
|
+
});
|
|
180
|
+
if (!response.ok) {
|
|
181
|
+
const text = await response.text();
|
|
182
|
+
return { success: false, error: `HTTP ${response.status}: ${text.slice(0, 200)}` };
|
|
183
|
+
}
|
|
184
|
+
const data = (await response.json());
|
|
185
|
+
if (data.errors && data.errors.length > 0) {
|
|
186
|
+
return { success: false, error: this.formatErrors(data.errors) };
|
|
187
|
+
}
|
|
188
|
+
const tweetId = typeof data.id_str === 'string' ? data.id_str : data.id !== undefined ? String(data.id) : undefined;
|
|
189
|
+
if (tweetId) {
|
|
190
|
+
return { success: true, tweetId };
|
|
191
|
+
}
|
|
192
|
+
return { success: false, error: 'Tweet created but no ID returned' };
|
|
193
|
+
}
|
|
194
|
+
catch (error) {
|
|
195
|
+
return { success: false, error: error instanceof Error ? error.message : String(error) };
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
async tryStatusUpdateFallback(errors, variables) {
|
|
199
|
+
if (!errors.some((error) => error.code === 226)) {
|
|
200
|
+
return null;
|
|
201
|
+
}
|
|
202
|
+
const input = this.statusUpdateInputFromCreateTweetVariables(variables);
|
|
203
|
+
if (!input) {
|
|
204
|
+
return null;
|
|
205
|
+
}
|
|
206
|
+
const fallback = await this.postStatusUpdate(input);
|
|
207
|
+
if (fallback.success) {
|
|
208
|
+
return fallback;
|
|
209
|
+
}
|
|
210
|
+
return {
|
|
211
|
+
success: false,
|
|
212
|
+
error: `${this.formatErrors(errors)} | fallback: ${fallback.error ?? 'Unknown error'}`,
|
|
213
|
+
};
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
return TwitterClientPosting;
|
|
217
|
+
}
|
|
218
|
+
//# sourceMappingURL=twitter-client-posting.js.map
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { AbstractConstructor, Mixin, TwitterClientBase } from './twitter-client-base.js';
|
|
2
|
+
import type { SearchResult } from './twitter-client-types.js';
|
|
3
|
+
/** Options for search methods */
|
|
4
|
+
export interface SearchFetchOptions {
|
|
5
|
+
/** Include raw GraphQL response in `_raw` field */
|
|
6
|
+
includeRaw?: boolean;
|
|
7
|
+
}
|
|
8
|
+
/** Options for paged search methods */
|
|
9
|
+
export interface SearchPaginationOptions extends SearchFetchOptions {
|
|
10
|
+
maxPages?: number;
|
|
11
|
+
/** Starting cursor for pagination (resume from previous fetch) */
|
|
12
|
+
cursor?: string;
|
|
13
|
+
}
|
|
14
|
+
export interface TwitterClientSearchMethods {
|
|
15
|
+
search(query: string, count?: number, options?: SearchFetchOptions): Promise<SearchResult>;
|
|
16
|
+
getAllSearchResults(query: string, options?: SearchPaginationOptions): Promise<SearchResult>;
|
|
17
|
+
}
|
|
18
|
+
export declare function withSearch<TBase extends AbstractConstructor<TwitterClientBase>>(Base: TBase): Mixin<TBase, TwitterClientSearchMethods>;
|
|
19
|
+
//# sourceMappingURL=twitter-client-search.d.ts.map
|