From 1cd916d48fe43a51131caf3d5b678798ca4883ec Mon Sep 17 00:00:00 2001 From: RrOrange <1370346834@qq.com> Date: Mon, 27 Mar 2023 17:41:29 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat:=20=E6=9B=B4=E6=94=B9=E5=93=94?= =?UTF-8?q?=E5=93=A9=E5=93=94=E5=93=A9=E8=A7=A3=E6=9E=90=E9=80=BB=E8=BE=91?= =?UTF-8?q?=20&=20=F0=9F=8E=88=20perf:=20=E6=8F=90=E5=8D=87=E8=AE=BA?= =?UTF-8?q?=E6=96=87=E8=A7=A3=E6=9E=90=E9=80=9F=E5=BA=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. 增加bv av互转 2. 删除代码中的av逻辑,出现av一律转换为bv 3. 论文解析从race转换为any --- apps/tools.js | 9 +++++++-- utils/biliInfo.js | 27 ++++++++++++--------------- utils/bilibili-bv-av-convert.js | 29 +++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+), 17 deletions(-) create mode 100644 utils/bilibili-bv-av-convert.js diff --git a/apps/tools.js b/apps/tools.js index cbcc635..b3147b4 100644 --- a/apps/tools.js +++ b/apps/tools.js @@ -19,6 +19,7 @@ import { getVideoInfo } from "../utils/biliInfo.js"; import { getBiliGptInputText } from "../utils/biliSummary.js"; import { getBodianAudio, getBodianMv, getBodianMusicInfo } from "../utils/bodian.js"; import { ChatGPTClient } from "@waylaidwanderer/chatgpt-api"; +import { av2BV } from "../utils/bilibili-bv-av-convert.js"; export class tools extends plugin { constructor() { @@ -298,7 +299,11 @@ export class tools extends plugin { } else if (url.includes("www.bilibili.com")) { url = urlRex.exec(url)[0]; } - + // av处理 + const matched = url.match(/(av|AV)(\w+)/); + if (matched) { + url = url.replace(matched[0], av2BV(Number(matched[2]))); + } // 动态 if (url.includes("t.bilibili.com")) { // 去除多余参数 @@ -819,7 +824,7 @@ export class tools extends plugin { const newWaitList = waitList.map(item => { return item + flag; }); - await Promise.race(newWaitList).then(resp => { + await Promise.any(newWaitList).then(resp => { e.reply(resp); }); } diff --git a/utils/biliInfo.js b/utils/biliInfo.js index 116a0a0..c102db6 100644 --- a/utils/biliInfo.js +++ b/utils/biliInfo.js @@ -4,21 +4,18 @@ async function getVideoInfo(url) { const baseVideoInfo = "http://api.bilibili.com/x/web-interface/view"; const videoId = /video\/[^\?\/ ]+/.exec(url)[0].split("/")[1]; // 获取视频信息,然后发送 - return fetch( - videoId.startsWith("BV") - ? `${baseVideoInfo}?bvid=${videoId}` - : `${baseVideoInfo}?aid=${videoId}`, - ).then(async resp => { - const respJson = await resp.json(); - const respData = respJson.data; - return { - title: respData.title, - desc: respData.desc, - duration: respData.duration, - dynamic: respJson.data.dynamic, - stat: respData.stat, - aid: respData.aid, - cid: respData.pages?.[0].cid, + return fetch(`${baseVideoInfo}?bvid=${videoId}`) + .then(async resp => { + const respJson = await resp.json(); + const respData = respJson.data; + return { + title: respData.title, + desc: respData.desc, + duration: respData.duration, + dynamic: respJson.data.dynamic, + stat: respData.stat, + aid: respData.aid, + cid: respData.pages?.[0].cid, }; }); } diff --git a/utils/bilibili-bv-av-convert.js b/utils/bilibili-bv-av-convert.js new file mode 100644 index 0000000..899835a --- /dev/null +++ b/utils/bilibili-bv-av-convert.js @@ -0,0 +1,29 @@ +const table = 'fZodR9XQDSUm21yCkr6zBqiveYah8bt4xsWpHnJE7jL5VG3guMTKNPAwcF' +const tr = Array.from(table).reduce((o, c, i) => Object.assign(o, { [c]: i }), {}) +const s = [11, 10, 3, 8, 4, 6] +const xor = 177451812 +const add = 8728348608 +/** 算法来源:https://www.zhihu.com/question/381784377/answer/1099438784 **/ + +/** + * Convert a BV string to AV number + * @param {string} bv The BV string to be converted to AV number + * @returns {number} The AV number converted from the provided BV string + */ +function bv2AV(bv) { + return (new Uint8Array(6).reduce((r, _, i) => r + tr[bv[s[i]]] * 58 ** i, 0) - add) ^ xor +} + +/** + * Convert a AV number to BV string + * @param {number} av The AV number to be converted to BV string + * @returns {string} The BV string converted from the provided AV number + */ +function av2BV(av) { + return (new Uint8Array(6).reduce((r, _, i) => { r.splice(s[i], 1, table[Math.floor(((av ^ xor) + add) / 58 ** i % 58)]); return r }, Array.from('BV1 4 1 7 '))).join('') +} + +export { + bv2AV, + av2BV +}