-
Notifications
You must be signed in to change notification settings - Fork 0
/
wikp
executable file
·34 lines (25 loc) · 939 Bytes
/
wikp
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
#!/home/ec2-user/.nvm/versions/node/v6.14.1/bin/node
// Returns the paragraphs from a Wikipedia link, stripped of reference numbers.
// Userful for text-to-speach, or other pure text applications
let url = process.argv[2];
if ( url ) {
let request = require("request");
const jsdom = require("jsdom");
const { JSDOM } = jsdom;
request(url, function(error, response, body) {
// Simulate a Document Object Model.
let { document } = (new JSDOM(body)).window;
// Grab all paragraphs & references
let paragraphs = document.querySelectorAll("p");
let references = document.querySelectorAll(".reference");
// Remove references
references.forEach(function(reference) {
reference.remove();
});
// Print out all the paragraphs
paragraphs.forEach(function(paragraph) {
console.log(paragraph.textContent + "\n");
});
});
} else
console.log("url missing or invalid.");