Skip to content

Commit

Permalink
feat(route/bilibili): add getRenderData to bypass the risk control me…
Browse files Browse the repository at this point in the history
…chanism (DIYgod#16718)
  • Loading branch information
Konano committed Sep 12, 2024
1 parent c9235bc commit c095623
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
22 changes: 22 additions & 0 deletions lib/routes/bilibili/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = () => {
Expand Down Expand Up @@ -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 () => {
Expand Down Expand Up @@ -262,4 +283,5 @@ export default {
getCidFromId,
getAidFromBvid,
getArticleDataFromCvid,
getRenderData,
};
5 changes: 5 additions & 0 deletions lib/routes/bilibili/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -122,4 +126,5 @@ export default {
getDmImgList,
addDmVerifyInfo,
bvidTime,
addRenderData,
};
3 changes: 2 additions & 1 deletion lib/routes/bilibili/video.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`, {
Expand All @@ -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`,
Expand Down

0 comments on commit c095623

Please sign in to comment.