feat: 更改哔哩哔哩解析逻辑 & 🎈 perf: 提升论文解析速度

1. 增加bv av互转
2. 删除代码中的av逻辑,出现av一律转换为bv
3. 论文解析从race转换为any
This commit is contained in:
RrOrange 2023-03-27 17:41:29 +08:00
parent 4e50f786e6
commit 1cd916d48f
3 changed files with 48 additions and 17 deletions

View File

@ -19,6 +19,7 @@ import { getVideoInfo } from "../utils/biliInfo.js";
import { getBiliGptInputText } from "../utils/biliSummary.js"; import { getBiliGptInputText } from "../utils/biliSummary.js";
import { getBodianAudio, getBodianMv, getBodianMusicInfo } from "../utils/bodian.js"; import { getBodianAudio, getBodianMv, getBodianMusicInfo } from "../utils/bodian.js";
import { ChatGPTClient } from "@waylaidwanderer/chatgpt-api"; import { ChatGPTClient } from "@waylaidwanderer/chatgpt-api";
import { av2BV } from "../utils/bilibili-bv-av-convert.js";
export class tools extends plugin { export class tools extends plugin {
constructor() { constructor() {
@ -298,7 +299,11 @@ export class tools extends plugin {
} else if (url.includes("www.bilibili.com")) { } else if (url.includes("www.bilibili.com")) {
url = urlRex.exec(url)[0]; 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")) { if (url.includes("t.bilibili.com")) {
// 去除多余参数 // 去除多余参数
@ -819,7 +824,7 @@ export class tools extends plugin {
const newWaitList = waitList.map(item => { const newWaitList = waitList.map(item => {
return item + flag; return item + flag;
}); });
await Promise.race(newWaitList).then(resp => { await Promise.any(newWaitList).then(resp => {
e.reply(resp); e.reply(resp);
}); });
} }

View File

@ -4,11 +4,8 @@ async function getVideoInfo(url) {
const baseVideoInfo = "http://api.bilibili.com/x/web-interface/view"; const baseVideoInfo = "http://api.bilibili.com/x/web-interface/view";
const videoId = /video\/[^\?\/ ]+/.exec(url)[0].split("/")[1]; const videoId = /video\/[^\?\/ ]+/.exec(url)[0].split("/")[1];
// 获取视频信息,然后发送 // 获取视频信息,然后发送
return fetch( return fetch(`${baseVideoInfo}?bvid=${videoId}`)
videoId.startsWith("BV") .then(async resp => {
? `${baseVideoInfo}?bvid=${videoId}`
: `${baseVideoInfo}?aid=${videoId}`,
).then(async resp => {
const respJson = await resp.json(); const respJson = await resp.json();
const respData = respJson.data; const respData = respJson.data;
return { return {

View File

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