apify 3.0.0-alpha.2 → 3.0.0-alpha.5
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/actor.d.ts +1092 -0
- package/actor.d.ts.map +1 -0
- package/actor.js +1221 -0
- package/actor.js.map +1 -0
- package/index.d.ts +4 -0
- package/index.d.ts.map +1 -0
- package/index.js +7 -0
- package/index.js.map +1 -0
- package/index.mjs +7 -0
- package/package.json +3 -6
- package/platform_event_manager.d.ts +55 -0
- package/platform_event_manager.d.ts.map +1 -0
- package/platform_event_manager.js +116 -0
- package/platform_event_manager.js.map +1 -0
- package/proxy_configuration.d.ts +210 -0
- package/proxy_configuration.d.ts.map +1 -0
- package/proxy_configuration.js +297 -0
- package/proxy_configuration.js.map +1 -0
- package/tsconfig.build.tsbuildinfo +1 -0
- package/utils.d.ts +11 -0
- package/utils.d.ts.map +1 -0
- package/utils.js +40 -0
- package/utils.js.map +1 -0
package/actor.d.ts
ADDED
|
@@ -0,0 +1,1092 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { ActorRun as ClientActorRun, ActorStartOptions, ApifyClient, ApifyClientOptions, TaskStartOptions, Webhook, WebhookEventType } from 'apify-client';
|
|
3
|
+
import { Configuration, ConfigurationOptions, Dataset, EventManager, EventTypeName, KeyValueStore, RecordOptions, RequestList, RequestListOptions, RequestQueue, Source } from '@crawlee/core';
|
|
4
|
+
import { Awaitable, Dictionary } from '@crawlee/utils';
|
|
5
|
+
import { ProxyConfiguration, ProxyConfigurationOptions } from './proxy_configuration';
|
|
6
|
+
/**
|
|
7
|
+
* `Apify` class serves as an alternative approach to the static helpers exported from the package. It allows to pass configuration
|
|
8
|
+
* that will be used on the instance methods. Environment variables will have precedence over this configuration.
|
|
9
|
+
* See {@link Configuration} for details about what can be configured and what are the default values.
|
|
10
|
+
*/
|
|
11
|
+
export declare class Actor {
|
|
12
|
+
/** @internal */
|
|
13
|
+
static _instance: Actor;
|
|
14
|
+
/**
|
|
15
|
+
* Configuration of this SDK instance (provided to its constructor). See {@link Configuration} for details.
|
|
16
|
+
* @internal
|
|
17
|
+
*/
|
|
18
|
+
readonly config: Configuration;
|
|
19
|
+
/**
|
|
20
|
+
* Default {@link ApifyClient} instance.
|
|
21
|
+
* @internal
|
|
22
|
+
*/
|
|
23
|
+
readonly apifyClient: ApifyClient;
|
|
24
|
+
/**
|
|
25
|
+
* Default {@link EventManager} instance.
|
|
26
|
+
* @internal
|
|
27
|
+
*/
|
|
28
|
+
readonly eventManager: EventManager;
|
|
29
|
+
private readonly storageManagers;
|
|
30
|
+
constructor(options?: ConfigurationOptions);
|
|
31
|
+
/**
|
|
32
|
+
* Runs the main user function that performs the job of the actor
|
|
33
|
+
* and terminates the process when the user function finishes.
|
|
34
|
+
*
|
|
35
|
+
* **The `Actor.main()` function is optional** and is provided merely for your convenience.
|
|
36
|
+
* It is mainly useful when you're running your code as an actor on the [Apify platform](https://apify.com/actors).
|
|
37
|
+
* However, if you want to use Apify SDK tools directly inside your existing projects, e.g.
|
|
38
|
+
* running in an [Express](https://expressjs.com/) server, on
|
|
39
|
+
* [Google Cloud functions](https://cloud.google.com/functions)
|
|
40
|
+
* or [AWS Lambda](https://aws.amazon.com/lambda/), it's better to avoid
|
|
41
|
+
* it since the function terminates the main process when it finishes!
|
|
42
|
+
*
|
|
43
|
+
* The `Actor.main()` function performs the following actions:
|
|
44
|
+
*
|
|
45
|
+
* - When running on the Apify platform (i.e. `APIFY_IS_AT_HOME` environment variable is set),
|
|
46
|
+
* it sets up a connection to listen for platform events.
|
|
47
|
+
* For example, to get a notification about an imminent migration to another server.
|
|
48
|
+
* See {@link Actor.events} for details.
|
|
49
|
+
* - It checks that either `APIFY_TOKEN` or `APIFY_LOCAL_STORAGE_DIR` environment variable
|
|
50
|
+
* is defined. If not, the functions sets `APIFY_LOCAL_STORAGE_DIR` to `./apify_storage`
|
|
51
|
+
* inside the current working directory. This is to simplify running code examples.
|
|
52
|
+
* - It invokes the user function passed as the `userFunc` parameter.
|
|
53
|
+
* - If the user function returned a promise, waits for it to resolve.
|
|
54
|
+
* - If the user function throws an exception or some other error is encountered,
|
|
55
|
+
* prints error details to console so that they are stored to the log.
|
|
56
|
+
* - Exits the Node.js process, with zero exit code on success and non-zero on errors.
|
|
57
|
+
*
|
|
58
|
+
* The user function can be synchronous:
|
|
59
|
+
*
|
|
60
|
+
* ```javascript
|
|
61
|
+
* Actor.main(() => {
|
|
62
|
+
* // My synchronous function that returns immediately
|
|
63
|
+
* console.log('Hello world from actor!');
|
|
64
|
+
* });
|
|
65
|
+
* ```
|
|
66
|
+
*
|
|
67
|
+
* If the user function returns a promise, it is considered asynchronous:
|
|
68
|
+
* ```javascript
|
|
69
|
+
* const { gotScraping } = require('got-scraping');
|
|
70
|
+
*
|
|
71
|
+
* Actor.main(() => {
|
|
72
|
+
* // My asynchronous function that returns a promise
|
|
73
|
+
* return gotScraping('http://www.example.com').then((html) => {
|
|
74
|
+
* console.log(html);
|
|
75
|
+
* });
|
|
76
|
+
* });
|
|
77
|
+
* ```
|
|
78
|
+
*
|
|
79
|
+
* To simplify your code, you can take advantage of the `async`/`await` keywords:
|
|
80
|
+
*
|
|
81
|
+
* ```javascript
|
|
82
|
+
* const { gotScraping } = require('got-scraping');
|
|
83
|
+
*
|
|
84
|
+
* Actor.main(async () => {
|
|
85
|
+
* // My asynchronous function
|
|
86
|
+
* const html = await request('http://www.example.com');
|
|
87
|
+
* console.log(html);
|
|
88
|
+
* });
|
|
89
|
+
* ```
|
|
90
|
+
*
|
|
91
|
+
* @param userFunc User function to be executed. If it returns a promise,
|
|
92
|
+
* the promise will be awaited. The user function is called with no arguments.
|
|
93
|
+
* @param options
|
|
94
|
+
* @ignore
|
|
95
|
+
*/
|
|
96
|
+
main<T>(userFunc: UserFunc, options?: MainOptions): Promise<T>;
|
|
97
|
+
/**
|
|
98
|
+
* @ignore
|
|
99
|
+
*/
|
|
100
|
+
init(): Promise<void>;
|
|
101
|
+
/**
|
|
102
|
+
* @ignore
|
|
103
|
+
*/
|
|
104
|
+
exit(options?: ExitOptions): Promise<void>;
|
|
105
|
+
/**
|
|
106
|
+
* @ignore
|
|
107
|
+
*/
|
|
108
|
+
on(event: EventTypeName, listener: (...args: any[]) => any): void;
|
|
109
|
+
/**
|
|
110
|
+
* @ignore
|
|
111
|
+
*/
|
|
112
|
+
off(event: EventTypeName, listener?: (...args: any[]) => any): void;
|
|
113
|
+
/**
|
|
114
|
+
* Runs an actor on the Apify platform using the current user account (determined by the `APIFY_TOKEN` environment variable),
|
|
115
|
+
* waits for the actor to finish and fetches its output.
|
|
116
|
+
*
|
|
117
|
+
* By passing the `waitSecs` option you can reduce the maximum amount of time to wait for the run to finish.
|
|
118
|
+
* If the value is less than or equal to zero, the function returns immediately after the run is started.
|
|
119
|
+
*
|
|
120
|
+
* The result of the function is an {@link ActorRun} object
|
|
121
|
+
* that contains details about the actor run and its output (if any).
|
|
122
|
+
*
|
|
123
|
+
* If you want to run an actor task rather than an actor, please use the
|
|
124
|
+
* {@link Actor.callTask} function instead.
|
|
125
|
+
*
|
|
126
|
+
* For more information about actors, read the
|
|
127
|
+
* [documentation](https://docs.apify.com/actor).
|
|
128
|
+
*
|
|
129
|
+
* **Example usage:**
|
|
130
|
+
*
|
|
131
|
+
* ```javascript
|
|
132
|
+
* const run = await Actor.call('apify/hello-world', { myInput: 123 });
|
|
133
|
+
* console.log(`Received message: ${run.output.body.message}`);
|
|
134
|
+
* ```
|
|
135
|
+
*
|
|
136
|
+
* Internally, the `call()` function invokes the
|
|
137
|
+
* [Run actor](https://apify.com/docs/api/v2#/reference/actors/run-collection/run-actor)
|
|
138
|
+
* and several other API endpoints to obtain the output.
|
|
139
|
+
*
|
|
140
|
+
* @param actId
|
|
141
|
+
* Allowed formats are `username/actor-name`, `userId/actor-name` or actor ID.
|
|
142
|
+
* @param [input]
|
|
143
|
+
* Input for the actor. If it is an object, it will be stringified to
|
|
144
|
+
* JSON and its content type set to `application/json; charset=utf-8`.
|
|
145
|
+
* Otherwise the `options.contentType` parameter must be provided.
|
|
146
|
+
* @param [options]
|
|
147
|
+
* @ignore
|
|
148
|
+
*/
|
|
149
|
+
call(actId: string, input?: unknown, options?: CallOptions): Promise<ClientActorRun>;
|
|
150
|
+
/**
|
|
151
|
+
* Runs an actor task on the Apify platform using the current user account (determined by the `APIFY_TOKEN` environment variable),
|
|
152
|
+
* waits for the task to finish and fetches its output.
|
|
153
|
+
*
|
|
154
|
+
* By passing the `waitSecs` option you can reduce the maximum amount of time to wait for the run to finish.
|
|
155
|
+
* If the value is less than or equal to zero, the function returns immediately after the run is started.
|
|
156
|
+
*
|
|
157
|
+
* The result of the function is an {@link ActorRun} object
|
|
158
|
+
* that contains details about the actor run and its output (if any).
|
|
159
|
+
*
|
|
160
|
+
* Note that an actor task is a saved input configuration and options for an actor.
|
|
161
|
+
* If you want to run an actor directly rather than an actor task, please use the
|
|
162
|
+
* {@link Actor.call} function instead.
|
|
163
|
+
*
|
|
164
|
+
* For more information about actor tasks, read the [documentation](https://docs.apify.com/tasks).
|
|
165
|
+
*
|
|
166
|
+
* **Example usage:**
|
|
167
|
+
*
|
|
168
|
+
* ```javascript
|
|
169
|
+
* const run = await Actor.callTask('bob/some-task');
|
|
170
|
+
* console.log(`Received message: ${run.output.body.message}`);
|
|
171
|
+
* ```
|
|
172
|
+
*
|
|
173
|
+
* Internally, the `callTask()` function calls the
|
|
174
|
+
* [Run task](https://apify.com/docs/api/v2#/reference/actor-tasks/run-collection/run-task)
|
|
175
|
+
* and several other API endpoints to obtain the output.
|
|
176
|
+
*
|
|
177
|
+
* @param taskId
|
|
178
|
+
* Allowed formats are `username/task-name`, `userId/task-name` or task ID.
|
|
179
|
+
* @param [input]
|
|
180
|
+
* Input overrides for the actor task. If it is an object, it will be stringified to
|
|
181
|
+
* JSON and its content type set to `application/json; charset=utf-8`.
|
|
182
|
+
* Provided input will be merged with actor task input.
|
|
183
|
+
* @param [options]
|
|
184
|
+
* @ignore
|
|
185
|
+
*/
|
|
186
|
+
callTask(taskId: string, input?: Dictionary, options?: CallTaskOptions): Promise<ClientActorRun>;
|
|
187
|
+
/**
|
|
188
|
+
* Transforms this actor run to an actor run of a given actor. The system stops the current container and starts
|
|
189
|
+
* the new container instead. All the default storages are preserved and the new input is stored under the `INPUT-METAMORPH-1` key
|
|
190
|
+
* in the same default key-value store.
|
|
191
|
+
*
|
|
192
|
+
* @param targetActorId
|
|
193
|
+
* Either `username/actor-name` or actor ID of an actor to which we want to metamorph.
|
|
194
|
+
* @param [input]
|
|
195
|
+
* Input for the actor. If it is an object, it will be stringified to
|
|
196
|
+
* JSON and its content type set to `application/json; charset=utf-8`.
|
|
197
|
+
* Otherwise, the `options.contentType` parameter must be provided.
|
|
198
|
+
* @param [options]
|
|
199
|
+
* @ignore
|
|
200
|
+
*/
|
|
201
|
+
metamorph(targetActorId: string, input?: unknown, options?: MetamorphOptions): Promise<void>;
|
|
202
|
+
/**
|
|
203
|
+
* Internally reboots this actor. The system stops the current container and starts
|
|
204
|
+
* a new container with the same run ID.
|
|
205
|
+
*
|
|
206
|
+
* @ignore
|
|
207
|
+
*/
|
|
208
|
+
reboot(): Promise<void>;
|
|
209
|
+
/**
|
|
210
|
+
* Creates an ad-hoc webhook for the current actor run, which lets you receive a notification when the actor run finished or failed.
|
|
211
|
+
* For more information about Apify actor webhooks, please see the [documentation](https://docs.apify.com/webhooks).
|
|
212
|
+
*
|
|
213
|
+
* Note that webhooks are only supported for actors running on the Apify platform.
|
|
214
|
+
* In local environment, the function will print a warning and have no effect.
|
|
215
|
+
*
|
|
216
|
+
* @param options
|
|
217
|
+
* @returns The return value is the Webhook object.
|
|
218
|
+
* For more information, see the [Get webhook](https://apify.com/docs/api/v2#/reference/webhooks/webhook-object/get-webhook) API endpoint.
|
|
219
|
+
* @ignore
|
|
220
|
+
*/
|
|
221
|
+
addWebhook(options: WebhookOptions): Promise<Webhook | undefined>;
|
|
222
|
+
/**
|
|
223
|
+
* Stores an object or an array of objects to the default {@link Dataset} of the current actor run.
|
|
224
|
+
*
|
|
225
|
+
* This is just a convenient shortcut for {@link Dataset.pushData}.
|
|
226
|
+
* For example, calling the following code:
|
|
227
|
+
* ```javascript
|
|
228
|
+
* await Actor.pushData({ myValue: 123 });
|
|
229
|
+
* ```
|
|
230
|
+
*
|
|
231
|
+
* is equivalent to:
|
|
232
|
+
* ```javascript
|
|
233
|
+
* const dataset = await Actor.openDataset();
|
|
234
|
+
* await dataset.pushData({ myValue: 123 });
|
|
235
|
+
* ```
|
|
236
|
+
*
|
|
237
|
+
* For more information, see {@link Actor.openDataset} and {@link Dataset.pushData}
|
|
238
|
+
*
|
|
239
|
+
* **IMPORTANT**: Make sure to use the `await` keyword when calling `pushData()`,
|
|
240
|
+
* otherwise the actor process might finish before the data are stored!
|
|
241
|
+
*
|
|
242
|
+
* @param item Object or array of objects containing data to be stored in the default dataset.
|
|
243
|
+
* The objects must be serializable to JSON and the JSON representation of each object must be smaller than 9MB.
|
|
244
|
+
* @ignore
|
|
245
|
+
*/
|
|
246
|
+
pushData(item: Dictionary): Promise<void>;
|
|
247
|
+
/**
|
|
248
|
+
* Opens a dataset and returns a promise resolving to an instance of the {@link Dataset} class.
|
|
249
|
+
*
|
|
250
|
+
* Datasets are used to store structured data where each object stored has the same attributes,
|
|
251
|
+
* such as online store products or real estate offers.
|
|
252
|
+
* The actual data is stored either on the local filesystem or in the cloud.
|
|
253
|
+
*
|
|
254
|
+
* For more details and code examples, see the {@link Dataset} class.
|
|
255
|
+
*
|
|
256
|
+
* @param [datasetIdOrName]
|
|
257
|
+
* ID or name of the dataset to be opened. If `null` or `undefined`,
|
|
258
|
+
* the function returns the default dataset associated with the actor run.
|
|
259
|
+
* @param [options]
|
|
260
|
+
* @ignore
|
|
261
|
+
*/
|
|
262
|
+
openDataset<Data extends Dictionary = Dictionary>(datasetIdOrName?: string | null, options?: OpenStorageOptions): Promise<Dataset<Data>>;
|
|
263
|
+
/**
|
|
264
|
+
* Gets a value from the default {@link KeyValueStore} associated with the current actor run.
|
|
265
|
+
*
|
|
266
|
+
* This is just a convenient shortcut for {@link KeyValueStore.getValue}.
|
|
267
|
+
* For example, calling the following code:
|
|
268
|
+
* ```javascript
|
|
269
|
+
* const value = await Actor.getValue('my-key');
|
|
270
|
+
* ```
|
|
271
|
+
*
|
|
272
|
+
* is equivalent to:
|
|
273
|
+
* ```javascript
|
|
274
|
+
* const store = await Actor.openKeyValueStore();
|
|
275
|
+
* const value = await store.getValue('my-key');
|
|
276
|
+
* ```
|
|
277
|
+
*
|
|
278
|
+
* To store the value to the default key-value store, you can use the {@link Actor.setValue} function.
|
|
279
|
+
*
|
|
280
|
+
* For more information, see {@link Actor.openKeyValueStore}
|
|
281
|
+
* and {@link KeyValueStore.getValue}.
|
|
282
|
+
*
|
|
283
|
+
* @param key Unique record key.
|
|
284
|
+
* @returns
|
|
285
|
+
* Returns a promise that resolves to an object, string
|
|
286
|
+
* or [`Buffer`](https://nodejs.org/api/buffer.html), depending
|
|
287
|
+
* on the MIME content type of the record, or `null`
|
|
288
|
+
* if the record is missing.
|
|
289
|
+
* @ignore
|
|
290
|
+
*/
|
|
291
|
+
getValue<T = unknown>(key: string): Promise<T | null>;
|
|
292
|
+
/**
|
|
293
|
+
* Stores or deletes a value in the default {@link KeyValueStore} associated with the current actor run.
|
|
294
|
+
*
|
|
295
|
+
* This is just a convenient shortcut for {@link KeyValueStore.setValue}.
|
|
296
|
+
* For example, calling the following code:
|
|
297
|
+
* ```javascript
|
|
298
|
+
* await Actor.setValue('OUTPUT', { foo: "bar" });
|
|
299
|
+
* ```
|
|
300
|
+
*
|
|
301
|
+
* is equivalent to:
|
|
302
|
+
* ```javascript
|
|
303
|
+
* const store = await Actor.openKeyValueStore();
|
|
304
|
+
* await store.setValue('OUTPUT', { foo: "bar" });
|
|
305
|
+
* ```
|
|
306
|
+
*
|
|
307
|
+
* To get a value from the default key-value store, you can use the {@link Actor.getValue} function.
|
|
308
|
+
*
|
|
309
|
+
* For more information, see {@link Actor.openKeyValueStore}
|
|
310
|
+
* and {@link KeyValueStore.getValue}.
|
|
311
|
+
*
|
|
312
|
+
* @param key
|
|
313
|
+
* Unique record key.
|
|
314
|
+
* @param value
|
|
315
|
+
* Record data, which can be one of the following values:
|
|
316
|
+
* - If `null`, the record in the key-value store is deleted.
|
|
317
|
+
* - If no `options.contentType` is specified, `value` can be any JavaScript object, and it will be stringified to JSON.
|
|
318
|
+
* - If `options.contentType` is set, `value` is taken as is, and it must be a `String` or [`Buffer`](https://nodejs.org/api/buffer.html).
|
|
319
|
+
* For any other value an error will be thrown.
|
|
320
|
+
* @param [options]
|
|
321
|
+
* @ignore
|
|
322
|
+
*/
|
|
323
|
+
setValue<T>(key: string, value: T | null, options?: RecordOptions): Promise<void>;
|
|
324
|
+
/**
|
|
325
|
+
* Gets the actor input value from the default {@link KeyValueStore} associated with the current actor run.
|
|
326
|
+
*
|
|
327
|
+
* This is just a convenient shortcut for [`keyValueStore.getValue('INPUT')`](core/class/KeyValueStore#getValue).
|
|
328
|
+
* For example, calling the following code:
|
|
329
|
+
* ```javascript
|
|
330
|
+
* const input = await Actor.getInput();
|
|
331
|
+
* ```
|
|
332
|
+
*
|
|
333
|
+
* is equivalent to:
|
|
334
|
+
* ```javascript
|
|
335
|
+
* const store = await Actor.openKeyValueStore();
|
|
336
|
+
* await store.getValue('INPUT');
|
|
337
|
+
* ```
|
|
338
|
+
*
|
|
339
|
+
* Note that the `getInput()` function does not cache the value read from the key-value store.
|
|
340
|
+
* If you need to use the input multiple times in your actor,
|
|
341
|
+
* it is far more efficient to read it once and store it locally.
|
|
342
|
+
*
|
|
343
|
+
* For more information, see {@link Actor.openKeyValueStore}
|
|
344
|
+
* and {@link KeyValueStore.getValue}.
|
|
345
|
+
*
|
|
346
|
+
* @returns
|
|
347
|
+
* Returns a promise that resolves to an object, string
|
|
348
|
+
* or [`Buffer`](https://nodejs.org/api/buffer.html), depending
|
|
349
|
+
* on the MIME content type of the record, or `null`
|
|
350
|
+
* if the record is missing.
|
|
351
|
+
* @ignore
|
|
352
|
+
*/
|
|
353
|
+
getInput<T extends Dictionary | string | Buffer>(): Promise<T | null>;
|
|
354
|
+
/**
|
|
355
|
+
* Opens a key-value store and returns a promise resolving to an instance of the {@link KeyValueStore} class.
|
|
356
|
+
*
|
|
357
|
+
* Key-value stores are used to store records or files, along with their MIME content type.
|
|
358
|
+
* The records are stored and retrieved using a unique key.
|
|
359
|
+
* The actual data is stored either on a local filesystem or in the Apify cloud.
|
|
360
|
+
*
|
|
361
|
+
* For more details and code examples, see the {@link KeyValueStore} class.
|
|
362
|
+
*
|
|
363
|
+
* @param [storeIdOrName]
|
|
364
|
+
* ID or name of the key-value store to be opened. If `null` or `undefined`,
|
|
365
|
+
* the function returns the default key-value store associated with the actor run.
|
|
366
|
+
* @param [options]
|
|
367
|
+
* @ignore
|
|
368
|
+
*/
|
|
369
|
+
openKeyValueStore(storeIdOrName?: string | null, options?: OpenStorageOptions): Promise<KeyValueStore>;
|
|
370
|
+
/**
|
|
371
|
+
* Opens a request list and returns a promise resolving to an instance
|
|
372
|
+
* of the {@link RequestList} class that is already initialized.
|
|
373
|
+
*
|
|
374
|
+
* {@link RequestList} represents a list of URLs to crawl, which is always stored in memory.
|
|
375
|
+
* To enable picking up where left off after a process restart, the request list sources
|
|
376
|
+
* are persisted to the key-value store at initialization of the list. Then, while crawling,
|
|
377
|
+
* a small state object is regularly persisted to keep track of the crawling status.
|
|
378
|
+
*
|
|
379
|
+
* For more details and code examples, see the {@link RequestList} class.
|
|
380
|
+
*
|
|
381
|
+
* **Example usage:**
|
|
382
|
+
*
|
|
383
|
+
* ```javascript
|
|
384
|
+
* const sources = [
|
|
385
|
+
* 'https://www.example.com',
|
|
386
|
+
* 'https://www.google.com',
|
|
387
|
+
* 'https://www.bing.com'
|
|
388
|
+
* ];
|
|
389
|
+
*
|
|
390
|
+
* const requestList = await RequestList.open('my-name', sources);
|
|
391
|
+
* ```
|
|
392
|
+
*
|
|
393
|
+
* @param listName
|
|
394
|
+
* Name of the request list to be opened. Setting a name enables the `RequestList`'s state to be persisted
|
|
395
|
+
* in the key-value store. This is useful in case of a restart or migration. Since `RequestList` is only
|
|
396
|
+
* stored in memory, a restart or migration wipes it clean. Setting a name will enable the `RequestList`'s
|
|
397
|
+
* state to survive those situations and continue where it left off.
|
|
398
|
+
*
|
|
399
|
+
* The name will be used as a prefix in key-value store, producing keys such as `NAME-REQUEST_LIST_STATE`
|
|
400
|
+
* and `NAME-REQUEST_LIST_SOURCES`.
|
|
401
|
+
*
|
|
402
|
+
* If `null`, the list will not be persisted and will only be stored in memory. Process restart
|
|
403
|
+
* will then cause the list to be crawled again from the beginning. We suggest always using a name.
|
|
404
|
+
* @param sources
|
|
405
|
+
* An array of sources of URLs for the {@link RequestList}. It can be either an array of strings,
|
|
406
|
+
* plain objects that define at least the `url` property, or an array of {@link Request} instances.
|
|
407
|
+
*
|
|
408
|
+
* **IMPORTANT:** The `sources` array will be consumed (left empty) after {@link RequestList} initializes.
|
|
409
|
+
* This is a measure to prevent memory leaks in situations when millions of sources are
|
|
410
|
+
* added.
|
|
411
|
+
*
|
|
412
|
+
* Additionally, the `requestsFromUrl` property may be used instead of `url`,
|
|
413
|
+
* which will instruct {@link RequestList} to download the source URLs from a given remote location.
|
|
414
|
+
* The URLs will be parsed from the received response. In this case you can limit the URLs
|
|
415
|
+
* using `regex` parameter containing regular expression pattern for URLs to be included.
|
|
416
|
+
*
|
|
417
|
+
* For details, see the {@link RequestListOptions.sources}
|
|
418
|
+
* @param [options]
|
|
419
|
+
* The {@link RequestList} options. Note that the `listName` parameter supersedes
|
|
420
|
+
* the {@link RequestListOptions.persistStateKey} and {@link RequestListOptions.persistRequestsKey}
|
|
421
|
+
* options and the `sources` parameter supersedes the {@link RequestListOptions.sources} option.
|
|
422
|
+
* @ignore
|
|
423
|
+
*/
|
|
424
|
+
openRequestList(listName: string | null, sources: Source[], options?: RequestListOptions): Promise<RequestList>;
|
|
425
|
+
/**
|
|
426
|
+
* Opens a request queue and returns a promise resolving to an instance
|
|
427
|
+
* of the {@link RequestQueue} class.
|
|
428
|
+
*
|
|
429
|
+
* {@link RequestQueue} represents a queue of URLs to crawl, which is stored either on local filesystem or in the cloud.
|
|
430
|
+
* The queue is used for deep crawling of websites, where you start with several URLs and then
|
|
431
|
+
* recursively follow links to other pages. The data structure supports both breadth-first
|
|
432
|
+
* and depth-first crawling orders.
|
|
433
|
+
*
|
|
434
|
+
* For more details and code examples, see the {@link RequestQueue} class.
|
|
435
|
+
*
|
|
436
|
+
* @param [queueIdOrName]
|
|
437
|
+
* ID or name of the request queue to be opened. If `null` or `undefined`,
|
|
438
|
+
* the function returns the default request queue associated with the actor run.
|
|
439
|
+
* @param [options]
|
|
440
|
+
* @ignore
|
|
441
|
+
*/
|
|
442
|
+
openRequestQueue(queueIdOrName?: string | null, options?: OpenStorageOptions): Promise<RequestQueue>;
|
|
443
|
+
/**
|
|
444
|
+
* Creates a proxy configuration and returns a promise resolving to an instance
|
|
445
|
+
* of the {@link ProxyConfiguration} class that is already initialized.
|
|
446
|
+
*
|
|
447
|
+
* Configures connection to a proxy server with the provided options. Proxy servers are used to prevent target websites from blocking
|
|
448
|
+
* your crawlers based on IP address rate limits or blacklists. Setting proxy configuration in your crawlers automatically configures
|
|
449
|
+
* them to use the selected proxies for all connections.
|
|
450
|
+
*
|
|
451
|
+
* For more details and code examples, see the {@link ProxyConfiguration} class.
|
|
452
|
+
*
|
|
453
|
+
* ```javascript
|
|
454
|
+
*
|
|
455
|
+
* // Returns initialized proxy configuration class
|
|
456
|
+
* const proxyConfiguration = await Actor.createProxyConfiguration({
|
|
457
|
+
* groups: ['GROUP1', 'GROUP2'] // List of Apify proxy groups
|
|
458
|
+
* countryCode: 'US'
|
|
459
|
+
* });
|
|
460
|
+
*
|
|
461
|
+
* const crawler = new CheerioCrawler({
|
|
462
|
+
* // ...
|
|
463
|
+
* proxyConfiguration,
|
|
464
|
+
* handlePageFunction: ({ proxyInfo }) => {
|
|
465
|
+
* const usedProxyUrl = proxyInfo.url; // Getting the proxy URL
|
|
466
|
+
* }
|
|
467
|
+
* })
|
|
468
|
+
*
|
|
469
|
+
* ```
|
|
470
|
+
*
|
|
471
|
+
* For compatibility with existing Actor Input UI (Input Schema), this function
|
|
472
|
+
* returns `undefined` when the following object is passed as `proxyConfigurationOptions`.
|
|
473
|
+
*
|
|
474
|
+
* ```
|
|
475
|
+
* { useApifyProxy: false }
|
|
476
|
+
* ```
|
|
477
|
+
* @ignore
|
|
478
|
+
*/
|
|
479
|
+
createProxyConfiguration(proxyConfigurationOptions?: ProxyConfigurationOptions & {
|
|
480
|
+
useApifyProxy?: boolean;
|
|
481
|
+
}): Promise<ProxyConfiguration | undefined>;
|
|
482
|
+
/**
|
|
483
|
+
* Returns a new {@link ApifyEnv} object which contains information parsed from all the `APIFY_XXX` environment variables.
|
|
484
|
+
*
|
|
485
|
+
* For the list of the `APIFY_XXX` environment variables, see
|
|
486
|
+
* [Actor documentation](https://docs.apify.com/actor/run#environment-variables).
|
|
487
|
+
* If some variables are not defined or are invalid, the corresponding value in the resulting object will be null.
|
|
488
|
+
* @ignore
|
|
489
|
+
*/
|
|
490
|
+
getEnv(): ApifyEnv;
|
|
491
|
+
/**
|
|
492
|
+
* Returns a new instance of the Apify API client. The `ApifyClient` class is provided
|
|
493
|
+
* by the [apify-client](https://www.npmjs.com/package/apify-client)
|
|
494
|
+
* NPM package, and it is automatically configured using the `APIFY_API_BASE_URL`, and `APIFY_TOKEN`
|
|
495
|
+
* environment variables. You can override the token via the available options. That's useful
|
|
496
|
+
* if you want to use the client as a different Apify user than the SDK internals are using.
|
|
497
|
+
* @ignore
|
|
498
|
+
*/
|
|
499
|
+
newClient(options?: ApifyClientOptions): ApifyClient;
|
|
500
|
+
/**
|
|
501
|
+
* Returns `true` when code is running on Apify platform and `false` otherwise (for example locally).
|
|
502
|
+
* @ignore
|
|
503
|
+
*/
|
|
504
|
+
isAtHome(): boolean;
|
|
505
|
+
/**
|
|
506
|
+
* Runs the main user function that performs the job of the actor
|
|
507
|
+
* and terminates the process when the user function finishes.
|
|
508
|
+
*
|
|
509
|
+
* **The `Actor.main()` function is optional** and is provided merely for your convenience.
|
|
510
|
+
* It is mainly useful when you're running your code as an actor on the [Apify platform](https://apify.com/actors).
|
|
511
|
+
* However, if you want to use Apify SDK tools directly inside your existing projects, e.g.
|
|
512
|
+
* running in an [Express](https://expressjs.com/) server, on
|
|
513
|
+
* [Google Cloud functions](https://cloud.google.com/functions)
|
|
514
|
+
* or [AWS Lambda](https://aws.amazon.com/lambda/), it's better to avoid
|
|
515
|
+
* it since the function terminates the main process when it finishes!
|
|
516
|
+
*
|
|
517
|
+
* The `Actor.main()` function performs the following actions:
|
|
518
|
+
*
|
|
519
|
+
* - When running on the Apify platform (i.e. `APIFY_IS_AT_HOME` environment variable is set),
|
|
520
|
+
* it sets up a connection to listen for platform events.
|
|
521
|
+
* For example, to get a notification about an imminent migration to another server.
|
|
522
|
+
* See {@link Actor.events} for details.
|
|
523
|
+
* - It checks that either `APIFY_TOKEN` or `APIFY_LOCAL_STORAGE_DIR` environment variable
|
|
524
|
+
* is defined. If not, the functions sets `APIFY_LOCAL_STORAGE_DIR` to `./apify_storage`
|
|
525
|
+
* inside the current working directory. This is to simplify running code examples.
|
|
526
|
+
* - It invokes the user function passed as the `userFunc` parameter.
|
|
527
|
+
* - If the user function returned a promise, waits for it to resolve.
|
|
528
|
+
* - If the user function throws an exception or some other error is encountered,
|
|
529
|
+
* prints error details to console so that they are stored to the log.
|
|
530
|
+
* - Exits the Node.js process, with zero exit code on success and non-zero on errors.
|
|
531
|
+
*
|
|
532
|
+
* The user function can be synchronous:
|
|
533
|
+
*
|
|
534
|
+
* ```javascript
|
|
535
|
+
* Actor.main(() => {
|
|
536
|
+
* // My synchronous function that returns immediately
|
|
537
|
+
* console.log('Hello world from actor!');
|
|
538
|
+
* });
|
|
539
|
+
* ```
|
|
540
|
+
*
|
|
541
|
+
* If the user function returns a promise, it is considered asynchronous:
|
|
542
|
+
* ```javascript
|
|
543
|
+
* const { gotScraping } = require('got-scraping');
|
|
544
|
+
*
|
|
545
|
+
* Actor.main(() => {
|
|
546
|
+
* // My asynchronous function that returns a promise
|
|
547
|
+
* return gotScraping('http://www.example.com').then((html) => {
|
|
548
|
+
* console.log(html);
|
|
549
|
+
* });
|
|
550
|
+
* });
|
|
551
|
+
* ```
|
|
552
|
+
*
|
|
553
|
+
* To simplify your code, you can take advantage of the `async`/`await` keywords:
|
|
554
|
+
*
|
|
555
|
+
* ```javascript
|
|
556
|
+
* const { gotScraping } = require('got-scraping');
|
|
557
|
+
*
|
|
558
|
+
* Actor.main(async () => {
|
|
559
|
+
* // My asynchronous function
|
|
560
|
+
* const html = await gotScraping('http://www.example.com');
|
|
561
|
+
* console.log(html);
|
|
562
|
+
* });
|
|
563
|
+
* ```
|
|
564
|
+
*
|
|
565
|
+
* @param userFunc User function to be executed. If it returns a promise,
|
|
566
|
+
* the promise will be awaited. The user function is called with no arguments.
|
|
567
|
+
* @param options
|
|
568
|
+
*/
|
|
569
|
+
static main<T>(userFunc: UserFunc<T>, options?: MainOptions): Promise<T>;
|
|
570
|
+
static init(): Promise<void>;
|
|
571
|
+
static exit(options?: ExitOptions): Promise<void>;
|
|
572
|
+
static on(event: EventTypeName, listener: (...args: any[]) => any): void;
|
|
573
|
+
static off(event: EventTypeName, listener?: (...args: any[]) => any): void;
|
|
574
|
+
/**
|
|
575
|
+
* Runs an actor on the Apify platform using the current user account (determined by the `APIFY_TOKEN` environment variable),
|
|
576
|
+
* waits for the actor to finish and fetches its output.
|
|
577
|
+
*
|
|
578
|
+
* By passing the `waitSecs` option you can reduce the maximum amount of time to wait for the run to finish.
|
|
579
|
+
* If the value is less than or equal to zero, the function returns immediately after the run is started.
|
|
580
|
+
*
|
|
581
|
+
* The result of the function is an {@link ActorRun} object
|
|
582
|
+
* that contains details about the actor run and its output (if any).
|
|
583
|
+
*
|
|
584
|
+
* If you want to run an actor task rather than an actor, please use the
|
|
585
|
+
* {@link Actor.callTask} function instead.
|
|
586
|
+
*
|
|
587
|
+
* For more information about actors, read the
|
|
588
|
+
* [documentation](https://docs.apify.com/actor).
|
|
589
|
+
*
|
|
590
|
+
* **Example usage:**
|
|
591
|
+
*
|
|
592
|
+
* ```javascript
|
|
593
|
+
* const run = await Actor.call('apify/hello-world', { myInput: 123 });
|
|
594
|
+
* console.log(`Received message: ${run.output.body.message}`);
|
|
595
|
+
* ```
|
|
596
|
+
*
|
|
597
|
+
* Internally, the `call()` function invokes the
|
|
598
|
+
* [Run actor](https://apify.com/docs/api/v2#/reference/actors/run-collection/run-actor)
|
|
599
|
+
* and several other API endpoints to obtain the output.
|
|
600
|
+
*
|
|
601
|
+
* @param actId
|
|
602
|
+
* Allowed formats are `username/actor-name`, `userId/actor-name` or actor ID.
|
|
603
|
+
* @param [input]
|
|
604
|
+
* Input for the actor. If it is an object, it will be stringified to
|
|
605
|
+
* JSON and its content type set to `application/json; charset=utf-8`.
|
|
606
|
+
* Otherwise the `options.contentType` parameter must be provided.
|
|
607
|
+
* @param [options]
|
|
608
|
+
*/
|
|
609
|
+
static call(actId: string, input?: unknown, options?: CallOptions): Promise<ClientActorRun>;
|
|
610
|
+
/**
|
|
611
|
+
* Runs an actor task on the Apify platform using the current user account (determined by the `APIFY_TOKEN` environment variable),
|
|
612
|
+
* waits for the task to finish and fetches its output.
|
|
613
|
+
*
|
|
614
|
+
* By passing the `waitSecs` option you can reduce the maximum amount of time to wait for the run to finish.
|
|
615
|
+
* If the value is less than or equal to zero, the function returns immediately after the run is started.
|
|
616
|
+
*
|
|
617
|
+
* The result of the function is an {@link ActorRun} object
|
|
618
|
+
* that contains details about the actor run and its output (if any).
|
|
619
|
+
*
|
|
620
|
+
* Note that an actor task is a saved input configuration and options for an actor.
|
|
621
|
+
* If you want to run an actor directly rather than an actor task, please use the
|
|
622
|
+
* {@link Actor.call} function instead.
|
|
623
|
+
*
|
|
624
|
+
* For more information about actor tasks, read the [documentation](https://docs.apify.com/tasks).
|
|
625
|
+
*
|
|
626
|
+
* **Example usage:**
|
|
627
|
+
*
|
|
628
|
+
* ```javascript
|
|
629
|
+
* const run = await Actor.callTask('bob/some-task');
|
|
630
|
+
* console.log(`Received message: ${run.output.body.message}`);
|
|
631
|
+
* ```
|
|
632
|
+
*
|
|
633
|
+
* Internally, the `callTask()` function calls the
|
|
634
|
+
* [Run task](https://apify.com/docs/api/v2#/reference/actor-tasks/run-collection/run-task)
|
|
635
|
+
* and several other API endpoints to obtain the output.
|
|
636
|
+
*
|
|
637
|
+
* @param taskId
|
|
638
|
+
* Allowed formats are `username/task-name`, `userId/task-name` or task ID.
|
|
639
|
+
* @param [input]
|
|
640
|
+
* Input overrides for the actor task. If it is an object, it will be stringified to
|
|
641
|
+
* JSON and its content type set to `application/json; charset=utf-8`.
|
|
642
|
+
* Provided input will be merged with actor task input.
|
|
643
|
+
* @param [options]
|
|
644
|
+
*/
|
|
645
|
+
static callTask(taskId: string, input?: Dictionary, options?: CallTaskOptions): Promise<ClientActorRun>;
|
|
646
|
+
/**
|
|
647
|
+
* Transforms this actor run to an actor run of a given actor. The system stops the current container and starts
|
|
648
|
+
* the new container instead. All the default storages are preserved and the new input is stored under the `INPUT-METAMORPH-1` key
|
|
649
|
+
* in the same default key-value store.
|
|
650
|
+
*
|
|
651
|
+
* @param targetActorId
|
|
652
|
+
* Either `username/actor-name` or actor ID of an actor to which we want to metamorph.
|
|
653
|
+
* @param [input]
|
|
654
|
+
* Input for the actor. If it is an object, it will be stringified to
|
|
655
|
+
* JSON and its content type set to `application/json; charset=utf-8`.
|
|
656
|
+
* Otherwise, the `options.contentType` parameter must be provided.
|
|
657
|
+
* @param [options]
|
|
658
|
+
*/
|
|
659
|
+
static metamorph(targetActorId: string, input?: unknown, options?: MetamorphOptions): Promise<void>;
|
|
660
|
+
/**
|
|
661
|
+
* Internally reboots this actor run. The system stops the current container and starts
|
|
662
|
+
* a new container with the same run id.
|
|
663
|
+
*/
|
|
664
|
+
static reboot(): Promise<void>;
|
|
665
|
+
/**
|
|
666
|
+
* Creates an ad-hoc webhook for the current actor run, which lets you receive a notification when the actor run finished or failed.
|
|
667
|
+
* For more information about Apify actor webhooks, please see the [documentation](https://docs.apify.com/webhooks).
|
|
668
|
+
*
|
|
669
|
+
* Note that webhooks are only supported for actors running on the Apify platform.
|
|
670
|
+
* In local environment, the function will print a warning and have no effect.
|
|
671
|
+
*
|
|
672
|
+
* @param options
|
|
673
|
+
* @returns The return value is the Webhook object.
|
|
674
|
+
* For more information, see the [Get webhook](https://apify.com/docs/api/v2#/reference/webhooks/webhook-object/get-webhook) API endpoint.
|
|
675
|
+
*/
|
|
676
|
+
static addWebhook(options: WebhookOptions): Promise<Webhook | undefined>;
|
|
677
|
+
/**
|
|
678
|
+
* Stores an object or an array of objects to the default {@link Dataset} of the current actor run.
|
|
679
|
+
*
|
|
680
|
+
* This is just a convenient shortcut for {@link Dataset.pushData}.
|
|
681
|
+
* For example, calling the following code:
|
|
682
|
+
* ```javascript
|
|
683
|
+
* await Actor.pushData({ myValue: 123 });
|
|
684
|
+
* ```
|
|
685
|
+
*
|
|
686
|
+
* is equivalent to:
|
|
687
|
+
* ```javascript
|
|
688
|
+
* const dataset = await Actor.openDataset();
|
|
689
|
+
* await dataset.pushData({ myValue: 123 });
|
|
690
|
+
* ```
|
|
691
|
+
*
|
|
692
|
+
* For more information, see {@link Actor.openDataset} and {@link Dataset.pushData}
|
|
693
|
+
*
|
|
694
|
+
* **IMPORTANT**: Make sure to use the `await` keyword when calling `pushData()`,
|
|
695
|
+
* otherwise the actor process might finish before the data are stored!
|
|
696
|
+
*
|
|
697
|
+
* @param item Object or array of objects containing data to be stored in the default dataset.
|
|
698
|
+
* The objects must be serializable to JSON and the JSON representation of each object must be smaller than 9MB.
|
|
699
|
+
*/
|
|
700
|
+
static pushData(item: Dictionary): Promise<void>;
|
|
701
|
+
/**
|
|
702
|
+
* Opens a dataset and returns a promise resolving to an instance of the {@link Dataset} class.
|
|
703
|
+
*
|
|
704
|
+
* Datasets are used to store structured data where each object stored has the same attributes,
|
|
705
|
+
* such as online store products or real estate offers.
|
|
706
|
+
* The actual data is stored either on the local filesystem or in the cloud.
|
|
707
|
+
*
|
|
708
|
+
* For more details and code examples, see the {@link Dataset} class.
|
|
709
|
+
*
|
|
710
|
+
* @param [datasetIdOrName]
|
|
711
|
+
* ID or name of the dataset to be opened. If `null` or `undefined`,
|
|
712
|
+
* the function returns the default dataset associated with the actor run.
|
|
713
|
+
* @param [options]
|
|
714
|
+
*/
|
|
715
|
+
static openDataset<Data extends Dictionary = Dictionary>(datasetIdOrName?: string | null, options?: OpenStorageOptions): Promise<Dataset<Data>>;
|
|
716
|
+
/**
|
|
717
|
+
* Gets a value from the default {@link KeyValueStore} associated with the current actor run.
|
|
718
|
+
*
|
|
719
|
+
* This is just a convenient shortcut for {@link KeyValueStore.getValue}.
|
|
720
|
+
* For example, calling the following code:
|
|
721
|
+
* ```javascript
|
|
722
|
+
* const value = await Actor.getValue('my-key');
|
|
723
|
+
* ```
|
|
724
|
+
*
|
|
725
|
+
* is equivalent to:
|
|
726
|
+
* ```javascript
|
|
727
|
+
* const store = await Actor.openKeyValueStore();
|
|
728
|
+
* const value = await store.getValue('my-key');
|
|
729
|
+
* ```
|
|
730
|
+
*
|
|
731
|
+
* To store the value to the default key-value store, you can use the {@link Actor.setValue} function.
|
|
732
|
+
*
|
|
733
|
+
* For more information, see {@link Actor.openKeyValueStore}
|
|
734
|
+
* and {@link KeyValueStore.getValue}.
|
|
735
|
+
*
|
|
736
|
+
* @param key Unique record key.
|
|
737
|
+
* @returns
|
|
738
|
+
* Returns a promise that resolves to an object, string
|
|
739
|
+
* or [`Buffer`](https://nodejs.org/api/buffer.html), depending
|
|
740
|
+
* on the MIME content type of the record, or `null`
|
|
741
|
+
* if the record is missing.
|
|
742
|
+
*/
|
|
743
|
+
static getValue<T = unknown>(key: string): Promise<T | null>;
|
|
744
|
+
/**
|
|
745
|
+
* Stores or deletes a value in the default {@link KeyValueStore} associated with the current actor run.
|
|
746
|
+
*
|
|
747
|
+
* This is just a convenient shortcut for {@link KeyValueStore.setValue}.
|
|
748
|
+
* For example, calling the following code:
|
|
749
|
+
* ```javascript
|
|
750
|
+
* await Actor.setValue('OUTPUT', { foo: "bar" });
|
|
751
|
+
* ```
|
|
752
|
+
*
|
|
753
|
+
* is equivalent to:
|
|
754
|
+
* ```javascript
|
|
755
|
+
* const store = await Actor.openKeyValueStore();
|
|
756
|
+
* await store.setValue('OUTPUT', { foo: "bar" });
|
|
757
|
+
* ```
|
|
758
|
+
*
|
|
759
|
+
* To get a value from the default key-value store, you can use the {@link Actor.getValue} function.
|
|
760
|
+
*
|
|
761
|
+
* For more information, see {@link Actor.openKeyValueStore}
|
|
762
|
+
* and {@link KeyValueStore.getValue}.
|
|
763
|
+
*
|
|
764
|
+
* @param key
|
|
765
|
+
* Unique record key.
|
|
766
|
+
* @param value
|
|
767
|
+
* Record data, which can be one of the following values:
|
|
768
|
+
* - If `null`, the record in the key-value store is deleted.
|
|
769
|
+
* - If no `options.contentType` is specified, `value` can be any JavaScript object, and it will be stringified to JSON.
|
|
770
|
+
* - If `options.contentType` is set, `value` is taken as is, and it must be a `String` or [`Buffer`](https://nodejs.org/api/buffer.html).
|
|
771
|
+
* For any other value an error will be thrown.
|
|
772
|
+
* @param [options]
|
|
773
|
+
*/
|
|
774
|
+
static setValue<T>(key: string, value: T | null, options?: RecordOptions): Promise<void>;
|
|
775
|
+
/**
|
|
776
|
+
* Gets the actor input value from the default {@link KeyValueStore} associated with the current actor run.
|
|
777
|
+
*
|
|
778
|
+
* This is just a convenient shortcut for {@link KeyValueStore.getValue | `keyValueStore.getValue('INPUT')`}.
|
|
779
|
+
* For example, calling the following code:
|
|
780
|
+
* ```javascript
|
|
781
|
+
* const input = await Actor.getInput();
|
|
782
|
+
* ```
|
|
783
|
+
*
|
|
784
|
+
* is equivalent to:
|
|
785
|
+
* ```javascript
|
|
786
|
+
* const store = await Actor.openKeyValueStore();
|
|
787
|
+
* await store.getValue('INPUT');
|
|
788
|
+
* ```
|
|
789
|
+
*
|
|
790
|
+
* Note that the `getInput()` function does not cache the value read from the key-value store.
|
|
791
|
+
* If you need to use the input multiple times in your actor,
|
|
792
|
+
* it is far more efficient to read it once and store it locally.
|
|
793
|
+
*
|
|
794
|
+
* For more information, see {@link Actor.openKeyValueStore} and {@link KeyValueStore.getValue}.
|
|
795
|
+
*
|
|
796
|
+
* @returns
|
|
797
|
+
* Returns a promise that resolves to an object, string
|
|
798
|
+
* or [`Buffer`](https://nodejs.org/api/buffer.html), depending
|
|
799
|
+
* on the MIME content type of the record, or `null`
|
|
800
|
+
* if the record is missing.
|
|
801
|
+
*/
|
|
802
|
+
static getInput<T extends Dictionary | string | Buffer>(): Promise<T | null>;
|
|
803
|
+
/**
|
|
804
|
+
* Opens a key-value store and returns a promise resolving to an instance of the {@link KeyValueStore} class.
|
|
805
|
+
*
|
|
806
|
+
* Key-value stores are used to store records or files, along with their MIME content type.
|
|
807
|
+
* The records are stored and retrieved using a unique key.
|
|
808
|
+
* The actual data is stored either on a local filesystem or in the Apify cloud.
|
|
809
|
+
*
|
|
810
|
+
* For more details and code examples, see the {@link KeyValueStore} class.
|
|
811
|
+
*
|
|
812
|
+
* @param [storeIdOrName]
|
|
813
|
+
* ID or name of the key-value store to be opened. If `null` or `undefined`,
|
|
814
|
+
* the function returns the default key-value store associated with the actor run.
|
|
815
|
+
* @param [options]
|
|
816
|
+
*/
|
|
817
|
+
static openKeyValueStore(storeIdOrName?: string | null, options?: OpenStorageOptions): Promise<KeyValueStore>;
|
|
818
|
+
/**
|
|
819
|
+
* Opens a request list and returns a promise resolving to an instance
|
|
820
|
+
* of the {@link RequestList} class that is already initialized.
|
|
821
|
+
*
|
|
822
|
+
* {@link RequestList} represents a list of URLs to crawl, which is always stored in memory.
|
|
823
|
+
* To enable picking up where left off after a process restart, the request list sources
|
|
824
|
+
* are persisted to the key-value store at initialization of the list. Then, while crawling,
|
|
825
|
+
* a small state object is regularly persisted to keep track of the crawling status.
|
|
826
|
+
*
|
|
827
|
+
* For more details and code examples, see the {@link RequestList} class.
|
|
828
|
+
*
|
|
829
|
+
* **Example usage:**
|
|
830
|
+
*
|
|
831
|
+
* ```javascript
|
|
832
|
+
* const sources = [
|
|
833
|
+
* 'https://www.example.com',
|
|
834
|
+
* 'https://www.google.com',
|
|
835
|
+
* 'https://www.bing.com'
|
|
836
|
+
* ];
|
|
837
|
+
*
|
|
838
|
+
* const requestList = await RequestList.open('my-name', sources);
|
|
839
|
+
* ```
|
|
840
|
+
*
|
|
841
|
+
* @param listName
|
|
842
|
+
* Name of the request list to be opened. Setting a name enables the `RequestList`'s state to be persisted
|
|
843
|
+
* in the key-value store. This is useful in case of a restart or migration. Since `RequestList` is only
|
|
844
|
+
* stored in memory, a restart or migration wipes it clean. Setting a name will enable the `RequestList`'s
|
|
845
|
+
* state to survive those situations and continue where it left off.
|
|
846
|
+
*
|
|
847
|
+
* The name will be used as a prefix in key-value store, producing keys such as `NAME-REQUEST_LIST_STATE`
|
|
848
|
+
* and `NAME-REQUEST_LIST_SOURCES`.
|
|
849
|
+
*
|
|
850
|
+
* If `null`, the list will not be persisted and will only be stored in memory. Process restart
|
|
851
|
+
* will then cause the list to be crawled again from the beginning. We suggest always using a name.
|
|
852
|
+
* @param sources
|
|
853
|
+
* An array of sources of URLs for the {@link RequestList}. It can be either an array of strings,
|
|
854
|
+
* plain objects that define at least the `url` property, or an array of {@link Request} instances.
|
|
855
|
+
*
|
|
856
|
+
* **IMPORTANT:** The `sources` array will be consumed (left empty) after {@link RequestList} initializes.
|
|
857
|
+
* This is a measure to prevent memory leaks in situations when millions of sources are
|
|
858
|
+
* added.
|
|
859
|
+
*
|
|
860
|
+
* Additionally, the `requestsFromUrl` property may be used instead of `url`,
|
|
861
|
+
* which will instruct {@link RequestList} to download the source URLs from a given remote location.
|
|
862
|
+
* The URLs will be parsed from the received response. In this case you can limit the URLs
|
|
863
|
+
* using `regex` parameter containing regular expression pattern for URLs to be included.
|
|
864
|
+
*
|
|
865
|
+
* For details, see the {@link RequestListOptions.sources}
|
|
866
|
+
* @param [options]
|
|
867
|
+
* The {@link RequestList} options. Note that the `listName` parameter supersedes
|
|
868
|
+
* the {@link RequestListOptions.persistStateKey} and {@link RequestListOptions.persistRequestsKey}
|
|
869
|
+
* options and the `sources` parameter supersedes the {@link RequestListOptions.sources} option.
|
|
870
|
+
*/
|
|
871
|
+
static openRequestList(listName: string | null, sources: Source[], options?: RequestListOptions): Promise<RequestList>;
|
|
872
|
+
/**
|
|
873
|
+
* Opens a request queue and returns a promise resolving to an instance
|
|
874
|
+
* of the {@link RequestQueue} class.
|
|
875
|
+
*
|
|
876
|
+
* {@link RequestQueue} represents a queue of URLs to crawl, which is stored either on local filesystem or in the cloud.
|
|
877
|
+
* The queue is used for deep crawling of websites, where you start with several URLs and then
|
|
878
|
+
* recursively follow links to other pages. The data structure supports both breadth-first
|
|
879
|
+
* and depth-first crawling orders.
|
|
880
|
+
*
|
|
881
|
+
* For more details and code examples, see the {@link RequestQueue} class.
|
|
882
|
+
*
|
|
883
|
+
* @param [queueIdOrName]
|
|
884
|
+
* ID or name of the request queue to be opened. If `null` or `undefined`,
|
|
885
|
+
* the function returns the default request queue associated with the actor run.
|
|
886
|
+
* @param [options]
|
|
887
|
+
*/
|
|
888
|
+
static openRequestQueue(queueIdOrName?: string | null, options?: OpenStorageOptions): Promise<RequestQueue>;
|
|
889
|
+
/**
|
|
890
|
+
* Creates a proxy configuration and returns a promise resolving to an instance
|
|
891
|
+
* of the {@link ProxyConfiguration} class that is already initialized.
|
|
892
|
+
*
|
|
893
|
+
* Configures connection to a proxy server with the provided options. Proxy servers are used to prevent target websites from blocking
|
|
894
|
+
* your crawlers based on IP address rate limits or blacklists. Setting proxy configuration in your crawlers automatically configures
|
|
895
|
+
* them to use the selected proxies for all connections.
|
|
896
|
+
*
|
|
897
|
+
* For more details and code examples, see the {@link ProxyConfiguration} class.
|
|
898
|
+
*
|
|
899
|
+
* ```javascript
|
|
900
|
+
*
|
|
901
|
+
* // Returns initialized proxy configuration class
|
|
902
|
+
* const proxyConfiguration = await Actor.createProxyConfiguration({
|
|
903
|
+
* groups: ['GROUP1', 'GROUP2'] // List of Apify proxy groups
|
|
904
|
+
* countryCode: 'US'
|
|
905
|
+
* });
|
|
906
|
+
*
|
|
907
|
+
* const crawler = new CheerioCrawler({
|
|
908
|
+
* // ...
|
|
909
|
+
* proxyConfiguration,
|
|
910
|
+
* handlePageFunction: ({ proxyInfo }) => {
|
|
911
|
+
* const usedProxyUrl = proxyInfo.url; // Getting the proxy URL
|
|
912
|
+
* }
|
|
913
|
+
* })
|
|
914
|
+
*
|
|
915
|
+
* ```
|
|
916
|
+
*
|
|
917
|
+
* For compatibility with existing Actor Input UI (Input Schema), this function
|
|
918
|
+
* returns `undefined` when the following object is passed as `proxyConfigurationOptions`.
|
|
919
|
+
*
|
|
920
|
+
* ```
|
|
921
|
+
* { useApifyProxy: false }
|
|
922
|
+
* ```
|
|
923
|
+
*/
|
|
924
|
+
static createProxyConfiguration(proxyConfigurationOptions?: ProxyConfigurationOptions & {
|
|
925
|
+
useApifyProxy?: boolean;
|
|
926
|
+
}): Promise<ProxyConfiguration | undefined>;
|
|
927
|
+
/**
|
|
928
|
+
* Returns a new {@link ApifyEnv} object which contains information parsed from all the `APIFY_XXX` environment variables.
|
|
929
|
+
*
|
|
930
|
+
* For the list of the `APIFY_XXX` environment variables, see
|
|
931
|
+
* [Actor documentation](https://docs.apify.com/actor/run#environment-variables).
|
|
932
|
+
* If some of the variables are not defined or are invalid, the corresponding value in the resulting object will be null.
|
|
933
|
+
*/
|
|
934
|
+
static getEnv(): ApifyEnv;
|
|
935
|
+
/**
|
|
936
|
+
* Returns a new instance of the Apify API client. The `ApifyClient` class is provided
|
|
937
|
+
* by the [apify-client](https://www.npmjs.com/package/apify-client)
|
|
938
|
+
* NPM package, and it is automatically configured using the `APIFY_API_BASE_URL`, and `APIFY_TOKEN`
|
|
939
|
+
* environment variables. You can override the token via the available options. That's useful
|
|
940
|
+
* if you want to use the client as a different Apify user than the SDK internals are using.
|
|
941
|
+
*/
|
|
942
|
+
static newClient(options?: ApifyClientOptions): ApifyClient;
|
|
943
|
+
/**
|
|
944
|
+
* Returns `true` when code is running on Apify platform and `false` otherwise (for example locally).
|
|
945
|
+
*/
|
|
946
|
+
static isAtHome(): boolean;
|
|
947
|
+
/** Default {@link ApifyClient} instance. */
|
|
948
|
+
static get apifyClient(): ApifyClient;
|
|
949
|
+
/** Default {@link Configuration} instance. */
|
|
950
|
+
static get config(): Configuration;
|
|
951
|
+
/** @internal */
|
|
952
|
+
static getDefaultInstance(): Actor;
|
|
953
|
+
private _openStorage;
|
|
954
|
+
private _getStorageManager;
|
|
955
|
+
}
|
|
956
|
+
export interface MainOptions extends ExitOptions {
|
|
957
|
+
purge?: boolean;
|
|
958
|
+
}
|
|
959
|
+
/**
|
|
960
|
+
* Parsed representation of the `APIFY_XXX` environmental variables.
|
|
961
|
+
* This object is returned by the {@link Actor.getEnv} function.
|
|
962
|
+
*/
|
|
963
|
+
export interface ApifyEnv {
|
|
964
|
+
/**
|
|
965
|
+
* ID of the actor (APIFY_ACTOR_ID)
|
|
966
|
+
*/
|
|
967
|
+
actorId: string | null;
|
|
968
|
+
/**
|
|
969
|
+
* ID of the actor run (APIFY_ACTOR_RUN_ID)
|
|
970
|
+
*/
|
|
971
|
+
actorRunId: string | null;
|
|
972
|
+
/**
|
|
973
|
+
* ID of the actor task (APIFY_ACTOR_TASK_ID)
|
|
974
|
+
*/
|
|
975
|
+
actorTaskId: string | null;
|
|
976
|
+
/**
|
|
977
|
+
* ID of the user who started the actor - note that it might be
|
|
978
|
+
* different than the owner ofthe actor (APIFY_USER_ID)
|
|
979
|
+
*/
|
|
980
|
+
userId: string | null;
|
|
981
|
+
/**
|
|
982
|
+
* Authentication token representing privileges given to the actor run,
|
|
983
|
+
* it can be passed to various Apify APIs (APIFY_TOKEN)
|
|
984
|
+
*/
|
|
985
|
+
token: string | null;
|
|
986
|
+
/**
|
|
987
|
+
* Date when the actor was started (APIFY_STARTED_AT)
|
|
988
|
+
*/
|
|
989
|
+
startedAt: Date | null;
|
|
990
|
+
/**
|
|
991
|
+
* Date when the actor will time out (APIFY_TIMEOUT_AT)
|
|
992
|
+
*/
|
|
993
|
+
timeoutAt: Date | null;
|
|
994
|
+
/**
|
|
995
|
+
* ID of the key-value store where input and output data of this
|
|
996
|
+
* actor is stored (APIFY_DEFAULT_KEY_VALUE_STORE_ID)
|
|
997
|
+
*/
|
|
998
|
+
defaultKeyValueStoreId: string | null;
|
|
999
|
+
/**
|
|
1000
|
+
* ID of the dataset where input and output data of this
|
|
1001
|
+
* actor is stored (APIFY_DEFAULT_DATASET_ID)
|
|
1002
|
+
*/
|
|
1003
|
+
defaultDatasetId: string | null;
|
|
1004
|
+
/**
|
|
1005
|
+
* Amount of memory allocated for the actor,
|
|
1006
|
+
* in megabytes (APIFY_MEMORY_MBYTES)
|
|
1007
|
+
*/
|
|
1008
|
+
memoryMbytes: number | null;
|
|
1009
|
+
}
|
|
1010
|
+
export declare type UserFunc<T = unknown> = () => Awaitable<T>;
|
|
1011
|
+
export interface CallOptions extends ActorStartOptions {
|
|
1012
|
+
/**
|
|
1013
|
+
* User API token that is used to run the actor. By default, it is taken from the `APIFY_TOKEN` environment variable.
|
|
1014
|
+
*/
|
|
1015
|
+
token?: string;
|
|
1016
|
+
}
|
|
1017
|
+
export interface CallTaskOptions extends TaskStartOptions {
|
|
1018
|
+
/**
|
|
1019
|
+
* User API token that is used to run the actor. By default, it is taken from the `APIFY_TOKEN` environment variable.
|
|
1020
|
+
*/
|
|
1021
|
+
token?: string;
|
|
1022
|
+
}
|
|
1023
|
+
export interface WebhookOptions {
|
|
1024
|
+
/**
|
|
1025
|
+
* Array of event types, which you can set for actor run, see
|
|
1026
|
+
* the [actor run events](https://docs.apify.com/webhooks/events#actor-run) in the Apify doc.
|
|
1027
|
+
*/
|
|
1028
|
+
eventTypes: readonly WebhookEventType[];
|
|
1029
|
+
/**
|
|
1030
|
+
* URL which will be requested using HTTP POST request, when actor run will reach the set event type.
|
|
1031
|
+
*/
|
|
1032
|
+
requestUrl: string;
|
|
1033
|
+
/**
|
|
1034
|
+
* Payload template is a JSON-like string that describes the structure of the webhook POST request payload.
|
|
1035
|
+
* It uses JSON syntax, extended with a double curly braces syntax for injecting variables `{{variable}}`.
|
|
1036
|
+
* Those variables are resolved at the time of the webhook's dispatch, and a list of available variables with their descriptions
|
|
1037
|
+
* is available in the [Apify webhook documentation](https://docs.apify.com/webhooks).
|
|
1038
|
+
* If `payloadTemplate` is omitted, the default payload template is used
|
|
1039
|
+
* ([view docs](https://docs.apify.com/webhooks/actions#payload-template)).
|
|
1040
|
+
*/
|
|
1041
|
+
payloadTemplate?: string;
|
|
1042
|
+
/**
|
|
1043
|
+
* Idempotency key enables you to ensure that a webhook will not be added multiple times in case of
|
|
1044
|
+
* an actor restart or other situation that would cause the `addWebhook()` function to be called again.
|
|
1045
|
+
* We suggest using the actor run ID as the idempotency key. You can get the run ID by calling
|
|
1046
|
+
* {@link Actor.getEnv} function.
|
|
1047
|
+
*/
|
|
1048
|
+
idempotencyKey?: string;
|
|
1049
|
+
}
|
|
1050
|
+
export interface MetamorphOptions {
|
|
1051
|
+
/**
|
|
1052
|
+
* Content type for the `input`. If not specified,
|
|
1053
|
+
* `input` is expected to be an object that will be stringified to JSON and content type set to
|
|
1054
|
+
* `application/json; charset=utf-8`. If `options.contentType` is specified, then `input` must be a
|
|
1055
|
+
* `String` or `Buffer`.
|
|
1056
|
+
*/
|
|
1057
|
+
contentType?: string;
|
|
1058
|
+
/**
|
|
1059
|
+
* Tag or number of the target actor build to metamorph into (e.g. `beta` or `1.2.345`).
|
|
1060
|
+
* If not provided, the run uses build tag or number from the default actor run configuration (typically `latest`).
|
|
1061
|
+
*/
|
|
1062
|
+
build?: string;
|
|
1063
|
+
/** @internal */
|
|
1064
|
+
customAfterSleepMillis?: number;
|
|
1065
|
+
}
|
|
1066
|
+
export interface ExitOptions {
|
|
1067
|
+
/** Exit code, defaults to 0 */
|
|
1068
|
+
exitCode?: number;
|
|
1069
|
+
/** Call `process.exit()`? Defaults to true */
|
|
1070
|
+
exit?: boolean;
|
|
1071
|
+
}
|
|
1072
|
+
export interface OpenStorageOptions {
|
|
1073
|
+
/**
|
|
1074
|
+
* If set to `true` then the cloud storage is used even if the `APIFY_LOCAL_STORAGE_DIR`
|
|
1075
|
+
* environment variable is set. This way it is possible to combine local and cloud storage.
|
|
1076
|
+
* @default false
|
|
1077
|
+
*/
|
|
1078
|
+
forceCloud?: boolean;
|
|
1079
|
+
}
|
|
1080
|
+
export { ClientActorRun as ActorRun };
|
|
1081
|
+
/**
|
|
1082
|
+
* Exit codes for the actor process.
|
|
1083
|
+
* The error codes must be in the range 1-128, to avoid collision with signal exits
|
|
1084
|
+
* and to ensure Docker will handle them correctly!
|
|
1085
|
+
* @internal should be removed if we decide to remove `Actor.main()`
|
|
1086
|
+
*/
|
|
1087
|
+
export declare const EXIT_CODES: {
|
|
1088
|
+
SUCCESS: number;
|
|
1089
|
+
ERROR_USER_FUNCTION_THREW: number;
|
|
1090
|
+
ERROR_UNKNOWN: number;
|
|
1091
|
+
};
|
|
1092
|
+
//# sourceMappingURL=actor.d.ts.map
|