add bilibili video danmaku (#1478)

This commit is contained in:
qixingchen
2019-01-29 16:49:14 +08:00
committed by DIYgod
parent 9c3c2e8cdd
commit ba424e30a8
5 changed files with 59 additions and 0 deletions

View File

@@ -297,6 +297,8 @@ RSSHub 提供下列 API 接口:
<route name="视频评论" author="Qixingchen" example="/bilibili/video/reply/21669336" path="/bilibili/video/reply/:aid" :paramsDesc="['可在视频页 URL 中找到']"/>
<route name="视频弹幕" author="Qixingchen" example="/bilibili/video/danmaku/21669336/1" path="/bilibili/video/danmaku/:aid/:pid?" :paramsDesc="['视频AV号,可在视频页 URL 中找到','分P号,不填默认为1']"/>
<route name="link 公告" author="Qixingchen" example="/bilibili/link/news/live" path="/bilibili/link/news/:product" :paramsDesc="['公告分类, 包括 直播:live 小视频:vc 相簿:wh']"/>
#### 直播开播 <Author uid="Qixingchen"/>

View File

@@ -123,6 +123,7 @@ router.get('/bilibili/partion/ranking/:tid/:days?', require('./routes/bilibili/p
router.get('/bilibili/bangumi/:seasonid', require('./routes/bilibili/bangumi')); // 弃用
router.get('/bilibili/bangumi/media/:mediaid', require('./routes/bilibili/bangumi'));
router.get('/bilibili/video/reply/:aid', 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'));
router.get('/bilibili/live/search/:key/:order', require('./routes/bilibili/liveSearch'));

View File

@@ -102,4 +102,24 @@ module.exports = {
}
return name;
},
getCidFromAid: async (ctx, aid, pid) => {
const key = `bili-Cid-from-Aid-${aid}-${pid}`;
let cid = await ctx.cache.get(key);
if (!cid) {
const cidResponse = await axios({
method: 'get',
url: `https://api.bilibili.com/x/web-interface/view?aid=${aid}`,
headers: {
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, 24 * 60 * 60);
}
}
return cid;
},
};

View File

@@ -0,0 +1,35 @@
const cheerio = require('cheerio');
const cache = require('./cache');
const fetch = require('node-fetch');
module.exports = async (ctx) => {
const aid = ctx.params.aid;
const pid = Number(ctx.params.pid || 1);
const limit = 50;
const cid = await cache.getCidFromAid(ctx, aid, pid);
const videoName = await cache.getVideoNameFromAid(ctx, aid);
const danmakuResponse = await fetch(`https://comment.bilibili.com/${cid}.xml`);
const danmakuText = await danmakuResponse.text();
let danmakuList = [];
const $ = cheerio.load(danmakuText, { xmlMode: true });
$('d').each((index, item) => {
danmakuList.push({ p: $(item).attr('p'), text: $(item).text() });
});
danmakuList = danmakuList.reverse().slice(0, limit);
ctx.state.data = {
title: `${videoName} 的 弹幕动态`,
link: `https://www.bilibili.com/video/av${aid}`,
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}`,
})),
};
};

View File

@@ -67,6 +67,7 @@
"lru-cache": "5.1.1",
"luxon": "^1.7.1",
"markdown-it": "^8.4.2",
"node-fetch": "^2.3.0",
"path-to-regexp": "3.0.0",
"pidusage": "^2.0.17",
"plist": "^3.0.1",