From c034cfb69b5d09c453860103e8d288637b91fa5d Mon Sep 17 00:00:00 2001 From: Tony Date: Tue, 26 Sep 2023 16:19:17 +0000 Subject: [PATCH] refactor(route): bilibili getCookie remove deadcode (#13409) --- lib/v2/bilibili/cache.js | 85 +++++++++++++++++++++++----------------- lib/v2/bilibili/video.js | 14 ++++--- 2 files changed, 57 insertions(+), 42 deletions(-) diff --git a/lib/v2/bilibili/cache.js b/lib/v2/bilibili/cache.js index ab84b4acd..7c08b8e65 100644 --- a/lib/v2/bilibili/cache.js +++ b/lib/v2/bilibili/cache.js @@ -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); diff --git a/lib/v2/bilibili/video.js b/lib/v2/bilibili/video.js index c31aadcd4..4d9207f8a 100644 --- a/lib/v2/bilibili/video.js +++ b/lib/v2/bilibili/video.js @@ -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}`); }