dp-worker 0.1.23 → 0.1.25
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/ops.js +44 -0
- package/dist/bird/ops.js.map +1 -1
- 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 +308 -0
- package/node_modules/dp-shared/dist/bird-contracts.d.ts.map +1 -1
- package/node_modules/dp-shared/dist/bird-contracts.js +228 -0
- package/node_modules/dp-shared/dist/bird-contracts.js.map +1 -1
- package/node_modules/dp-shared/dist/post-drafts-contracts.d.ts +3 -3
- package/package.json +2 -2
- package/vendor/bird/LICENSE +21 -0
- package/vendor/bird/README.md +58 -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
- package/vendor/bird/upstream-package.json +61 -0
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import { TWITTER_MEDIA_METADATA_URL, TWITTER_UPLOAD_URL } from './twitter-client-constants.js';
|
|
2
|
+
export function withMedia(Base) {
|
|
3
|
+
class TwitterClientMedia extends Base {
|
|
4
|
+
// biome-ignore lint/complexity/noUselessConstructor lint/suspicious/noExplicitAny: TS mixin constructor requirement.
|
|
5
|
+
constructor(...args) {
|
|
6
|
+
super(...args);
|
|
7
|
+
}
|
|
8
|
+
mediaCategoryForMime(mimeType) {
|
|
9
|
+
if (mimeType.startsWith('image/')) {
|
|
10
|
+
if (mimeType === 'image/gif') {
|
|
11
|
+
return 'tweet_gif';
|
|
12
|
+
}
|
|
13
|
+
return 'tweet_image';
|
|
14
|
+
}
|
|
15
|
+
if (mimeType.startsWith('video/')) {
|
|
16
|
+
return 'tweet_video';
|
|
17
|
+
}
|
|
18
|
+
return null;
|
|
19
|
+
}
|
|
20
|
+
async uploadMedia(input) {
|
|
21
|
+
const category = this.mediaCategoryForMime(input.mimeType);
|
|
22
|
+
if (!category) {
|
|
23
|
+
return { success: false, error: `Unsupported media type: ${input.mimeType}` };
|
|
24
|
+
}
|
|
25
|
+
try {
|
|
26
|
+
const initParams = new URLSearchParams({
|
|
27
|
+
command: 'INIT',
|
|
28
|
+
total_bytes: String(input.data.byteLength),
|
|
29
|
+
media_type: input.mimeType,
|
|
30
|
+
media_category: category,
|
|
31
|
+
});
|
|
32
|
+
const initResp = await this.fetchWithTimeout(TWITTER_UPLOAD_URL, {
|
|
33
|
+
method: 'POST',
|
|
34
|
+
headers: this.getUploadHeaders(),
|
|
35
|
+
body: initParams,
|
|
36
|
+
});
|
|
37
|
+
if (!initResp.ok) {
|
|
38
|
+
const text = await initResp.text();
|
|
39
|
+
return { success: false, error: `HTTP ${initResp.status}: ${text.slice(0, 200)}` };
|
|
40
|
+
}
|
|
41
|
+
const initBody = (await initResp.json());
|
|
42
|
+
const mediaId = typeof initBody.media_id_string === 'string'
|
|
43
|
+
? initBody.media_id_string
|
|
44
|
+
: initBody.media_id !== undefined
|
|
45
|
+
? String(initBody.media_id)
|
|
46
|
+
: undefined;
|
|
47
|
+
if (!mediaId) {
|
|
48
|
+
return { success: false, error: 'Media upload INIT did not return media_id' };
|
|
49
|
+
}
|
|
50
|
+
const chunkSize = 5 * 1024 * 1024;
|
|
51
|
+
let segmentIndex = 0;
|
|
52
|
+
for (let offset = 0; offset < input.data.byteLength; offset += chunkSize) {
|
|
53
|
+
const chunk = input.data.slice(offset, Math.min(input.data.byteLength, offset + chunkSize));
|
|
54
|
+
const form = new FormData();
|
|
55
|
+
form.set('command', 'APPEND');
|
|
56
|
+
form.set('media_id', mediaId);
|
|
57
|
+
form.set('segment_index', String(segmentIndex));
|
|
58
|
+
form.set('media', new Blob([chunk], { type: input.mimeType }), 'media');
|
|
59
|
+
const appendResp = await this.fetchWithTimeout(TWITTER_UPLOAD_URL, {
|
|
60
|
+
method: 'POST',
|
|
61
|
+
headers: this.getUploadHeaders(),
|
|
62
|
+
body: form,
|
|
63
|
+
});
|
|
64
|
+
if (!appendResp.ok) {
|
|
65
|
+
const text = await appendResp.text();
|
|
66
|
+
return { success: false, error: `HTTP ${appendResp.status}: ${text.slice(0, 200)}` };
|
|
67
|
+
}
|
|
68
|
+
segmentIndex += 1;
|
|
69
|
+
}
|
|
70
|
+
const finalizeParams = new URLSearchParams({ command: 'FINALIZE', media_id: mediaId });
|
|
71
|
+
const finalizeResp = await this.fetchWithTimeout(TWITTER_UPLOAD_URL, {
|
|
72
|
+
method: 'POST',
|
|
73
|
+
headers: this.getUploadHeaders(),
|
|
74
|
+
body: finalizeParams,
|
|
75
|
+
});
|
|
76
|
+
if (!finalizeResp.ok) {
|
|
77
|
+
const text = await finalizeResp.text();
|
|
78
|
+
return { success: false, error: `HTTP ${finalizeResp.status}: ${text.slice(0, 200)}` };
|
|
79
|
+
}
|
|
80
|
+
const finalizeBody = (await finalizeResp.json());
|
|
81
|
+
const info = finalizeBody.processing_info;
|
|
82
|
+
if (info?.state && info.state !== 'succeeded') {
|
|
83
|
+
let attempts = 0;
|
|
84
|
+
while (attempts < 20) {
|
|
85
|
+
if (info.state === 'failed') {
|
|
86
|
+
const msg = info.error?.message || info.error?.name || 'Media processing failed';
|
|
87
|
+
return { success: false, error: msg };
|
|
88
|
+
}
|
|
89
|
+
const delaySecs = Number.isFinite(info.check_after_secs) ? Math.max(1, info.check_after_secs) : 2;
|
|
90
|
+
await this.sleep(delaySecs * 1000);
|
|
91
|
+
const statusUrl = `${TWITTER_UPLOAD_URL}?${new URLSearchParams({
|
|
92
|
+
command: 'STATUS',
|
|
93
|
+
media_id: mediaId,
|
|
94
|
+
}).toString()}`;
|
|
95
|
+
const statusResp = await this.fetchWithTimeout(statusUrl, {
|
|
96
|
+
method: 'GET',
|
|
97
|
+
headers: this.getUploadHeaders(),
|
|
98
|
+
});
|
|
99
|
+
if (!statusResp.ok) {
|
|
100
|
+
const text = await statusResp.text();
|
|
101
|
+
return { success: false, error: `HTTP ${statusResp.status}: ${text.slice(0, 200)}` };
|
|
102
|
+
}
|
|
103
|
+
const statusBody = (await statusResp.json());
|
|
104
|
+
if (!statusBody.processing_info) {
|
|
105
|
+
break;
|
|
106
|
+
}
|
|
107
|
+
info.state = statusBody.processing_info.state;
|
|
108
|
+
info.check_after_secs = statusBody.processing_info.check_after_secs;
|
|
109
|
+
info.error = statusBody.processing_info.error;
|
|
110
|
+
if (info.state === 'succeeded') {
|
|
111
|
+
break;
|
|
112
|
+
}
|
|
113
|
+
attempts += 1;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
if (input.alt && input.mimeType.startsWith('image/')) {
|
|
117
|
+
const metaResp = await this.fetchWithTimeout(TWITTER_MEDIA_METADATA_URL, {
|
|
118
|
+
method: 'POST',
|
|
119
|
+
headers: this.getJsonHeaders(),
|
|
120
|
+
body: JSON.stringify({ media_id: mediaId, alt_text: { text: input.alt } }),
|
|
121
|
+
});
|
|
122
|
+
if (!metaResp.ok) {
|
|
123
|
+
const text = await metaResp.text();
|
|
124
|
+
return { success: false, error: `HTTP ${metaResp.status}: ${text.slice(0, 200)}` };
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
return { success: true, mediaId };
|
|
128
|
+
}
|
|
129
|
+
catch (error) {
|
|
130
|
+
return { success: false, error: error instanceof Error ? error.message : String(error) };
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
return TwitterClientMedia;
|
|
135
|
+
}
|
|
136
|
+
//# sourceMappingURL=twitter-client-media.js.map
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import type { AbstractConstructor, Mixin, TwitterClientBase } from './twitter-client-base.js';
|
|
2
|
+
import type { TweetData } from './twitter-client-types.js';
|
|
3
|
+
declare const TIMELINE_IDS: {
|
|
4
|
+
readonly forYou: "VGltZWxpbmU6DAC2CwABAAAAB2Zvcl95b3UAAA==";
|
|
5
|
+
readonly trending: "VGltZWxpbmU6DAC2CwABAAAACHRyZW5kaW5nAAA=";
|
|
6
|
+
readonly news: "VGltZWxpbmU6DAC2CwABAAAABG5ld3MAAA==";
|
|
7
|
+
readonly sports: "VGltZWxpbmU6DAC2CwABAAAABnNwb3J0cwAA";
|
|
8
|
+
readonly entertainment: "VGltZWxpbmU6DAC2CwABAAAADWVudGVydGFpbm1lbnQAAA==";
|
|
9
|
+
};
|
|
10
|
+
export type ExploreTab = keyof typeof TIMELINE_IDS;
|
|
11
|
+
/** Options for news fetch methods */
|
|
12
|
+
export interface NewsFetchOptions {
|
|
13
|
+
/** Include raw GraphQL response in `_raw` field */
|
|
14
|
+
includeRaw?: boolean;
|
|
15
|
+
/** Also fetch related tweets for each news item */
|
|
16
|
+
withTweets?: boolean;
|
|
17
|
+
/** Number of tweets to fetch per news item (default: 5) */
|
|
18
|
+
tweetsPerItem?: number;
|
|
19
|
+
/** Filter to show only AI-curated news items */
|
|
20
|
+
aiOnly?: boolean;
|
|
21
|
+
/** Fetch from specific tabs only (default: all tabs) */
|
|
22
|
+
tabs?: ExploreTab[];
|
|
23
|
+
}
|
|
24
|
+
export interface NewsItem {
|
|
25
|
+
id: string;
|
|
26
|
+
headline: string;
|
|
27
|
+
category?: string;
|
|
28
|
+
timeAgo?: string;
|
|
29
|
+
postCount?: number;
|
|
30
|
+
description?: string;
|
|
31
|
+
url?: string;
|
|
32
|
+
tweets?: TweetData[];
|
|
33
|
+
_raw?: any;
|
|
34
|
+
}
|
|
35
|
+
export type NewsResult = {
|
|
36
|
+
success: true;
|
|
37
|
+
items: NewsItem[];
|
|
38
|
+
} | {
|
|
39
|
+
success: false;
|
|
40
|
+
error: string;
|
|
41
|
+
};
|
|
42
|
+
export interface TwitterClientNewsMethods {
|
|
43
|
+
getNews(count?: number, options?: NewsFetchOptions): Promise<NewsResult>;
|
|
44
|
+
}
|
|
45
|
+
export declare function withNews<TBase extends AbstractConstructor<TwitterClientBase>>(Base: TBase): Mixin<TBase, TwitterClientNewsMethods>;
|
|
46
|
+
export {};
|
|
47
|
+
//# sourceMappingURL=twitter-client-news.d.ts.map
|
|
@@ -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
|