refactor(route): bilibili getCookie remove deadcode (#13409)

This commit is contained in:
Tony 2023-09-26 16:19:17 +00:00 committed by GitHub
parent adf40af494
commit c034cfb69b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 57 additions and 42 deletions

View File

@ -2,6 +2,7 @@ const got = require('@/utils/got');
const utils = require('./utils');
const cheerio = require('cheerio');
const config = require('@/config').value;
const logger = require('@/utils/logger');
module.exports = {
getCookie: (ctx) => {
@ -12,8 +13,8 @@ module.exports = {
return ctx.cache.tryGet(key, async () => {
// default Referer: https://www.bilibili.com is limited
// Bilibili return cookies with multiple set-cookie
let response = await got('https://www.bilibili.com/');
const setCookie = response.headers['set-cookie'];
let response = await got('https://space.bilibili.com/1');
const setCookie = response.headers['set-cookie']; // should contain buvid3 and b_nut
if (typeof setCookie === 'undefined') {
return '';
}
@ -29,20 +30,25 @@ module.exports = {
cookie.push(['bvuid4', encodeURIComponent(response.data.data.b_4)].join('='));
const e = Math.floor(Date.now() / 1000);
const hexsign = utils.hexsign(e);
await got('https://space.bilibili.com/1', {
headers: {
Referer: 'https://www.bilibili.com/',
Cookie: cookie.join('; '),
},
});
response = await got.post(`https://api.bilibili.com/bapis/bilibili.api.ticket.v1.Ticket/GenWebTicket?key_id=ec02&hexsign=${hexsign}&context[ts]=${e}&csrf=`, {
headers: {
Referer: 'https://space.bilibili.com/1',
Cookie: cookie.join('; '),
},
});
cookie.push(['bili_ticket', response.data.data.ticket].join('='));
cookie.push(['bili_ticket_expires', (parseInt(response.data.data.created_at) + parseInt(response.data.data.ttl)).toString()].join('='));
// await got('https://space.bilibili.com/1', {
// headers: {
// Referer: 'https://www.bilibili.com/',
// Cookie: cookie.join('; '),
// },
// });
try {
response = await got.post(`https://api.bilibili.com/bapis/bilibili.api.ticket.v1.Ticket/GenWebTicket?key_id=ec02&hexsign=${hexsign}&context[ts]=${e}&csrf=`, {
headers: {
Referer: 'https://space.bilibili.com/1',
Cookie: cookie.join('; '),
},
});
cookie.push(['bili_ticket', response.data.data.ticket].join('='));
cookie.push(['bili_ticket_expires', (parseInt(response.data.data.created_at) + parseInt(response.data.data.ttl)).toString()].join('='));
} catch (e) {
// HTTPError: Response code 429 (Too Many Requests)
}
return cookie.join('; ');
});
},
@ -59,18 +65,23 @@ module.exports = {
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: spaceResponse } = await got('https://space.bilibili.com/1', {
// headers: {
// Referer: 'https://www.bilibili.com/',
// Cookie: cookie,
// },
// });
// const jsUrl = 'https:' + spaceResponse.match(/[^"]*9.space[^"]*/);
const jsUrl = 'https://s1.hdslb.com/bfs/seed/laputa-header/bili-header.umd.js';
const { body: jsResponse } = await got(jsUrl, {
headers: {
Referer: 'https://space.bilibili.com/1',
},
});
// const array = [
// 46, 47, 18, 2, 53, 8, 23, 32, 15, 50, 10, 31, 58, 3, 45, 35, 27, 43, 5, 49, 33, 9, 42, 19, 29, 28, 14, 39, 12, 38, 41, 13, 37, 48, 7, 16, 24, 55, 40, 61, 26, 17, 0, 1, 60, 51, 30, 4, 22, 25, 54, 21, 56, 59, 6, 63, 57,
// 62, 11, 36, 20, 34, 44, 52,
// ];
const array = JSON.parse(jsResponse.match(/\[(?:\d+,){63}\d+\]/));
const o = [];
array.forEach((t) => {
@ -84,12 +95,12 @@ module.exports = {
return ctx.cache.tryGet(key, async () => {
const cookie = await module.exports.getCookie(ctx);
const verifyString = await module.exports.getVerifyString(ctx);
await got(`https://space.bilibili.com/${uid}/`, {
headers: {
Referer: 'https://www.bilibili.com/',
Cookie: cookie,
},
});
// await got(`https://space.bilibili.com/${uid}/`, {
// headers: {
// Referer: 'https://www.bilibili.com/',
// Cookie: cookie,
// },
// });
const params = utils.addVerifyInfo(`mid=${uid}&token=&platform=web&web_location=1550101`, verifyString);
const { data: nameResponse } = await got(`https://api.bilibili.com/x/space/wbi/acc/info?${params}`, {
headers: {
@ -108,12 +119,12 @@ module.exports = {
if (!name || !face) {
const cookie = await module.exports.getCookie(ctx);
const verifyString = await module.exports.getVerifyString(ctx);
await got(`https://space.bilibili.com/${uid}/`, {
headers: {
Referer: `https://www.bilibili.com/`,
Cookie: cookie,
},
});
// await got(`https://space.bilibili.com/${uid}/`, {
// headers: {
// Referer: `https://www.bilibili.com/`,
// Cookie: cookie,
// },
// });
const params = utils.addVerifyInfo(`mid=${uid}&token=&platform=web&web_location=1550101`, verifyString);
const { data: nameResponse } = await got(`https://api.bilibili.com/x/space/wbi/acc/info?${params}`, {
headers: {
@ -121,9 +132,11 @@ module.exports = {
Cookie: cookie,
},
});
if (nameResponse.data) {
if (nameResponse.data.name) {
name = nameResponse.data.name;
face = nameResponse.data.face;
} else {
logger.error(`Error when visiting /x/space/wbi/acc/info: ${JSON.stringify(nameResponse)}`);
}
ctx.cache.set(nameKey, name);
ctx.cache.set(faceKey, face);

View File

@ -1,6 +1,7 @@
const got = require('@/utils/got');
const cache = require('./cache');
const utils = require('./utils');
const logger = require('@/utils/logger');
module.exports = async (ctx) => {
const uid = ctx.params.uid;
@ -9,12 +10,12 @@ module.exports = async (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`, {
headers: {
Referer: `https://space.bilibili.com/${uid}/`,
Cookie: cookie,
},
});
// await got(`https://space.bilibili.com/${uid}/video?tid=0&page=1&keyword=&order=pubdate`, {
// headers: {
// Referer: `https://space.bilibili.com/${uid}/`,
// Cookie: cookie,
// },
// });
const params = utils.addVerifyInfo(`mid=${uid}&ps=30&tid=0&pn=1&keyword=&order=pubdate&platform=web&web_location=1550101&order_avoided=true`, verifyString);
const response = await got(`https://api.bilibili.com/x/space/wbi/arc/search?${params}`, {
headers: {
@ -24,6 +25,7 @@ module.exports = async (ctx) => {
});
const data = response.data;
if (data.code) {
logger.error(JSON.stringify(data.data));
throw new Error(`Got error code ${data.code} while fetching: ${data.message}`);
}