-
Notifications
You must be signed in to change notification settings - Fork 140
/
eval.js
49 lines (41 loc) · 1.13 KB
/
eval.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
// eval.js
import { execSync } from 'node:child_process'
import { readFileSync, writeFileSync, existsSync } from 'node:fs'
import { slugify } from 'bellajs'
import { isValid as isValidUrl } from './src/utils/linker.js'
import { extract, extractFromHtml } from './src/main.js'
if (!existsSync('evaluation')) {
execSync('mkdir evaluation')
}
const extractFromUrl = async (url) => {
try {
console.time('extraction')
const art = await extract(url)
console.log(art)
if (art) {
const slug = slugify(art.title)
writeFileSync(`evaluation/${slug}.html`, art.content, 'utf8')
}
console.timeEnd('extraction')
} catch (err) {
console.trace(err)
}
}
const extractFromFile = async (fpath) => {
try {
const html = readFileSync(fpath, 'utf8')
const art = await extractFromHtml(html)
console.log(art)
} catch (err) {
console.trace(err)
}
}
const init = (argv) => {
if (argv.length === 3) {
const input = argv[2]
const isUrl = isValidUrl(input)
return isUrl ? extractFromUrl(input) : existsSync(input) ? extractFromFile(input) : false
}
return 'Nothing to do!'
}
init(process.argv)