From 9cf71fc7a62ec461cb61b9ea6e8bb78a068df243 Mon Sep 17 00:00:00 2001 From: cyx20080216 <63776734+cyx20080216@users.noreply.github.com> Date: Mon, 26 Dec 2022 12:23:32 +0800 Subject: [PATCH] =?UTF-8?q?feat(route):=20=E6=B7=BB=E5=8A=A0=E5=AF=B9?= =?UTF-8?q?=E5=93=94=E5=93=A9=E5=93=94=E5=93=A9=E6=96=B0=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E5=92=8C=E6=A0=A1=E9=AA=8C=E4=BF=A1=E6=81=AF=E7=9A=84=E6=94=AF?= =?UTF-8?q?=E6=8C=81=20(#11480)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat(route): support verify info for bilibili * feat(route): add verify info when request * fix(route): fix the format Signed-off-by: cyx20080216 * fix(route): fix the error when lint * fix(route): get verify string from bilibili when it was called * fix(route): use a simple way to calculate w_rid * fix(route): get img_url and sub_url from api * fix(route): get the url of js from space page * fix(route): add referer * fix(route): add canonical name * fix(route): remove unnecessary action * Update lib/v2/bilibili/utils.js Signed-off-by: cyx20080216 --- lib/v2/bilibili/cache.js | 35 ++++++++++++++++++++++++++++++++++- lib/v2/bilibili/utils.js | 10 ++++++++++ lib/v2/bilibili/video-all.js | 7 +++++-- lib/v2/bilibili/video.js | 4 +++- 4 files changed, 52 insertions(+), 4 deletions(-) diff --git a/lib/v2/bilibili/cache.js b/lib/v2/bilibili/cache.js index 9dedad9c1..9f453d0e7 100644 --- a/lib/v2/bilibili/cache.js +++ b/lib/v2/bilibili/cache.js @@ -4,10 +4,43 @@ module.exports = { getCookie: (ctx) => { const key = 'bili-cookie'; return ctx.cache.tryGet(key, async () => { - const response = await got(`https://www.bilibili.com/`); + const response = await got("https://www.bilibili.com/"); return response.headers['set-cookie']; }); }, + getVerifyString: (ctx) => { + const key = 'bili-verify-string'; + return ctx.cache.tryGet(key, async () => { + const cookie = await module.exports.getCookie(ctx); + const { data: navResponse } = await got("https://api.bilibili.com/x/web-interface/nav", { + headers: { + Referer: "https://www.bilibili.com/", + Cookie: cookie, + }, + }); + const imgUrl = navResponse.data.wbi_img.img_url; + const subUrl = navResponse.data.wbi_img.sub_url; + const r = imgUrl.substring(imgUrl.lastIndexOf('/') + 1, imgUrl.length).split('.')[0] + subUrl.substring(subUrl.lastIndexOf('/') + 1, subUrl.length).split('.')[0]; + const { body: spaceResponse } = await got("https://space.bilibili.com/1", { + headers: { + Referer: "https://www.bilibili.com/", + Cookie: cookie, + }, + }); + const jsUrl = "https:" + spaceResponse.match(/[^"]*9.space[^"]*/); + const { body: jsResponse } = await got(jsUrl, { + headers: { + Referer: "https://space.bilibili.com/1", + }, + }); + const array = JSON.parse(jsResponse.match(/\[(?:\d+,){63}\d+\]/)); + const o = []; + array.forEach(((t) => { + r.charAt(t) && o.push(r.charAt(t)); + })); + return o.join('').slice(0, 32); + }); + }, getUsernameFromUID: (ctx, uid) => { const key = 'bili-username-from-uid-' + uid; return ctx.cache.tryGet(key, async () => { diff --git a/lib/v2/bilibili/utils.js b/lib/v2/bilibili/utils.js index 4a5dc0596..1f2aaf278 100644 --- a/lib/v2/bilibili/utils.js +++ b/lib/v2/bilibili/utils.js @@ -1,9 +1,19 @@ +const crypto = require('crypto'); + const iframe = (aid, page, bvid) => ``; +const addVerifyInfo = (params, verifyString) => { + const md5 = crypto.createHash('md5'); + const wts = Math.round(Date.now() / 1000); + const w_rid = md5.update(`${params}&wts=${wts}${verifyString}`).digest('hex'); + return `${params}&w_rid=${w_rid}&wts=${wts}`; +}; + module.exports = { iframe, + addVerifyInfo, bvidTime: 1589990400, }; diff --git a/lib/v2/bilibili/video-all.js b/lib/v2/bilibili/video-all.js index 2cf71c68c..edf58b3fa 100644 --- a/lib/v2/bilibili/video-all.js +++ b/lib/v2/bilibili/video-all.js @@ -7,6 +7,7 @@ module.exports = async (ctx) => { const uid = ctx.params.uid; const disableEmbed = ctx.params.disableEmbed; const cookie = await cache.getCookie(ctx); + const verifyString = cache.getVerifyString(ctx); const [name, face] = await cache.getUsernameAndFaceFromUID(ctx, uid); await got(`https://space.bilibili.com/${uid}/video?tid=0&page=1&keyword=&order=pubdate`, { @@ -15,7 +16,8 @@ module.exports = async (ctx) => { Cookie: cookie, }, }); - const response = await got(`https://api.bilibili.com/x/space/arc/search?mid=${uid}&ps=30&tid=0&pn=1&keyword=&order=pubdate&jsonp=jsonp`, { + const params = utils.addVerifyInfo(`mid=${uid}&ps=30&tid=0&pn=1&keyword=&order=pubdate&order_avoided=true`, verifyString); + 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`, Cookie: cookie, @@ -32,7 +34,8 @@ module.exports = async (ctx) => { Cookie: cookie, }, }); - return await got(`https://api.bilibili.com/x/space/arc/search?mid=${uid}&ps=30&tid=0&pn=${pageId}&keyword=&order=pubdate&jsonp=jsonp`, { + const params = utils.addVerifyInfo(`mid=${uid}&ps=30&tid=0&pn=${pageId}&keyword=&order=pubdate&order_avoided=true`, verifyString); + return await got(`https://api.bilibili.com/x/space/wbi/arc/search?${params}`, { headers: { Referer: `https://space.bilibili.com/${uid}/video?tid=0&page=${pageId}&keyword=&order=pubdate`, Cookie: cookie, diff --git a/lib/v2/bilibili/video.js b/lib/v2/bilibili/video.js index d55ed7582..31affdb10 100644 --- a/lib/v2/bilibili/video.js +++ b/lib/v2/bilibili/video.js @@ -6,6 +6,7 @@ module.exports = async (ctx) => { const uid = ctx.params.uid; const disableEmbed = ctx.params.disableEmbed; const cookie = await cache.getCookie(ctx); + const verifyString = await cache.getVerifyString(ctx); const [name, face] = await cache.getUsernameAndFaceFromUID(ctx, uid); await got(`https://space.bilibili.com/${uid}/video?tid=0&page=1&keyword=&order=pubdate`, { @@ -14,7 +15,8 @@ module.exports = async (ctx) => { Cookie: cookie, }, }); - const response = await got(`https://api.bilibili.com/x/space/arc/search?mid=${uid}&ps=30&tid=0&pn=1&keyword=&order=pubdate&jsonp=jsonp`, { + const params = utils.addVerifyInfo(`mid=${uid}&ps=30&tid=0&pn=1&keyword=&order=pubdate&order_avoided=true`, verifyString); + 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`, Cookie: cookie,