webcrawlerapi-js 1.1.5 → 1.1.7
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/README.md +40 -78
- package/dist/@types/api.d.ts +3 -3
- package/dist/@types/model.d.ts +12 -0
- package/dist/api.js +52 -39
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -20,34 +20,36 @@ The package is regularly updated with new features and improvements to ensure th
|
|
|
20
20
|
## JavaScript Examples
|
|
21
21
|
|
|
22
22
|
### Synchronous Job
|
|
23
|
-
In this mode, the promise will be resolved when all data is ready. Synchronous jobs are perfect for quick scraping tasks where you need immediate results and don't want to handle polling logic.
|
|
23
|
+
In this mode, the promise will be resolved when all data is ready. Synchronous jobs are perfect for quick scraping tasks where you need immediate results and don't want to handle polling logic. However, for long running jobs we recommend to use async crawling or the webhook.
|
|
24
24
|
|
|
25
25
|
```javascript
|
|
26
26
|
import webcrawlerapi from "webcrawlerapi-js"
|
|
27
27
|
|
|
28
28
|
async function main() {
|
|
29
|
-
const client = new WebcrawlerClient(
|
|
30
|
-
"YOUR API ACCESS KEY HERE",
|
|
31
|
-
)
|
|
29
|
+
const client = new WebcrawlerClient("YOUR API ACCESS KEY HERE")
|
|
32
30
|
|
|
33
31
|
try {
|
|
34
|
-
const
|
|
32
|
+
const crawlJob = await client.crawl({
|
|
35
33
|
"items_limit": 10,
|
|
36
|
-
"url": "https://
|
|
34
|
+
"url": "https://books.toscrape.com/",
|
|
37
35
|
"scrape_type": "markdown"
|
|
38
36
|
})
|
|
39
37
|
|
|
40
|
-
|
|
38
|
+
// Get job details
|
|
39
|
+
const job = await client.getJob(crawlJob.id);
|
|
40
|
+
console.log("Job status:", job.status);
|
|
41
|
+
|
|
42
|
+
// Output item content
|
|
43
|
+
for (const item of job.job_items) {
|
|
41
44
|
const content = await item.getContent()
|
|
45
|
+
console.log("---- Title: " + item.title + "----")
|
|
42
46
|
console.log(content.slice(0, 100))
|
|
43
47
|
}
|
|
44
|
-
console.log(syncJob)
|
|
45
|
-
|
|
46
48
|
} catch (error) {
|
|
47
49
|
if (error instanceof webcrawlerapi.WebcrawlerApiError) {
|
|
48
50
|
console.error("Webcrawler error:", error.message)
|
|
49
51
|
}
|
|
50
|
-
|
|
52
|
+
console.error(error)
|
|
51
53
|
}
|
|
52
54
|
}
|
|
53
55
|
|
|
@@ -61,27 +63,32 @@ In this mode, you get a job ID immediately and can poll for results. Asynchronou
|
|
|
61
63
|
import webcrawlerapi from "webcrawlerapi-js"
|
|
62
64
|
|
|
63
65
|
async function main() {
|
|
64
|
-
const client = new WebcrawlerClient(
|
|
65
|
-
"YOUR API ACCESS KEY HERE",
|
|
66
|
-
)
|
|
66
|
+
const client = new WebcrawlerClient("YOUR API ACCESS KEY HERE")
|
|
67
67
|
|
|
68
68
|
try {
|
|
69
|
-
|
|
69
|
+
try {
|
|
70
70
|
const jobWithId = await client.crawlAsync({
|
|
71
71
|
"items_limit": 10,
|
|
72
|
-
"url": "https://
|
|
72
|
+
"url": "https://books.toscrape.com/",
|
|
73
73
|
"scrape_type": "markdown"
|
|
74
74
|
})
|
|
75
|
-
|
|
75
|
+
|
|
76
76
|
// Poll for job completion
|
|
77
77
|
const jobId = jobWithId.id
|
|
78
78
|
let asyncJob = await client.getJob(jobId)
|
|
79
|
-
|
|
79
|
+
|
|
80
80
|
// Check job status
|
|
81
|
-
console.log("Job
|
|
82
|
-
|
|
81
|
+
console.log("Job id:", asyncJob.id)
|
|
82
|
+
console.log("Dashboard link:", `https://dash.webcrawlerapi.com/jobs/job/${asyncJob.id}`)
|
|
83
|
+
|
|
84
|
+
//wait until job is done
|
|
85
|
+
while (asyncJob.status !== "done") {
|
|
86
|
+
await new Promise(resolve => setTimeout(resolve, 1000));
|
|
87
|
+
asyncJob = await client.getJob(jobId)
|
|
88
|
+
}
|
|
89
|
+
|
|
83
90
|
// Once complete, access results
|
|
84
|
-
if (asyncJob.status === "
|
|
91
|
+
if (asyncJob.status === "done") {
|
|
85
92
|
for (const item of asyncJob.job_items) {
|
|
86
93
|
const content = await item.getContent()
|
|
87
94
|
console.log(content.slice(0, 100))
|
|
@@ -91,6 +98,8 @@ async function main() {
|
|
|
91
98
|
if (error instanceof webcrawlerapi.WebcrawlerApiError) {
|
|
92
99
|
console.error("Webcrawler error:", error.message)
|
|
93
100
|
}
|
|
101
|
+
console.error(error)
|
|
102
|
+
return
|
|
94
103
|
}
|
|
95
104
|
}
|
|
96
105
|
|
|
@@ -102,87 +111,40 @@ main().catch(console.error)
|
|
|
102
111
|
The SDK provides full TypeScript support with type safety and error handling. Our TypeScript integration ensures you catch potential issues at compile-time and provides excellent IDE support with autocompletion and inline documentation.
|
|
103
112
|
|
|
104
113
|
```typescript
|
|
105
|
-
import {
|
|
114
|
+
import {WebcrawlerClient, WebcrawlerApiError} from "webcrawlerapi-js";
|
|
106
115
|
|
|
107
116
|
async function main() {
|
|
108
|
-
const client = new WebcrawlerClient(
|
|
109
|
-
"YOUR_API_KEY",
|
|
110
|
-
// Optional base URL, defaults to production API
|
|
111
|
-
"http://localhost:8080"
|
|
112
|
-
)
|
|
117
|
+
const client = new WebcrawlerClient("YOUR_API_KEY")
|
|
113
118
|
|
|
114
119
|
try {
|
|
115
120
|
// Basic crawl with markdown
|
|
116
121
|
const syncJob = await client.crawl({
|
|
117
|
-
"items_limit":
|
|
122
|
+
"items_limit": 10,
|
|
118
123
|
"url": "https://books.toscrape.com/",
|
|
119
|
-
"scrape_type": "markdown"
|
|
120
|
-
})
|
|
121
|
-
|
|
122
|
-
// Type-safe status check
|
|
123
|
-
if (syncJob.status === "done") {
|
|
124
|
-
for (const item of syncJob.job_items) {
|
|
125
|
-
if (item.markdown_content_url) {
|
|
126
|
-
// Access typed properties
|
|
127
|
-
console.log("Status code:", item.page_status_code)
|
|
128
|
-
console.log("Content URL:", item.markdown_content_url)
|
|
129
|
-
}
|
|
130
|
-
}
|
|
124
|
+
"scrape_type": "markdown",
|
|
131
125
|
}
|
|
132
|
-
|
|
133
|
-
//
|
|
134
|
-
const
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
"url": "https://books.toscrape.com/"
|
|
138
|
-
}
|
|
139
|
-
})
|
|
140
|
-
|
|
141
|
-
if (scrapeJob.status === "done") {
|
|
142
|
-
const content = scrapeJob.getContent()
|
|
143
|
-
console.log("Scraped content:", content)
|
|
126
|
+
)
|
|
127
|
+
// Type-safe status check
|
|
128
|
+
for (const item of syncJob.job_items) {
|
|
129
|
+
const content = await item.getContent()
|
|
130
|
+
console.log(content.slice(0,100))
|
|
144
131
|
}
|
|
145
|
-
|
|
146
132
|
} catch (error) {
|
|
147
|
-
// Type-safe error handling
|
|
148
133
|
if (error instanceof WebcrawlerApiError) {
|
|
149
134
|
console.error("API Error:", error.errorCode, error.message)
|
|
150
|
-
// errorCode is typed and can be used for specific error handling
|
|
151
|
-
if (error.errorCode === "invalid_request") {
|
|
152
|
-
// Handle invalid request
|
|
153
|
-
}
|
|
154
135
|
}
|
|
136
|
+
console.log(error)
|
|
155
137
|
}
|
|
156
138
|
}
|
|
157
139
|
|
|
158
140
|
main().catch(console.error)
|
|
159
141
|
```
|
|
160
142
|
|
|
161
|
-
### Error Handling
|
|
162
|
-
|
|
163
|
-
The SDK provides typed error handling through the `WebcrawlerApiError` class. Our error handling system is designed to provide detailed, actionable feedback that helps you quickly identify and resolve issues in your scraping pipeline.
|
|
164
|
-
|
|
165
|
-
```typescript
|
|
166
|
-
try {
|
|
167
|
-
const syncJob = await client.crawl({
|
|
168
|
-
"items_limit": 2,
|
|
169
|
-
"url": "https://books.toscrape.com/",
|
|
170
|
-
// This will cause an error due to missing scrape_type
|
|
171
|
-
})
|
|
172
|
-
} catch (error) {
|
|
173
|
-
if (error instanceof WebcrawlerApiError) {
|
|
174
|
-
// Typed error properties
|
|
175
|
-
console.error("Error code:", error.errorCode)
|
|
176
|
-
console.error("Error message:", error.message)
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
```
|
|
180
|
-
|
|
181
143
|
## Response Structure
|
|
182
144
|
|
|
183
145
|
Example of a job response. The response structure is carefully designed to provide comprehensive information about your scraping job, including detailed status updates, timing information, and resource usage metrics.
|
|
184
146
|
|
|
185
|
-
```
|
|
147
|
+
```js
|
|
186
148
|
{
|
|
187
149
|
id: '49c4942b-b7d9-4d62-94b5-b54a3016ac51',
|
|
188
150
|
url: 'https://stripe.com/',
|
package/dist/@types/api.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { CrawlRequest, Job, JobId, ScrapeRequest, ScrapeResponse } from "./model";
|
|
1
|
+
import { CrawlRequest, Job, JobId, ScrapeRequest, ScrapeResponse, Action } from "./model";
|
|
2
2
|
import { JobStatus } from "./constants";
|
|
3
3
|
import { WebcrawlerApiError } from "./errors";
|
|
4
4
|
export { WebcrawlerApiError, JobStatus };
|
|
@@ -12,8 +12,8 @@ export declare class WebcrawlerClient {
|
|
|
12
12
|
scrapeWithMeta(scrapeRequest: ScrapeRequest, maxPollingRetries?: number): Promise<ScrapeResponse>;
|
|
13
13
|
scrape(scrapeRequest: ScrapeRequest, maxPollingRetries?: number): Promise<any>;
|
|
14
14
|
getScrapeResult(scrapeID: string): Promise<ScrapeResponse>;
|
|
15
|
-
crawl(crawlRequest: CrawlRequest): Promise<Job>;
|
|
16
|
-
crawlAsync(crawlRequest: CrawlRequest): Promise<JobId>;
|
|
15
|
+
crawl(crawlRequest: CrawlRequest, actions?: Action | Action[]): Promise<Job>;
|
|
16
|
+
crawlAsync(crawlRequest: CrawlRequest, actions?: Action | Action[]): Promise<JobId>;
|
|
17
17
|
getJob(jobID: string): Promise<Job>;
|
|
18
18
|
private sendRequest;
|
|
19
19
|
}
|
package/dist/@types/model.d.ts
CHANGED
|
@@ -26,6 +26,7 @@ export interface CrawlRequest {
|
|
|
26
26
|
blacklist_regexp?: string;
|
|
27
27
|
allow_subdomains?: boolean;
|
|
28
28
|
webhook_url?: string;
|
|
29
|
+
actions?: Action[];
|
|
29
30
|
}
|
|
30
31
|
export interface Job {
|
|
31
32
|
id: string;
|
|
@@ -61,3 +62,14 @@ export interface JobItem {
|
|
|
61
62
|
cleaned_content_url?: string;
|
|
62
63
|
getContent(): Promise<string | null>;
|
|
63
64
|
}
|
|
65
|
+
export interface Action {
|
|
66
|
+
type: string;
|
|
67
|
+
}
|
|
68
|
+
export interface UploadS3Action extends Action {
|
|
69
|
+
type: 'upload_s3';
|
|
70
|
+
path: string;
|
|
71
|
+
access_key_id: string;
|
|
72
|
+
secret_access_key: string;
|
|
73
|
+
bucket: string;
|
|
74
|
+
endpoint?: string;
|
|
75
|
+
}
|
package/dist/api.js
CHANGED
|
@@ -23,6 +23,44 @@ const BASE_PATH = "https://api.webcrawlerapi.com";
|
|
|
23
23
|
const initialPullDelayMs = 2000;
|
|
24
24
|
const MaxPullRetries = 100;
|
|
25
25
|
__exportStar(require("./model"), exports);
|
|
26
|
+
// Helper function to add getContent method to job items
|
|
27
|
+
function addGetContentMethod(job) {
|
|
28
|
+
// Transform each job item to include getContent method
|
|
29
|
+
job.job_items = job.job_items.map(item => ({
|
|
30
|
+
...item,
|
|
31
|
+
getContent: async function () {
|
|
32
|
+
if (job.status !== constants_1.JobStatus.DONE || this.status !== constants_1.JobStatus.DONE) {
|
|
33
|
+
return null;
|
|
34
|
+
}
|
|
35
|
+
let contentUrl;
|
|
36
|
+
switch (job.scrape_type) {
|
|
37
|
+
case 'html':
|
|
38
|
+
contentUrl = this.raw_content_url;
|
|
39
|
+
break;
|
|
40
|
+
case 'cleaned':
|
|
41
|
+
contentUrl = this.cleaned_content_url;
|
|
42
|
+
break;
|
|
43
|
+
case 'markdown':
|
|
44
|
+
contentUrl = this.markdown_content_url;
|
|
45
|
+
break;
|
|
46
|
+
}
|
|
47
|
+
if (!contentUrl) {
|
|
48
|
+
return null;
|
|
49
|
+
}
|
|
50
|
+
const response = await fetch(contentUrl, {
|
|
51
|
+
headers: {
|
|
52
|
+
'Accept-Encoding': 'gzip, deflate, br',
|
|
53
|
+
'Accept': '*/*'
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
if (!response.ok) {
|
|
57
|
+
throw new Error(`Failed to fetch content: ${response.statusText}`);
|
|
58
|
+
}
|
|
59
|
+
return await response.text();
|
|
60
|
+
}
|
|
61
|
+
}));
|
|
62
|
+
return job;
|
|
63
|
+
}
|
|
26
64
|
class WebcrawlerClient {
|
|
27
65
|
constructor(apiKey, basePath = BASE_PATH, apiVersion = "v1") {
|
|
28
66
|
this.apiVersion = "v1";
|
|
@@ -92,8 +130,12 @@ class WebcrawlerClient {
|
|
|
92
130
|
};
|
|
93
131
|
return await this.sendRequest(url, requestOptions);
|
|
94
132
|
}
|
|
95
|
-
async crawl(crawlRequest) {
|
|
133
|
+
async crawl(crawlRequest, actions) {
|
|
96
134
|
const url = `${this.basePath}/${this.apiVersion}/crawl`;
|
|
135
|
+
const requestBody = {
|
|
136
|
+
...crawlRequest,
|
|
137
|
+
actions: actions ? (Array.isArray(actions) ? actions : [actions]) : undefined
|
|
138
|
+
};
|
|
97
139
|
const requestOptions = {
|
|
98
140
|
'method': 'POST',
|
|
99
141
|
'headers': {
|
|
@@ -104,7 +146,7 @@ class WebcrawlerClient {
|
|
|
104
146
|
'Pragma': 'no-cache',
|
|
105
147
|
'Expires': '0'
|
|
106
148
|
},
|
|
107
|
-
'body': JSON.stringify(
|
|
149
|
+
'body': JSON.stringify(requestBody),
|
|
108
150
|
};
|
|
109
151
|
const jobIdResponse = await this.sendRequest(url, requestOptions);
|
|
110
152
|
if (jobIdResponse.id === '') {
|
|
@@ -116,40 +158,6 @@ class WebcrawlerClient {
|
|
|
116
158
|
const timestamp = new Date().getTime();
|
|
117
159
|
const job = await this.getJob(`${jobIdResponse.id}?t=${timestamp}`);
|
|
118
160
|
if (job.status !== constants_1.JobStatus.IN_PROGRESS && job.status !== constants_1.JobStatus.NEW) {
|
|
119
|
-
// Transform each job item to include getContent method
|
|
120
|
-
job.job_items = job.job_items.map(item => ({
|
|
121
|
-
...item,
|
|
122
|
-
getContent: async function () {
|
|
123
|
-
if (job.status !== constants_1.JobStatus.DONE || this.status !== constants_1.JobStatus.DONE) {
|
|
124
|
-
return null;
|
|
125
|
-
}
|
|
126
|
-
let contentUrl;
|
|
127
|
-
switch (job.scrape_type) {
|
|
128
|
-
case 'html':
|
|
129
|
-
contentUrl = this.raw_content_url;
|
|
130
|
-
break;
|
|
131
|
-
case 'cleaned':
|
|
132
|
-
contentUrl = this.cleaned_content_url;
|
|
133
|
-
break;
|
|
134
|
-
case 'markdown':
|
|
135
|
-
contentUrl = this.markdown_content_url;
|
|
136
|
-
break;
|
|
137
|
-
}
|
|
138
|
-
if (!contentUrl) {
|
|
139
|
-
return null;
|
|
140
|
-
}
|
|
141
|
-
const response = await fetch(contentUrl, {
|
|
142
|
-
headers: {
|
|
143
|
-
'Accept-Encoding': 'gzip, deflate, br',
|
|
144
|
-
'Accept': '*/*'
|
|
145
|
-
}
|
|
146
|
-
});
|
|
147
|
-
if (!response.ok) {
|
|
148
|
-
throw new Error(`Failed to fetch content: ${response.statusText}`);
|
|
149
|
-
}
|
|
150
|
-
return await response.text();
|
|
151
|
-
}
|
|
152
|
-
}));
|
|
153
161
|
return job;
|
|
154
162
|
}
|
|
155
163
|
if (job.recommended_pull_delay_ms > 0) {
|
|
@@ -158,15 +166,19 @@ class WebcrawlerClient {
|
|
|
158
166
|
}
|
|
159
167
|
throw new errors_1.WebcrawlerApiError('timeout', 'Crawling took too long, please retry or increase the number of polling retries', 0);
|
|
160
168
|
}
|
|
161
|
-
async crawlAsync(crawlRequest) {
|
|
169
|
+
async crawlAsync(crawlRequest, actions) {
|
|
162
170
|
const url = `${this.basePath}/${this.apiVersion}/crawl`;
|
|
171
|
+
const requestBody = {
|
|
172
|
+
...crawlRequest,
|
|
173
|
+
actions: actions ? (Array.isArray(actions) ? actions : [actions]) : undefined
|
|
174
|
+
};
|
|
163
175
|
const requestOptions = {
|
|
164
176
|
'method': 'POST',
|
|
165
177
|
'headers': {
|
|
166
178
|
'Content-Type': 'application/json',
|
|
167
179
|
'Authorization': `Bearer ${this.apiKey}`
|
|
168
180
|
},
|
|
169
|
-
'body': JSON.stringify(
|
|
181
|
+
'body': JSON.stringify(requestBody),
|
|
170
182
|
};
|
|
171
183
|
return await this.sendRequest(url, requestOptions);
|
|
172
184
|
}
|
|
@@ -183,7 +195,8 @@ class WebcrawlerClient {
|
|
|
183
195
|
'Expires': '0'
|
|
184
196
|
}
|
|
185
197
|
};
|
|
186
|
-
|
|
198
|
+
const job = await this.sendRequest(url, requestOptions);
|
|
199
|
+
return addGetContentMethod(job);
|
|
187
200
|
}
|
|
188
201
|
async sendRequest(url, requestOptions) {
|
|
189
202
|
let response;
|