scraply 1.0.1 → 1.0.3

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.1",
4
+ "version": "1.0.3",
5
5
  "main": "src/scraply.js",
6
6
  "type": "module",
7
7
  "scripts": {
package/readme.md CHANGED
@@ -1,4 +1,89 @@
1
+ # Scraply
1
2
  Scraply is a customizable and efficient web crawler and data scraper for Node.js, designed to handle various web crawling needs with ease. You can define the URLs to crawl, configure patterns to include/exclude, and format the output data in JSON. Scraply is built to be flexible, with user-configurable settings and dynamic paths.
2
3
 
3
- Installation
4
- `npm install scraply`
4
+ Bug Reports & Dev Stuff on: [Scraply's GitHub](https://github.com/pauserratgutierrez/scraply)
5
+
6
+ ## Installation
7
+ Using npm:
8
+ ``npm install scraply``
9
+
10
+ ## Working Example
11
+ Initialize Scraply with provided URLs to start crawling:
12
+ ```
13
+ import { scraply } from 'scraply';
14
+ scraply({
15
+ CRAWLER: {
16
+ INITIAL_URLS: ['https://example.com']
17
+ }
18
+ });
19
+ ```
20
+
21
+ ## How Scraply Works
22
+ ### Persistent Data Storage
23
+ Scraply persistently saves the state of the crawler in JSON files (the queue, crawled data, etc.). If the crawler is interrupted or rate-limited, all progress is saved, and the crawler will automatically stop. When restarted, Scraply resumes crawling exactly where it left off, without reprocessing already crawled URLs.
24
+
25
+ ### Handling Rate Limiting
26
+ Scraply is designed to handle rate-limiting gracefully. If the crawler encounters rate-limited responses (e.g., status code `429`), it stops processing further requests and saves everything in the queue. Once restarted, it resumes the crawling process from where it stopped.
27
+
28
+ This makes Scraply ideal for long-running, continuous crawling tasks. You can integrate Scraply with GitHub Actions or other CI/CD pipelines to perform endless crawling jobs over time. Simply schedule Scraply to run periodically, and it will continue gathering data without duplicating work.
29
+
30
+ ### Integration with GitHub Actions
31
+ Scraply can be easily integrated into a GitHub Action workflow for continuous, long-running crawling tasks. You can set it up to crawl for a set duration or number of URLs, persistently saving the progress, and then resuming where it left off on the next run.
32
+
33
+ ## Config Options
34
+ Scraply allows you to pass a configuration object to the main ```scraply()``` function to customize the crawling behavior. Below are the current configuration options:
35
+ ```
36
+ MAIN_DIR: 'dataset',
37
+
38
+ CRAWLER: {
39
+ INITIAL_URLS: [
40
+ 'https://crawler-test.com/'
41
+ ],
42
+ INCLUDE_URLS: [
43
+ 'https://crawler-test.com/.*'
44
+ ],
45
+ ALLOWED_CONTENT_TYPES: [
46
+ 'text/html'
47
+ ],
48
+ EXCLUDE_PATTERNS: [
49
+ '/cdn-cgi/',
50
+ /\.(zip|rar|webp|png|jpg|jpeg|gif|mp3|mp4|pdf|css|js|svg|ico|eot|ttf|woff|woff2|otf|webm|ogg|wav|flac|m4a|mkv|mov|avi|wmv|flv|swf|exe|msi|dmg|iso|bin)$/,
51
+ ],
52
+ DOM_ELEMENTS_REMOVE: [
53
+ 'script',
54
+ 'noscript',
55
+ 'style',
56
+ 'meta',
57
+ 'link',
58
+ 'svg',
59
+ 'path',
60
+ 'img',
61
+ 'input',
62
+ 'textarea',
63
+ 'embed',
64
+ 'object',
65
+ 'iframe',
66
+ 'nav',
67
+ 'header',
68
+ 'footer',
69
+ 'aside',
70
+ 'button'
71
+ ],
72
+ RETRY_STATUS_CODES: [408, 429, 500, 502, 503, 504],
73
+ REQUEST_TIMEOUT: 4000,
74
+ MAX_REDIRECTS: 3,
75
+ MAX_RETRIES: 2,
76
+ CRAWL_DELAY_MS: 200,
77
+ CRAWL_ERROR_RETRY_DELAY_MS: 800,
78
+ },
79
+
80
+ DATA_FORMATTER: {
81
+ EXCLUDED_PATTERNS: [],
82
+ CATEGORISED_PATHS: {
83
+ 'https://crawler-test.com': {
84
+ 'mobile': 'mobile.json',
85
+ 'fallback': 'general.json'
86
+ },
87
+ },
88
+ }
89
+ ```
package/src/loadConfig.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import path from 'node:path';
2
- import { DEFAULT_CONFIG } from './config.js';
2
+ import { DEFAULT_CONFIG } from './defaultConfig.js';
3
3
 
4
4
  // A utility function to perform a deep merge of objects
5
5
  function deepMerge(target, source) {
@@ -21,5 +21,10 @@ export function loadConfig(userConfig = {}) {
21
21
  config.DATA_FORMATTER.FORMATTED_PATH = path.join(config.MAIN_DIR, 'formatted');
22
22
  config.DATA_FORMATTER.ERROR_REPORT_PATH = path.join(config.MAIN_DIR, 'error-report.json');
23
23
 
24
+ // If INCLUDE_URLS is not specified, set it to INITIAL_URLS by default
25
+ if (!config.CRAWLER.INCLUDE_URLS || config.CRAWLER.INCLUDE_URLS.length === 0) {
26
+ config.CRAWLER.INCLUDE_URLS = config.CRAWLER.INITIAL_URLS.map(url => `${url}.*`);
27
+ }
28
+
24
29
  return config;
25
30
  };
package/src/scraply.js CHANGED
@@ -4,16 +4,11 @@ import { loadJSON, saveQueue, deleteDataFiles } from './utils/crawl/fileOperatio
4
4
  import { processURL } from './utils/crawl/url/processor.js';
5
5
  import { formatData, saveSortedFormattedJSON, saveHardcodedExtraLinks } from './utils/format/formatData.js';
6
6
 
7
- const userConfig = {};
8
-
9
- // Load and merge the configuration
10
- const CONFIG = loadConfig(userConfig);
11
- global.CONFIG = CONFIG;
12
-
13
7
  let urlData = [];
14
8
  let urlMetadata = {};
9
+ let CONFIG = {};
15
10
 
16
- export const initializeCrawler = () => {
11
+ const init = () => {
17
12
  urlData = loadJSON(CONFIG.CRAWLER.QUEUE_PATH);
18
13
 
19
14
  if (urlData.length === 0) { // If the queue is empty, start fresh with the initial URLs.
@@ -37,14 +32,14 @@ export const initializeCrawler = () => {
37
32
  deleteDataFiles(CONFIG.CRAWLER.CRAWLED_PATH);
38
33
  deleteDataFiles(CONFIG.DATA_FORMATTER.ERROR_REPORT_PATH);
39
34
 
40
- initializeCrawler();
35
+ init();
41
36
  } else { // If there are URLs that haven't been processed yet, resume from the queue.
42
37
  console.log(`Resuming from ${CONFIG.CRAWLER.QUEUE_PATH} with ${urlData.length} total found URLs\n`);
43
38
  }
44
39
  }
45
40
  };
46
41
 
47
- export const scraply = async () => {
42
+ const start = async () => {
48
43
  console.log(`STARTING CRAWLER
49
44
  - Initial URLs: ${CONFIG.CRAWLER.INITIAL_URLS}
50
45
  - Include URLs: ${CONFIG.CRAWLER.INCLUDE_URLS}
@@ -115,3 +110,12 @@ export const scraply = async () => {
115
110
 
116
111
  console.log(`Errors: ${errorData.length} -> ${CONFIG.DATA_FORMATTER.ERROR_REPORT_PATH}.`);
117
112
  };
113
+
114
+ // Main function to be exported and used
115
+ export const scraply = async (userConfig = {}) => {
116
+ CONFIG = loadConfig(userConfig);
117
+ global.CONFIG = CONFIG;
118
+
119
+ init();
120
+ await start();
121
+ };
File without changes