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,178 @@
|
|
|
1
|
+
import { TWITTER_API_BASE } from './twitter-client-constants.js';
|
|
2
|
+
export function withFollow(Base) {
|
|
3
|
+
class TwitterClientFollow extends Base {
|
|
4
|
+
// biome-ignore lint/complexity/noUselessConstructor lint/suspicious/noExplicitAny: TS mixin constructor requirement.
|
|
5
|
+
constructor(...args) {
|
|
6
|
+
super(...args);
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Follow a user by their user ID
|
|
10
|
+
*/
|
|
11
|
+
async follow(userId) {
|
|
12
|
+
await this.ensureClientUserId();
|
|
13
|
+
// Try REST API first (more reliable for follow/unfollow)
|
|
14
|
+
const restResult = await this.followViaRest(userId, 'create');
|
|
15
|
+
if (restResult.success) {
|
|
16
|
+
return restResult;
|
|
17
|
+
}
|
|
18
|
+
// Fallback to GraphQL
|
|
19
|
+
return this.followViaGraphQL(userId, true);
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Unfollow a user by their user ID
|
|
23
|
+
*/
|
|
24
|
+
async unfollow(userId) {
|
|
25
|
+
await this.ensureClientUserId();
|
|
26
|
+
// Try REST API first (more reliable for follow/unfollow)
|
|
27
|
+
const restResult = await this.followViaRest(userId, 'destroy');
|
|
28
|
+
if (restResult.success) {
|
|
29
|
+
return restResult;
|
|
30
|
+
}
|
|
31
|
+
// Fallback to GraphQL
|
|
32
|
+
return this.followViaGraphQL(userId, false);
|
|
33
|
+
}
|
|
34
|
+
async followViaRest(userId, action) {
|
|
35
|
+
const urls = [
|
|
36
|
+
`https://x.com/i/api/1.1/friendships/${action}.json`,
|
|
37
|
+
`https://api.twitter.com/1.1/friendships/${action}.json`,
|
|
38
|
+
];
|
|
39
|
+
const params = new URLSearchParams({
|
|
40
|
+
user_id: userId,
|
|
41
|
+
skip_status: 'true',
|
|
42
|
+
});
|
|
43
|
+
let lastError;
|
|
44
|
+
for (const url of urls) {
|
|
45
|
+
try {
|
|
46
|
+
const response = await this.fetchWithTimeout(url, {
|
|
47
|
+
method: 'POST',
|
|
48
|
+
headers: {
|
|
49
|
+
...this.getBaseHeaders(),
|
|
50
|
+
'content-type': 'application/x-www-form-urlencoded',
|
|
51
|
+
},
|
|
52
|
+
body: params.toString(),
|
|
53
|
+
});
|
|
54
|
+
if (!response.ok) {
|
|
55
|
+
const text = await response.text();
|
|
56
|
+
// Parse error response
|
|
57
|
+
try {
|
|
58
|
+
const errorData = JSON.parse(text);
|
|
59
|
+
if (errorData.errors && errorData.errors.length > 0) {
|
|
60
|
+
const error = errorData.errors[0];
|
|
61
|
+
// Code 160: Already following / already not following
|
|
62
|
+
if (error.code === 160) {
|
|
63
|
+
return { success: true };
|
|
64
|
+
}
|
|
65
|
+
// Code 162: Blocked from following
|
|
66
|
+
if (error.code === 162) {
|
|
67
|
+
return { success: false, error: 'You have been blocked from following this account' };
|
|
68
|
+
}
|
|
69
|
+
// Code 108: Cannot find specified user
|
|
70
|
+
if (error.code === 108) {
|
|
71
|
+
return { success: false, error: 'User not found' };
|
|
72
|
+
}
|
|
73
|
+
lastError = `${error.message} (code ${error.code})`;
|
|
74
|
+
continue;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
catch {
|
|
78
|
+
// Not JSON, continue with generic error
|
|
79
|
+
}
|
|
80
|
+
lastError = `HTTP ${response.status}: ${text.slice(0, 200)}`;
|
|
81
|
+
continue;
|
|
82
|
+
}
|
|
83
|
+
const data = (await response.json());
|
|
84
|
+
if (data.errors && data.errors.length > 0) {
|
|
85
|
+
lastError = data.errors.map((e) => e.message).join(', ');
|
|
86
|
+
continue;
|
|
87
|
+
}
|
|
88
|
+
// Success - user data returned
|
|
89
|
+
if (data.id_str || data.screen_name) {
|
|
90
|
+
return {
|
|
91
|
+
success: true,
|
|
92
|
+
userId: data.id_str,
|
|
93
|
+
username: data.screen_name,
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
return { success: true };
|
|
97
|
+
}
|
|
98
|
+
catch (error) {
|
|
99
|
+
lastError = error instanceof Error ? error.message : String(error);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
return { success: false, error: lastError ?? `Unknown error during ${action}` };
|
|
103
|
+
}
|
|
104
|
+
async followViaGraphQL(userId, follow) {
|
|
105
|
+
const operationName = follow ? 'CreateFriendship' : 'DestroyFriendship';
|
|
106
|
+
const variables = {
|
|
107
|
+
user_id: userId,
|
|
108
|
+
};
|
|
109
|
+
const tryOnce = async () => {
|
|
110
|
+
let lastError;
|
|
111
|
+
let had404 = false;
|
|
112
|
+
const queryIds = await this.getFollowQueryIds(follow);
|
|
113
|
+
for (const queryId of queryIds) {
|
|
114
|
+
const url = `${TWITTER_API_BASE}/${queryId}/${operationName}`;
|
|
115
|
+
try {
|
|
116
|
+
const response = await this.fetchWithTimeout(url, {
|
|
117
|
+
method: 'POST',
|
|
118
|
+
headers: this.getHeaders(),
|
|
119
|
+
body: JSON.stringify({ variables, queryId }),
|
|
120
|
+
});
|
|
121
|
+
if (response.status === 404) {
|
|
122
|
+
had404 = true;
|
|
123
|
+
lastError = 'HTTP 404';
|
|
124
|
+
continue;
|
|
125
|
+
}
|
|
126
|
+
if (!response.ok) {
|
|
127
|
+
const text = await response.text();
|
|
128
|
+
lastError = `HTTP ${response.status}: ${text.slice(0, 200)}`;
|
|
129
|
+
continue;
|
|
130
|
+
}
|
|
131
|
+
const data = (await response.json());
|
|
132
|
+
if (data.errors && data.errors.length > 0) {
|
|
133
|
+
lastError = data.errors.map((e) => e.message).join(', ');
|
|
134
|
+
continue;
|
|
135
|
+
}
|
|
136
|
+
const result = data.data?.user?.result;
|
|
137
|
+
return {
|
|
138
|
+
success: true,
|
|
139
|
+
userId: result?.rest_id,
|
|
140
|
+
username: result?.legacy?.screen_name,
|
|
141
|
+
had404,
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
catch (error) {
|
|
145
|
+
lastError = error instanceof Error ? error.message : String(error);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
return {
|
|
149
|
+
success: false,
|
|
150
|
+
error: lastError ?? `Unknown error during ${operationName}`,
|
|
151
|
+
had404,
|
|
152
|
+
};
|
|
153
|
+
};
|
|
154
|
+
const firstAttempt = await tryOnce();
|
|
155
|
+
if (firstAttempt.success) {
|
|
156
|
+
return { success: true, userId: firstAttempt.userId, username: firstAttempt.username };
|
|
157
|
+
}
|
|
158
|
+
if (firstAttempt.had404) {
|
|
159
|
+
await this.refreshQueryIds();
|
|
160
|
+
const secondAttempt = await tryOnce();
|
|
161
|
+
if (secondAttempt.success) {
|
|
162
|
+
return { success: true, userId: secondAttempt.userId, username: secondAttempt.username };
|
|
163
|
+
}
|
|
164
|
+
return { success: false, error: secondAttempt.error };
|
|
165
|
+
}
|
|
166
|
+
return { success: false, error: firstAttempt.error };
|
|
167
|
+
}
|
|
168
|
+
async getFollowQueryIds(follow) {
|
|
169
|
+
const primary = await this.getQueryId(follow ? 'CreateFriendship' : 'DestroyFriendship');
|
|
170
|
+
const fallbacks = follow
|
|
171
|
+
? ['8h9JVdV8dlSyqyRDJEPCsA', 'OPwKc1HXnBT_bWXfAlo-9g']
|
|
172
|
+
: ['ppXWuagMNXgvzx6WoXBW0Q', '8h9JVdV8dlSyqyRDJEPCsA'];
|
|
173
|
+
return Array.from(new Set([primary, ...fallbacks]));
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
return TwitterClientFollow;
|
|
177
|
+
}
|
|
178
|
+
//# sourceMappingURL=twitter-client-follow.js.map
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { AbstractConstructor, Mixin, TwitterClientBase } from './twitter-client-base.js';
|
|
2
|
+
import type { SearchResult } from './twitter-client-types.js';
|
|
3
|
+
/** Options for home timeline fetch methods */
|
|
4
|
+
export interface HomeTimelineFetchOptions {
|
|
5
|
+
/** Include raw GraphQL response in `_raw` field */
|
|
6
|
+
includeRaw?: boolean;
|
|
7
|
+
}
|
|
8
|
+
export interface TwitterClientHomeMethods {
|
|
9
|
+
getHomeTimeline(count?: number, options?: HomeTimelineFetchOptions): Promise<SearchResult>;
|
|
10
|
+
getHomeLatestTimeline(count?: number, options?: HomeTimelineFetchOptions): Promise<SearchResult>;
|
|
11
|
+
}
|
|
12
|
+
export declare function withHome<TBase extends AbstractConstructor<TwitterClientBase>>(Base: TBase): Mixin<TBase, TwitterClientHomeMethods>;
|
|
13
|
+
//# sourceMappingURL=twitter-client-home.d.ts.map
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import { TWITTER_API_BASE } from './twitter-client-constants.js';
|
|
2
|
+
import { buildHomeTimelineFeatures } from './twitter-client-features.js';
|
|
3
|
+
import { extractCursorFromInstructions, parseTweetsFromInstructions } from './twitter-client-utils.js';
|
|
4
|
+
const QUERY_UNSPECIFIED_REGEX = /query:\s*unspecified/i;
|
|
5
|
+
function isQueryIdMismatch(errors) {
|
|
6
|
+
return errors.some((error) => QUERY_UNSPECIFIED_REGEX.test(error.message ?? ''));
|
|
7
|
+
}
|
|
8
|
+
export function withHome(Base) {
|
|
9
|
+
class TwitterClientHome extends Base {
|
|
10
|
+
// biome-ignore lint/complexity/noUselessConstructor lint/suspicious/noExplicitAny: TS mixin constructor requirement.
|
|
11
|
+
constructor(...args) {
|
|
12
|
+
super(...args);
|
|
13
|
+
}
|
|
14
|
+
async getHomeTimelineQueryIds() {
|
|
15
|
+
const primary = await this.getQueryId('HomeTimeline');
|
|
16
|
+
return Array.from(new Set([primary, 'edseUwk9sP5Phz__9TIRnA']));
|
|
17
|
+
}
|
|
18
|
+
async getHomeLatestTimelineQueryIds() {
|
|
19
|
+
const primary = await this.getQueryId('HomeLatestTimeline');
|
|
20
|
+
return Array.from(new Set([primary, 'iOEZpOdfekFsxSlPQCQtPg']));
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Get the authenticated user's "For You" home timeline
|
|
24
|
+
*/
|
|
25
|
+
async getHomeTimeline(count = 20, options = {}) {
|
|
26
|
+
return this.fetchHomeTimeline('HomeTimeline', count, options);
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Get the authenticated user's "Following" (latest/chronological) home timeline
|
|
30
|
+
*/
|
|
31
|
+
async getHomeLatestTimeline(count = 20, options = {}) {
|
|
32
|
+
return this.fetchHomeTimeline('HomeLatestTimeline', count, options);
|
|
33
|
+
}
|
|
34
|
+
async fetchHomeTimeline(operation, count, options) {
|
|
35
|
+
const { includeRaw = false } = options;
|
|
36
|
+
const features = buildHomeTimelineFeatures();
|
|
37
|
+
const pageSize = 20;
|
|
38
|
+
const seen = new Set();
|
|
39
|
+
const tweets = [];
|
|
40
|
+
let cursor;
|
|
41
|
+
const fetchPage = async (pageCount, pageCursor) => {
|
|
42
|
+
let lastError;
|
|
43
|
+
let had404 = false;
|
|
44
|
+
const queryIds = operation === 'HomeTimeline'
|
|
45
|
+
? await this.getHomeTimelineQueryIds()
|
|
46
|
+
: await this.getHomeLatestTimelineQueryIds();
|
|
47
|
+
for (const queryId of queryIds) {
|
|
48
|
+
const variables = {
|
|
49
|
+
count: pageCount,
|
|
50
|
+
includePromotedContent: true,
|
|
51
|
+
latestControlAvailable: true,
|
|
52
|
+
requestContext: 'launch',
|
|
53
|
+
withCommunity: true,
|
|
54
|
+
...(pageCursor ? { cursor: pageCursor } : {}),
|
|
55
|
+
};
|
|
56
|
+
const params = new URLSearchParams({
|
|
57
|
+
variables: JSON.stringify(variables),
|
|
58
|
+
features: JSON.stringify(features),
|
|
59
|
+
});
|
|
60
|
+
const url = `${TWITTER_API_BASE}/${queryId}/${operation}?${params.toString()}`;
|
|
61
|
+
try {
|
|
62
|
+
const response = await this.fetchWithTimeout(url, {
|
|
63
|
+
method: 'GET',
|
|
64
|
+
headers: this.getHeaders(),
|
|
65
|
+
});
|
|
66
|
+
if (response.status === 404) {
|
|
67
|
+
had404 = true;
|
|
68
|
+
lastError = `HTTP ${response.status}`;
|
|
69
|
+
continue;
|
|
70
|
+
}
|
|
71
|
+
if (!response.ok) {
|
|
72
|
+
const text = await response.text();
|
|
73
|
+
return { success: false, error: `HTTP ${response.status}: ${text.slice(0, 200)}`, had404 };
|
|
74
|
+
}
|
|
75
|
+
const data = (await response.json());
|
|
76
|
+
if (data.errors && data.errors.length > 0) {
|
|
77
|
+
const errorMessage = data.errors.map((e) => e.message).join(', ');
|
|
78
|
+
return {
|
|
79
|
+
success: false,
|
|
80
|
+
error: errorMessage,
|
|
81
|
+
had404: had404 || isQueryIdMismatch(data.errors),
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
const instructions = data.data?.home?.home_timeline_urt?.instructions;
|
|
85
|
+
const pageTweets = parseTweetsFromInstructions(instructions, { quoteDepth: this.quoteDepth, includeRaw });
|
|
86
|
+
const nextCursor = extractCursorFromInstructions(instructions);
|
|
87
|
+
return { success: true, tweets: pageTweets, cursor: nextCursor, had404 };
|
|
88
|
+
}
|
|
89
|
+
catch (error) {
|
|
90
|
+
lastError = error instanceof Error ? error.message : String(error);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
return { success: false, error: lastError ?? 'Unknown error fetching home timeline', had404 };
|
|
94
|
+
};
|
|
95
|
+
const fetchWithRefresh = async (pageCount, pageCursor) => {
|
|
96
|
+
const firstAttempt = await fetchPage(pageCount, pageCursor);
|
|
97
|
+
if (firstAttempt.success) {
|
|
98
|
+
return firstAttempt;
|
|
99
|
+
}
|
|
100
|
+
if (firstAttempt.had404) {
|
|
101
|
+
await this.refreshQueryIds();
|
|
102
|
+
const secondAttempt = await fetchPage(pageCount, pageCursor);
|
|
103
|
+
if (secondAttempt.success) {
|
|
104
|
+
return secondAttempt;
|
|
105
|
+
}
|
|
106
|
+
return { success: false, error: secondAttempt.error };
|
|
107
|
+
}
|
|
108
|
+
return { success: false, error: firstAttempt.error };
|
|
109
|
+
};
|
|
110
|
+
while (tweets.length < count) {
|
|
111
|
+
const pageCount = Math.min(pageSize, count - tweets.length);
|
|
112
|
+
const page = await fetchWithRefresh(pageCount, cursor);
|
|
113
|
+
if (!page.success) {
|
|
114
|
+
return { success: false, error: page.error };
|
|
115
|
+
}
|
|
116
|
+
let added = 0;
|
|
117
|
+
for (const tweet of page.tweets) {
|
|
118
|
+
if (seen.has(tweet.id)) {
|
|
119
|
+
continue;
|
|
120
|
+
}
|
|
121
|
+
seen.add(tweet.id);
|
|
122
|
+
tweets.push(tweet);
|
|
123
|
+
added += 1;
|
|
124
|
+
if (tweets.length >= count) {
|
|
125
|
+
break;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
if (!page.cursor || page.cursor === cursor || page.tweets.length === 0 || added === 0) {
|
|
129
|
+
break;
|
|
130
|
+
}
|
|
131
|
+
cursor = page.cursor;
|
|
132
|
+
}
|
|
133
|
+
return { success: true, tweets };
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
return TwitterClientHome;
|
|
137
|
+
}
|
|
138
|
+
//# sourceMappingURL=twitter-client-home.js.map
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { AbstractConstructor, Mixin, TwitterClientBase } from './twitter-client-base.js';
|
|
2
|
+
import type { TimelineFetchOptions, TimelinePaginationOptions } from './twitter-client-timelines.js';
|
|
3
|
+
import type { ListsResult, SearchResult } from './twitter-client-types.js';
|
|
4
|
+
export interface TwitterClientListMethods {
|
|
5
|
+
getOwnedLists(count?: number): Promise<ListsResult>;
|
|
6
|
+
getListMemberships(count?: number): Promise<ListsResult>;
|
|
7
|
+
getListTimeline(listId: string, count?: number, options?: TimelineFetchOptions): Promise<SearchResult>;
|
|
8
|
+
getAllListTimeline(listId: string, options?: TimelinePaginationOptions): Promise<SearchResult>;
|
|
9
|
+
}
|
|
10
|
+
export declare function withLists<TBase extends AbstractConstructor<TwitterClientBase>>(Base: TBase): Mixin<TBase, TwitterClientListMethods>;
|
|
11
|
+
//# sourceMappingURL=twitter-client-lists.d.ts.map
|
|
@@ -0,0 +1,321 @@
|
|
|
1
|
+
// ABOUTME: Mixin for Twitter Lists GraphQL operations.
|
|
2
|
+
// ABOUTME: Provides methods to fetch user's owned lists, memberships, and list timelines.
|
|
3
|
+
import { TWITTER_API_BASE } from './twitter-client-constants.js';
|
|
4
|
+
import { buildListsFeatures } from './twitter-client-features.js';
|
|
5
|
+
import { extractCursorFromInstructions, parseTweetsFromInstructions } from './twitter-client-utils.js';
|
|
6
|
+
function parseList(listResult) {
|
|
7
|
+
if (!listResult.id_str || !listResult.name) {
|
|
8
|
+
return null;
|
|
9
|
+
}
|
|
10
|
+
const owner = listResult.user_results?.result;
|
|
11
|
+
return {
|
|
12
|
+
id: listResult.id_str,
|
|
13
|
+
name: listResult.name,
|
|
14
|
+
description: listResult.description,
|
|
15
|
+
memberCount: listResult.member_count,
|
|
16
|
+
subscriberCount: listResult.subscriber_count,
|
|
17
|
+
isPrivate: listResult.mode?.toLowerCase() === 'private',
|
|
18
|
+
createdAt: listResult.created_at,
|
|
19
|
+
owner: owner
|
|
20
|
+
? {
|
|
21
|
+
id: owner.rest_id ?? '',
|
|
22
|
+
username: owner.legacy?.screen_name ?? '',
|
|
23
|
+
name: owner.legacy?.name ?? '',
|
|
24
|
+
}
|
|
25
|
+
: undefined,
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
function parseListsFromInstructions(instructions) {
|
|
29
|
+
const lists = [];
|
|
30
|
+
if (!instructions) {
|
|
31
|
+
return lists;
|
|
32
|
+
}
|
|
33
|
+
for (const instruction of instructions) {
|
|
34
|
+
if (!instruction.entries) {
|
|
35
|
+
continue;
|
|
36
|
+
}
|
|
37
|
+
for (const entry of instruction.entries) {
|
|
38
|
+
const listResult = entry.content?.itemContent?.list;
|
|
39
|
+
if (listResult) {
|
|
40
|
+
const parsed = parseList(listResult);
|
|
41
|
+
if (parsed) {
|
|
42
|
+
lists.push(parsed);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
return lists;
|
|
48
|
+
}
|
|
49
|
+
export function withLists(Base) {
|
|
50
|
+
class TwitterClientLists extends Base {
|
|
51
|
+
// biome-ignore lint/complexity/noUselessConstructor lint/suspicious/noExplicitAny: TS mixin constructor requirement.
|
|
52
|
+
constructor(...args) {
|
|
53
|
+
super(...args);
|
|
54
|
+
}
|
|
55
|
+
async getListOwnershipsQueryIds() {
|
|
56
|
+
const primary = await this.getQueryId('ListOwnerships');
|
|
57
|
+
return Array.from(new Set([primary, 'wQcOSjSQ8NtgxIwvYl1lMg']));
|
|
58
|
+
}
|
|
59
|
+
async getListMembershipsQueryIds() {
|
|
60
|
+
const primary = await this.getQueryId('ListMemberships');
|
|
61
|
+
return Array.from(new Set([primary, 'BlEXXdARdSeL_0KyKHHvvg']));
|
|
62
|
+
}
|
|
63
|
+
async getListTimelineQueryIds() {
|
|
64
|
+
const primary = await this.getQueryId('ListLatestTweetsTimeline');
|
|
65
|
+
return Array.from(new Set([primary, '2TemLyqrMpTeAmysdbnVqw']));
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Get lists owned by the authenticated user
|
|
69
|
+
*/
|
|
70
|
+
async getOwnedLists(count = 100) {
|
|
71
|
+
const userResult = await this.getCurrentUser();
|
|
72
|
+
if (!userResult.success || !userResult.user) {
|
|
73
|
+
return { success: false, error: userResult.error ?? 'Could not determine current user' };
|
|
74
|
+
}
|
|
75
|
+
const variables = {
|
|
76
|
+
userId: userResult.user.id,
|
|
77
|
+
count,
|
|
78
|
+
isListMembershipShown: true,
|
|
79
|
+
isListMemberTargetUserId: userResult.user.id,
|
|
80
|
+
};
|
|
81
|
+
const features = buildListsFeatures();
|
|
82
|
+
const params = new URLSearchParams({
|
|
83
|
+
variables: JSON.stringify(variables),
|
|
84
|
+
features: JSON.stringify(features),
|
|
85
|
+
});
|
|
86
|
+
const tryOnce = async () => {
|
|
87
|
+
let lastError;
|
|
88
|
+
let had404 = false;
|
|
89
|
+
const queryIds = await this.getListOwnershipsQueryIds();
|
|
90
|
+
for (const queryId of queryIds) {
|
|
91
|
+
const url = `${TWITTER_API_BASE}/${queryId}/ListOwnerships?${params.toString()}`;
|
|
92
|
+
try {
|
|
93
|
+
const response = await this.fetchWithTimeout(url, {
|
|
94
|
+
method: 'GET',
|
|
95
|
+
headers: this.getHeaders(),
|
|
96
|
+
});
|
|
97
|
+
if (response.status === 404) {
|
|
98
|
+
had404 = true;
|
|
99
|
+
lastError = `HTTP ${response.status}`;
|
|
100
|
+
continue;
|
|
101
|
+
}
|
|
102
|
+
if (!response.ok) {
|
|
103
|
+
const text = await response.text();
|
|
104
|
+
return { success: false, error: `HTTP ${response.status}: ${text.slice(0, 200)}`, had404 };
|
|
105
|
+
}
|
|
106
|
+
const data = (await response.json());
|
|
107
|
+
if (data.errors && data.errors.length > 0) {
|
|
108
|
+
return { success: false, error: data.errors.map((e) => e.message).join(', '), had404 };
|
|
109
|
+
}
|
|
110
|
+
const instructions = data.data?.user?.result?.timeline?.timeline?.instructions;
|
|
111
|
+
const lists = parseListsFromInstructions(instructions);
|
|
112
|
+
return { success: true, lists, had404 };
|
|
113
|
+
}
|
|
114
|
+
catch (error) {
|
|
115
|
+
lastError = error instanceof Error ? error.message : String(error);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
return { success: false, error: lastError ?? 'Unknown error fetching owned lists', had404 };
|
|
119
|
+
};
|
|
120
|
+
const firstAttempt = await tryOnce();
|
|
121
|
+
if (firstAttempt.success) {
|
|
122
|
+
return { success: true, lists: firstAttempt.lists };
|
|
123
|
+
}
|
|
124
|
+
if (firstAttempt.had404) {
|
|
125
|
+
await this.refreshQueryIds();
|
|
126
|
+
const secondAttempt = await tryOnce();
|
|
127
|
+
if (secondAttempt.success) {
|
|
128
|
+
return { success: true, lists: secondAttempt.lists };
|
|
129
|
+
}
|
|
130
|
+
return { success: false, error: secondAttempt.error };
|
|
131
|
+
}
|
|
132
|
+
return { success: false, error: firstAttempt.error };
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Get lists the authenticated user is a member of
|
|
136
|
+
*/
|
|
137
|
+
async getListMemberships(count = 100) {
|
|
138
|
+
const userResult = await this.getCurrentUser();
|
|
139
|
+
if (!userResult.success || !userResult.user) {
|
|
140
|
+
return { success: false, error: userResult.error ?? 'Could not determine current user' };
|
|
141
|
+
}
|
|
142
|
+
const variables = {
|
|
143
|
+
userId: userResult.user.id,
|
|
144
|
+
count,
|
|
145
|
+
isListMembershipShown: true,
|
|
146
|
+
isListMemberTargetUserId: userResult.user.id,
|
|
147
|
+
};
|
|
148
|
+
const features = buildListsFeatures();
|
|
149
|
+
const params = new URLSearchParams({
|
|
150
|
+
variables: JSON.stringify(variables),
|
|
151
|
+
features: JSON.stringify(features),
|
|
152
|
+
});
|
|
153
|
+
const tryOnce = async () => {
|
|
154
|
+
let lastError;
|
|
155
|
+
let had404 = false;
|
|
156
|
+
const queryIds = await this.getListMembershipsQueryIds();
|
|
157
|
+
for (const queryId of queryIds) {
|
|
158
|
+
const url = `${TWITTER_API_BASE}/${queryId}/ListMemberships?${params.toString()}`;
|
|
159
|
+
try {
|
|
160
|
+
const response = await this.fetchWithTimeout(url, {
|
|
161
|
+
method: 'GET',
|
|
162
|
+
headers: this.getHeaders(),
|
|
163
|
+
});
|
|
164
|
+
if (response.status === 404) {
|
|
165
|
+
had404 = true;
|
|
166
|
+
lastError = `HTTP ${response.status}`;
|
|
167
|
+
continue;
|
|
168
|
+
}
|
|
169
|
+
if (!response.ok) {
|
|
170
|
+
const text = await response.text();
|
|
171
|
+
return { success: false, error: `HTTP ${response.status}: ${text.slice(0, 200)}`, had404 };
|
|
172
|
+
}
|
|
173
|
+
const data = (await response.json());
|
|
174
|
+
if (data.errors && data.errors.length > 0) {
|
|
175
|
+
return { success: false, error: data.errors.map((e) => e.message).join(', '), had404 };
|
|
176
|
+
}
|
|
177
|
+
const instructions = data.data?.user?.result?.timeline?.timeline?.instructions;
|
|
178
|
+
const lists = parseListsFromInstructions(instructions);
|
|
179
|
+
return { success: true, lists, had404 };
|
|
180
|
+
}
|
|
181
|
+
catch (error) {
|
|
182
|
+
lastError = error instanceof Error ? error.message : String(error);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
return { success: false, error: lastError ?? 'Unknown error fetching list memberships', had404 };
|
|
186
|
+
};
|
|
187
|
+
const firstAttempt = await tryOnce();
|
|
188
|
+
if (firstAttempt.success) {
|
|
189
|
+
return { success: true, lists: firstAttempt.lists };
|
|
190
|
+
}
|
|
191
|
+
if (firstAttempt.had404) {
|
|
192
|
+
await this.refreshQueryIds();
|
|
193
|
+
const secondAttempt = await tryOnce();
|
|
194
|
+
if (secondAttempt.success) {
|
|
195
|
+
return { success: true, lists: secondAttempt.lists };
|
|
196
|
+
}
|
|
197
|
+
return { success: false, error: secondAttempt.error };
|
|
198
|
+
}
|
|
199
|
+
return { success: false, error: firstAttempt.error };
|
|
200
|
+
}
|
|
201
|
+
/**
|
|
202
|
+
* Get tweets from a list timeline
|
|
203
|
+
*/
|
|
204
|
+
async getListTimeline(listId, count = 20, options = {}) {
|
|
205
|
+
return this.getListTimelinePaged(listId, count, options);
|
|
206
|
+
}
|
|
207
|
+
/**
|
|
208
|
+
* Get all tweets from a list timeline (paginated)
|
|
209
|
+
*/
|
|
210
|
+
async getAllListTimeline(listId, options) {
|
|
211
|
+
return this.getListTimelinePaged(listId, Number.POSITIVE_INFINITY, options);
|
|
212
|
+
}
|
|
213
|
+
/**
|
|
214
|
+
* Internal paginated list timeline fetcher
|
|
215
|
+
*/
|
|
216
|
+
async getListTimelinePaged(listId, limit, options = {}) {
|
|
217
|
+
const features = buildListsFeatures();
|
|
218
|
+
const pageSize = 20;
|
|
219
|
+
const seen = new Set();
|
|
220
|
+
const tweets = [];
|
|
221
|
+
let cursor = options.cursor;
|
|
222
|
+
let nextCursor;
|
|
223
|
+
let pagesFetched = 0;
|
|
224
|
+
const { includeRaw = false, maxPages } = options;
|
|
225
|
+
const fetchPage = async (pageCount, pageCursor) => {
|
|
226
|
+
let lastError;
|
|
227
|
+
let had404 = false;
|
|
228
|
+
const queryIds = await this.getListTimelineQueryIds();
|
|
229
|
+
const variables = {
|
|
230
|
+
listId,
|
|
231
|
+
count: pageCount,
|
|
232
|
+
...(pageCursor ? { cursor: pageCursor } : {}),
|
|
233
|
+
};
|
|
234
|
+
const params = new URLSearchParams({
|
|
235
|
+
variables: JSON.stringify(variables),
|
|
236
|
+
features: JSON.stringify(features),
|
|
237
|
+
});
|
|
238
|
+
for (const queryId of queryIds) {
|
|
239
|
+
const url = `${TWITTER_API_BASE}/${queryId}/ListLatestTweetsTimeline?${params.toString()}`;
|
|
240
|
+
try {
|
|
241
|
+
const response = await this.fetchWithTimeout(url, {
|
|
242
|
+
method: 'GET',
|
|
243
|
+
headers: this.getHeaders(),
|
|
244
|
+
});
|
|
245
|
+
if (response.status === 404) {
|
|
246
|
+
had404 = true;
|
|
247
|
+
lastError = `HTTP ${response.status}`;
|
|
248
|
+
continue;
|
|
249
|
+
}
|
|
250
|
+
if (!response.ok) {
|
|
251
|
+
const text = await response.text();
|
|
252
|
+
return { success: false, error: `HTTP ${response.status}: ${text.slice(0, 200)}`, had404 };
|
|
253
|
+
}
|
|
254
|
+
const data = (await response.json());
|
|
255
|
+
if (data.errors && data.errors.length > 0) {
|
|
256
|
+
return { success: false, error: data.errors.map((e) => e.message).join(', '), had404 };
|
|
257
|
+
}
|
|
258
|
+
const instructions = data.data?.list?.tweets_timeline?.timeline?.instructions;
|
|
259
|
+
const pageTweets = parseTweetsFromInstructions(instructions, { quoteDepth: this.quoteDepth, includeRaw });
|
|
260
|
+
const nextCursor = extractCursorFromInstructions(instructions);
|
|
261
|
+
return { success: true, tweets: pageTweets, cursor: nextCursor, had404 };
|
|
262
|
+
}
|
|
263
|
+
catch (error) {
|
|
264
|
+
lastError = error instanceof Error ? error.message : String(error);
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
return { success: false, error: lastError ?? 'Unknown error fetching list timeline', had404 };
|
|
268
|
+
};
|
|
269
|
+
const fetchWithRefresh = async (pageCount, pageCursor) => {
|
|
270
|
+
const firstAttempt = await fetchPage(pageCount, pageCursor);
|
|
271
|
+
if (firstAttempt.success) {
|
|
272
|
+
return firstAttempt;
|
|
273
|
+
}
|
|
274
|
+
if (firstAttempt.had404) {
|
|
275
|
+
await this.refreshQueryIds();
|
|
276
|
+
const secondAttempt = await fetchPage(pageCount, pageCursor);
|
|
277
|
+
if (secondAttempt.success) {
|
|
278
|
+
return secondAttempt;
|
|
279
|
+
}
|
|
280
|
+
return { success: false, error: secondAttempt.error };
|
|
281
|
+
}
|
|
282
|
+
return { success: false, error: firstAttempt.error };
|
|
283
|
+
};
|
|
284
|
+
const unlimited = limit === Number.POSITIVE_INFINITY;
|
|
285
|
+
while (unlimited || tweets.length < limit) {
|
|
286
|
+
const pageCount = unlimited ? pageSize : Math.min(pageSize, limit - tweets.length);
|
|
287
|
+
const page = await fetchWithRefresh(pageCount, cursor);
|
|
288
|
+
if (!page.success) {
|
|
289
|
+
return { success: false, error: page.error };
|
|
290
|
+
}
|
|
291
|
+
pagesFetched += 1;
|
|
292
|
+
let added = 0;
|
|
293
|
+
for (const tweet of page.tweets) {
|
|
294
|
+
if (seen.has(tweet.id)) {
|
|
295
|
+
continue;
|
|
296
|
+
}
|
|
297
|
+
seen.add(tweet.id);
|
|
298
|
+
tweets.push(tweet);
|
|
299
|
+
added += 1;
|
|
300
|
+
if (!unlimited && tweets.length >= limit) {
|
|
301
|
+
break;
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
const pageCursor = page.cursor;
|
|
305
|
+
if (!pageCursor || pageCursor === cursor || page.tweets.length === 0 || added === 0) {
|
|
306
|
+
nextCursor = undefined;
|
|
307
|
+
break;
|
|
308
|
+
}
|
|
309
|
+
if (maxPages && pagesFetched >= maxPages) {
|
|
310
|
+
nextCursor = pageCursor;
|
|
311
|
+
break;
|
|
312
|
+
}
|
|
313
|
+
cursor = pageCursor;
|
|
314
|
+
nextCursor = pageCursor;
|
|
315
|
+
}
|
|
316
|
+
return { success: true, tweets, nextCursor };
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
return TwitterClientLists;
|
|
320
|
+
}
|
|
321
|
+
//# sourceMappingURL=twitter-client-lists.js.map
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { AbstractConstructor, Mixin, TwitterClientBase } from './twitter-client-base.js';
|
|
2
|
+
import type { UploadMediaResult } from './twitter-client-types.js';
|
|
3
|
+
export interface TwitterClientMediaMethods {
|
|
4
|
+
uploadMedia(input: {
|
|
5
|
+
data: Uint8Array;
|
|
6
|
+
mimeType: string;
|
|
7
|
+
alt?: string;
|
|
8
|
+
}): Promise<UploadMediaResult>;
|
|
9
|
+
}
|
|
10
|
+
export declare function withMedia<TBase extends AbstractConstructor<TwitterClientBase>>(Base: TBase): Mixin<TBase, TwitterClientMediaMethods>;
|
|
11
|
+
//# sourceMappingURL=twitter-client-media.d.ts.map
|