miao-plugin/tools/char-img-download.js

97 lines
2.1 KiB
JavaScript
Raw Normal View History

2022-04-07 20:51:47 +00:00
import {Data} from "../components/index.js";
2022-04-05 14:24:02 +00:00
import lodash from "lodash";
import fs from "fs";
import request from "request";
const _root = process.cwd() + "/plugins/miao-plugin/";
const _cRoot = _root + "resources/meta/character/";
2022-04-07 20:51:47 +00:00
let readDir = fs.readdirSync(_cRoot);
console.log(readDir);
2022-04-05 14:24:02 +00:00
let imgs = [];
function img(char, url, target) {
imgs.push({
url,
file: `${char.name}/${target}`
})
}
2022-04-07 20:51:47 +00:00
lodash.forEach(readDir, (c) => {
console.log(c);
if (!fs.existsSync(`${_cRoot}/${c}/data.json`)) {
return;
}
let char = Data.readJSON(`${_cRoot}/${c}/`, 'data.json');
2022-04-05 14:24:02 +00:00
2022-04-05 15:01:02 +00:00
if (char.name) {
2022-04-05 14:24:02 +00:00
// 正面
2022-04-07 20:51:47 +00:00
// 角色条
img(char, char.imgs.profile, "profile.png");
// 名片
img(char, char.imgs.party, "party.png");
// img(char, char.imgs.char, "char.png");
2022-04-05 14:24:02 +00:00
// 立绘-竖版
2022-04-07 20:51:47 +00:00
img(char, char.imgs.gacha_card, "gacha_card.png");
2022-04-05 14:24:02 +00:00
// 立绘
2022-04-07 20:51:47 +00:00
img(char, char.imgs.gacha_splash, "gacha_splash.png");
2022-04-05 14:24:02 +00:00
// 正面像
2022-04-07 20:51:47 +00:00
img(char, char.imgs.face, "face.png");
img(char, char.imgs.side, "face.png");
2022-04-05 14:24:02 +00:00
// 天赋
2022-05-17 07:38:23 +00:00
if (char.talent) {
img(char, char.talent.a.icon, "talent_a.png");
img(char, char.talent.e.icon, "talent_e.png");
img(char, char.talent.q.icon, "talent_q.png");
}
2022-04-05 14:24:02 +00:00
// 被动天赋
lodash.forEach(char.passive, (p, idx) => {
2022-04-07 20:51:47 +00:00
img(char, p.icon, `passive_${idx}.png`);
2022-04-05 14:24:02 +00:00
});
// 命座
lodash.forEach(char.cons, (con, idx) => {
img(char, con.icon, `cons_${idx}.png`)
});
}
})
let cacheFile = async function () {
let cacheFn = async function (file) {
if (fs.existsSync(`${_cRoot}/${file.file}`)) {
console.log(`已存在,跳过 ${file.file}`);
2022-04-07 20:51:47 +00:00
return true;
2022-04-05 14:24:02 +00:00
}
2022-04-05 15:01:02 +00:00
try {
2022-04-07 20:51:47 +00:00
let stream = fs.createWriteStream(`${_cRoot}/${file.file}`);
await request("https://genshin.honeyhunterworld.com/" + file.url).pipe(stream);
2022-04-05 15:01:02 +00:00
2022-04-07 20:51:47 +00:00
return new Promise((resolve) => {
stream.on('finish', resolve)
});
} catch (e) {
2022-04-05 15:01:02 +00:00
return false;
}
2022-04-05 14:24:02 +00:00
console.log(`下载成功: ${file.file}`);
return true;
};
console.log('开始下载');
2022-04-05 15:01:02 +00:00
await Data.asyncPool(5, imgs, cacheFn);
2022-04-05 14:24:02 +00:00
}
await cacheFile();
console.log('下载成功');