🎈 perf: 提升部分功能性能 & 纠正部分正则

1. 提升#医药查询性能
2. 提升#cat性能
3. 提升#推荐软件性能
4. 提升#cat性能
5. 纠正正则
This commit is contained in:
zhiyu1998 2023-04-01 14:35:07 +08:00
parent 0cf43cba77
commit 13db370f0f

View File

@ -37,7 +37,7 @@ export class query extends plugin {
fnc: "buyerShow", fnc: "buyerShow",
}, },
{ {
reg: "^#(累了)$", reg: "^#累了$",
fnc: "cospro", fnc: "cospro",
}, },
{ {
@ -45,11 +45,11 @@ export class query extends plugin {
fnc: "youthLearning", fnc: "youthLearning",
}, },
{ {
reg: "^#(搜书)(.*)$$", reg: "^#搜书(.*)$$",
fnc: "searchBook", fnc: "searchBook",
}, },
{ {
reg: "^#(bookid)(.*)$$", reg: "^#bookid(.*)$$",
fnc: "searchBookById", fnc: "searchBookById",
} }
], ],
@ -57,122 +57,109 @@ export class query extends plugin {
} }
async doctor(e) { async doctor(e) {
let keyword = e.msg.replace("#医药查询", "").trim(); const keyword = e.msg.replace("#医药查询", "").trim();
const url = `https://api2.dayi.org.cn/api/search2?keyword=${keyword}&pageNo=1&pageSize=10`; const url = `https://api2.dayi.org.cn/api/search2?keyword=${keyword}&pageNo=1&pageSize=10`;
let res = await fetch(url) try {
.then(resp => resp.json()) const res = await fetch(url).then(resp => resp.json()).then(resp => resp.list);
.then(resp => resp.list); const promises = res.map(async element => {
let msg = []; const title = this.removeTag(element.title);
for (const element of res) { const template = `${title}\n标签:${element.secondTitle}\n介绍:${element.introduction}`;
const title = this.removeTag(element.title);
const template = ` if (title === keyword) {
${title}\n const browser = await puppeteer.browserInit();
标签${element.secondTitle}\n const page = await browser.newPage();
介绍${element.introduction} await page.goto(`https://www.dayi.org.cn/drug/${element.id}`);
`; const buff = await page.screenshot({
// 如果完全匹配,直接响应页面 fullPage: true,
if (title === keyword) { type: "jpeg",
const browser = await puppeteer.browserInit(); omitBackground: false,
const page = await browser.newPage(); quality: 90,
await page.goto(`https://www.dayi.org.cn/drug/${element.id}`); });
let buff = await page.screenshot({ await e.reply(segment.image(buff));
fullPage: true, browser.close();
type: "jpeg", }
omitBackground: false,
quality: 90, return {
}); message: {
browser.close(); type: "text",
await e.reply(segment.image(buff)); text: template
} },
msg.push({ nickname: Bot.nickname,
message: { type: "text", text: `${template}` }, user_id: Bot.user_id
nickname: Bot.nickname, };
user_id: Bot.user_id,
}); });
const msg = await Promise.all(promises);
e.reply(await Bot.makeForwardMsg(msg))
} catch (err) {
logger.error(err);
} }
/** 最后回复消息 */ return true;
return !!this.reply(await Bot.makeForwardMsg(msg));
} }
async cat(e) { async cat(e) {
let images = []; const [shibes, cats] = await Promise.allSettled([
let reqRes = [ fetch(`https://shibe.online/api/cats?count=${CAT_LIMIT}`).then(data => data.json()),
...(await fetch(`https://shibe.online/api/cats?count=${CAT_LIMIT}`).then(data => fetch(`https://api.thecatapi.com/v1/images/search?limit=${CAT_LIMIT}`).then(data => data.json()),
data.json(), ]);
)),
...(await fetch(`https://api.thecatapi.com/v1/images/search?limit=${CAT_LIMIT}`) const shibeUrls = shibes.status === "fulfilled" ? shibes.value : [];
.then(data => data.json()) const catUrls = cats.status === "fulfilled" ? cats.value.map(item => item.url) : [];
.then(json => json.map(item => item.url))), const reqRes = [...shibeUrls, ...catUrls];
];
e.reply("涩图也不看了,就看猫是吧"); e.reply("涩图也不看了,就看猫是吧");
reqRes.forEach(item => {
images.push({ const images = reqRes.map(item => ({
message: segment.image(item), message: segment.image(item),
nickname: this.e.sender.card || this.e.user_id, nickname: this.e.sender.card || this.e.user_id,
user_id: this.e.user_id, user_id: this.e.user_id,
}); }));
}); e.reply(await Bot.makeForwardMsg(images));
return !!(await this.reply(await Bot.makeForwardMsg(images))); return true;
} }
async softwareRecommended(e) { async softwareRecommended(e) {
// 接口 // 接口
const pcUrl = "https://www.ghxi.com/ghapi?type=query&n=pc"; const urls = [
const andUrl = "https://www.ghxi.com/ghapi?type=query&n=and"; "https://www.ghxi.com/ghapi?type=query&n=pc",
// 一起请求 "https://www.ghxi.com/ghapi?type=query&n=and"
const res = [
await fetch(pcUrl)
.then(resp => resp.json())
.catch(err => logger.error(err)),
await fetch(andUrl)
.then(resp => resp.json())
.catch(err => logger.error(err)),
]; ];
// 时间复杂度(n^2) 待优化 // 一起请求
const msg = res.map(async recommend => { const promises = urls.map(url =>
return recommend.data.list.map(element => { fetch(url)
.then(resp => resp.json())
.catch(err => logger.error(err))
);
const results = await Promise.allSettled(promises);
const msg = results
.filter(result => result.status === 'fulfilled') // 只保留已解决的 Promise
.flatMap(result => result.value.data.list.map(element => {
const template = `推荐软件:${element.title}\n地址:${element.url}\n`; const template = `推荐软件:${element.title}\n地址:${element.url}\n`;
return { return {
message: { type: "text", text: template }, message: { type: "text", text: template },
nickname: e.sender.card || e.user_id, nickname: e.sender.card || e.user_id,
user_id: e.user_id, user_id: e.user_id
}; };
}); }));
});
await Promise.all(msg).then(res => { // 异步操作
res.forEach(async item => { e.reply(await Bot.makeForwardMsg(msg));
e.reply(await Bot.makeForwardMsg(item));
});
});
return true; return true;
} }
async buyerShow(e) { async buyerShow(e) {
// http://3650000.xyz/api/?type=img const p1 = fetch("https://api.vvhan.com/api/tao").then(resp => resp.url);
// https://api.vvhan.com/api/tao const p2 = fetch("https://api.uomg.com/api/rand.img3?format=json")
// https://api.uomg.com/api/rand.img3?format=json .then(resp => resp.json())
// const randomIndex = Math.floor(Math.random() * urls.length); .then(resp => resp.imgurl);
// const randomElement = urls.splice(randomIndex, 1)[0];
const p1 = new Promise((resolve, reject) => { const results = await Promise.allSettled([p1, p2]);
fetch("https://api.vvhan.com/api/tao") const images = results.filter(result => result.status === "fulfilled").map(result => result.value);
.then(resp => {
return resolve(resp.url); for (const img of images) {
}) e.reply(segment.image(img));
.catch(err => reject(err)); }
});
const p2 = new Promise((resolve, reject) => {
fetch("https://api.uomg.com/api/rand.img3?format=json")
.then(resp => resp.json())
.then(resp => {
return resolve(resp.imgurl);
})
.catch(err => reject(err));
});
Promise.all([p1, p2]).then(res => {
res.forEach(item => {
e.reply(segment.image(item));
});
});
return true; return true;
} }
@ -276,24 +263,23 @@ export class query extends plugin {
} }
async cospro(e) { async cospro(e) {
let [res1, res2] = (await Promise.allSettled([
fetch("https://imgapi.cn/cos2.php?return=jsonpro").then(resp => resp.json()),
fetch("https://imgapi.cn/cos.php?return=jsonpro").then(resp => resp.json())
]))
.filter(result => result.status === "fulfilled").map(result => result.value);
let req = [ let req = [
...(await fetch("https://imgapi.cn/cos2.php?return=jsonpro") ...res1.imgurls,
.then(resp => resp.json()) ...res2.imgurls
.then(json => json.imgurls)),
...(await fetch("https://imgapi.cn/cos.php?return=jsonpro")
.then(resp => resp.json())
.then(json => json.imgurls)),
]; ];
e.reply("哪天克火掉一定是在这个群里面..."); e.reply("哪天克火掉一定是在这个群里面...");
let images = []; let images = req.map(item => ({
req.forEach(item => { message: segment.image(encodeURI(item)),
images.push({ nickname: this.e.sender.card || this.e.user_id,
message: segment.image(encodeURI(item)), user_id: this.e.user_id,
nickname: this.e.sender.card || this.e.user_id, }));
user_id: this.e.user_id, e.reply(await Bot.makeForwardMsg(images))
}); return true;
});
return !!(await this.reply(await Bot.makeForwardMsg(images)));
} }
// 搜书 // 搜书