scrapfly-sdk 0.7.3 → 0.10.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.
- package/esm/browserconfig.d.ts +59 -0
- package/esm/browserconfig.d.ts.map +1 -0
- package/esm/browserconfig.js +207 -0
- package/esm/client.d.ts +209 -1
- package/esm/client.d.ts.map +1 -1
- package/esm/client.js +902 -5
- package/esm/crawl.d.ts +118 -0
- package/esm/crawl.d.ts.map +1 -0
- package/esm/crawl.js +230 -0
- package/esm/crawlerconfig.d.ts +119 -0
- package/esm/crawlerconfig.d.ts.map +1 -0
- package/esm/crawlerconfig.js +398 -0
- package/esm/crawlerresult.d.ts +253 -0
- package/esm/crawlerresult.d.ts.map +1 -0
- package/esm/crawlerresult.js +327 -0
- package/esm/errors.d.ts +4 -0
- package/esm/errors.d.ts.map +1 -1
- package/esm/errors.js +6 -0
- package/esm/extractionconfig.d.ts +2 -0
- package/esm/extractionconfig.d.ts.map +1 -1
- package/esm/extractionconfig.js +10 -0
- package/esm/main.d.ts +9 -1
- package/esm/main.d.ts.map +1 -1
- package/esm/main.js +6 -1
- package/esm/monitoringconfig.d.ts +36 -0
- package/esm/monitoringconfig.d.ts.map +1 -0
- package/esm/monitoringconfig.js +10 -0
- package/esm/polyfill.d.ts +2 -0
- package/esm/polyfill.d.ts.map +1 -1
- package/esm/polyfill.js +12 -0
- package/esm/scrapeconfig.d.ts +27 -0
- package/esm/scrapeconfig.d.ts.map +1 -1
- package/esm/scrapeconfig.js +44 -1
- package/esm/screenshotconfig.d.ts +2 -2
- package/esm/screenshotconfig.d.ts.map +1 -1
- package/esm/utils.d.ts.map +1 -1
- package/esm/utils.js +16 -2
- package/package.json +1 -1
- package/script/browserconfig.d.ts +59 -0
- package/script/browserconfig.d.ts.map +1 -0
- package/script/browserconfig.js +211 -0
- package/script/client.d.ts +209 -1
- package/script/client.d.ts.map +1 -1
- package/script/client.js +901 -4
- package/script/crawl.d.ts +118 -0
- package/script/crawl.d.ts.map +1 -0
- package/script/crawl.js +257 -0
- package/script/crawlerconfig.d.ts +119 -0
- package/script/crawlerconfig.d.ts.map +1 -0
- package/script/crawlerconfig.js +402 -0
- package/script/crawlerresult.d.ts +253 -0
- package/script/crawlerresult.d.ts.map +1 -0
- package/script/crawlerresult.js +335 -0
- package/script/errors.d.ts +4 -0
- package/script/errors.d.ts.map +1 -1
- package/script/errors.js +9 -1
- package/script/extractionconfig.d.ts +2 -0
- package/script/extractionconfig.d.ts.map +1 -1
- package/script/extractionconfig.js +10 -0
- package/script/main.d.ts +9 -1
- package/script/main.d.ts.map +1 -1
- package/script/main.js +17 -1
- package/script/monitoringconfig.d.ts +36 -0
- package/script/monitoringconfig.d.ts.map +1 -0
- package/script/monitoringconfig.js +13 -0
- package/script/polyfill.d.ts +2 -0
- package/script/polyfill.d.ts.map +1 -1
- package/script/polyfill.js +14 -1
- package/script/scrapeconfig.d.ts +27 -0
- package/script/scrapeconfig.d.ts.map +1 -1
- package/script/scrapeconfig.js +44 -1
- package/script/screenshotconfig.d.ts +2 -2
- package/script/screenshotconfig.d.ts.map +1 -1
- package/script/utils.d.ts.map +1 -1
- package/script/utils.js +16 -2
package/esm/crawl.d.ts
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import type { ScrapflyClient } from './client.js';
|
|
2
|
+
import type { CrawlerConfig, CrawlerContentFormat } from './crawlerconfig.js';
|
|
3
|
+
import type { CrawlerArtifact, CrawlerStatus, CrawlerUrls } from './crawlerresult.js';
|
|
4
|
+
/**
|
|
5
|
+
* High-level fluent wrapper around a single crawler job lifecycle.
|
|
6
|
+
*
|
|
7
|
+
* Mirrors the Python SDK's `Crawl` class. Internally calls into `ScrapflyClient`'s
|
|
8
|
+
* lower-level crawler methods, but adds:
|
|
9
|
+
*
|
|
10
|
+
* - polling helpers (`wait()`)
|
|
11
|
+
* - per-instance caching of the most recent status and downloaded artifacts
|
|
12
|
+
* - convenience methods that pre-fill the crawler UUID
|
|
13
|
+
*
|
|
14
|
+
* Use this when you want a simple `start → wait → read` flow. Use the lower-level
|
|
15
|
+
* `client.crawl*` methods directly if you need finer control (e.g. polling at
|
|
16
|
+
* irregular intervals or reusing a status response across calls).
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```ts
|
|
20
|
+
* const crawl = new Crawl(client, new CrawlerConfig({
|
|
21
|
+
* url: 'https://web-scraping.dev/products',
|
|
22
|
+
* page_limit: 10,
|
|
23
|
+
* content_formats: ['markdown'],
|
|
24
|
+
* }));
|
|
25
|
+
*
|
|
26
|
+
* await crawl.start();
|
|
27
|
+
* await crawl.wait({ pollInterval: 2 });
|
|
28
|
+
*
|
|
29
|
+
* for (const entry of (await crawl.urls({ status: 'visited' })).urls) {
|
|
30
|
+
* const md = await crawl.read(entry.url, 'markdown');
|
|
31
|
+
* console.log(entry.url, md?.length);
|
|
32
|
+
* }
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
35
|
+
export declare class Crawl {
|
|
36
|
+
private readonly client;
|
|
37
|
+
private readonly config;
|
|
38
|
+
private _uuid?;
|
|
39
|
+
private _status?;
|
|
40
|
+
private readonly _artifactCache;
|
|
41
|
+
constructor(client: ScrapflyClient, config: CrawlerConfig);
|
|
42
|
+
/** The crawler UUID returned by the API after `start()`, or undefined before. */
|
|
43
|
+
get uuid(): string | undefined;
|
|
44
|
+
/** True once `start()` has been called and the crawler has a UUID. */
|
|
45
|
+
get started(): boolean;
|
|
46
|
+
/**
|
|
47
|
+
* Schedule the crawl on the Scrapfly API.
|
|
48
|
+
*
|
|
49
|
+
* Returns `this` for chaining. Throws `ScrapflyCrawlerError` if `start()` has
|
|
50
|
+
* already been called on this instance.
|
|
51
|
+
*/
|
|
52
|
+
start(): Promise<this>;
|
|
53
|
+
private requireUuid;
|
|
54
|
+
/**
|
|
55
|
+
* Fetch (or return cached) status for this crawl.
|
|
56
|
+
*
|
|
57
|
+
* @param refresh — when true (default), always re-query the API. When false,
|
|
58
|
+
* return the cached status from the previous call (or fetch fresh if none).
|
|
59
|
+
*/
|
|
60
|
+
status(refresh?: boolean): Promise<CrawlerStatus>;
|
|
61
|
+
/**
|
|
62
|
+
* Block until the crawler reaches a terminal state, polling status periodically.
|
|
63
|
+
*
|
|
64
|
+
* Returns `this` for chaining when the job completes successfully. Throws
|
|
65
|
+
* `ScrapflyCrawlerError` if the job fails, is cancelled, or `maxWait` is exceeded.
|
|
66
|
+
*
|
|
67
|
+
* @param opts.pollInterval — seconds between polls (default 5)
|
|
68
|
+
* @param opts.maxWait — maximum total seconds to wait (default: no limit)
|
|
69
|
+
* @param opts.verbose — log progress on every poll
|
|
70
|
+
* @param opts.allowCancelled — when `true`, return normally if the crawler
|
|
71
|
+
* reaches CANCELLED instead of throwing. Useful for the cancel-then-wait
|
|
72
|
+
* pattern where the caller already triggered the cancellation. Defaults
|
|
73
|
+
* to `false`, preserving the prior throw-on-cancel behavior for callers
|
|
74
|
+
* that observe external interrupts.
|
|
75
|
+
*/
|
|
76
|
+
wait(opts?: {
|
|
77
|
+
pollInterval?: number;
|
|
78
|
+
maxWait?: number;
|
|
79
|
+
verbose?: boolean;
|
|
80
|
+
allowCancelled?: boolean;
|
|
81
|
+
}): Promise<this>;
|
|
82
|
+
/** Cancel the crawl. Returns `true` on success. */
|
|
83
|
+
cancel(): Promise<boolean>;
|
|
84
|
+
/**
|
|
85
|
+
* List crawled URLs (paginated, optionally filtered by status).
|
|
86
|
+
*
|
|
87
|
+
* Convenience wrapper around `client.crawlUrls(uuid, opts)`. **New endpoint** —
|
|
88
|
+
* not available in the Python SDK as of 0.8.27.
|
|
89
|
+
*/
|
|
90
|
+
urls(opts?: {
|
|
91
|
+
status?: 'visited' | 'pending' | 'failed';
|
|
92
|
+
page?: number;
|
|
93
|
+
per_page?: number;
|
|
94
|
+
}): Promise<CrawlerUrls>;
|
|
95
|
+
/**
|
|
96
|
+
* Read raw content for a single crawled URL using `plain=true` mode.
|
|
97
|
+
*
|
|
98
|
+
* Returns `null` if the URL has no content for that format. Throws
|
|
99
|
+
* `ScrapflyCrawlerError` for any non-404 error. **New mode** — not available in
|
|
100
|
+
* the Python SDK as of 0.8.27.
|
|
101
|
+
*/
|
|
102
|
+
read(url: string, format?: CrawlerContentFormat): Promise<string | null>;
|
|
103
|
+
/**
|
|
104
|
+
* Batch-read content for multiple URLs in one round-trip.
|
|
105
|
+
*
|
|
106
|
+
* Convenience wrapper around `client.crawlContentsBatch`. Limited to 100 URLs.
|
|
107
|
+
*/
|
|
108
|
+
readBatch(urls: string[], formats?: CrawlerContentFormat[]): Promise<Record<string, Record<string, string>>>;
|
|
109
|
+
/**
|
|
110
|
+
* Download the WARC artifact (cached after first call).
|
|
111
|
+
*/
|
|
112
|
+
warc(): Promise<CrawlerArtifact>;
|
|
113
|
+
/**
|
|
114
|
+
* Download the HAR artifact (cached after first call).
|
|
115
|
+
*/
|
|
116
|
+
har(): Promise<CrawlerArtifact>;
|
|
117
|
+
}
|
|
118
|
+
//# sourceMappingURL=crawl.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"crawl.d.ts","sourceRoot":"","sources":["../src/crawl.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAClD,OAAO,KAAK,EAAE,aAAa,EAAE,oBAAoB,EAAE,MAAM,oBAAoB,CAAC;AAC9E,OAAO,KAAK,EACV,eAAe,EAGf,aAAa,EACb,WAAW,EACZ,MAAM,oBAAoB,CAAC;AAE5B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AACH,qBAAa,KAAK;IAKJ,OAAO,CAAC,QAAQ,CAAC,MAAM;IAAkB,OAAO,CAAC,QAAQ,CAAC,MAAM;IAJ5E,OAAO,CAAC,KAAK,CAAC,CAAS;IACvB,OAAO,CAAC,OAAO,CAAC,CAAgB;IAChC,OAAO,CAAC,QAAQ,CAAC,cAAc,CAAwD;gBAE1D,MAAM,EAAE,cAAc,EAAmB,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QtC"}
|
package/esm/crawl.js
ADDED
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
import * as errors from './errors.js';
|
|
2
|
+
import { log } from './logger.js';
|
|
3
|
+
/**
|
|
4
|
+
* High-level fluent wrapper around a single crawler job lifecycle.
|
|
5
|
+
*
|
|
6
|
+
* Mirrors the Python SDK's `Crawl` class. Internally calls into `ScrapflyClient`'s
|
|
7
|
+
* lower-level crawler methods, but adds:
|
|
8
|
+
*
|
|
9
|
+
* - polling helpers (`wait()`)
|
|
10
|
+
* - per-instance caching of the most recent status and downloaded artifacts
|
|
11
|
+
* - convenience methods that pre-fill the crawler UUID
|
|
12
|
+
*
|
|
13
|
+
* Use this when you want a simple `start → wait → read` flow. Use the lower-level
|
|
14
|
+
* `client.crawl*` methods directly if you need finer control (e.g. polling at
|
|
15
|
+
* irregular intervals or reusing a status response across calls).
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```ts
|
|
19
|
+
* const crawl = new Crawl(client, new CrawlerConfig({
|
|
20
|
+
* url: 'https://web-scraping.dev/products',
|
|
21
|
+
* page_limit: 10,
|
|
22
|
+
* content_formats: ['markdown'],
|
|
23
|
+
* }));
|
|
24
|
+
*
|
|
25
|
+
* await crawl.start();
|
|
26
|
+
* await crawl.wait({ pollInterval: 2 });
|
|
27
|
+
*
|
|
28
|
+
* for (const entry of (await crawl.urls({ status: 'visited' })).urls) {
|
|
29
|
+
* const md = await crawl.read(entry.url, 'markdown');
|
|
30
|
+
* console.log(entry.url, md?.length);
|
|
31
|
+
* }
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
export class Crawl {
|
|
35
|
+
constructor(client, config) {
|
|
36
|
+
Object.defineProperty(this, "client", {
|
|
37
|
+
enumerable: true,
|
|
38
|
+
configurable: true,
|
|
39
|
+
writable: true,
|
|
40
|
+
value: client
|
|
41
|
+
});
|
|
42
|
+
Object.defineProperty(this, "config", {
|
|
43
|
+
enumerable: true,
|
|
44
|
+
configurable: true,
|
|
45
|
+
writable: true,
|
|
46
|
+
value: config
|
|
47
|
+
});
|
|
48
|
+
Object.defineProperty(this, "_uuid", {
|
|
49
|
+
enumerable: true,
|
|
50
|
+
configurable: true,
|
|
51
|
+
writable: true,
|
|
52
|
+
value: void 0
|
|
53
|
+
});
|
|
54
|
+
Object.defineProperty(this, "_status", {
|
|
55
|
+
enumerable: true,
|
|
56
|
+
configurable: true,
|
|
57
|
+
writable: true,
|
|
58
|
+
value: void 0
|
|
59
|
+
});
|
|
60
|
+
Object.defineProperty(this, "_artifactCache", {
|
|
61
|
+
enumerable: true,
|
|
62
|
+
configurable: true,
|
|
63
|
+
writable: true,
|
|
64
|
+
value: new Map()
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
/** The crawler UUID returned by the API after `start()`, or undefined before. */
|
|
68
|
+
get uuid() {
|
|
69
|
+
return this._uuid;
|
|
70
|
+
}
|
|
71
|
+
/** True once `start()` has been called and the crawler has a UUID. */
|
|
72
|
+
get started() {
|
|
73
|
+
return this._uuid !== undefined;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Schedule the crawl on the Scrapfly API.
|
|
77
|
+
*
|
|
78
|
+
* Returns `this` for chaining. Throws `ScrapflyCrawlerError` if `start()` has
|
|
79
|
+
* already been called on this instance.
|
|
80
|
+
*/
|
|
81
|
+
async start() {
|
|
82
|
+
if (this._uuid) {
|
|
83
|
+
throw new errors.ScrapflyCrawlerError('Crawl already started', { code: 'ALREADY_STARTED' });
|
|
84
|
+
}
|
|
85
|
+
const response = await this.client.crawl(this.config);
|
|
86
|
+
this._uuid = response.crawler_uuid;
|
|
87
|
+
log.debug('crawl started', { crawler_uuid: this._uuid });
|
|
88
|
+
return this;
|
|
89
|
+
}
|
|
90
|
+
requireUuid() {
|
|
91
|
+
if (!this._uuid) {
|
|
92
|
+
throw new errors.ScrapflyCrawlerError('Crawl has not been started yet. Call start() first.', { code: 'NOT_STARTED' });
|
|
93
|
+
}
|
|
94
|
+
return this._uuid;
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Fetch (or return cached) status for this crawl.
|
|
98
|
+
*
|
|
99
|
+
* @param refresh — when true (default), always re-query the API. When false,
|
|
100
|
+
* return the cached status from the previous call (or fetch fresh if none).
|
|
101
|
+
*/
|
|
102
|
+
async status(refresh = true) {
|
|
103
|
+
const uuid = this.requireUuid();
|
|
104
|
+
if (refresh || !this._status) {
|
|
105
|
+
this._status = await this.client.crawlStatus(uuid);
|
|
106
|
+
}
|
|
107
|
+
return this._status;
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Block until the crawler reaches a terminal state, polling status periodically.
|
|
111
|
+
*
|
|
112
|
+
* Returns `this` for chaining when the job completes successfully. Throws
|
|
113
|
+
* `ScrapflyCrawlerError` if the job fails, is cancelled, or `maxWait` is exceeded.
|
|
114
|
+
*
|
|
115
|
+
* @param opts.pollInterval — seconds between polls (default 5)
|
|
116
|
+
* @param opts.maxWait — maximum total seconds to wait (default: no limit)
|
|
117
|
+
* @param opts.verbose — log progress on every poll
|
|
118
|
+
* @param opts.allowCancelled — when `true`, return normally if the crawler
|
|
119
|
+
* reaches CANCELLED instead of throwing. Useful for the cancel-then-wait
|
|
120
|
+
* pattern where the caller already triggered the cancellation. Defaults
|
|
121
|
+
* to `false`, preserving the prior throw-on-cancel behavior for callers
|
|
122
|
+
* that observe external interrupts.
|
|
123
|
+
*/
|
|
124
|
+
async wait(opts) {
|
|
125
|
+
const pollInterval = opts?.pollInterval ?? 5;
|
|
126
|
+
const maxWait = opts?.maxWait;
|
|
127
|
+
const verbose = opts?.verbose ?? false;
|
|
128
|
+
const allowCancelled = opts?.allowCancelled ?? false;
|
|
129
|
+
const start = Date.now();
|
|
130
|
+
while (true) {
|
|
131
|
+
const status = await this.status(true);
|
|
132
|
+
if (verbose) {
|
|
133
|
+
log.info(`crawl ${this._uuid} status=${status.status} visited=${status.state.urls_visited}/${status.state.urls_extracted}`);
|
|
134
|
+
}
|
|
135
|
+
if (status.is_finished || status.status === 'CANCELLED') {
|
|
136
|
+
if (status.isFailed) {
|
|
137
|
+
throw new errors.ScrapflyCrawlerError(`Crawl ${this._uuid} failed: ${status.state.stop_reason ?? 'unknown reason'}`, { code: 'FAILED', stop_reason: status.state.stop_reason, status });
|
|
138
|
+
}
|
|
139
|
+
if (status.isCancelled) {
|
|
140
|
+
if (allowCancelled) {
|
|
141
|
+
if (verbose) {
|
|
142
|
+
log.info(`crawl ${this._uuid} was cancelled (allowCancelled=true)`);
|
|
143
|
+
}
|
|
144
|
+
return this;
|
|
145
|
+
}
|
|
146
|
+
throw new errors.ScrapflyCrawlerError(`Crawl ${this._uuid} was cancelled (reason: ${status.state.stop_reason ?? 'unknown'})`, { code: 'CANCELLED', stop_reason: status.state.stop_reason, status });
|
|
147
|
+
}
|
|
148
|
+
// status.isComplete
|
|
149
|
+
return this;
|
|
150
|
+
}
|
|
151
|
+
if (maxWait !== undefined) {
|
|
152
|
+
const elapsedSeconds = (Date.now() - start) / 1000;
|
|
153
|
+
if (elapsedSeconds + pollInterval > maxWait) {
|
|
154
|
+
throw new errors.ScrapflyCrawlerError(`Timed out waiting for crawl ${this._uuid} after ${maxWait}s`, { code: 'TIMEOUT', status });
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
await new Promise((resolve) => setTimeout(resolve, pollInterval * 1000));
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
/** Cancel the crawl. Returns `true` on success. */
|
|
161
|
+
async cancel() {
|
|
162
|
+
const uuid = this.requireUuid();
|
|
163
|
+
return await this.client.crawlCancel(uuid);
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* List crawled URLs (paginated, optionally filtered by status).
|
|
167
|
+
*
|
|
168
|
+
* Convenience wrapper around `client.crawlUrls(uuid, opts)`. **New endpoint** —
|
|
169
|
+
* not available in the Python SDK as of 0.8.27.
|
|
170
|
+
*/
|
|
171
|
+
async urls(opts) {
|
|
172
|
+
const uuid = this.requireUuid();
|
|
173
|
+
return await this.client.crawlUrls(uuid, opts);
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* Read raw content for a single crawled URL using `plain=true` mode.
|
|
177
|
+
*
|
|
178
|
+
* Returns `null` if the URL has no content for that format. Throws
|
|
179
|
+
* `ScrapflyCrawlerError` for any non-404 error. **New mode** — not available in
|
|
180
|
+
* the Python SDK as of 0.8.27.
|
|
181
|
+
*/
|
|
182
|
+
async read(url, format = 'html') {
|
|
183
|
+
const uuid = this.requireUuid();
|
|
184
|
+
try {
|
|
185
|
+
const result = await this.client.crawlContents(uuid, { url, format, plain: true });
|
|
186
|
+
// crawlContents in plain mode always returns string
|
|
187
|
+
return typeof result === 'string' ? result : JSON.stringify(result);
|
|
188
|
+
}
|
|
189
|
+
catch (e) {
|
|
190
|
+
// Treat 404 as "no content for this URL" rather than re-throwing
|
|
191
|
+
if (e instanceof errors.ScrapflyError && (e.args?.http_status_code === 404)) {
|
|
192
|
+
return null;
|
|
193
|
+
}
|
|
194
|
+
throw e;
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
/**
|
|
198
|
+
* Batch-read content for multiple URLs in one round-trip.
|
|
199
|
+
*
|
|
200
|
+
* Convenience wrapper around `client.crawlContentsBatch`. Limited to 100 URLs.
|
|
201
|
+
*/
|
|
202
|
+
async readBatch(urls, formats = ['html']) {
|
|
203
|
+
const uuid = this.requireUuid();
|
|
204
|
+
return await this.client.crawlContentsBatch(uuid, urls, formats);
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* Download the WARC artifact (cached after first call).
|
|
208
|
+
*/
|
|
209
|
+
async warc() {
|
|
210
|
+
const uuid = this.requireUuid();
|
|
211
|
+
const cached = this._artifactCache.get('warc');
|
|
212
|
+
if (cached)
|
|
213
|
+
return cached;
|
|
214
|
+
const artifact = await this.client.crawlArtifact(uuid, 'warc');
|
|
215
|
+
this._artifactCache.set('warc', artifact);
|
|
216
|
+
return artifact;
|
|
217
|
+
}
|
|
218
|
+
/**
|
|
219
|
+
* Download the HAR artifact (cached after first call).
|
|
220
|
+
*/
|
|
221
|
+
async har() {
|
|
222
|
+
const uuid = this.requireUuid();
|
|
223
|
+
const cached = this._artifactCache.get('har');
|
|
224
|
+
if (cached)
|
|
225
|
+
return cached;
|
|
226
|
+
const artifact = await this.client.crawlArtifact(uuid, 'har');
|
|
227
|
+
this._artifactCache.set('har', artifact);
|
|
228
|
+
return artifact;
|
|
229
|
+
}
|
|
230
|
+
}
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import type { Rec } from './types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Content formats supported by the Crawler API.
|
|
4
|
+
*
|
|
5
|
+
* - `html` — original HTML
|
|
6
|
+
* - `clean_html` — sanitized/cleaned HTML
|
|
7
|
+
* - `markdown` — LLM-ready markdown
|
|
8
|
+
* - `text` — plain-text extraction
|
|
9
|
+
* - `json` — parse as JSON (for API responses)
|
|
10
|
+
* - `extracted_data` — structured data from extraction rules
|
|
11
|
+
* - `page_metadata` — title, description, etc.
|
|
12
|
+
*/
|
|
13
|
+
export type CrawlerContentFormat = 'html' | 'clean_html' | 'markdown' | 'text' | 'json' | 'extracted_data' | 'page_metadata';
|
|
14
|
+
/**
|
|
15
|
+
* Webhook event names emitted by the crawler.
|
|
16
|
+
*
|
|
17
|
+
* Source of truth: `apps/scrapfly/scrape-engine/scrape_engine/scrape_engine/crawler/webhook_manager.py`
|
|
18
|
+
* lines 11-20. Verified to match the public docs and the JSON example payloads.
|
|
19
|
+
*/
|
|
20
|
+
export type CrawlerWebhookEventName = 'crawler_started' | 'crawler_url_visited' | 'crawler_url_skipped' | 'crawler_url_discovered' | 'crawler_url_failed' | 'crawler_stopped' | 'crawler_cancelled' | 'crawler_finished';
|
|
21
|
+
/**
|
|
22
|
+
* Constructor options for {@link CrawlerConfig}.
|
|
23
|
+
*
|
|
24
|
+
* Every field except `url` is optional. Fields left unset are NOT sent to the API,
|
|
25
|
+
* which lets the server apply its documented defaults (e.g. `respect_robots_txt=true`).
|
|
26
|
+
*/
|
|
27
|
+
export type CrawlerConfigOptions = {
|
|
28
|
+
url: string;
|
|
29
|
+
page_limit?: number;
|
|
30
|
+
max_depth?: number;
|
|
31
|
+
max_duration?: number;
|
|
32
|
+
max_api_credit?: number;
|
|
33
|
+
exclude_paths?: string[];
|
|
34
|
+
include_only_paths?: string[];
|
|
35
|
+
ignore_base_path_restriction?: boolean;
|
|
36
|
+
follow_external_links?: boolean;
|
|
37
|
+
allowed_external_domains?: string[];
|
|
38
|
+
follow_internal_subdomains?: boolean;
|
|
39
|
+
allowed_internal_subdomains?: string[];
|
|
40
|
+
headers?: Rec<string>;
|
|
41
|
+
delay?: number;
|
|
42
|
+
user_agent?: string;
|
|
43
|
+
max_concurrency?: number;
|
|
44
|
+
rendering_delay?: number;
|
|
45
|
+
use_sitemaps?: boolean;
|
|
46
|
+
respect_robots_txt?: boolean;
|
|
47
|
+
ignore_no_follow?: boolean;
|
|
48
|
+
cache?: boolean;
|
|
49
|
+
cache_ttl?: number;
|
|
50
|
+
cache_clear?: boolean;
|
|
51
|
+
content_formats?: CrawlerContentFormat[];
|
|
52
|
+
extraction_rules?: Rec<any>;
|
|
53
|
+
asp?: boolean;
|
|
54
|
+
proxy_pool?: string;
|
|
55
|
+
country?: string;
|
|
56
|
+
webhook_name?: string;
|
|
57
|
+
webhook_events?: CrawlerWebhookEventName[];
|
|
58
|
+
};
|
|
59
|
+
/**
|
|
60
|
+
* Configuration for a Scrapfly Crawler API job.
|
|
61
|
+
*
|
|
62
|
+
* Mirrors the documented `POST /crawl` request body field-for-field. Fields left
|
|
63
|
+
* unset are dropped from the serialized output so the server applies its own defaults.
|
|
64
|
+
*
|
|
65
|
+
* @example
|
|
66
|
+
* ```ts
|
|
67
|
+
* const config = new CrawlerConfig({
|
|
68
|
+
* url: 'https://web-scraping.dev/products',
|
|
69
|
+
* page_limit: 10,
|
|
70
|
+
* content_formats: ['markdown'],
|
|
71
|
+
* });
|
|
72
|
+
* ```
|
|
73
|
+
*/
|
|
74
|
+
export declare class CrawlerConfig {
|
|
75
|
+
url: string;
|
|
76
|
+
page_limit?: number;
|
|
77
|
+
max_depth?: number;
|
|
78
|
+
max_duration?: number;
|
|
79
|
+
max_api_credit?: number;
|
|
80
|
+
exclude_paths?: string[];
|
|
81
|
+
include_only_paths?: string[];
|
|
82
|
+
ignore_base_path_restriction?: boolean;
|
|
83
|
+
follow_external_links?: boolean;
|
|
84
|
+
allowed_external_domains?: string[];
|
|
85
|
+
follow_internal_subdomains?: boolean;
|
|
86
|
+
allowed_internal_subdomains?: string[];
|
|
87
|
+
headers?: Rec<string>;
|
|
88
|
+
delay?: number;
|
|
89
|
+
user_agent?: string;
|
|
90
|
+
max_concurrency?: number;
|
|
91
|
+
rendering_delay?: number;
|
|
92
|
+
use_sitemaps?: boolean;
|
|
93
|
+
respect_robots_txt?: boolean;
|
|
94
|
+
ignore_no_follow?: boolean;
|
|
95
|
+
cache?: boolean;
|
|
96
|
+
cache_ttl?: number;
|
|
97
|
+
cache_clear?: boolean;
|
|
98
|
+
content_formats?: CrawlerContentFormat[];
|
|
99
|
+
extraction_rules?: Rec<any>;
|
|
100
|
+
asp?: boolean;
|
|
101
|
+
proxy_pool?: string;
|
|
102
|
+
country?: string;
|
|
103
|
+
webhook_name?: string;
|
|
104
|
+
webhook_events?: CrawlerWebhookEventName[];
|
|
105
|
+
constructor(options: CrawlerConfigOptions);
|
|
106
|
+
private validateOptions;
|
|
107
|
+
/**
|
|
108
|
+
* Serialize to a flat object suitable for `JSON.stringify()` as a `POST /crawl` body.
|
|
109
|
+
*
|
|
110
|
+
* Unlike `ScrapeConfig.toApiParams()`, the crawler API takes its config as a JSON
|
|
111
|
+
* **body**, not URL query parameters. The API key is therefore NOT included here —
|
|
112
|
+
* it's added to the URL by the client.
|
|
113
|
+
*
|
|
114
|
+
* Fields left undefined are dropped so the server applies its own documented
|
|
115
|
+
* defaults (e.g. `respect_robots_txt=true`).
|
|
116
|
+
*/
|
|
117
|
+
toApiParams(): Rec<any>;
|
|
118
|
+
}
|
|
119
|
+
//# sourceMappingURL=crawlerconfig.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"crawlerconfig.d.ts","sourceRoot":"","sources":["../src/crawlerconfig.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,YAAY,CAAC;AAEtC;;;;;;;;;;GAUG;AACH,MAAM,MAAM,oBAAoB,GAC5B,MAAM,GACN,YAAY,GACZ,UAAU,GACV,MAAM,GACN,MAAM,GACN,gBAAgB,GAChB,eAAe,CAAC;AAYpB;;;;;GAKG;AACH,MAAM,MAAM,uBAAuB,GAC/B,iBAAiB,GACjB,qBAAqB,GACrB,qBAAqB,GACrB,wBAAwB,GACxB,oBAAoB,GACpB,iBAAiB,GACjB,mBAAmB,GACnB,kBAAkB,CAAC;AAavB;;;;;GAKG;AACH,MAAM,MAAM,oBAAoB,GAAG;IACjC,GAAG,EAAE,MAAM,CAAC;IAGZ,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,cAAc,CAAC,EAAE,MAAM,CAAC;IAGxB,aAAa,CAAC,EAAE,MAAM,EAAE,CAAC;IACzB,kBAAkB,CAAC,EAAE,MAAM,EAAE,CAAC;IAG9B,4BAA4B,CAAC,EAAE,OAAO,CAAC;IACvC,qBAAqB,CAAC,EAAE,OAAO,CAAC;IAChC,wBAAwB,CAAC,EAAE,MAAM,EAAE,CAAC;IACpC,0BAA0B,CAAC,EAAE,OAAO,CAAC;IACrC,2BAA2B,CAAC,EAAE,MAAM,EAAE,CAAC;IAGvC,OAAO,CAAC,EAAE,GAAG,CAAC,MAAM,CAAC,CAAC;IACtB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,eAAe,CAAC,EA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