@the-convocation/twitter-scraper 0.15.0 → 0.16.0

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.
@@ -1,3 +1,5 @@
1
+ import * as _sinclair_typebox from '@sinclair/typebox';
2
+ import { Static } from '@sinclair/typebox';
1
3
  import { Cookie } from 'tough-cookie';
2
4
  import fetch from 'cross-fetch';
3
5
 
@@ -21,13 +23,6 @@ interface FetchTransformOptions {
21
23
  response: (response: Response) => Response | Promise<Response>;
22
24
  }
23
25
 
24
- declare class ApiError extends Error {
25
- readonly response: Response;
26
- readonly data: any;
27
- private constructor();
28
- static fromResponse(response: Response): Promise<ApiError>;
29
- }
30
-
31
26
  /**
32
27
  * Information about a rate-limiting event. Both the request and response
33
28
  * information are provided.
@@ -81,6 +76,185 @@ declare class ErrorRateLimitStrategy implements RateLimitStrategy {
81
76
  onRateLimit({ response: res }: RateLimitEvent): Promise<void>;
82
77
  }
83
78
 
79
+ declare class ApiError extends Error {
80
+ readonly response: Response;
81
+ readonly data: any;
82
+ constructor(response: Response, data: any, message: string);
83
+ static fromResponse(response: Response): Promise<ApiError>;
84
+ }
85
+ declare class AuthenticationError extends Error {
86
+ constructor(message?: string);
87
+ }
88
+ interface TwitterApiErrorPosition {
89
+ line: number;
90
+ column: number;
91
+ }
92
+ interface TwitterApiErrorTraceInfo {
93
+ trace_id: string;
94
+ }
95
+ interface TwitterApiErrorExtensions {
96
+ code?: number;
97
+ kind?: string;
98
+ name?: string;
99
+ source?: string;
100
+ tracing?: TwitterApiErrorTraceInfo;
101
+ }
102
+ interface TwitterApiErrorRaw extends TwitterApiErrorExtensions {
103
+ message?: string;
104
+ locations?: TwitterApiErrorPosition[];
105
+ path?: string[];
106
+ extensions?: TwitterApiErrorExtensions;
107
+ }
108
+
109
+ interface TwitterUserAuthFlowInitRequest {
110
+ flow_name: string;
111
+ input_flow_data: Record<string, unknown>;
112
+ }
113
+ interface TwitterUserAuthFlowSubtaskRequest {
114
+ flow_token: string;
115
+ subtask_inputs: ({
116
+ subtask_id: string;
117
+ } & Record<string, unknown>)[];
118
+ }
119
+ type TwitterUserAuthFlowRequest = TwitterUserAuthFlowInitRequest | TwitterUserAuthFlowSubtaskRequest;
120
+ interface TwitterUserAuthFlowResponse {
121
+ errors?: TwitterApiErrorRaw[];
122
+ flow_token?: string;
123
+ status?: string;
124
+ subtasks?: TwitterUserAuthSubtask[];
125
+ }
126
+ declare const TwitterUserAuthSubtask: _sinclair_typebox.TObject<{
127
+ subtask_id: _sinclair_typebox.TString;
128
+ enter_text: _sinclair_typebox.TOptional<_sinclair_typebox.TObject<{}>>;
129
+ }>;
130
+ type TwitterUserAuthSubtask = Static<typeof TwitterUserAuthSubtask>;
131
+ type FlowTokenResultSuccess = {
132
+ status: 'success';
133
+ response: TwitterUserAuthFlowResponse;
134
+ };
135
+ type FlowTokenResultError = {
136
+ status: 'error';
137
+ err: Error;
138
+ };
139
+ type FlowTokenResult = FlowTokenResultSuccess | FlowTokenResultError;
140
+ interface TwitterUserAuthCredentials {
141
+ username: string;
142
+ password: string;
143
+ email?: string;
144
+ twoFactorSecret?: string;
145
+ }
146
+ /**
147
+ * The API interface provided to custom subtask handlers for interacting with the Twitter authentication flow.
148
+ * This interface allows handlers to send flow requests and access the current flow token.
149
+ *
150
+ * The API is passed to each subtask handler and provides methods necessary for implementing
151
+ * custom authentication subtasks. It abstracts away the low-level details of communicating
152
+ * with Twitter's authentication API.
153
+ *
154
+ * @example
155
+ * ```typescript
156
+ * import { Scraper, FlowSubtaskHandler } from "@the-convocation/twitter-scraper";
157
+ *
158
+ * // A custom subtask handler that implements a hypothetical example subtask
159
+ * const exampleHandler: FlowSubtaskHandler = async (subtaskId, response, credentials, api) => {
160
+ * // Process the example subtask somehow
161
+ * const data = await processExampleTask();
162
+ *
163
+ * // Submit the processed data using the provided API
164
+ * return await api.sendFlowRequest({
165
+ * flow_token: api.getFlowToken(),
166
+ * subtask_inputs: [{
167
+ * subtask_id: subtaskId,
168
+ * example_data: {
169
+ * value: data,
170
+ * link: "next_link"
171
+ * }
172
+ * }]
173
+ * });
174
+ * };
175
+ *
176
+ * const scraper = new Scraper();
177
+ * scraper.registerAuthSubtaskHandler("ExampleSubtask", exampleHandler);
178
+ * ```
179
+ */
180
+ interface FlowSubtaskHandlerApi {
181
+ /**
182
+ * Send a flow request to the Twitter API.
183
+ * @param request The request object containing flow token and subtask inputs
184
+ * @returns The result of the flow task
185
+ */
186
+ sendFlowRequest: (request: TwitterUserAuthFlowRequest) => Promise<FlowTokenResult>;
187
+ /**
188
+ * Gets the current flow token.
189
+ * @returns The current flow token
190
+ */
191
+ getFlowToken: () => string;
192
+ }
193
+ /**
194
+ * A handler function for processing Twitter authentication flow subtasks.
195
+ * Library consumers can implement and register custom handlers for new or
196
+ * existing subtask types using the Scraper.registerAuthSubtaskHandler method.
197
+ *
198
+ * Each subtask handler is called when its corresponding subtask ID is encountered
199
+ * during the authentication flow. The handler receives the subtask ID, the previous
200
+ * response data, the user's credentials, and an API interface for interacting with
201
+ * the authentication flow.
202
+ *
203
+ * Handlers should process their specific subtask and return either a successful response
204
+ * or an error. Success responses typically lead to the next subtask in the flow, while
205
+ * errors will halt the authentication process.
206
+ *
207
+ * @param subtaskId - The identifier of the subtask being handled
208
+ * @param previousResponse - The complete response from the previous authentication flow step
209
+ * @param credentials - The user's authentication credentials including username, password, etc.
210
+ * @param api - An interface providing methods to interact with the authentication flow
211
+ * @returns A promise resolving to either a successful flow response or an error
212
+ *
213
+ * @example
214
+ * ```typescript
215
+ * import { Scraper, FlowSubtaskHandler } from "@the-convocation/twitter-scraper";
216
+ *
217
+ * // Custom handler for a hypothetical verification subtask
218
+ * const verificationHandler: FlowSubtaskHandler = async (
219
+ * subtaskId,
220
+ * response,
221
+ * credentials,
222
+ * api
223
+ * ) => {
224
+ * // Extract the verification data from the response
225
+ * const verificationData = response.subtasks?.[0].exampleData?.value;
226
+ * if (!verificationData) {
227
+ * return {
228
+ * status: 'error',
229
+ * err: new Error('No verification data found in response')
230
+ * };
231
+ * }
232
+ *
233
+ * // Process the verification data somehow
234
+ * const result = await processVerification(verificationData);
235
+ *
236
+ * // Submit the result using the flow API
237
+ * return await api.sendFlowRequest({
238
+ * flow_token: api.getFlowToken(),
239
+ * subtask_inputs: [{
240
+ * subtask_id: subtaskId,
241
+ * example_verification: {
242
+ * value: result,
243
+ * link: "next_link"
244
+ * }
245
+ * }]
246
+ * });
247
+ * };
248
+ *
249
+ * const scraper = new Scraper();
250
+ * scraper.registerAuthSubtaskHandler("ExampleVerificationSubtask", verificationHandler);
251
+ *
252
+ * // Later, when logging in...
253
+ * await scraper.login("username", "password");
254
+ * ```
255
+ */
256
+ type FlowSubtaskHandler = (subtaskId: string, previousResponse: TwitterUserAuthFlowResponse, credentials: TwitterUserAuthCredentials, api: FlowSubtaskHandlerApi) => Promise<FlowTokenResult>;
257
+
84
258
  interface LegacyUserRaw {
85
259
  created_at?: string;
86
260
  description?: string;
@@ -372,6 +546,13 @@ declare class Scraper {
372
546
  * - Reusing Scraper objects is recommended to minimize the time spent authenticating unnecessarily.
373
547
  */
374
548
  constructor(options?: Partial<ScraperOptions> | undefined);
549
+ /**
550
+ * Registers a subtask handler for the given subtask ID. This
551
+ * will override any existing handler for the same subtask.
552
+ * @param subtaskId The ID of the subtask to register the handler for.
553
+ * @param subtaskHandler The handler function to register.
554
+ */
555
+ registerAuthSubtaskHandler(subtaskId: string, subtaskHandler: FlowSubtaskHandler): void;
375
556
  /**
376
557
  * Fetches a Twitter profile.
377
558
  * @param username The Twitter username of the profile to fetch, without an `@` at the beginning.
@@ -598,4 +779,4 @@ declare class Scraper {
598
779
  private handleResponse;
599
780
  }
600
781
 
601
- export { ApiError, ErrorRateLimitStrategy, type FetchParameters, type FetchTransformOptions, type Mention, type Photo, type PlaceRaw, type Profile, type QueryProfilesResponse, type QueryTweetsResponse, type RateLimitEvent, type RateLimitStrategy, Scraper, type ScraperOptions, SearchMode, type Tweet, type TweetQuery, type Video, WaitingRateLimitStrategy };
782
+ export { ApiError, AuthenticationError, ErrorRateLimitStrategy, type FetchParameters, type FetchTransformOptions, type FlowSubtaskHandler, type FlowSubtaskHandlerApi, type FlowTokenResult, type FlowTokenResultError, type FlowTokenResultSuccess, type Mention, type Photo, type PlaceRaw, type Profile, type QueryProfilesResponse, type QueryTweetsResponse, type RateLimitEvent, type RateLimitStrategy, Scraper, type ScraperOptions, SearchMode, type Tweet, type TweetQuery, type TwitterApiErrorExtensions, type TwitterApiErrorPosition, type TwitterApiErrorRaw, type TwitterApiErrorTraceInfo, type TwitterUserAuthCredentials, type TwitterUserAuthFlowInitRequest, type TwitterUserAuthFlowRequest, type TwitterUserAuthFlowResponse, type TwitterUserAuthFlowSubtaskRequest, type Video, WaitingRateLimitStrategy };
package/package.json CHANGED
@@ -7,7 +7,7 @@
7
7
  "scraper",
8
8
  "crawler"
9
9
  ],
10
- "version": "0.15.0",
10
+ "version": "0.16.0",
11
11
  "main": "dist/default/cjs/index.js",
12
12
  "types": "./dist/types/index.d.ts",
13
13
  "exports": {