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,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
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
import { TWITTER_API_BASE } from './twitter-client-constants.js';
|
|
2
|
+
import { buildSearchFeatures } from './twitter-client-features.js';
|
|
3
|
+
import { extractCursorFromInstructions, parseTweetsFromInstructions } from './twitter-client-utils.js';
|
|
4
|
+
const RAW_QUERY_MISSING_REGEX = /must be defined/i;
|
|
5
|
+
function isQueryIdMismatch(payload) {
|
|
6
|
+
try {
|
|
7
|
+
const parsed = JSON.parse(payload);
|
|
8
|
+
return (parsed.errors?.some((error) => {
|
|
9
|
+
if (error?.extensions?.code === 'GRAPHQL_VALIDATION_FAILED') {
|
|
10
|
+
return true;
|
|
11
|
+
}
|
|
12
|
+
if (error?.path?.includes('rawQuery') && RAW_QUERY_MISSING_REGEX.test(error.message ?? '')) {
|
|
13
|
+
return true;
|
|
14
|
+
}
|
|
15
|
+
return false;
|
|
16
|
+
}) ?? false);
|
|
17
|
+
}
|
|
18
|
+
catch {
|
|
19
|
+
return false;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
export function withSearch(Base) {
|
|
23
|
+
class TwitterClientSearch extends Base {
|
|
24
|
+
// biome-ignore lint/complexity/noUselessConstructor lint/suspicious/noExplicitAny: TS mixin constructor requirement.
|
|
25
|
+
constructor(...args) {
|
|
26
|
+
super(...args);
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Search for tweets matching a query
|
|
30
|
+
*/
|
|
31
|
+
async search(query, count = 20, options = {}) {
|
|
32
|
+
return this.searchPaged(query, count, options);
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Get all search results (paged)
|
|
36
|
+
*/
|
|
37
|
+
async getAllSearchResults(query, options) {
|
|
38
|
+
return this.searchPaged(query, Number.POSITIVE_INFINITY, options);
|
|
39
|
+
}
|
|
40
|
+
async searchPaged(query, limit, options = {}) {
|
|
41
|
+
const features = buildSearchFeatures();
|
|
42
|
+
const pageSize = 20;
|
|
43
|
+
const seen = new Set();
|
|
44
|
+
const tweets = [];
|
|
45
|
+
let cursor = options.cursor;
|
|
46
|
+
let nextCursor;
|
|
47
|
+
let pagesFetched = 0;
|
|
48
|
+
const { includeRaw = false, maxPages } = options;
|
|
49
|
+
const fetchPage = async (pageCount, pageCursor) => {
|
|
50
|
+
let lastError;
|
|
51
|
+
let had404 = false;
|
|
52
|
+
const queryIds = await this.getSearchTimelineQueryIds();
|
|
53
|
+
for (const queryId of queryIds) {
|
|
54
|
+
const variables = {
|
|
55
|
+
rawQuery: query,
|
|
56
|
+
count: pageCount,
|
|
57
|
+
querySource: 'typed_query',
|
|
58
|
+
product: 'Latest',
|
|
59
|
+
...(pageCursor ? { cursor: pageCursor } : {}),
|
|
60
|
+
};
|
|
61
|
+
const params = new URLSearchParams({
|
|
62
|
+
variables: JSON.stringify(variables),
|
|
63
|
+
});
|
|
64
|
+
const url = `${TWITTER_API_BASE}/${queryId}/SearchTimeline?${params.toString()}`;
|
|
65
|
+
try {
|
|
66
|
+
const response = await this.fetchWithTimeout(url, {
|
|
67
|
+
method: 'POST',
|
|
68
|
+
headers: this.getHeaders(),
|
|
69
|
+
body: JSON.stringify({ features, queryId }),
|
|
70
|
+
});
|
|
71
|
+
if (response.status === 404) {
|
|
72
|
+
had404 = true;
|
|
73
|
+
lastError = `HTTP ${response.status}`;
|
|
74
|
+
continue;
|
|
75
|
+
}
|
|
76
|
+
if (!response.ok) {
|
|
77
|
+
const text = await response.text();
|
|
78
|
+
const shouldRefreshQueryIds = (response.status === 400 || response.status === 422) && isQueryIdMismatch(text);
|
|
79
|
+
return {
|
|
80
|
+
success: false,
|
|
81
|
+
error: `HTTP ${response.status}: ${text.slice(0, 200)}`,
|
|
82
|
+
had404: had404 || shouldRefreshQueryIds,
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
const data = (await response.json());
|
|
86
|
+
if (data.errors && data.errors.length > 0) {
|
|
87
|
+
const shouldRefreshQueryIds = data.errors.some((error) => error?.extensions?.code === 'GRAPHQL_VALIDATION_FAILED');
|
|
88
|
+
return {
|
|
89
|
+
success: false,
|
|
90
|
+
error: data.errors.map((e) => e.message).join(', '),
|
|
91
|
+
had404: had404 || shouldRefreshQueryIds,
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
const instructions = data.data?.search_by_raw_query?.search_timeline?.timeline?.instructions;
|
|
95
|
+
const pageTweets = parseTweetsFromInstructions(instructions, { quoteDepth: this.quoteDepth, includeRaw });
|
|
96
|
+
const nextCursor = extractCursorFromInstructions(instructions);
|
|
97
|
+
return { success: true, tweets: pageTweets, cursor: nextCursor, had404 };
|
|
98
|
+
}
|
|
99
|
+
catch (error) {
|
|
100
|
+
lastError = error instanceof Error ? error.message : String(error);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
return { success: false, error: lastError ?? 'Unknown error fetching search results', had404 };
|
|
104
|
+
};
|
|
105
|
+
const fetchWithRefresh = async (pageCount, pageCursor) => {
|
|
106
|
+
const firstAttempt = await fetchPage(pageCount, pageCursor);
|
|
107
|
+
if (firstAttempt.success) {
|
|
108
|
+
return firstAttempt;
|
|
109
|
+
}
|
|
110
|
+
if (firstAttempt.had404) {
|
|
111
|
+
await this.refreshQueryIds();
|
|
112
|
+
const secondAttempt = await fetchPage(pageCount, pageCursor);
|
|
113
|
+
if (secondAttempt.success) {
|
|
114
|
+
return secondAttempt;
|
|
115
|
+
}
|
|
116
|
+
return { success: false, error: secondAttempt.error };
|
|
117
|
+
}
|
|
118
|
+
return { success: false, error: firstAttempt.error };
|
|
119
|
+
};
|
|
120
|
+
const unlimited = limit === Number.POSITIVE_INFINITY;
|
|
121
|
+
while (unlimited || tweets.length < limit) {
|
|
122
|
+
const pageCount = unlimited ? pageSize : Math.min(pageSize, limit - tweets.length);
|
|
123
|
+
const page = await fetchWithRefresh(pageCount, cursor);
|
|
124
|
+
if (!page.success) {
|
|
125
|
+
return { success: false, error: page.error };
|
|
126
|
+
}
|
|
127
|
+
pagesFetched += 1;
|
|
128
|
+
let added = 0;
|
|
129
|
+
for (const tweet of page.tweets) {
|
|
130
|
+
if (seen.has(tweet.id)) {
|
|
131
|
+
continue;
|
|
132
|
+
}
|
|
133
|
+
seen.add(tweet.id);
|
|
134
|
+
tweets.push(tweet);
|
|
135
|
+
added += 1;
|
|
136
|
+
if (!unlimited && tweets.length >= limit) {
|
|
137
|
+
break;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
const pageCursor = page.cursor;
|
|
141
|
+
if (!pageCursor || pageCursor === cursor || page.tweets.length === 0 || added === 0) {
|
|
142
|
+
nextCursor = undefined;
|
|
143
|
+
break;
|
|
144
|
+
}
|
|
145
|
+
if (maxPages && pagesFetched >= maxPages) {
|
|
146
|
+
nextCursor = pageCursor;
|
|
147
|
+
break;
|
|
148
|
+
}
|
|
149
|
+
cursor = pageCursor;
|
|
150
|
+
nextCursor = pageCursor;
|
|
151
|
+
}
|
|
152
|
+
return { success: true, tweets, nextCursor };
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
return TwitterClientSearch;
|
|
156
|
+
}
|
|
157
|
+
//# sourceMappingURL=twitter-client-search.js.map
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { AbstractConstructor, Mixin, TwitterClientBase } from './twitter-client-base.js';
|
|
2
|
+
import type { SearchResult } from './twitter-client-types.js';
|
|
3
|
+
/** Options for timeline fetch methods */
|
|
4
|
+
export interface TimelineFetchOptions {
|
|
5
|
+
/** Include raw GraphQL response in `_raw` field */
|
|
6
|
+
includeRaw?: boolean;
|
|
7
|
+
}
|
|
8
|
+
/** Options for paged timeline fetch methods */
|
|
9
|
+
export interface TimelinePaginationOptions extends TimelineFetchOptions {
|
|
10
|
+
maxPages?: number;
|
|
11
|
+
/** Starting cursor for pagination (resume from previous fetch) */
|
|
12
|
+
cursor?: string;
|
|
13
|
+
}
|
|
14
|
+
export interface TwitterClientTimelineMethods {
|
|
15
|
+
getBookmarks(count?: number, options?: TimelineFetchOptions): Promise<SearchResult>;
|
|
16
|
+
getAllBookmarks(options?: TimelinePaginationOptions): Promise<SearchResult>;
|
|
17
|
+
getLikes(count?: number, options?: TimelineFetchOptions): Promise<SearchResult>;
|
|
18
|
+
getAllLikes(options?: TimelinePaginationOptions): Promise<SearchResult>;
|
|
19
|
+
getBookmarkFolderTimeline(folderId: string, count?: number, options?: TimelineFetchOptions): Promise<SearchResult>;
|
|
20
|
+
getAllBookmarkFolderTimeline(folderId: string, options?: TimelinePaginationOptions): Promise<SearchResult>;
|
|
21
|
+
}
|
|
22
|
+
export declare function withTimelines<TBase extends AbstractConstructor<TwitterClientBase>>(Base: TBase): Mixin<TBase, TwitterClientTimelineMethods>;
|
|
23
|
+
//# sourceMappingURL=twitter-client-timelines.d.ts.map
|