-
Notifications
You must be signed in to change notification settings - Fork 0
/
get-images.js
36 lines (32 loc) · 909 Bytes
/
get-images.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
const axios = require('axios');
const fs = require('fs');
const villagers = require('./villagers.json');
// http://acnhapi.com/icons/villagers/1
const download_image = (url, image_path) =>
axios({
url,
responseType: 'stream',
}).then(
(response) =>
new Promise((resolve, reject) => {
response.data
.pipe(fs.createWriteStream(image_path))
.on('finish', () => resolve())
.on('error', (e) => reject(e));
})
);
// async to rate limit
const wait = (amount = 0) =>
new Promise((resolve) => setTimeout(resolve, amount));
async function start() {
for (const key in villagers) {
const villager = villagers[key];
await download_image(
`http://acnhapi.com/icons/villagers/${villager.id}`,
`data/villagers/${villager.name['name-en']}.png`
);
// wait one second after each request
await wait(1000);
}
}
start();