feat: bilibili bvid /video/reply

This commit is contained in:
DIYgod
2020-04-28 15:15:51 +08:00
parent 158edd177a
commit 67bd2786bc
4 changed files with 39 additions and 16 deletions

View File

@@ -174,7 +174,7 @@ Tiny Tiny RSS 会给所有 iframe 元素添加 `sandbox="allow-scripts"` 属性
### 视频评论
<Route author="Qixingchen" example="/bilibili/video/reply/21669336" path="/bilibili/video/reply/:aid" :paramsDesc="['可在视频页 URL 中找到']"/>
<Route author="Qixingchen" example="/bilibili/video/reply/BV1vA411b7ip" path="/bilibili/video/reply/:bvid" :paramsDesc="['可在视频页 URL 中找到']"/>
### 视频弹幕

View File

@@ -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'));

View File

@@ -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;
},
};

View File

@@ -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}<br> ${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}`,
})),
};
};