webcrawlerapi-js 1.1.0 → 1.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/@types/api.d.ts +4 -0
- package/dist/api.js +18 -1
- package/package.json +8 -3
- package/dist/@types/index.d.ts +0 -4
- package/dist/api.d.ts +0 -15
- package/dist/configuration.d.ts +0 -16
- package/dist/configuration.js +0 -20
- package/dist/constants.d.ts +0 -13
- package/dist/errors.d.ts +0 -12
- package/dist/index.js +0 -20
- package/dist/model.d.ts +0 -62
- package/src/api.ts +0 -229
- package/src/constants.ts +0 -14
- package/src/errors.ts +0 -25
- package/src/index.ts +0 -4
- package/src/model.ts +0 -67
- package/tsconfig.json +0 -23
package/dist/@types/api.d.ts
CHANGED
|
@@ -1,4 +1,8 @@
|
|
|
1
1
|
import { CrawlRequest, Job, JobId, ScrapeRequest, ScrapeResponse } from "./model";
|
|
2
|
+
import { JobStatus } from "./constants";
|
|
3
|
+
import { WebcrawlerApiError } from "./errors";
|
|
4
|
+
export { WebcrawlerApiError, JobStatus };
|
|
5
|
+
export * from "./model";
|
|
2
6
|
export declare class WebcrawlerClient {
|
|
3
7
|
protected apiVersion: string;
|
|
4
8
|
protected apiKey: string;
|
package/dist/api.js
CHANGED
|
@@ -1,11 +1,28 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
2
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.WebcrawlerClient = void 0;
|
|
17
|
+
exports.WebcrawlerClient = exports.JobStatus = exports.WebcrawlerApiError = void 0;
|
|
4
18
|
const constants_1 = require("./constants");
|
|
19
|
+
Object.defineProperty(exports, "JobStatus", { enumerable: true, get: function () { return constants_1.JobStatus; } });
|
|
5
20
|
const errors_1 = require("./errors");
|
|
21
|
+
Object.defineProperty(exports, "WebcrawlerApiError", { enumerable: true, get: function () { return errors_1.WebcrawlerApiError; } });
|
|
6
22
|
const BASE_PATH = "https://api.webcrawlerapi.com";
|
|
7
23
|
const initialPullDelayMs = 2000;
|
|
8
24
|
const MaxPullRetries = 100;
|
|
25
|
+
__exportStar(require("./model"), exports);
|
|
9
26
|
class WebcrawlerClient {
|
|
10
27
|
constructor(apiKey, basePath = BASE_PATH, apiVersion = "v1") {
|
|
11
28
|
this.apiVersion = "v1";
|
package/package.json
CHANGED
|
@@ -1,10 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "webcrawlerapi-js",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.2",
|
|
4
4
|
|
|
5
5
|
"description": "JS client for WebcrawlerAPI",
|
|
6
|
-
"main": "./dist/
|
|
7
|
-
"types": "./dist/
|
|
6
|
+
"main": "./dist/api.js",
|
|
7
|
+
"types": "./dist/@types/api.d.ts",
|
|
8
|
+
"files": [
|
|
9
|
+
"dist/",
|
|
10
|
+
"README.md",
|
|
11
|
+
"LICENSE"
|
|
12
|
+
],
|
|
8
13
|
"scripts": {
|
|
9
14
|
"build": "tsc --outDir dist/"
|
|
10
15
|
},
|
package/dist/@types/index.d.ts
DELETED
package/dist/api.d.ts
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import { CrawlRequest, Job, JobId, ScrapeRequest, ScrapeResponse } from "./model";
|
|
2
|
-
export declare class WebcrawlerClient {
|
|
3
|
-
protected apiVersion: string;
|
|
4
|
-
protected apiKey: string;
|
|
5
|
-
protected basePath: string;
|
|
6
|
-
constructor(apiKey: string, basePath?: string, apiVersion?: string);
|
|
7
|
-
scrapeAsync(scrapeRequest: ScrapeRequest): Promise<JobId>;
|
|
8
|
-
scrapeWithMeta(scrapeRequest: ScrapeRequest, maxPollingRetries?: number): Promise<ScrapeResponse>;
|
|
9
|
-
scrape(scrapeRequest: ScrapeRequest, maxPollingRetries?: number): Promise<any>;
|
|
10
|
-
getScrapeResult(scrapeID: string): Promise<ScrapeResponse>;
|
|
11
|
-
crawl(crawlRequest: CrawlRequest): Promise<Job>;
|
|
12
|
-
crawlAsync(crawlRequest: CrawlRequest): Promise<JobId>;
|
|
13
|
-
getJob(jobID: string): Promise<Job>;
|
|
14
|
-
private sendRequest;
|
|
15
|
-
}
|
package/dist/configuration.d.ts
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
export interface ConfigurationParameters {
|
|
2
|
-
apiKey?: string;
|
|
3
|
-
basePath?: string;
|
|
4
|
-
apiVersion?: string;
|
|
5
|
-
}
|
|
6
|
-
export declare class Configuration {
|
|
7
|
-
/**
|
|
8
|
-
* Base options for http call
|
|
9
|
-
*/
|
|
10
|
-
baseOptions?: any;
|
|
11
|
-
/**
|
|
12
|
-
* API version, for example: "v1"
|
|
13
|
-
*/
|
|
14
|
-
apiVersion: string;
|
|
15
|
-
constructor(param?: ConfigurationParameters);
|
|
16
|
-
}
|
package/dist/configuration.js
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Configuration = void 0;
|
|
4
|
-
const packageJson = require("../package.json");
|
|
5
|
-
class Configuration {
|
|
6
|
-
constructor(param = {}) {
|
|
7
|
-
/**
|
|
8
|
-
* API version, for example: "v1"
|
|
9
|
-
*/
|
|
10
|
-
this.apiVersion = "v1";
|
|
11
|
-
this.apiVersion = param.apiVersion;
|
|
12
|
-
if (!this.baseOptions) {
|
|
13
|
-
this.baseOptions = {};
|
|
14
|
-
}
|
|
15
|
-
this.baseOptions = {
|
|
16
|
-
headers: Object.assign({ 'User-Agent': `webcrawlerapi-js/${packageJson.version}`, 'Content-Type': 'application/json' }, this.baseOptions.headers)
|
|
17
|
-
};
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
exports.Configuration = Configuration;
|
package/dist/constants.d.ts
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
export declare const JobStatus: {
|
|
2
|
-
readonly IN_PROGRESS: "in_progress";
|
|
3
|
-
readonly NEW: "new";
|
|
4
|
-
readonly DONE: "done";
|
|
5
|
-
};
|
|
6
|
-
export declare const HttpStatus: {
|
|
7
|
-
readonly OK: 200;
|
|
8
|
-
readonly BAD_REQUEST: 400;
|
|
9
|
-
readonly UNAUTHORIZED: 401;
|
|
10
|
-
readonly FORBIDDEN: 403;
|
|
11
|
-
readonly NOT_FOUND: 404;
|
|
12
|
-
readonly INTERNAL_SERVER_ERROR: 500;
|
|
13
|
-
};
|
package/dist/errors.d.ts
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
export declare class WebcrawlerApiError extends Error {
|
|
2
|
-
errorCode: string;
|
|
3
|
-
errorMessage: string;
|
|
4
|
-
statusCode?: number;
|
|
5
|
-
constructor(errorCode: string, errorMessage: string, statusCode?: number);
|
|
6
|
-
}
|
|
7
|
-
export interface ErrorResponse {
|
|
8
|
-
error_code?: string;
|
|
9
|
-
error_message?: string;
|
|
10
|
-
error?: string;
|
|
11
|
-
}
|
|
12
|
-
export declare function createErrorFromResponse(response: Response, data: ErrorResponse): WebcrawlerApiError;
|
package/dist/index.js
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
-
};
|
|
16
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
__exportStar(require("./api"), exports);
|
|
18
|
-
__exportStar(require("./model"), exports);
|
|
19
|
-
__exportStar(require("./constants"), exports);
|
|
20
|
-
__exportStar(require("./errors"), exports);
|
package/dist/model.d.ts
DELETED
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
export interface ScrapeRequest {
|
|
2
|
-
input: any;
|
|
3
|
-
crawler_id: string;
|
|
4
|
-
max_retries?: number;
|
|
5
|
-
debug?: boolean;
|
|
6
|
-
omit_cache?: boolean;
|
|
7
|
-
}
|
|
8
|
-
export interface JobId {
|
|
9
|
-
id: string;
|
|
10
|
-
}
|
|
11
|
-
export interface ScrapeResponse {
|
|
12
|
-
id: string;
|
|
13
|
-
url: string;
|
|
14
|
-
status: any;
|
|
15
|
-
page_status_code: number;
|
|
16
|
-
created_at: string;
|
|
17
|
-
structured_data: any;
|
|
18
|
-
recommended_pull_delay_ms: number;
|
|
19
|
-
}
|
|
20
|
-
export interface CrawlRequest {
|
|
21
|
-
url: string;
|
|
22
|
-
items_limit?: number;
|
|
23
|
-
scrape_type?: string;
|
|
24
|
-
whitelist_regexp?: string;
|
|
25
|
-
blacklist_regexp?: string;
|
|
26
|
-
allow_subdomains?: boolean;
|
|
27
|
-
webhook_url?: string;
|
|
28
|
-
}
|
|
29
|
-
export interface Job {
|
|
30
|
-
id: string;
|
|
31
|
-
org_id: string;
|
|
32
|
-
url: string;
|
|
33
|
-
status: string;
|
|
34
|
-
scrape_type: string;
|
|
35
|
-
whitelist_regexp: string;
|
|
36
|
-
blacklist_regexp: string;
|
|
37
|
-
allow_subdomains: boolean;
|
|
38
|
-
items_limit: number;
|
|
39
|
-
created_at: string;
|
|
40
|
-
finished_at: string;
|
|
41
|
-
updated_at: string;
|
|
42
|
-
webhook_url: string;
|
|
43
|
-
recommended_pull_delay_ms: number;
|
|
44
|
-
job_items: JobItem[];
|
|
45
|
-
}
|
|
46
|
-
export interface JobItem {
|
|
47
|
-
id: string;
|
|
48
|
-
job_id: string;
|
|
49
|
-
original_url: string;
|
|
50
|
-
page_status_code: number;
|
|
51
|
-
markdown_content_url: string;
|
|
52
|
-
status: string;
|
|
53
|
-
title: string;
|
|
54
|
-
last_error: string;
|
|
55
|
-
created_at: string;
|
|
56
|
-
updated_at: string;
|
|
57
|
-
cost: number;
|
|
58
|
-
referred_url: string;
|
|
59
|
-
raw_content_url?: string;
|
|
60
|
-
cleaned_content_url?: string;
|
|
61
|
-
getContent(): Promise<string | null>;
|
|
62
|
-
}
|
package/src/api.ts
DELETED
|
@@ -1,229 +0,0 @@
|
|
|
1
|
-
import {CrawlRequest, Job, JobId, ScrapeRequest, ScrapeResponse} from "./model";
|
|
2
|
-
import { JobStatus } from "./constants";
|
|
3
|
-
import { WebcrawlerApiError, createErrorFromResponse, ErrorResponse } from "./errors";
|
|
4
|
-
|
|
5
|
-
const BASE_PATH = "https://api.webcrawlerapi.com"
|
|
6
|
-
const initialPullDelayMs = 2000
|
|
7
|
-
const MaxPullRetries = 100
|
|
8
|
-
|
|
9
|
-
export class WebcrawlerClient {
|
|
10
|
-
protected apiVersion: string = "v1";
|
|
11
|
-
protected apiKey: string
|
|
12
|
-
protected basePath: string
|
|
13
|
-
|
|
14
|
-
constructor(apiKey: string, basePath: string = BASE_PATH, apiVersion: string = "v1") {
|
|
15
|
-
this.apiKey = apiKey ;
|
|
16
|
-
this.basePath = basePath;
|
|
17
|
-
this.apiVersion = apiVersion;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
public async scrapeAsync(scrapeRequest: ScrapeRequest): Promise<JobId> {
|
|
21
|
-
const url = `${this.basePath}/${this.apiVersion}/scrape`;
|
|
22
|
-
|
|
23
|
-
const requestOptions = {
|
|
24
|
-
'method': 'POST',
|
|
25
|
-
'headers': {
|
|
26
|
-
'Content-Type': 'application/json',
|
|
27
|
-
'Authorization': `Bearer ${this.apiKey}`,
|
|
28
|
-
"User-Agent": "WebcrawlerAPI-NodeJS-Client"
|
|
29
|
-
},
|
|
30
|
-
'body': JSON.stringify(scrapeRequest),
|
|
31
|
-
};
|
|
32
|
-
|
|
33
|
-
return await this.sendRequest(url, requestOptions);
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
public async scrapeWithMeta(scrapeRequest: ScrapeRequest, maxPollingRetries: number = MaxPullRetries): Promise<ScrapeResponse> {
|
|
37
|
-
const url = `${this.basePath}/${this.apiVersion}/scrape`;
|
|
38
|
-
|
|
39
|
-
const requestOptions = {
|
|
40
|
-
'method': 'POST',
|
|
41
|
-
'headers': {
|
|
42
|
-
'Content-Type': 'application/json',
|
|
43
|
-
'Authorization': `Bearer ${this.apiKey}`
|
|
44
|
-
},
|
|
45
|
-
'body': JSON.stringify(scrapeRequest),
|
|
46
|
-
};
|
|
47
|
-
|
|
48
|
-
const jobIdResponse: JobId = await this.sendRequest(url, requestOptions);
|
|
49
|
-
|
|
50
|
-
if (jobIdResponse.id === '') {
|
|
51
|
-
throw new WebcrawlerApiError('invalid_response', 'Failed to fetch job status', 0);
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
let delayIntervalMs = initialPullDelayMs;
|
|
55
|
-
for (let i = 0; i < maxPollingRetries; i++) {
|
|
56
|
-
await new Promise(resolve => setTimeout(resolve, delayIntervalMs));
|
|
57
|
-
const scrapeResult = await this.getScrapeResult(jobIdResponse.id);
|
|
58
|
-
if (scrapeRequest.debug) {
|
|
59
|
-
console.log(`Scrape result: ${JSON.stringify(scrapeResult)}`);
|
|
60
|
-
}
|
|
61
|
-
if (scrapeResult.status !== JobStatus.IN_PROGRESS && scrapeResult.status !== JobStatus.NEW) {
|
|
62
|
-
return scrapeResult;
|
|
63
|
-
}
|
|
64
|
-
if (scrapeResult.recommended_pull_delay_ms > 0) {
|
|
65
|
-
delayIntervalMs = scrapeResult.recommended_pull_delay_ms;
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
throw new WebcrawlerApiError('timeout', 'Scraping took too long, please retry or increase the number of polling retries', 0);
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
public async scrape(scrapeRequest: ScrapeRequest, maxPollingRetries: number = MaxPullRetries): Promise<any> {
|
|
72
|
-
const scrapeResult = await this.scrapeWithMeta(scrapeRequest, maxPollingRetries);
|
|
73
|
-
return scrapeResult.structured_data;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
public async getScrapeResult(scrapeID: string): Promise<ScrapeResponse> {
|
|
77
|
-
const url = `${this.basePath}/${this.apiVersion}/scrape/${scrapeID}`;
|
|
78
|
-
const requestOptions = {
|
|
79
|
-
'method': 'GET',
|
|
80
|
-
'headers': {
|
|
81
|
-
'Content-Type': 'application/json',
|
|
82
|
-
'Authorization': `Bearer ${this.apiKey}`,
|
|
83
|
-
"User-Agent": "WebcrawlerAPI-NodeJS-Client",
|
|
84
|
-
'Cache-Control': 'no-cache, no-store, must-revalidate',
|
|
85
|
-
'Pragma': 'no-cache',
|
|
86
|
-
'Expires': '0'
|
|
87
|
-
},
|
|
88
|
-
};
|
|
89
|
-
return await this.sendRequest(url, requestOptions);
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
public async crawl(crawlRequest: CrawlRequest): Promise<Job> {
|
|
93
|
-
const url = `${this.basePath}/${this.apiVersion}/crawl`;
|
|
94
|
-
|
|
95
|
-
const requestOptions = {
|
|
96
|
-
'method': 'POST',
|
|
97
|
-
'headers': {
|
|
98
|
-
'Content-Type': 'application/json',
|
|
99
|
-
'Authorization': `Bearer ${this.apiKey}`,
|
|
100
|
-
"User-Agent": "WebcrawlerAPI-NodeJS-Client",
|
|
101
|
-
'Cache-Control': 'no-cache, no-store, must-revalidate',
|
|
102
|
-
'Pragma': 'no-cache',
|
|
103
|
-
'Expires': '0'
|
|
104
|
-
},
|
|
105
|
-
'body': JSON.stringify(crawlRequest),
|
|
106
|
-
};
|
|
107
|
-
|
|
108
|
-
const jobIdResponse: JobId = await this.sendRequest(url, requestOptions);
|
|
109
|
-
|
|
110
|
-
if (jobIdResponse.id === '') {
|
|
111
|
-
throw new WebcrawlerApiError('invalid_response', 'Failed to fetch job status', 0);
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
let delayIntervalMs = initialPullDelayMs;
|
|
115
|
-
for (let i = 0; i < MaxPullRetries; i++) {
|
|
116
|
-
await new Promise(resolve => setTimeout(resolve, delayIntervalMs));
|
|
117
|
-
const timestamp = new Date().getTime();
|
|
118
|
-
const job = await this.getJob(`${jobIdResponse.id}?t=${timestamp}`);
|
|
119
|
-
if (job.status !== JobStatus.IN_PROGRESS && job.status !== JobStatus.NEW) {
|
|
120
|
-
// Transform each job item to include getContent method
|
|
121
|
-
job.job_items = job.job_items.map(item => ({
|
|
122
|
-
...item,
|
|
123
|
-
getContent: async function(): Promise<string | null> {
|
|
124
|
-
if (job.status !== JobStatus.DONE || this.status !== JobStatus.DONE) {
|
|
125
|
-
return null;
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
let contentUrl: string | undefined;
|
|
129
|
-
switch (job.scrape_type) {
|
|
130
|
-
case 'html':
|
|
131
|
-
contentUrl = this.raw_content_url;
|
|
132
|
-
break;
|
|
133
|
-
case 'cleaned':
|
|
134
|
-
contentUrl = this.cleaned_content_url;
|
|
135
|
-
break;
|
|
136
|
-
case 'markdown':
|
|
137
|
-
contentUrl = this.markdown_content_url;
|
|
138
|
-
break;
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
if (!contentUrl) {
|
|
142
|
-
return null;
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
const response = await fetch(contentUrl, {
|
|
146
|
-
headers: {
|
|
147
|
-
'Accept-Encoding': 'gzip, deflate, br',
|
|
148
|
-
'Accept': '*/*'
|
|
149
|
-
}
|
|
150
|
-
});
|
|
151
|
-
|
|
152
|
-
if (!response.ok) {
|
|
153
|
-
throw new Error(`Failed to fetch content: ${response.statusText}`);
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
return await response.text();
|
|
157
|
-
}
|
|
158
|
-
}));
|
|
159
|
-
return job;
|
|
160
|
-
}
|
|
161
|
-
if (job.recommended_pull_delay_ms > 0) {
|
|
162
|
-
delayIntervalMs = job.recommended_pull_delay_ms;
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
throw new WebcrawlerApiError('timeout', 'Crawling took too long, please retry or increase the number of polling retries', 0);
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
public async crawlAsync(crawlRequest: CrawlRequest): Promise<JobId> {
|
|
169
|
-
const url = `${this.basePath}/${this.apiVersion}/crawl`;
|
|
170
|
-
|
|
171
|
-
const requestOptions = {
|
|
172
|
-
'method': 'POST',
|
|
173
|
-
'headers': {
|
|
174
|
-
'Content-Type': 'application/json',
|
|
175
|
-
'Authorization': `Bearer ${this.apiKey}`
|
|
176
|
-
},
|
|
177
|
-
'body': JSON.stringify(crawlRequest),
|
|
178
|
-
};
|
|
179
|
-
|
|
180
|
-
return await this.sendRequest(url, requestOptions);
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
public async getJob(jobID: string): Promise<Job> {
|
|
184
|
-
const url = `${this.basePath}/${this.apiVersion}/job/${jobID}`;
|
|
185
|
-
const requestOptions = {
|
|
186
|
-
'method': 'GET',
|
|
187
|
-
'headers': {
|
|
188
|
-
'Content-Type': 'application/json',
|
|
189
|
-
'Authorization': `Bearer ${this.apiKey}`,
|
|
190
|
-
"User-Agent": "WebcrawlerAPI-NodeJS-Client",
|
|
191
|
-
'Cache-Control': 'no-cache, no-store, must-revalidate',
|
|
192
|
-
'Pragma': 'no-cache',
|
|
193
|
-
'Expires': '0'
|
|
194
|
-
}
|
|
195
|
-
};
|
|
196
|
-
return await this.sendRequest(url, requestOptions);
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
private async sendRequest(url: string, requestOptions: any): Promise<any> {
|
|
200
|
-
let response: Response;
|
|
201
|
-
try {
|
|
202
|
-
response = await fetch(url, requestOptions);
|
|
203
|
-
} catch (e) {
|
|
204
|
-
throw new WebcrawlerApiError('network_error', `Failed to send request: ${e}`, 0);
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
if (!response.ok) {
|
|
208
|
-
try {
|
|
209
|
-
const errorData = await response.json() as ErrorResponse;
|
|
210
|
-
throw createErrorFromResponse(response, errorData);
|
|
211
|
-
} catch (e) {
|
|
212
|
-
if (e instanceof WebcrawlerApiError) {
|
|
213
|
-
throw e;
|
|
214
|
-
}
|
|
215
|
-
// If we can't parse the error response, create a generic error
|
|
216
|
-
throw new WebcrawlerApiError(
|
|
217
|
-
'unknown_error',
|
|
218
|
-
`Request failed with status ${response.status} ${response.statusText}`,
|
|
219
|
-
response.status
|
|
220
|
-
);
|
|
221
|
-
}
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
return response.json();
|
|
225
|
-
}
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
package/src/constants.ts
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
export const JobStatus = {
|
|
2
|
-
IN_PROGRESS: 'in_progress',
|
|
3
|
-
NEW: 'new',
|
|
4
|
-
DONE: 'done'
|
|
5
|
-
} as const;
|
|
6
|
-
|
|
7
|
-
export const HttpStatus = {
|
|
8
|
-
OK: 200,
|
|
9
|
-
BAD_REQUEST: 400,
|
|
10
|
-
UNAUTHORIZED: 401,
|
|
11
|
-
FORBIDDEN: 403,
|
|
12
|
-
NOT_FOUND: 404,
|
|
13
|
-
INTERNAL_SERVER_ERROR: 500
|
|
14
|
-
} as const;
|
package/src/errors.ts
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
export class WebcrawlerApiError extends Error {
|
|
2
|
-
public errorCode: string;
|
|
3
|
-
public errorMessage: string;
|
|
4
|
-
public statusCode?: number;
|
|
5
|
-
|
|
6
|
-
constructor(errorCode: string, errorMessage: string, statusCode?: number) {
|
|
7
|
-
super(errorMessage);
|
|
8
|
-
this.name = 'WebcrawlerApiError';
|
|
9
|
-
this.errorCode = errorCode;
|
|
10
|
-
this.errorMessage = errorMessage;
|
|
11
|
-
this.statusCode = statusCode;
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export interface ErrorResponse {
|
|
16
|
-
error_code?: string;
|
|
17
|
-
error_message?: string;
|
|
18
|
-
error?: string;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
export function createErrorFromResponse(response: Response, data: ErrorResponse): WebcrawlerApiError {
|
|
22
|
-
const errorCode = data.error_code || 'unknown_error';
|
|
23
|
-
const errorMessage = data.error_message || data.error || 'Unknown error';
|
|
24
|
-
return new WebcrawlerApiError(errorCode, errorMessage, response.status);
|
|
25
|
-
}
|
package/src/index.ts
DELETED
package/src/model.ts
DELETED
|
@@ -1,67 +0,0 @@
|
|
|
1
|
-
export interface ScrapeRequest {
|
|
2
|
-
input: any;
|
|
3
|
-
crawler_id: string;
|
|
4
|
-
max_retries?: number;
|
|
5
|
-
debug?: boolean;
|
|
6
|
-
omit_cache?: boolean;
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
export interface JobId {
|
|
10
|
-
id: string;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export interface ScrapeResponse {
|
|
14
|
-
id: string;
|
|
15
|
-
url: string;
|
|
16
|
-
status: any;
|
|
17
|
-
page_status_code: number;
|
|
18
|
-
created_at: string;
|
|
19
|
-
structured_data: any;
|
|
20
|
-
recommended_pull_delay_ms: number;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export interface CrawlRequest {
|
|
24
|
-
url: string;
|
|
25
|
-
items_limit?: number;
|
|
26
|
-
scrape_type?: string;
|
|
27
|
-
whitelist_regexp?: string;
|
|
28
|
-
blacklist_regexp?: string;
|
|
29
|
-
allow_subdomains?: boolean;
|
|
30
|
-
webhook_url?: string;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
export interface Job {
|
|
34
|
-
id: string;
|
|
35
|
-
org_id: string;
|
|
36
|
-
url: string;
|
|
37
|
-
status: string;
|
|
38
|
-
scrape_type: string;
|
|
39
|
-
whitelist_regexp: string;
|
|
40
|
-
blacklist_regexp: string;
|
|
41
|
-
allow_subdomains: boolean;
|
|
42
|
-
items_limit: number;
|
|
43
|
-
created_at: string;
|
|
44
|
-
finished_at: string;
|
|
45
|
-
updated_at: string;
|
|
46
|
-
webhook_url: string;
|
|
47
|
-
recommended_pull_delay_ms: number;
|
|
48
|
-
job_items: JobItem[];
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
export interface JobItem {
|
|
52
|
-
id: string;
|
|
53
|
-
job_id: string;
|
|
54
|
-
original_url: string;
|
|
55
|
-
page_status_code: number;
|
|
56
|
-
markdown_content_url: string;
|
|
57
|
-
status: string;
|
|
58
|
-
title: string;
|
|
59
|
-
last_error: string;
|
|
60
|
-
created_at: string;
|
|
61
|
-
updated_at: string;
|
|
62
|
-
cost: number;
|
|
63
|
-
referred_url: string;
|
|
64
|
-
raw_content_url?: string;
|
|
65
|
-
cleaned_content_url?: string;
|
|
66
|
-
getContent(): Promise<string | null>;
|
|
67
|
-
}
|
package/tsconfig.json
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
"lib": [
|
|
4
|
-
"es2021"
|
|
5
|
-
],
|
|
6
|
-
"module": "commonjs",
|
|
7
|
-
"target": "es2021",
|
|
8
|
-
"strict": true,
|
|
9
|
-
"esModuleInterop": true,
|
|
10
|
-
"skipLibCheck": true,
|
|
11
|
-
"forceConsistentCasingInFileNames": true,
|
|
12
|
-
"outDir": "dist",
|
|
13
|
-
"declarationDir": "dist/@types",
|
|
14
|
-
"declaration": true
|
|
15
|
-
},
|
|
16
|
-
"include": [
|
|
17
|
-
"src/**/*"
|
|
18
|
-
],
|
|
19
|
-
"exclude": [
|
|
20
|
-
"dist",
|
|
21
|
-
"node_modules"
|
|
22
|
-
]
|
|
23
|
-
}
|