-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·60 lines (46 loc) · 1.54 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
#!/usr/bin/env node
const { program } = require('commander')
const axios = require('axios').default
const jsdom = require('jsdom')
const { JSDOM } = jsdom
const { format } = require('@fast-csv/format')
const { stripHtml } = require('string-strip-html')
program
.name('download-table-csv')
.argument('<url>', 'remote URL to download')
.argument('<selector>', 'CSS selector for table')
.option('-c, --require-head-columns', 'require rows to have the same number of columns as the header')
.option('-h, --header-row <header>', 'replace header row with custom text')
program.parse()
const options = program.opts()
const stream = format()
stream.pipe(process.stdout)
let headerRowCount = 0
axios.get(program.args[0])
.then((r) => {
const dom = new JSDOM(r.data)
const table = dom.window.document.querySelector(program.args[1])
if (!table) {
console.error("Could not find selector")
exit(-1)
}
for (const row of table.rows) {
let values = []
// If we're on the first row...
if (headerRowCount == 0) {
// Save the column count for the "-c" option
headerRowCount = row.cells.length
if (options.headerRow) {
process.stdout.write(options.headerRow + "\n")
continue
}
}
for (const cell of row.cells) {
const string = stripHtml(cell.innerHTML).result
if (string.length > 0) values.push(string)
}
if (options.requireHeadColumns)
if (values.length !== headerRowCount) continue
stream.write(values)
}
})