-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathindex.ts
268 lines (246 loc) · 7.37 KB
/
index.ts
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#!/usr/bin/env node
import execa = require('execa')
import { promises as fs } from 'fs'
import { safeLoad } from 'js-yaml'
import minimist = require('minimist')
import puppeteer = require('puppeteer')
import * as tempy from 'tempy'
const { copyFile, readFile, writeFile } = fs
interface Command {
[key: string]: any
command: 'click' | 'each' | 'getAll' | 'getOne' | 'go' | 'if' | 'input' | 'scrape' | 'waitForPage'
}
interface CommandClick extends Command {
selector: string
}
interface CommandEach extends Command {
actions: Command[]
from: string
}
interface CommandGet extends Command { // getAll or getOne
property: string
saveAs: string
selector: string
}
interface CommandGo extends Command {
url: string
}
interface CommandIf extends Command {
actions: Command[]
negate?: boolean
selector: string
test: 'contains'
value: string
}
interface CommandInput extends Command {
selector: string
value: string
}
interface CommandScrape extends Command {
selector: string
}
let page: puppeteer.Page
let sourceText = ''
let vars: {[name: string]: string | string[]} = {}
const doAction = async (action: Command) => {
if (action.command === 'click') {
let opts = action as CommandClick
console.log(`Clicking on element ${opts.selector}...`)
await page.click(opts.selector)
} else if (action.command === 'each') {
let opts = action as CommandEach
const iterable = typeof vars[opts.from] === 'string'
? [vars[opts.from] as string]
: Array.from(vars[opts.from])
for (const item of iterable) {
const replaceThis = (str: string): string => {
return str.replace(/{{this}}/g, item)
}
for (const subaction of opts.actions) {
const clonedAction = { ...subaction }
for (const key in clonedAction) {
if (typeof clonedAction[key] === 'string') {
clonedAction[key] = replaceThis(clonedAction[key])
}
}
await doAction(clonedAction)
}
}
} else if (action.command === 'getAll') {
let opts = action as CommandGet
console.log(`Getting selectors matching "${opts.selector}...`)
const results = await page.evaluate((selector, prop) => {
const matches = Array.from(document.querySelectorAll(selector))
return matches.map((el) => String(el[prop]))
}, opts.selector, opts.property)
console.log(` ${results.length} matches found. Storing into "${opts.saveAs}".`)
vars[opts.saveAs] = results
} else if (action.command === 'getOne') {
let opts = action as CommandGet
console.log(`Getting selector matching "${opts.selector}...`)
const result = await page.evaluate((selector, prop) => {
const el = document.querySelector(selector)
return el && String(el[prop])
}, opts.selector, opts.property)
console.log(` ${result ? '1' : 'No'} match found. Storing into "${opts.saveAs}".`)
vars[opts.saveAs] = result
} else if (action.command === 'go') {
let opts = action as CommandGo
console.log(`Loading URL ${opts.url}...`)
await page.goto(opts.url)
} else if (action.command === 'if') {
let opts = action as CommandIf
console.log(`Checking if ${opts.negate ? 'not ' : ''}${opts.test} "${opts.value}"...`)
let isTrue = false
if (opts.test === 'contains') {
isTrue = await page.evaluate((selector, text) => {
const el = document.querySelector(selector)
return el && el.textContent && el.textContent.includes(text)
}, opts.selector, opts.value)
}
if (opts.negate) { isTrue = !isTrue }
if (isTrue) {
console.log(` is true; running actions.`)
for (const subaction of opts.actions) {
await doAction(subaction)
}
} else {
console.log(' is false; skipping actions.')
}
} else if (action.command === 'input') {
let opts = action as CommandInput
console.log(`Typing into field "${opts.selector}...`)
await page.type(opts.selector, opts.value)
} else if (action.command === 'scrape') {
let opts = action as CommandScrape
console.log(`Scraping text from "${opts.selector}"...`)
sourceText += await page.evaluate(selector => {
const blockElements = [
'address',
'article',
'aside',
'blockquote',
'details',
'dialog',
'dd',
'div',
'dl',
'dt',
'fieldset',
'figcaption',
'figure',
'footer',
'form',
'h1',
'h2',
'h3',
'h4',
'h5',
'h6',
'header',
'hgroup',
'hr',
'li',
'main',
'nav',
'ol',
'p',
'pre',
'section',
'table',
'ul',
].map(name => name.toUpperCase())
const codeElements = [
'noscript',
'script',
'style'
].map(name => name.toUpperCase())
const inlineBreaks = [
'br'
].map(name => name.toUpperCase())
const domToString = (node: Node) => {
let str = ''
for (let child of Array.from(node.childNodes)) {
if (child.nodeType === 3) { // text node
str += child.textContent
} else if (child.nodeType === 1 && !codeElements.includes(child.nodeName)) { // element node
if (blockElements.includes(child.nodeName)) {
str += `\n${domToString(child)}.\n`
} else {
str += domToString(child)
if (inlineBreaks.includes(child.nodeName)) {
str += '. '
}
}
}
}
return str
}
const body = document.querySelector<HTMLElement>(selector)
return body ? domToString(body) : ''
}, opts.selector)
} else if (action.command === 'waitForPage') {
console.log('Waiting for page to load...')
await page.waitForNavigation()
}
}
;(async () => {
const cliOpts = {
boolean: ['debug', 'headless'],
default: {
debug: false,
delay: 0,
headless: true,
height: 1000,
width: 2000
}
}
const args = minimist(process.argv.slice(2), cliOpts)
const commandsFile = args._[0]
if (!commandsFile) {
throw new Error('Missing commands filename')
}
const outputFile = args._[1]
if (!outputFile) {
throw new Error('Missing output filename')
}
const input = await readFile(commandsFile, 'utf8')
const actions = safeLoad(input) as Command[]
const browser = await puppeteer.launch({
headless: args.headless,
devtools: args.debug,
slowMo: args.delay
})
page = await browser.newPage()
if (args.debug) {
page.on('console', msg => console.log('PAGE LOG:', msg.text()))
}
await page.setViewport({
width: args.width,
height: args.height
})
for (const action of actions) {
await doAction(action)
}
await browser.close()
const textFilename = tempy.file()
await writeFile(textFilename, sourceText, 'utf8')
console.log('Converting text to audio...')
const audioFilename = tempy.file()
let ttsArgs = [
'node_modules/.bin/tts',
textFilename,
audioFilename
]
for (const name in args) {
if (name === '_' || Object.keys(cliOpts.default).includes(name)) { continue }
ttsArgs.push(`--${name}`, args[name])
}
console.log(` Running 'node ${ttsArgs.join(' ')}'`)
await execa('node', ttsArgs, {
stdout: 'inherit',
stderr: 'inherit'
})
await copyFile(audioFilename, outputFile)
console.log(`Done. Wrote file to ${outputFile}`)
})()