From 481f625a0520fdcf9a70f26863d4e43a182de2eb Mon Sep 17 00:00:00 2001 From: DIYgod Date: Tue, 28 Apr 2020 15:23:22 +0800 Subject: [PATCH] feat: bilibili bvid /video/danamku --- docs/social-media.md | 2 +- lib/router.js | 2 +- lib/routes/bilibili/cache.js | 10 +++++----- lib/routes/bilibili/danmaku.js | 19 ++++++++++++++----- lib/routes/bilibili/reply.js | 2 +- 5 files changed, 22 insertions(+), 13 deletions(-) diff --git a/docs/social-media.md b/docs/social-media.md index 7c89dfa5a0..a1916460a0 100644 --- a/docs/social-media.md +++ b/docs/social-media.md @@ -178,7 +178,7 @@ Tiny Tiny RSS 会给所有 iframe 元素添加 `sandbox="allow-scripts"` 属性 ### 视频弹幕 - + ### link 公告 diff --git a/lib/router.js b/lib/router.js index a9cf682a95..3201a42bb2 100755 --- a/lib/router.js +++ b/lib/router.js @@ -58,7 +58,7 @@ 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/:bvid', require('./routes/bilibili/reply')); -router.get('/bilibili/video/danmaku/:aid/:pid?', require('./routes/bilibili/danmaku')); +router.get('/bilibili/video/danmaku/:bvid/: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')); router.get('/bilibili/live/search/:key/:order', require('./routes/bilibili/liveSearch')); diff --git a/lib/routes/bilibili/cache.js b/lib/routes/bilibili/cache.js index ea65e24f81..b999c71ef9 100644 --- a/lib/routes/bilibili/cache.js +++ b/lib/routes/bilibili/cache.js @@ -56,7 +56,7 @@ module.exports = { return name; }, getVideoNameFromId: async (ctx, aid, bvid) => { - const key = `bili-videoName-from-id-${bvid || aid}`; + const key = `bili-videoname-from-id-${bvid || aid}`; let name = await ctx.cache.get(key); if (!name) { @@ -73,15 +73,15 @@ module.exports = { } return name; }, - getCidFromAid: async (ctx, aid, pid) => { - const key = `bili-Cid-from-Aid-${aid}-${pid}`; + getCidFromId: async (ctx, aid, pid, bvid) => { + const key = `bili-cid-from-id-${bvid || aid}-${pid}`; let cid = await ctx.cache.get(key); if (!cid) { const cidResponse = await got({ method: 'get', - url: `https://api.bilibili.com/x/web-interface/view?aid=${aid}`, + url: `https://api.bilibili.com/x/web-interface/view?${bvid ? `bvid=${bvid}` : `aid=${aid}`}`, headers: { - Referer: `https://www.bilibili.com/video/av${aid}`, + Referer: `https://www.bilibili.com/video/${bvid || `av${aid}`}`, }, }); cid = cidResponse.data.data.pages[pid - 1].cid; diff --git a/lib/routes/bilibili/danmaku.js b/lib/routes/bilibili/danmaku.js index 25b3235595..7dca557f7e 100644 --- a/lib/routes/bilibili/danmaku.js +++ b/lib/routes/bilibili/danmaku.js @@ -4,16 +4,25 @@ const got = require('@/utils/got'); const zlib = require('zlib'); module.exports = async (ctx) => { - const aid = ctx.params.aid; + let bvid = ctx.params.bvid; + let aid; + if (!bvid.startsWith('BV')) { + aid = bvid; + bvid = null; + } const pid = Number(ctx.params.pid || 1); const limit = 50; - const cid = await cache.getCidFromAid(ctx, aid, pid); + const cid = await cache.getCidFromId(ctx, aid, pid, bvid); - const videoName = await cache.getVideoNameFromAid(ctx, aid); + const videoName = await cache.getVideoNameFromId(ctx, aid, bvid); + const link = `https://www.bilibili.com/video/${bvid || `av${aid}`}`; const danmakuResponse = await got.get(`https://comment.bilibili.com/${cid}.xml`, { decompress: false, responseType: 'buffer', + headers: { + Referer: link, + }, }); let danmakuText = danmakuResponse.body; @@ -34,14 +43,14 @@ module.exports = async (ctx) => { ctx.state.data = { title: `${videoName} 的 弹幕动态`, - link: `https://www.bilibili.com/video/av${aid}`, + link: link, description: `${videoName} 的 弹幕动态`, item: danmakuList.map((item) => ({ title: item.text, description: item.text, pubDate: new Date(item.p.split(',')[4] * 1000).toUTCString(), guid: `${cid}-${item.p.split(',')[4]}-${item.p.split(',')[7]}`, - link: `https://www.bilibili.com/video/av${aid}`, + link: link, })), }; }; diff --git a/lib/routes/bilibili/reply.js b/lib/routes/bilibili/reply.js index 1c4ddf3238..aed64e0910 100644 --- a/lib/routes/bilibili/reply.js +++ b/lib/routes/bilibili/reply.js @@ -26,7 +26,7 @@ module.exports = async (ctx) => { ctx.state.data = { title: `${name} 的 评论`, - link: `https://www.bilibili.com/video/av${aid}`, + link: link, description: `${name} 的评论`, item: data.map((item) => ({ title: `${item.member.uname} : ${item.content.message}`,