scraply 1.0.17 → 1.0.19

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "scraply",
3
3
  "description": "A simple, configurable and functional content scraper",
4
- "version": "1.0.17",
4
+ "version": "1.0.19",
5
5
  "main": "src/scraply.js",
6
6
  "type": "module",
7
7
  "scripts": {
@@ -42,8 +42,8 @@ export const DEFAULT_CONFIG = {
42
42
  CRAWL_DELAY_MS: 200,
43
43
  CRAWL_ERROR_RETRY_DELAY_MS: 800,
44
44
  CRAWL_RATE_LIMIT_FALLBACK_DELAY_MS: 60000,
45
- EXIT_CODE_RATE_LIMIT: 10,
46
- EXIT_ON_RATE_LIMIT: true
45
+ EXIT_ON_RATE_LIMIT: true,
46
+ EXIT_CODE_RATE_LIMIT: 10
47
47
  },
48
48
 
49
49
  DATA_FORMATTER: {
@@ -6,9 +6,6 @@ export const cleanHTML = ($) => {
6
6
  $aux(CONFIG.CRAWLER.DOM_ELEMENTS_REMOVE.join(',')).remove();
7
7
  $aux('*').contents().filter((_, el) => el.type === 'comment').remove();
8
8
 
9
- // Get the text content of the body and decode HTML entities
10
- // let bodyText = he.decode($aux('body').text(), { level: 'all' });
11
-
12
9
  // Get the text content of the body element, ensuring spaces between child elements
13
10
  let bodyText = getTextWithSpaces($aux, $aux('body'));
14
11
 
@@ -19,12 +19,9 @@ export async function fetchURL(url, retries = 2) {
19
19
  };
20
20
  };
21
21
 
22
- return {
23
- data: response.data,
24
- status: response.status
25
- };
22
+ return { data: response.data, status: response.status };
26
23
  } catch (error) {
27
- if (retries > 0 && shouldRetry(error)) {
24
+ if (retries > 0 && (await shouldRetry(error))) {
28
25
  const retryCount = CONFIG.CRAWLER.MAX_RETRIES - retries + 1;
29
26
  console.log(`Retrying (${retryCount}/${CONFIG.CRAWLER.MAX_RETRIES}) -> ${url}`);
30
27
 
@@ -32,11 +29,14 @@ export async function fetchURL(url, retries = 2) {
32
29
 
33
30
  return fetchURL(url, retries - 1);
34
31
  }
32
+
33
+ // If still 429 after retries, exit with configured code
34
+ if (error.response?.status === 429) {
35
+ console.log(`Force exiting with code ${CONFIG.CRAWLER.EXIT_CODE_RATE_LIMIT} after retries...`);
36
+ process.exit(CONFIG.CRAWLER.EXIT_CODE_RATE_LIMIT);
37
+ }
35
38
 
36
39
  console.error(`Failed to fetch ${url} -> ${error.message}`);
37
- return {
38
- error: error.message,
39
- status: error.response?.status
40
- };
40
+ return { error: error.message, status: error.response?.status };
41
41
  };
42
42
  };
@@ -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
- let waitTime = null;
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.floow(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(`Exiting due to rate limit.`);
31
- process.exit(CONFIG.CRAWLER.EXIT_CODE_RATE_LIMIT); // GitHub Actions Docker uses values ranged from 0 to 255, so any bigger value will be % 256!
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
  }
@@ -7,7 +7,7 @@ import { saveDataset, saveQueue } from '../fileOperations.js';
7
7
 
8
8
  export const processURL = async (entry, fileNumber, urlData) => {
9
9
  const startTime = new Date().getTime();
10
- const { url, referrer, depth } = entry;
10
+ const { url, depth } = entry;
11
11
 
12
12
  if (entry.file || (entry.error && !(await shouldRetry({ response: { status: entry.status } })))) return;
13
13
 
@@ -21,7 +21,7 @@ export const processURL = async (entry, fileNumber, urlData) => {
21
21
  enqueueURLs(urlData, $, url, depth + 1);
22
22
 
23
23
  const content = cleanHTML($);
24
- const filename = saveDataset({ url, referrerURL: referrer, statusCode: status, depth, content }, fileNumber);
24
+ const filename = saveDataset({ url, content }, fileNumber);
25
25
 
26
26
  entry.file = filename;
27
27
  entry.status = status;