-
Notifications
You must be signed in to change notification settings - Fork 23
/
imgfetch.js
52 lines (46 loc) · 1.46 KB
/
imgfetch.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
48
49
50
51
52
const fumoData = require('./fumo_data.json');
const fs = require('fs');
const path = require('path');
const url = require('url');
const https = require('https');
function downloadFumo(fumo) {
const targetFileName = 'static/img/' + fumo.id + path.extname(fumo.img);
console.log(fumo.img, '=>', targetFileName);
return new Promise((resolve, reject) => {
const file = fs.createWriteStream(targetFileName);
const imgUrl = url.parse(fumo.img);
https.get({
host: imgUrl.host,
path: imgUrl.path,
headers: {
'Referer': fumo.gift_link,
}
}, (resp) => {
resp.pipe(file);
file.on('finish', () => {
file.close();
resolve(file);
})
}).on('error', (err) => {
reject(err);
});
});
}
async function doWork() {
if (!fs.existsSync('static/img')) {
fs.mkdirSync('static/img', { recursive: true });
}
for (let character of fumoData.characters) {
console.log('Downloading images of ' + character.ch_name + ' fumos...');
for (let key of ['regular', 'straps', 'puppets', 'dekas']) {
if (character.hasOwnProperty(key)) {
for (let fumo of character[key]) {
if (fumo.id !== 'inu-sakuya') {
await downloadFumo(fumo);
}
}
}
}
}
}
doWork();