From 6fdd85e0fed0afa1cf3af25f0966e0904216235c Mon Sep 17 00:00:00 2001 From: cyx20080216 <63776734+cyx20080216@users.noreply.github.com> Date: Wed, 28 Sep 2022 22:11:07 +0800 Subject: [PATCH 1/2] =?UTF-8?q?fix(route):=20Anti-Anti-Spider=20=E5=93=94?= =?UTF-8?q?=E5=93=A9=E5=93=94=E5=93=A9=20(#10886)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix(route): Anti-Anti-Reptilian 哔哩哔哩 * fix(route): Anti-Anti-Reptilian 哔哩哔哩 * fix(route): Remove debugging statement * fix(route): Comply with code specifications by adding a semicolon. * fix(route): Resolve the requested changes Use normative key Use cache.tryGet Use more simplified got request Remove unnecessary template Use default UA * fix(route): Remove unnecessary await * fix(route): Use simplified got request. * fix(route): Use default UA * fix(route): Remove unnecessary require Co-authored-by: cyx20080216 --- lib/v2/bilibili/cache.js | 41 ++++++++++++++++++++++++++++-------- lib/v2/bilibili/video-all.js | 34 +++++++++++++++++++++++------- lib/v2/bilibili/video.js | 12 ++++++++--- 3 files changed, 67 insertions(+), 20 deletions(-) diff --git a/lib/v2/bilibili/cache.js b/lib/v2/bilibili/cache.js index 7f233d8ede972..866904272734c 100644 --- a/lib/v2/bilibili/cache.js +++ b/lib/v2/bilibili/cache.js @@ -1,30 +1,53 @@ const got = require('@/utils/got'); module.exports = { + getCookie: (ctx) => { + const key = 'bili-cookie'; + return ctx.cache.tryGet(key, async () => { + const response = await got(`https://www.bilibili.com/`); + return response.headers['set-cookie']; + }); + }, getUsernameFromUID: (ctx, uid) => { const key = 'bili-username-from-uid-' + uid; return ctx.cache.tryGet(key, async () => { - const { data: nameResponse } = await got(`https://api.bilibili.com/x/space/acc/info?mid=${uid}&jsonp=jsonp`, { + const cookie = await module.exports.getCookie(ctx); + await got(`https://space.bilibili.com/${uid}/`, { headers: { - Referer: `https://space.bilibili.com/${uid}`, - }, + Referer: 'https://www.bilibili.com/', + Cookie: cookie + } + }); + const {data: nameResponse} = await got(`https://api.bilibili.com/x/space/acc/info?mid=${uid}&token=&platform=web&jsonp=jsonp`, { + headers: { + Referer: `https://space.bilibili.com/${uid}/`, + Cookie: cookie + } }); return nameResponse.data.name; }); }, getUsernameAndFaceFromUID: async (ctx, uid) => { + const cookie = await module.exports.getCookie(ctx); const nameKey = 'bili-username-from-uid-' + uid; const faceKey = 'bili-userface-from-uid-' + uid; let name = await ctx.cache.get(nameKey); let face = await ctx.cache.get(faceKey); - if (!name) { - const nameResponse = await got(`https://api.bilibili.com/x/space/acc/info?mid=${uid}&jsonp=jsonp`, { + if (!name || !face) { + await got(`https://space.bilibili.com/${uid}/`, { headers: { - Referer: `https://space.bilibili.com/${uid}`, - }, + Referer: `https://www.bilibili.com/`, + Cookie: cookie + } + }); + const {data: nameResponse} = await got(`https://api.bilibili.com/x/space/acc/info?mid=${uid}&token=&platform=web&jsonp=jsonp`, { + headers: { + Referer: `https://space.bilibili.com/${uid}/`, + Cookie: cookie + } }); - name = nameResponse.data.data.name; - face = nameResponse.data.data.face; + name = nameResponse.data.name; + face = nameResponse.data.face; ctx.cache.set(nameKey, name); ctx.cache.set(faceKey, face); } diff --git a/lib/v2/bilibili/video-all.js b/lib/v2/bilibili/video-all.js index f025119eddd44..b1a1a73f19c8f 100644 --- a/lib/v2/bilibili/video-all.js +++ b/lib/v2/bilibili/video-all.js @@ -6,27 +6,45 @@ const { parseDate } = require('@/utils/parse-date'); module.exports = async (ctx) => { const uid = ctx.params.uid; const disableEmbed = ctx.params.disableEmbed; + const cookie = await cache.getCookie(ctx); const [name, face] = await cache.getUsernameAndFaceFromUID(ctx, uid); - 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`, { + 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 response = await got(`https://api.bilibili.com/x/space/arc/search?mid=${uid}&ps=30&tid=0&pn=1&keyword=&order=pubdate&jsonp=jsonp`, { + headers: { + Referer: `https://space.bilibili.com/${uid}/video?tid=0&page=1&keyword=&order=pubdate`, + Cookie: cookie }, }); const vlist = [...response.data.data.list.vlist]; const pageTotal = Math.ceil(response.data.data.page.count / response.data.data.page.ps); + + const getPage = async (pageId) => { + await got(`https://space.bilibili.com/${uid}/video?tid=0&page=${pageId}&keyword=&order=pubdate`, { + headers: { + Referer: `https://space.bilibili.com/${uid}/`, + 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`, { + headers: { + Referer: `https://space.bilibili.com/${uid}/video?tid=0&page=${pageId}&keyword=&order=pubdate`, + Cookie: cookie + }, + }); + }; + const promises = []; if (pageTotal > 1) { for (let i = 2; i <= pageTotal; i++) { - promises.push( - got(`https://api.bilibili.com/x/space/arc/search?mid=${uid}&ps=30&tid=0&pn=${i}&keyword=&order=pubdate&jsonp=jsonp`, { - headers: { - Referer: `https://space.bilibili.com/${uid}/`, - }, - }) - ); + promises.push(getPage(i)); } const rets = await Promise.all(promises); rets.forEach((ret) => { diff --git a/lib/v2/bilibili/video.js b/lib/v2/bilibili/video.js index ec0b77117ac12..f10855fad7108 100644 --- a/lib/v2/bilibili/video.js +++ b/lib/v2/bilibili/video.js @@ -5,13 +5,19 @@ const utils = require('./utils'); module.exports = async (ctx) => { const uid = ctx.params.uid; const disableEmbed = ctx.params.disableEmbed; + const cookie = await cache.getCookie(ctx); const [name, face] = await cache.getUsernameAndFaceFromUID(ctx, uid); - const response = await got({ - method: 'get', - url: `https://api.bilibili.com/x/space/arc/search?mid=${uid}&ps=10&tid=0&pn=1&order=pubdate&jsonp=jsonp`, + 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 response = await got(`https://api.bilibili.com/x/space/arc/search?mid=${uid}&ps=30&tid=0&pn=1&keyword=&order=pubdate&jsonp=jsonp`, { + headers: { + Referer: `https://space.bilibili.com/${uid}/video?tid=0&page=1&keyword=&order=pubdate`, + Cookie: cookie }, }); const data = response.data; From 5f157b02a1d2ae9053256a3dff0cbe40d331b0d7 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Wed, 28 Sep 2022 14:13:46 +0000 Subject: [PATCH 2/2] style: auto format --- lib/v2/bilibili/cache.js | 20 ++++++++++---------- lib/v2/bilibili/video-all.js | 8 ++++---- lib/v2/bilibili/video.js | 4 ++-- 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/lib/v2/bilibili/cache.js b/lib/v2/bilibili/cache.js index 866904272734c..9dedad9c16d7b 100644 --- a/lib/v2/bilibili/cache.js +++ b/lib/v2/bilibili/cache.js @@ -15,14 +15,14 @@ module.exports = { await got(`https://space.bilibili.com/${uid}/`, { headers: { Referer: 'https://www.bilibili.com/', - Cookie: cookie - } + Cookie: cookie, + }, }); - const {data: nameResponse} = await got(`https://api.bilibili.com/x/space/acc/info?mid=${uid}&token=&platform=web&jsonp=jsonp`, { + const { data: nameResponse } = await got(`https://api.bilibili.com/x/space/acc/info?mid=${uid}&token=&platform=web&jsonp=jsonp`, { headers: { Referer: `https://space.bilibili.com/${uid}/`, - Cookie: cookie - } + Cookie: cookie, + }, }); return nameResponse.data.name; }); @@ -37,14 +37,14 @@ module.exports = { await got(`https://space.bilibili.com/${uid}/`, { headers: { Referer: `https://www.bilibili.com/`, - Cookie: cookie - } + Cookie: cookie, + }, }); - const {data: nameResponse} = await got(`https://api.bilibili.com/x/space/acc/info?mid=${uid}&token=&platform=web&jsonp=jsonp`, { + const { data: nameResponse } = await got(`https://api.bilibili.com/x/space/acc/info?mid=${uid}&token=&platform=web&jsonp=jsonp`, { headers: { Referer: `https://space.bilibili.com/${uid}/`, - Cookie: cookie - } + Cookie: cookie, + }, }); name = nameResponse.data.name; face = nameResponse.data.face; diff --git a/lib/v2/bilibili/video-all.js b/lib/v2/bilibili/video-all.js index b1a1a73f19c8f..2cf71c68cc2f8 100644 --- a/lib/v2/bilibili/video-all.js +++ b/lib/v2/bilibili/video-all.js @@ -12,13 +12,13 @@ module.exports = async (ctx) => { await got(`https://space.bilibili.com/${uid}/video?tid=0&page=1&keyword=&order=pubdate`, { headers: { Referer: `https://space.bilibili.com/${uid}/`, - Cookie: cookie + 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`, { headers: { Referer: `https://space.bilibili.com/${uid}/video?tid=0&page=1&keyword=&order=pubdate`, - Cookie: cookie + Cookie: cookie, }, }); @@ -29,13 +29,13 @@ module.exports = async (ctx) => { await got(`https://space.bilibili.com/${uid}/video?tid=0&page=${pageId}&keyword=&order=pubdate`, { headers: { Referer: `https://space.bilibili.com/${uid}/`, - Cookie: cookie + 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`, { headers: { Referer: `https://space.bilibili.com/${uid}/video?tid=0&page=${pageId}&keyword=&order=pubdate`, - Cookie: cookie + Cookie: cookie, }, }); }; diff --git a/lib/v2/bilibili/video.js b/lib/v2/bilibili/video.js index f10855fad7108..d55ed7582e4c3 100644 --- a/lib/v2/bilibili/video.js +++ b/lib/v2/bilibili/video.js @@ -11,13 +11,13 @@ module.exports = async (ctx) => { await got(`https://space.bilibili.com/${uid}/video?tid=0&page=1&keyword=&order=pubdate`, { headers: { Referer: `https://space.bilibili.com/${uid}/`, - Cookie: cookie + 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`, { headers: { Referer: `https://space.bilibili.com/${uid}/video?tid=0&page=1&keyword=&order=pubdate`, - Cookie: cookie + Cookie: cookie, }, }); const data = response.data;