-
Notifications
You must be signed in to change notification settings - Fork 0
/
getWordList.js
47 lines (42 loc) · 1.37 KB
/
getWordList.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
40
41
42
43
44
45
46
47
const axios = require('axios').default
const fs = require('fs')
const url = 'https://data.public.lu/api/1/datasets/letzebuerger-online-dictionnaire-komplett-wuertlescht-complete-wordlist/'
// from https://stackoverflow.com/questions/55374755/node-js-axios-download-file-stream-and-writefile
async function downloadFile(fileUrl, outputLocationPath) {
const writer = fs.createWriteStream(outputLocationPath);
return axios({
method: 'get',
url: fileUrl,
responseType: 'stream',
}).then(response => {
//ensure that the user can call `then()` only when the file has
//been downloaded entirely.
return new Promise((resolve, reject) => {
response.data.pipe(writer);
let error = null;
writer.on('error', err => {
error = err;
writer.close();
reject(err);
});
writer.on('close', () => {
if (!error) {
resolve(true);
}
//no need to call the reject here, as it will have been called in the
//'error' stream;
});
});
});
}
axios.get(url)
.then(function (response) {
downloadFile(response['data']['resources'][0]['url'], './data/'+response['data']['resources'][0]['title'])
})
.catch(function (error) {
// handle error
console.log(error);
})
.then(function () {
// always executed
});