-
Notifications
You must be signed in to change notification settings - Fork 14
/
icons.js
80 lines (70 loc) · 2.26 KB
/
icons.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
const axios = require('axios');
const fs = require('fs');
const path = require('path');
const { parse } = require('csv');
const iconv = require('iconv-lite');
const url = 'http://dl.pvp.xoyo.com/prod/icons/';
const start = 11220;
async function download(link, name) {
const response = await axios({
method: 'GET',
url: link,
responseType: 'stream'
});
// pipe the result stream into a file on disc
response.data.pipe(fs.createWriteStream(path.resolve(__dirname, 'icons', name)));
// return a promise and resolve when download finishes
return new Promise((resolve, reject) => {
response.data.on('end', () => {
console.log(`${name} success`)
resolve()
});
response.data.on('error', () => {
console.log(`${name} error`)
resolve()
});
})
}
let firstLineLoaded = false;
let keys = [];
const paths = [];
function readTab() {
console.time(`reading icons`);
fs.createReadStream('./raw/icon.txt')
.pipe(iconv.decodeStream('gb2312'))
.pipe(iconv.encodeStream('utf8'))
.pipe(parse({ delimiter: '\t' }))
.on('data', function(row) {
if (!firstLineLoaded) {
// 读取第一行
firstLineLoaded = true;
keys = row;
} else {
const item = row.reduce((acc, cur, i) => {
const keyName = keys[i];
acc[keyName] = cur;
return acc;
}, {});
paths.push(item);
}
})
.on('end',function() {
console.timeEnd(`reading icons`);
(async function() {
for (let link of paths) {
if (link.ID < start) {
continue;
}
const pngUrl = url + link.FileName.replace('UITex', 'png');
const fileName = `${link.ID}.png`;
try {
await download(pngUrl, fileName);
} catch (e) {
console.log(`${link.ID} : ${fileName} error`)
// do nothing
}
}
})();
});
}
readTab();