mirror of
https://github.com/Jerryplusy/rc-plugin.git
synced 2025-10-14 16:19:18 +00:00
🎈 perf: 取消搜书复杂的交互
This commit is contained in:
parent
1b55d9fff5
commit
3c4fc7a9c2
@ -9,7 +9,7 @@ import fs from "node:fs";
|
||||
// 常量
|
||||
import { CAT_LIMIT } from "../utils/constant.js";
|
||||
// 书库
|
||||
import { getZHelper, getYiBook, getBookDetail, getZBook } from "../utils/books.js";
|
||||
import { getYiBook, getZBook } from "../utils/books.js";
|
||||
|
||||
export class query extends plugin {
|
||||
constructor() {
|
||||
@ -299,70 +299,15 @@ export class query extends plugin {
|
||||
}
|
||||
};
|
||||
|
||||
const [/*zHelper, yiBook, */zBook] = await Promise.all([/*getZHelper(e, keyword), getYiBook(e, keyword), */getZBook(e, keyword)]);
|
||||
|
||||
// replyMessage(yiBook);
|
||||
replyMessage(zBook);
|
||||
|
||||
/*if (zHelper && zHelper.length > 0) {
|
||||
await replyMessage(zHelper);
|
||||
const replyText = "请选择一个你想要的ID、来源,例如:\n" +
|
||||
"11918807 superlib\n" +
|
||||
"只回复11918807 默认zlibrary\n" +
|
||||
"书源若不对应则回复无效链接,数字字母之间空格";
|
||||
await e.reply(replyText);
|
||||
this.setContext("searchBookContext");
|
||||
}*/
|
||||
|
||||
// 集成易书、zBook
|
||||
const bookList = await Promise.allSettled([getYiBook(e, keyword), getZBook(e, keyword)]);
|
||||
bookList.filter(one => one.status === "fulfilled")
|
||||
.map(item => {
|
||||
replyMessage(item.value)
|
||||
})
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
// 通过id搜书
|
||||
async searchBookById(e) {
|
||||
let keyword = e.msg.replace(/#bookid/, "").trim();
|
||||
if (_.isEmpty(keyword)) {
|
||||
e.reply(`请输入书名,例如:#搜书 12`);
|
||||
return true;
|
||||
}
|
||||
|
||||
let id, source;
|
||||
if (keyword.includes(" ")) {
|
||||
[id, source] = keyword.split(" ");
|
||||
} else {
|
||||
id = /\d+/.exec(keyword)[0];
|
||||
source = "";
|
||||
}
|
||||
const res = await getBookDetail(e, id, source);
|
||||
await this.reply(await Bot.makeForwardMsg(res));
|
||||
}
|
||||
|
||||
/**
|
||||
* @link searchBook 的上下文
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
async searchBookContext() {
|
||||
// 当前消息
|
||||
const curMsg = this.e;
|
||||
// 上一个消息
|
||||
// const preMsg = this.getContext();
|
||||
if (!curMsg.msg) {
|
||||
this.e.reply("请回复id和来源!");
|
||||
return;
|
||||
}
|
||||
// 获取id和来源
|
||||
let id, source;
|
||||
if (curMsg.msg.includes(" ")) {
|
||||
[id, source] = curMsg.msg.split(" ");
|
||||
} else {
|
||||
id = /\d+/.exec(curMsg.msg)[0];
|
||||
source = "";
|
||||
}
|
||||
const res = await getBookDetail(curMsg, id, source);
|
||||
await this.reply(await Bot.makeForwardMsg(res));
|
||||
this.finish("searchBookContext");
|
||||
}
|
||||
|
||||
// 竹白百科
|
||||
async zhubaiSearch(e) {
|
||||
const keyword = e.msg.replace("#竹白", "").trim();
|
||||
|
159
utils/books.js
159
utils/books.js
@ -1,50 +1,6 @@
|
||||
import axios from "axios";
|
||||
import HttpProxyAgent from "https-proxy-agent";
|
||||
|
||||
/**
|
||||
* 获取ZHelper的数据
|
||||
* @param keyword
|
||||
* @returns {Promise<AxiosResponse<any>>}
|
||||
*/
|
||||
async function getZHelper(e, keyword) {
|
||||
const sendTemplate = {
|
||||
nickname: e.sender.card || e.user_id,
|
||||
user_id: e.user_id,
|
||||
};
|
||||
return axios
|
||||
.post("https://api.ylibrary.org/api/search/", {
|
||||
headers: {
|
||||
"user-agent":
|
||||
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36 Edg/111.0.1660.14",
|
||||
referer: "https://search.zhelper.net/",
|
||||
},
|
||||
keyword: keyword,
|
||||
page: 1,
|
||||
sensitive: false,
|
||||
})
|
||||
.then(async resp => {
|
||||
return resp.data.data.map(item => {
|
||||
const { title, author, publisher, isbn, extension, filesize, year, id, source } =
|
||||
item;
|
||||
// 数据组合
|
||||
return {
|
||||
message: {
|
||||
type: "text",
|
||||
text:
|
||||
`${id}: <${title}>\n` +
|
||||
`作者:${author}\n` +
|
||||
`书籍类型:${extension}\n` +
|
||||
`出版年月:${year}\n` +
|
||||
`来源:${source}\n` +
|
||||
`ISBN:${isbn || "暂无"}\n` +
|
||||
`出版社:${publisher}\n` +
|
||||
`文件大小:${(Number(filesize) / 1024 / 1024).toFixed(2)}MB`,
|
||||
},
|
||||
...sendTemplate,
|
||||
};
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取易书下载的来源
|
||||
@ -103,62 +59,6 @@ async function getYiBook(e, keyword) {
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取书籍下载方式
|
||||
* @param e
|
||||
* @param id
|
||||
* @param source
|
||||
* @returns {Promise<AxiosResponse<any>>}
|
||||
*/
|
||||
async function getBookDetail(e, id, source) {
|
||||
return axios
|
||||
.post("https://api.ylibrary.org/api/detail/", {
|
||||
headers: {
|
||||
"user-agent":
|
||||
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36 Edg/111.0.1660.14",
|
||||
referer: "https://search.zhelper.net/",
|
||||
},
|
||||
id: id,
|
||||
source: source || "zlibrary",
|
||||
})
|
||||
.then(async resp => {
|
||||
const {
|
||||
author,
|
||||
extension,
|
||||
filesize,
|
||||
id,
|
||||
in_libgen,
|
||||
ipfs_cid,
|
||||
md5,
|
||||
publisher,
|
||||
source,
|
||||
title,
|
||||
year,
|
||||
} = resp.data;
|
||||
const Libgen = `https://libgendown.1kbtool.com/${md5}`;
|
||||
const ipfs = `https://ipfs-checker.1kbtool.com/${ipfs_cid}?filename=${encodeURIComponent(
|
||||
title,
|
||||
)}_${source}-search.${extension}`;
|
||||
const reqUrl = `${md5}#${filesize}#${encodeURIComponent(title)}_${encodeURIComponent(
|
||||
author,
|
||||
)}_${id}_${source}-search.${extension}`;
|
||||
const cleverPass = `https://rapidupload.1kbtool.com/${reqUrl}`;
|
||||
const cleverPass2 = `https://rulite.1kbtool.com/${reqUrl}`;
|
||||
return [
|
||||
`Libgen:${Libgen}`,
|
||||
`ipfs:${ipfs}`,
|
||||
`秒传:${cleverPass}`,
|
||||
`秒传Lite:${cleverPass2}`,
|
||||
].map(item => {
|
||||
return {
|
||||
message: { type: "text", text: item },
|
||||
nickname: e.sender.card || e.user_id,
|
||||
user_id: e.user_id,
|
||||
};
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* ZBook的下载网址
|
||||
* @type {string[]}
|
||||
@ -182,7 +82,7 @@ async function getZBook(e, keyword) {
|
||||
user_id: e.user_id,
|
||||
};
|
||||
return axios
|
||||
.get(`https://zbook.lol/search?title=${encodeURIComponent(keyword)}&limit=20`)
|
||||
.get(`https://zbook.lol/search?title=${encodeURIComponent(keyword)}&limit=10`)
|
||||
.then(resp => {
|
||||
return resp.data.books.map(item => {
|
||||
const {
|
||||
@ -207,7 +107,6 @@ async function getZBook(e, keyword) {
|
||||
)}.${extension}`
|
||||
}
|
||||
);
|
||||
// const { url, speed } = await findFastestUrl(bookDownloadUrls);
|
||||
return {
|
||||
message: {
|
||||
type: "text",
|
||||
@ -221,8 +120,7 @@ async function getZBook(e, keyword) {
|
||||
`ISBN:${isbn || "暂无"}\n` +
|
||||
`出版社:${publisher}\n` +
|
||||
`文件大小:${(Number(filesize) / 1024 / 1024).toFixed(2)}MB\n\n` +
|
||||
// `竞速下载:最快链接-${url}, 预计下载速度:${speed}\n` +
|
||||
`其他下载直链:${bookDownloadUrls.join("\n")}`,
|
||||
`其他下载直链:${bookDownloadUrls.join("\n\n")}`,
|
||||
},
|
||||
...sendTemplate,
|
||||
};
|
||||
@ -230,55 +128,4 @@ async function getZBook(e, keyword) {
|
||||
});
|
||||
}
|
||||
|
||||
async function measureDownloadSpeed(url) {
|
||||
const startTime = performance.now();
|
||||
const response = await fetch(url, {
|
||||
headers: {
|
||||
"User-Agent":
|
||||
"Mozilla/5.0 (Linux; Android 5.0; SM-G900P Build/LRX21T) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Mobile Safari/537.36",
|
||||
},
|
||||
agent: new HttpProxyAgent("http://127.0.0.1:7890"),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`Error fetching ${url}: ${response.status}`);
|
||||
}
|
||||
|
||||
const contentLength = response.headers.get("content-length") || 0;
|
||||
|
||||
// Instead of using response.buffer(), we will use response.body to read data
|
||||
const data = [];
|
||||
for await (const chunk of response.body) {
|
||||
data.push(chunk);
|
||||
}
|
||||
const buffer = Buffer.concat(data);
|
||||
|
||||
const endTime = performance.now();
|
||||
const duration = (endTime - startTime) / 1000; // in seconds
|
||||
const speed = contentLength / 1024 / duration; // in KB/s
|
||||
|
||||
return {
|
||||
url,
|
||||
speed,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 链接竞速,暂时不做,浪费网页流量,保护接口
|
||||
* @param urls
|
||||
* @returns {Promise<unknown extends (object & {then(onfulfilled: infer F): any}) ? (F extends ((value: infer V, ...args: any) => any) ? Awaited<V> : never) : unknown>}
|
||||
*/
|
||||
async function findFastestUrl(urls) {
|
||||
const promises = urls.map(url =>
|
||||
measureDownloadSpeed(url).catch(error => {
|
||||
console.error(error);
|
||||
return { url, speed: 0 };
|
||||
}),
|
||||
);
|
||||
|
||||
const results = await Promise.all(promises);
|
||||
results.sort((a, b) => b.speed - a.speed);
|
||||
return results[0];
|
||||
}
|
||||
|
||||
export { getYiBook, getZHelper, getBookDetail, getZBook };
|
||||
export { getYiBook, getZBook };
|
||||
|
Loading…
x
Reference in New Issue
Block a user