🎈 perf: 更新小蓝鸟解析算法

This commit is contained in:
zhiyu1998 2023-02-21 02:16:49 +08:00
parent a074888405
commit 0fb26456a0

View File

@ -376,20 +376,21 @@ export class tools extends plugin {
if (!fs.existsSync(downloadPath)) { if (!fs.existsSync(downloadPath)) {
mkdirsSync(downloadPath); mkdirsSync(downloadPath);
} }
// 开始读取数据 // 逐个遍历判断
if (resp.includes.media[0].type === "photo") { for (let item of resp.includes.media) {
// 图片 if (item.type === "photo") {
resp.includes.media.map(item => { // 图片
const filePath = `${downloadPath}/${item.url.split("/").pop()}`; const filePath = `${downloadPath}/${item.url.split("/").pop()}`;
this.downloadImgs(item.url, downloadPath).then(tmp => { this.downloadImg(item.url, downloadPath).then(_ => {
e.reply(segment.image(fs.readFileSync(filePath))); e.reply(segment.image(fs.readFileSync(filePath)));
fs.unlinkSync(filePath)
}); });
}); } else if (item.type === "video") {
} else { // 视频
// 视频 this.downloadVideo(resp.includes.media[0].variants[0].url, true).then(_ => {
this.downloadVideo(resp.includes.media[0].variants[0].url, true).then(video => { e.reply(segment.video(`${downloadPath}/temp.mp4`));
e.reply(segment.video(`${downloadPath}/temp.mp4`)); });
}); }
} }
}); });
return true; return true;
@ -607,7 +608,7 @@ export class tools extends plugin {
} }
// 工具:下载一张网络图片 // 工具:下载一张网络图片
async downloadImgs(img, dir) { async downloadImg (img, dir) {
const filename = img.split("/").pop(); const filename = img.split("/").pop();
const filepath = `${dir}/${filename}`; const filepath = `${dir}/${filename}`;
const writer = fs.createWriteStream(filepath); const writer = fs.createWriteStream(filepath);
@ -628,8 +629,16 @@ export class tools extends plugin {
.then(res => { .then(res => {
res.data.pipe(writer); res.data.pipe(writer);
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
writer.on("finish", () => resolve(filepath)); writer.on("finish", () => {
writer.on("error", reject); writer.close(() => {
resolve(filepath);
});
});
writer.on("error", (err) => {
fs.unlink(filepath, () => {
reject(err);
});
});
}); });
}); });
} }