-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfetchDataset.js
More file actions
37 lines (32 loc) · 925 Bytes
/
fetchDataset.js
File metadata and controls
37 lines (32 loc) · 925 Bytes
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
const ora = require("ora");
const fetch = require("node-fetch");
const unzip = require("unzipper");
const fetchDataset = async (name, url, entryCallback) => {
const downloadSpinner = ora(
`Downloading "${name}" fiches from ${url}`
).start();
const dataStream = await fetch(url, { responsename: "stream" }).then(
(r) => r.body
);
let count = 0;
return dataStream
.pipe(unzip.Parse())
.on("entry", function (entry) {
if (entry.path.match(/^[FRN]\d+\.xml/)) {
entryCallback(entry, downloadSpinner);
count += 1;
} else {
entry.autodrain();
}
})
.on("finish", () => {
downloadSpinner.succeed(`Download of ${count} files "${name}" succeeded`);
})
.on("error", (e) => {
downloadSpinner.warn(`Download of "${name}" files failed`);
console.log("e", e);
throw e;
})
.promise();
};
module.exports = fetchDataset;