scraply 1.0.18 → 1.0.20
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/package.json +1 -1
- package/readme.md +8 -7
- package/src/defaultConfig.js +7 -6
- package/src/utils/crawl/url/fetch.js +17 -14
- package/src/utils/crawl/url/handlers.js +18 -16
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -70,15 +70,16 @@ CRAWLER: {
|
|
|
70
70
|
'aside',
|
|
71
71
|
'button'
|
|
72
72
|
],
|
|
73
|
-
RETRY_STATUS_CODES: [408,
|
|
74
|
-
REQUEST_TIMEOUT:
|
|
75
|
-
MAX_REDIRECTS:
|
|
76
|
-
|
|
73
|
+
RETRY_STATUS_CODES: [408, 500, 502, 503, 504],
|
|
74
|
+
REQUEST_TIMEOUT: 3000,
|
|
75
|
+
MAX_REDIRECTS: 2,
|
|
76
|
+
MAX_CONTENT_LENGTH: 20 * 1024 * 1024, // 20MB
|
|
77
|
+
MAX_RETRIES: 1,
|
|
77
78
|
CRAWL_DELAY_MS: 200,
|
|
78
|
-
CRAWL_ERROR_RETRY_DELAY_MS:
|
|
79
|
+
CRAWL_ERROR_RETRY_DELAY_MS: 1000,
|
|
79
80
|
CRAWL_RATE_LIMIT_FALLBACK_DELAY_MS: 60000,
|
|
80
|
-
|
|
81
|
-
|
|
81
|
+
EXIT_ON_RATE_LIMIT: true, // If true, forces exit instantly. If false, only exits after retries (if still 429)
|
|
82
|
+
EXIT_CODE_RATE_LIMIT: 10
|
|
82
83
|
},
|
|
83
84
|
|
|
84
85
|
DATA_FORMATTER: {
|
package/src/defaultConfig.js
CHANGED
|
@@ -35,14 +35,15 @@ export const DEFAULT_CONFIG = {
|
|
|
35
35
|
'aside',
|
|
36
36
|
'button'
|
|
37
37
|
],
|
|
38
|
-
RETRY_STATUS_CODES: [408,
|
|
39
|
-
REQUEST_TIMEOUT:
|
|
40
|
-
MAX_REDIRECTS:
|
|
41
|
-
|
|
38
|
+
RETRY_STATUS_CODES: [408, 500, 502, 503, 504],
|
|
39
|
+
REQUEST_TIMEOUT: 3000,
|
|
40
|
+
MAX_REDIRECTS: 2,
|
|
41
|
+
MAX_CONTENT_LENGTH: 20 * 1024 * 1024, // 20MB
|
|
42
|
+
MAX_RETRIES: 1,
|
|
42
43
|
CRAWL_DELAY_MS: 200,
|
|
43
|
-
CRAWL_ERROR_RETRY_DELAY_MS:
|
|
44
|
+
CRAWL_ERROR_RETRY_DELAY_MS: 1000,
|
|
44
45
|
CRAWL_RATE_LIMIT_FALLBACK_DELAY_MS: 60000,
|
|
45
|
-
EXIT_ON_RATE_LIMIT: true,
|
|
46
|
+
EXIT_ON_RATE_LIMIT: true, // If true, forces exit instantly. If false, only exits after retries (if still 429)
|
|
46
47
|
EXIT_CODE_RATE_LIMIT: 10
|
|
47
48
|
},
|
|
48
49
|
|
|
@@ -6,25 +6,25 @@ export async function fetchURL(url, retries = 2) {
|
|
|
6
6
|
try {
|
|
7
7
|
const response = await axios.get(url, {
|
|
8
8
|
timeout: CONFIG.CRAWLER.REQUEST_TIMEOUT,
|
|
9
|
-
maxRedirects: CONFIG.CRAWLER.MAX_REDIRECTS
|
|
9
|
+
maxRedirects: CONFIG.CRAWLER.MAX_REDIRECTS,
|
|
10
|
+
maxContentLength: CONFIG.CRAWLER.MAX_CONTENT_LENGTH
|
|
10
11
|
});
|
|
11
12
|
|
|
12
13
|
const { 'content-type': contentType } = response.headers;
|
|
13
14
|
|
|
15
|
+
if (!contentType) {
|
|
16
|
+
console.log(`Missing Content-Type header for ${url}`);
|
|
17
|
+
return { error: `Missing Content-Type header`, status: response.status };
|
|
18
|
+
};
|
|
19
|
+
|
|
14
20
|
// Validate content type
|
|
15
21
|
if (!CONFIG.CRAWLER.ALLOWED_CONTENT_TYPES.some(type => contentType.includes(type))) {
|
|
16
|
-
return {
|
|
17
|
-
error: `Content-Type ${contentType} is not allowed.`,
|
|
18
|
-
status: response.status
|
|
19
|
-
};
|
|
22
|
+
return { error: `Content-Type ${contentType} is not allowed.`, status: response.status };
|
|
20
23
|
};
|
|
21
24
|
|
|
22
|
-
return {
|
|
23
|
-
data: response.data,
|
|
24
|
-
status: response.status
|
|
25
|
-
};
|
|
25
|
+
return { data: response.data, status: response.status };
|
|
26
26
|
} catch (error) {
|
|
27
|
-
if (retries > 0 && shouldRetry(error)) {
|
|
27
|
+
if (retries > 0 && (await shouldRetry(error))) {
|
|
28
28
|
const retryCount = CONFIG.CRAWLER.MAX_RETRIES - retries + 1;
|
|
29
29
|
console.log(`Retrying (${retryCount}/${CONFIG.CRAWLER.MAX_RETRIES}) -> ${url}`);
|
|
30
30
|
|
|
@@ -32,11 +32,14 @@ export async function fetchURL(url, retries = 2) {
|
|
|
32
32
|
|
|
33
33
|
return fetchURL(url, retries - 1);
|
|
34
34
|
}
|
|
35
|
+
|
|
36
|
+
// If still 429 after retries, exit with configured code
|
|
37
|
+
if (error.response?.status === 429) {
|
|
38
|
+
console.log(`Force exiting with code ${CONFIG.CRAWLER.EXIT_CODE_RATE_LIMIT} (after retries)...`);
|
|
39
|
+
process.exit(CONFIG.CRAWLER.EXIT_CODE_RATE_LIMIT);
|
|
40
|
+
}
|
|
35
41
|
|
|
36
42
|
console.error(`Failed to fetch ${url} -> ${error.message}`);
|
|
37
|
-
return {
|
|
38
|
-
error: error.message,
|
|
39
|
-
status: error.response?.status
|
|
40
|
-
};
|
|
43
|
+
return { error: error.message, status: error.response?.status };
|
|
41
44
|
};
|
|
42
45
|
};
|
|
@@ -11,25 +11,27 @@ export const shouldRetry = async (error) => {
|
|
|
11
11
|
const rateLimitReset = headers?.['x-ratelimit-reset'];
|
|
12
12
|
|
|
13
13
|
if (status === 429) {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
if (retryAfter) {
|
|
17
|
-
waitTime = isNaN(retryAfter)
|
|
18
|
-
? Math.ceil((new Date(retryAfter).getTime() - Date.now()) / 1000) // HTTP date
|
|
19
|
-
: parseInt(retryAfter, 10); // Seconds
|
|
20
|
-
console.log(`Rate limited. Retrying after ${waitTime} seconds...`);
|
|
21
|
-
} else if (rateLimitReset) {
|
|
22
|
-
waitTime = Math.max(parseInt(rateLimitReset, 10) - Math.floor(Date.now() / 1000), 0);
|
|
23
|
-
console.log(`Rate limited. Retrying after ${waitTime} seconds...`);
|
|
24
|
-
} else {
|
|
25
|
-
waitTime = CONFIG.CRAWLER.CRAWL_RATE_LIMIT_FALLBACK_DELAY_MS / 1000;
|
|
26
|
-
console.log(`Rate limited. No 'retry-after' or 'x-ratelimit-reset' headers found. Falling back to ${waitTime} seconds...`);
|
|
27
|
-
}
|
|
14
|
+
console.log(`RATE LIMIT Detected.`);
|
|
28
15
|
|
|
29
16
|
if (CONFIG.CRAWLER.EXIT_ON_RATE_LIMIT) {
|
|
30
|
-
console.log(`
|
|
31
|
-
process.exit(CONFIG.CRAWLER.EXIT_CODE_RATE_LIMIT); // GitHub Actions Docker uses values ranged from 0 to 255
|
|
17
|
+
console.log(`Force exiting with code ${CONFIG.CRAWLER.EXIT_CODE_RATE_LIMIT}...`);
|
|
18
|
+
process.exit(CONFIG.CRAWLER.EXIT_CODE_RATE_LIMIT); // GitHub Actions Docker uses values ranged from 0 to 255. Any bigger value will be % 256
|
|
32
19
|
} else {
|
|
20
|
+
let waitTime = null;
|
|
21
|
+
|
|
22
|
+
if (retryAfter) {
|
|
23
|
+
waitTime = isNaN(retryAfter)
|
|
24
|
+
? Math.ceil((new Date(retryAfter).getTime() - Date.now()) / 1000) // HTTP date
|
|
25
|
+
: parseInt(retryAfter, 10); // Seconds
|
|
26
|
+
console.log(`'retry-after' headers found.`);
|
|
27
|
+
} else if (rateLimitReset) {
|
|
28
|
+
waitTime = Math.max(parseInt(rateLimitReset, 10) - Math.floor(Date.now() / 1000), 0);
|
|
29
|
+
console.log(`'x-ratelimit-reset' headers found.`);
|
|
30
|
+
} else {
|
|
31
|
+
waitTime = CONFIG.CRAWLER.CRAWL_RATE_LIMIT_FALLBACK_DELAY_MS / 1000;
|
|
32
|
+
console.log(`No 'retry-after' or 'x-ratelimit-reset' headers found. Using fallback delay.`);
|
|
33
|
+
}
|
|
34
|
+
console.log(`Retrying after ${waitTime} seconds...`);
|
|
33
35
|
await delay(waitTime * 1000);
|
|
34
36
|
}
|
|
35
37
|
}
|