From 67bd2786bcef1b47f21c6abd5c60277f3a1b1386 Mon Sep 17 00:00:00 2001 From: DIYgod Date: Tue, 28 Apr 2020 15:15:51 +0800 Subject: [PATCH] feat: bilibili bvid /video/reply --- docs/social-media.md | 2 +- lib/router.js | 2 +- lib/routes/bilibili/cache.js | 32 +++++++++++++++++++++++--------- lib/routes/bilibili/reply.js | 19 ++++++++++++++----- 4 files changed, 39 insertions(+), 16 deletions(-) diff --git a/docs/social-media.md b/docs/social-media.md index e748419c35..7c89dfa5a0 100644 --- a/docs/social-media.md +++ b/docs/social-media.md @@ -174,7 +174,7 @@ Tiny Tiny RSS 会给所有 iframe 元素添加 `sandbox="allow-scripts"` 属性 ### 视频评论 - + ### 视频弹幕 diff --git a/lib/router.js b/lib/router.js index ccbe3c0cc1..a9cf682a95 100755 --- a/lib/router.js +++ b/lib/router.js @@ -57,7 +57,7 @@ router.get('/bilibili/partion/ranking/:tid/:days?/:disableEmbed?', require('./ro router.get('/bilibili/bangumi/:seasonid', require('./routes/bilibili/bangumi')); // 弃用 router.get('/bilibili/bangumi/media/:mediaid', require('./routes/bilibili/bangumi')); router.get('/bilibili/video/page/:bvid/:disableEmbed?', require('./routes/bilibili/page')); -router.get('/bilibili/video/reply/:aid', require('./routes/bilibili/reply')); +router.get('/bilibili/video/reply/:bvid', require('./routes/bilibili/reply')); router.get('/bilibili/video/danmaku/:aid/:pid?', require('./routes/bilibili/danmaku')); router.get('/bilibili/link/news/:product', require('./routes/bilibili/linkNews')); router.get('/bilibili/live/room/:roomID', require('./routes/bilibili/liveRoom')); diff --git a/lib/routes/bilibili/cache.js b/lib/routes/bilibili/cache.js index 04a6070cca..ea65e24f81 100644 --- a/lib/routes/bilibili/cache.js +++ b/lib/routes/bilibili/cache.js @@ -55,14 +55,14 @@ module.exports = { } return name; }, - getVideoNameFromAid: async (ctx, aid) => { - const key = `bili-videoName-from-aid-${aid}`; + getVideoNameFromId: async (ctx, aid, bvid) => { + const key = `bili-videoName-from-id-${bvid || aid}`; let name = await ctx.cache.get(key); if (!name) { const nameResponse = await got({ method: 'get', - url: `https://www.bilibili.com/video/av${aid}`, + url: `https://www.bilibili.com/video/${bvid || `av${aid}`}`, responseType: 'buffer', }); const responseHtml = iconv.decode(nameResponse.data, 'UTF-8'); @@ -84,13 +84,27 @@ module.exports = { Referer: `https://www.bilibili.com/video/av${aid}`, }, }); - if (cidResponse && cidResponse.data && cidResponse.data.data && cidResponse.data.data.pages && cidResponse.data.data.pages.length >= pid) { - cid = cidResponse.data.data.pages[pid - 1].cid; - } - if (!cid) { - ctx.cache.set(key, cid); - } + cid = cidResponse.data.data.pages[pid - 1].cid; + ctx.cache.set(key, cid); } return cid; }, + getAidFromBvid: async (ctx, bvid) => { + const key = `bili-cid-from-bvid-${bvid}`; + let aid = await ctx.cache.get(key); + if (!aid) { + const response = await got({ + method: 'get', + url: `https://api.bilibili.com/x/web-interface/view?bvid=${bvid}`, + headers: { + Referer: `https://www.bilibili.com/video/${bvid}`, + }, + }); + if (response.data && response.data.data && response.data.data.aid) { + aid = response.data.data.aid; + } + ctx.cache.set(key, aid); + } + return aid; + }, }; diff --git a/lib/routes/bilibili/reply.js b/lib/routes/bilibili/reply.js index 3f40120586..1c4ddf3238 100644 --- a/lib/routes/bilibili/reply.js +++ b/lib/routes/bilibili/reply.js @@ -2,14 +2,23 @@ const got = require('@/utils/got'); const cache = require('./cache'); module.exports = async (ctx) => { - const aid = ctx.params.aid; - const name = await cache.getVideoNameFromAid(ctx, aid); + let bvid = ctx.params.bvid; + let aid; + if (!bvid.startsWith('BV')) { + aid = bvid; + bvid = null; + } + const name = await cache.getVideoNameFromId(ctx, aid, bvid); + if (!aid) { + aid = await cache.getAidFromBvid(ctx, bvid); + } + const link = `https://www.bilibili.com/video/${bvid || `av${aid}`}`; const response = await got({ method: 'get', url: `https://api.bilibili.com/x/v2/reply?type=1&oid=${aid}&sort=0`, headers: { - Referer: `https://www.bilibili.com/video/av${aid}`, + Referer: link, }, }); @@ -21,9 +30,9 @@ module.exports = async (ctx) => { description: `${name} 的评论`, item: data.map((item) => ({ title: `${item.member.uname} : ${item.content.message}`, - description: `#${item.floor}
${item.member.uname} : ${item.content.message}`, + description: `${item.member.uname} : ${item.content.message}`, pubDate: new Date(item.ctime * 1000).toUTCString(), - link: `https://www.bilibili.com/video/av${aid}/#reply${item.rpid}`, + link: `${link}/#reply${item.rpid}`, })), }; };