-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathscraper.js
39 lines (34 loc) · 959 Bytes
/
scraper.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
const fs = require('fs');
const fetch = require("node-fetch");
let root = "https://xkcd.com/1975/alto/menu/";
let retrieved = [];
function getID(id) {
fetch(root + id).then(r => r.json())
.then(data => {
console.log(data.id);
fs.writeFileSync("json/" + id + ".json", JSON.stringify(data));
data.entries.forEach(ent => {
let next = ent.reaction.subMenu;
// not already retrieved
if (retrieved.indexOf(next) < 0 && next !== undefined && next !== "") {
console.log(ent);
getID(ent.reaction.subMenu);
retrieved.push(next);
}
});
});
}
try {
fs.mkdirSync('json');
} catch (err) {
if (err.code !== 'EEXIST') {
throw err;
}
}
fetch("https://xkcd.com/1975/alto/root").then(r => r.json())
.then(data => {
fs.writeFileSync("json/root.json", JSON.stringify(data));
data.Menu.entries.forEach(ent => {
getID(ent.reaction.subMenu);
});
});