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 +}