web-tts 0.1.5 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +3 -3
  2. package/dist/index.js +0 -238
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "web-tts",
3
- "version": "0.1.5",
3
+ "version": "0.2.0",
4
4
  "description": "Convert webpages to audio speech",
5
5
  "main": "dist/index.js",
6
6
  "bin": {
@@ -38,7 +38,7 @@
38
38
  "minimist": "^1.2.5",
39
39
  "puppeteer": "^5.2.1",
40
40
  "tempy": "^0.6.0",
41
- "tts-cli": "^2.0.3"
41
+ "tts-cli": "^3.0.0"
42
42
  },
43
43
  "devDependencies": {
44
44
  "@types/js-yaml": "^3.12.5",
@@ -78,5 +78,5 @@
78
78
  "all": false,
79
79
  "report-dir": "./coverage"
80
80
  },
81
- "gitHead": "4160f16e9656526828e01c5781eed1eee5f1c007"
81
+ "gitHead": "eb798cd60ee25c96f5267bc222f72a59d8ef294f"
82
82
  }
package/dist/index.js DELETED
@@ -1,238 +0,0 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- Object.defineProperty(exports, "__esModule", { value: true });
12
- const execa = require("execa");
13
- const fs_1 = require("fs");
14
- const js_yaml_1 = require("js-yaml");
15
- const minimist = require("minimist");
16
- const puppeteer = require("puppeteer");
17
- const tempy = require("tempy");
18
- const { copyFile, readFile, writeFile } = fs_1.promises;
19
- let page;
20
- let sourceText = '';
21
- let vars = {};
22
- const doAction = (action) => __awaiter(void 0, void 0, void 0, function* () {
23
- if (action.command === 'click') {
24
- let opts = action;
25
- console.log(`Clicking on element ${opts.selector}...`);
26
- yield page.click(opts.selector);
27
- }
28
- else if (action.command === 'each') {
29
- let opts = action;
30
- const iterable = typeof vars[opts.from] === 'string'
31
- ? [vars[opts.from]]
32
- : Array.from(vars[opts.from]);
33
- for (const item of iterable) {
34
- const replaceThis = (str) => {
35
- return str.replace(/{{this}}/g, item);
36
- };
37
- for (const subaction of opts.actions) {
38
- const clonedAction = Object.assign({}, subaction);
39
- for (const key in clonedAction) {
40
- if (typeof clonedAction[key] === 'string') {
41
- clonedAction[key] = replaceThis(clonedAction[key]);
42
- }
43
- }
44
- yield doAction(clonedAction);
45
- }
46
- }
47
- }
48
- else if (action.command === 'getAll') {
49
- let opts = action;
50
- console.log(`Getting selectors matching "${opts.selector}...`);
51
- const results = yield page.evaluate((selector, prop) => {
52
- const matches = Array.from(document.querySelectorAll(selector));
53
- return matches.map((el) => String(el[prop]));
54
- }, opts.selector, opts.property);
55
- console.log(` ${results.length} matches found. Storing into "${opts.saveAs}".`);
56
- vars[opts.saveAs] = results;
57
- }
58
- else if (action.command === 'getOne') {
59
- let opts = action;
60
- console.log(`Getting selector matching "${opts.selector}...`);
61
- const result = yield page.evaluate((selector, prop) => {
62
- const el = document.querySelector(selector);
63
- return el && String(el[prop]);
64
- }, opts.selector, opts.property);
65
- console.log(` ${result ? '1' : 'No'} match found. Storing into "${opts.saveAs}".`);
66
- vars[opts.saveAs] = result;
67
- }
68
- else if (action.command === 'go') {
69
- let opts = action;
70
- console.log(`Loading URL ${opts.url}...`);
71
- yield page.goto(opts.url);
72
- }
73
- else if (action.command === 'if') {
74
- let opts = action;
75
- console.log(`Checking if ${opts.negate ? 'not ' : ''}${opts.test} "${opts.value}"...`);
76
- let isTrue = false;
77
- if (opts.test === 'contains') {
78
- isTrue = yield page.evaluate((selector, text) => {
79
- const el = document.querySelector(selector);
80
- return el && el.textContent && el.textContent.includes(text);
81
- }, opts.selector, opts.value);
82
- }
83
- if (opts.negate) {
84
- isTrue = !isTrue;
85
- }
86
- if (isTrue) {
87
- console.log(` is true; running actions.`);
88
- for (const subaction of opts.actions) {
89
- yield doAction(subaction);
90
- }
91
- }
92
- else {
93
- console.log(' is false; skipping actions.');
94
- }
95
- }
96
- else if (action.command === 'input') {
97
- let opts = action;
98
- console.log(`Typing into field "${opts.selector}...`);
99
- yield page.type(opts.selector, opts.value);
100
- }
101
- else if (action.command === 'scrape') {
102
- let opts = action;
103
- console.log(`Scraping text from "${opts.selector}"...`);
104
- sourceText += yield page.evaluate(selector => {
105
- const blockElements = [
106
- 'address',
107
- 'article',
108
- 'aside',
109
- 'blockquote',
110
- 'details',
111
- 'dialog',
112
- 'dd',
113
- 'div',
114
- 'dl',
115
- 'dt',
116
- 'fieldset',
117
- 'figcaption',
118
- 'figure',
119
- 'footer',
120
- 'form',
121
- 'h1',
122
- 'h2',
123
- 'h3',
124
- 'h4',
125
- 'h5',
126
- 'h6',
127
- 'header',
128
- 'hgroup',
129
- 'hr',
130
- 'li',
131
- 'main',
132
- 'nav',
133
- 'ol',
134
- 'p',
135
- 'pre',
136
- 'section',
137
- 'table',
138
- 'ul',
139
- ].map(name => name.toUpperCase());
140
- const codeElements = [
141
- 'noscript',
142
- 'script',
143
- 'style'
144
- ].map(name => name.toUpperCase());
145
- const inlineBreaks = [
146
- 'br'
147
- ].map(name => name.toUpperCase());
148
- const domToString = (node) => {
149
- let str = '';
150
- for (let child of Array.from(node.childNodes)) {
151
- if (child.nodeType === 3) { // text node
152
- str += child.textContent;
153
- }
154
- else if (child.nodeType === 1 && !codeElements.includes(child.nodeName)) { // element node
155
- if (blockElements.includes(child.nodeName)) {
156
- str += `\n${domToString(child)}.\n`;
157
- }
158
- else {
159
- str += domToString(child);
160
- if (inlineBreaks.includes(child.nodeName)) {
161
- str += '. ';
162
- }
163
- }
164
- }
165
- }
166
- return str;
167
- };
168
- const body = document.querySelector(selector);
169
- return body ? domToString(body) : '';
170
- }, opts.selector);
171
- }
172
- else if (action.command === 'waitForPage') {
173
- console.log('Waiting for page to load...');
174
- yield page.waitForNavigation();
175
- }
176
- });
177
- (() => __awaiter(void 0, void 0, void 0, function* () {
178
- const cliOpts = {
179
- boolean: ['debug', 'headless'],
180
- default: {
181
- debug: false,
182
- delay: 0,
183
- headless: true,
184
- height: 1000,
185
- width: 2000
186
- }
187
- };
188
- const args = minimist(process.argv.slice(2), cliOpts);
189
- const commandsFile = args._[0];
190
- if (!commandsFile) {
191
- throw new Error('Missing commands filename');
192
- }
193
- const outputFile = args._[1];
194
- if (!outputFile) {
195
- throw new Error('Missing output filename');
196
- }
197
- const input = yield readFile(commandsFile, 'utf8');
198
- const actions = js_yaml_1.safeLoad(input);
199
- const browser = yield puppeteer.launch({
200
- headless: args.headless,
201
- devtools: args.debug,
202
- slowMo: args.delay
203
- });
204
- page = yield browser.newPage();
205
- if (args.debug) {
206
- page.on('console', msg => console.log('PAGE LOG:', msg.text()));
207
- }
208
- yield page.setViewport({
209
- width: args.width,
210
- height: args.height
211
- });
212
- for (const action of actions) {
213
- yield doAction(action);
214
- }
215
- yield browser.close();
216
- const textFilename = tempy.file();
217
- yield writeFile(textFilename, sourceText, 'utf8');
218
- console.log('Converting text to audio...');
219
- const audioFilename = tempy.file();
220
- let ttsArgs = [
221
- 'node_modules/.bin/tts',
222
- textFilename,
223
- audioFilename
224
- ];
225
- for (const name in args) {
226
- if (name === '_' || Object.keys(cliOpts.default).includes(name)) {
227
- continue;
228
- }
229
- ttsArgs.push(`--${name}`, args[name]);
230
- }
231
- console.log(` Running 'node ${ttsArgs.join(' ')}'`);
232
- yield execa('node', ttsArgs, {
233
- stdout: 'inherit',
234
- stderr: 'inherit'
235
- });
236
- yield copyFile(audioFilename, outputFile);
237
- console.log(`Done. Wrote file to ${outputFile}`);
238
- }))();