-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
41 lines (38 loc) · 1.2 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import { getErrorMessage } from "./lib/utils.js";
import { getScrapeResults } from "./lib/scrape.js";
import readline from "readline";
/**
* @typedef {import('./lib/types.js').ScraperOptions} ScraperOptions
*/
(async () => {
process.env.LOG = "CONSOLE";
try {
const { url, pageLimit } = await getScraperBodyFromCommandLine();
const data = await getScrapeResults(url, { pageLimit });
console.log(data.results);
} catch (error) {
console.error({ error: getErrorMessage(error) });
process.exit(1);
}
})();
/**
*
* @returns {Promise<{url:string,pageLimit:number}>} - All args to start scraper
*/
async function getScraperBodyFromCommandLine() {
return new Promise((resolve, reject) => {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
rl.question("URL to analyse: ", (url) => {
rl.question("Max pages to visit (default 5): ", (maxPages) => {
let pageLimit = maxPages.trim() ? parseInt(maxPages, 10) : 5;
pageLimit = isNaN(pageLimit) || pageLimit < 1 ? 5 : pageLimit;
rl.close();
if (!url) reject(new Error("URL is required"));
else resolve({ url, pageLimit });
});
});
});
}