// 主库 import fetch from "node-fetch"; import fs from "node:fs"; // 爬虫库 import puppeteer from "../../../lib/puppeteer/puppeteer.js"; // http库 import axios from "axios"; // 常量 import { CAT_LIMIT } from "../constants/constant.js"; // 书库 import { getZHelper, getYiBook, getZBook } from "../utils/books.js"; // 工具类 import _ from "lodash"; import TokenBucket from '../utils/token-bucket.js' export class query extends plugin { constructor() { super({ name: "R插件查询类", dsc: "R插件查询相关指令", event: "message.group", priority: 500, rule: [ { reg: "^#医药查询(.*)$", fnc: "doctor", }, { reg: "^#cat$", fnc: "cat", }, { reg: "^#推荐软件$", fnc: "softwareRecommended", }, { reg: "^#买家秀$", fnc: "buyerShow", }, { reg: "^#累了$", fnc: "cospro", }, { reg: "^#青年大学习$", fnc: "youthLearning", }, { reg: "^#搜书(.*)$", fnc: "searchBook", }, { reg: "^#竹白(.*)", fnc: "zhubaiSearch", }, ], }); } async doctor(e) { const keyword = e.msg.replace("#医药查询", "").trim(); const url = `https://api2.dayi.org.cn/api/search2?keyword=${keyword}&pageNo=1&pageSize=10`; try { const res = await fetch(url) .then(resp => resp.json()) .then(resp => resp.list); const promises = res.map(async element => { const title = this.removeTag(element.title); const template = `${title}\n标签:${element.secondTitle}\n介绍:${element.introduction}`; if (title === keyword) { const browser = await puppeteer.browserInit(); const page = await browser.newPage(); await page.goto(`https://www.dayi.org.cn/drug/${element.id}`); const buff = await page.screenshot({ fullPage: true, type: "jpeg", omitBackground: false, quality: 90, }); await e.reply(segment.image(buff)); browser.close(); } return { message: { type: "text", text: template, }, 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; } async cat(e) { const [shibes, cats] = await Promise.allSettled([ fetch(`https://shibe.online/api/cats?count=${CAT_LIMIT}`).then(data => data.json()), fetch(`https://api.thecatapi.com/v1/images/search?limit=${CAT_LIMIT}`).then(data => data.json(), ), ]); const shibeUrls = shibes.status === "fulfilled" ? shibes.value : []; const catUrls = cats.status === "fulfilled" ? cats.value.map(item => item.url) : []; const reqRes = [...shibeUrls, ...catUrls]; e.reply("涩图也不看了,就看猫是吧"); const images = reqRes.map(item => ({ message: segment.image(item), nickname: this.e.sender.card || this.e.user_id, user_id: this.e.user_id, })); e.reply(await Bot.makeForwardMsg(images)); return true; } async softwareRecommended(e) { // 接口 const urls = [ "https://www.ghxi.com/ghapi?type=query&n=pc", "https://www.ghxi.com/ghapi?type=query&n=and", ]; // 一起请求 const promises = urls.map(url => 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`; return { message: { type: "text", text: template }, nickname: e.sender.card || e.user_id, user_id: e.user_id, }; }), ); // 异步操作 e.reply(await Bot.makeForwardMsg(msg)); return true; } async buyerShow(e) { const p1 = fetch("https://api.vvhan.com/api/tao").then(resp => resp.url); const p2 = fetch("https://api.uomg.com/api/rand.img3?format=json") .then(resp => resp.json()) .then(resp => resp.imgurl); const results = await Promise.allSettled([p1, p2]); const images = results .filter(result => result.status === "fulfilled") .map(result => result.value); for (const img of images) { e.reply(segment.image(img)); } return true; } // 青年大学习 async youthLearning(e) { await axios .get( "https://qczj.h5yunban.com/qczj-youth-learning/cgi-bin/common-api/course/current", { headers: { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36 Edg/95.0.1020.53", }, }, ) .then(resp => { // logger.info(resp.data); return resp.data.result.uri.replace("index.html", "m.html"); }) .then(async uri => { axios .get(uri, { headers: { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36 Edg/95.0.1020.53", }, }) .then(resp => { const content = resp.data; const resList = content.match(/