-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
86 lines (70 loc) · 2.21 KB
/
index.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
const { program } = require("commander");
const { JSDOM } = require("jsdom");
const fetch = require("isomorphic-fetch");
const fs = require("fs");
const createLogger = require("./factories/logger");
const createDataSource = require("./factories/dataSource");
const createProgressBar = require("./factories/progressBar");
const tempDir = ".tmp";
const defaultOutFile = "out.csv";
async function main() {
const appConfText = await fs.promises.readFile("package.json", {
encoding: "utf8",
});
const appConf = JSON.parse(appConfText);
const logger = createLogger(appConf.name);
let dataSourcePath,
selector,
outputPath = `${tempDir}/${defaultOutFile}`;
program.name(appConf.name).version(appConf.version);
program.option("-o,--output <output>");
program
.arguments("<data-source>")
.arguments("<selector>")
.action((dsPath, sel) => {
dataSourcePath = dsPath;
selector = sel;
});
program.parse(process.argv);
const programOptions = program.opts();
if (programOptions.output) {
outputPath = programOptions.output;
} else {
try {
await fs.promises.stat(tempDir);
} catch (err) {
await fs.promises.mkdir(tempDir);
}
}
const dataSource = await createDataSource(dataSourcePath);
const progressBar = createProgressBar(dataSource);
fs.promises.writeFile(outputPath, "url, value\n");
await Promise.all(
dataSource.map(async (url) => {
let res, text;
try {
res = await fetch(url);
if (!(res.status < 400)) {
throw new Error(
`Error: received http status ${res.status} using url: ${url}`
);
} else if (res.status > 400) {
res.status > 400;
}
text = await res.text();
const dom = await new JSDOM(text);
dom.window.document;
const value = dom.window.document.querySelector(selector).textContent;
fs.promises.appendFile(outputPath, `${url},${value}\n`);
return value;
} catch (err) {
logger.error(`selector failed using url: ${url}`);
fs.promises.appendFile(outputPath, `${url},${null}\n`);
return null;
} finally {
progressBar.tick();
}
})
);
}
module.exports = main;