feat(route/bilibili): add getRenderData to bypass the risk control mechanism (#16718)

This commit is contained in:
Nano
2024-09-12 23:17:42 +08:00
committed by GitHub
parent c9235bc5f8
commit c095623823
3 changed files with 29 additions and 1 deletions
+22
View File
@@ -5,6 +5,7 @@ import { load } from 'cheerio';
import { config } from '@/config';
import logger from '@/utils/logger';
import puppeteer from '@/utils/puppeteer';
import { JSDOM } from 'jsdom';
let disableConfigCookie = false;
const getCookie = () => {
@@ -39,6 +40,26 @@ const clearCookie = () => {
disableConfigCookie = true;
};
const getRenderData = (uid) => {
const key = 'bili-web-render-data';
return cache.tryGet(key, async () => {
const cookie = await getCookie();
const { data: response } = await got(`https://space.bilibili.com/${uid}`, {
headers: {
Referer: 'https://www.bilibili.com/',
Cookie: cookie,
},
});
const dom = new JSDOM(response);
const document = dom.window.document;
const scriptElement = document.querySelector('#__RENDER_DATA__');
const innerText = scriptElement ? scriptElement.textContent || '{}' : '{}';
const renderData = JSON.parse(decodeURIComponent(innerText));
const accessId = renderData.access_id;
return accessId;
});
};
const getWbiVerifyString = () => {
const key = 'bili-wbi-verify-string';
return cache.tryGet(key, async () => {
@@ -262,4 +283,5 @@ export default {
getCidFromId,
getAidFromBvid,
getArticleDataFromCvid,
getRenderData,
};
+5
View File
@@ -66,6 +66,10 @@ function hexsign(e) {
return o;
}
function addRenderData(params, renderData) {
return `${params}&w_webid=${encodeURIComponent(renderData)}`;
}
function addWbiVerifyInfo(params, wbiVerifyString) {
const searchParams = new URLSearchParams(params);
searchParams.sort();
@@ -122,4 +126,5 @@ export default {
getDmImgList,
addDmVerifyInfo,
bvidTime,
addRenderData,
};
+2 -1
View File
@@ -35,6 +35,7 @@ async function handler(ctx) {
const cookie = await cache.getCookie();
const wbiVerifyString = await cache.getWbiVerifyString();
const dmImgList = utils.getDmImgList();
const renderData = await cache.getRenderData(uid);
const [name, face] = await cache.getUsernameAndFaceFromUID(uid);
// await got(`https://space.bilibili.com/${uid}/video?tid=0&page=1&keyword=&order=pubdate`, {
@@ -43,7 +44,7 @@ async function handler(ctx) {
// Cookie: cookie,
// },
// });
const params = utils.addWbiVerifyInfo(utils.addDmVerifyInfo(`mid=${uid}&ps=30&tid=0&pn=1&keyword=&order=pubdate&platform=web&web_location=1550101&order_avoided=true`, dmImgList), wbiVerifyString);
const params = utils.addWbiVerifyInfo(utils.addRenderData(utils.addDmVerifyInfo(`mid=${uid}&ps=30&tid=0&pn=1&keyword=&order=pubdate&platform=web&web_location=1550101&order_avoided=true`, dmImgList), renderData), wbiVerifyString);
const response = await got(`https://api.bilibili.com/x/space/wbi/arc/search?${params}`, {
headers: {
Referer: `https://space.bilibili.com/${uid}/video?tid=0&page=1&keyword=&order=pubdate`,