feat(route): 添加对哔哩哔哩新接口和校验信息的支持 (#11480)
* feat(route): support verify info for bilibili * feat(route): add verify info when request * fix(route): fix the format Signed-off-by: cyx20080216 <cyx20080216@outlook.com> * 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 <cyx20080216@outlook.com>
This commit is contained in:
parent
a69dfacf2d
commit
9cf71fc7a6
|
|
@ -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 () => {
|
||||
|
|
|
|||
|
|
@ -1,9 +1,19 @@
|
|||
const crypto = require('crypto');
|
||||
|
||||
const iframe = (aid, page, bvid) =>
|
||||
`<iframe src="https://player.bilibili.com/player.html?${bvid ? `bvid=${bvid}` : `aid=${aid}`}${
|
||||
page ? `&page=${page}` : ''
|
||||
}&high_quality=1" width="650" height="477" scrolling="no" border="0" frameborder="no" framespacing="0" allowfullscreen="true"></iframe>`;
|
||||
|
||||
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,
|
||||
};
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
|
|||
Loading…
Reference in New Issue