From cbf016b76abccc1aa7bfcbe2d06e51ccc9cda6c8 Mon Sep 17 00:00:00 2001 From: JimenezLi <75196426+JimenezLi@users.noreply.github.com> Date: Sun, 27 Aug 2023 22:31:57 +0800 Subject: [PATCH] feat(route): add bilibili watchlater (#13128) * feat(route): add bilibili watchlater * docs(route): add docs for /bilibili/watchlater * Update lib/v2/bilibili/watchlater.js --------- --- lib/v2/bilibili/maintainer.js | 1 + lib/v2/bilibili/router.js | 1 + lib/v2/bilibili/watchlater.js | 44 +++++++++++++++++++++++++++++ website/docs/routes/social-media.md | 12 ++++++++ 4 files changed, 58 insertions(+) create mode 100644 lib/v2/bilibili/watchlater.js diff --git a/lib/v2/bilibili/maintainer.js b/lib/v2/bilibili/maintainer.js index fefdae01a..8fa28bc0d 100644 --- a/lib/v2/bilibili/maintainer.js +++ b/lib/v2/bilibili/maintainer.js @@ -40,5 +40,6 @@ module.exports = { '/video/page/:bvid/:disableEmbed?': ['sxzz'], '/video/reply/:bvid': ['Qixingchen'], '/vsearch/:kw/:order?/:disableEmbed?/:tid?': ['Symty'], + '/watchlater/:uid/:disableEmbed?': ['JimenezLi'], '/weekly/:disableEmbed?': ['ttttmr'], }; diff --git a/lib/v2/bilibili/router.js b/lib/v2/bilibili/router.js index 72601dd8d..2e27b72b6 100644 --- a/lib/v2/bilibili/router.js +++ b/lib/v2/bilibili/router.js @@ -40,5 +40,6 @@ module.exports = (router) => { router.get('/video/page/:bvid/:disableEmbed?', require('./page')); router.get('/video/reply/:bvid', require('./reply')); router.get('/vsearch/:kw/:order?/:disableEmbed?/:tid?', require('./vsearch')); + router.get('/watchlater/:uid/:disableEmbed?', require('./watchlater')); router.get('/weekly/:disableEmbed?', require('./weekly_recommend')); }; diff --git a/lib/v2/bilibili/watchlater.js b/lib/v2/bilibili/watchlater.js new file mode 100644 index 000000000..dac765a4e --- /dev/null +++ b/lib/v2/bilibili/watchlater.js @@ -0,0 +1,44 @@ +const got = require('@/utils/got'); +const cache = require('./cache'); +const config = require('@/config').value; +const utils = require('./utils'); +const { parseDate } = require('@/utils/parse-date'); + +module.exports = async (ctx) => { + const uid = ctx.params.uid; + const disableEmbed = ctx.params.disableEmbed; + const name = await cache.getUsernameFromUID(ctx, uid); + + const cookie = config.bilibili.cookies[uid]; + if (cookie === undefined) { + throw Error('缺少对应 uid 的 Bilibili 用户登录后的 Cookie 值'); + } + + const response = await got({ + method: 'get', + url: `https://api.bilibili.com/x/v2/history/toview`, + headers: { + Referer: `https://space.bilibili.com/${uid}/`, + Cookie: cookie, + }, + }); + if (response.data.code) { + const message = response.data.code === -6 ? '对应 uid 的 Bilibili 用户的 Cookie 已过期' : response.data.message; + throw Error(`Error code ${response.data.code}: ${message}`); + } + const list = response.data.data.list; + + const out = list.map((item) => ({ + title: item.title, + description: `${item.desc}

在稍后再看列表中查看${!disableEmbed ? `

${utils.iframe(item.aid)}` : ''}
`, + pubDate: parseDate(item.add_at * 1000), + link: item.pubdate > utils.bvidTime && item.bvid ? `https://www.bilibili.com/video/${item.bvid}` : `https://www.bilibili.com/video/av${item.aid}`, + author: item.owner.name, + })); + + ctx.state.data = { + title: `${name} 稍后再看`, + link: 'https://www.bilibili.com/watchlater#/list', + item: out, + }; +}; diff --git a/website/docs/routes/social-media.md b/website/docs/routes/social-media.md index 31e4050fe..f1665627d 100644 --- a/website/docs/routes/social-media.md +++ b/website/docs/routes/social-media.md @@ -298,6 +298,18 @@ UP 主关注用户现在需要 b 站登录后的 Cookie 值,所以只能自建 +### 用户稍后再看 {#bilibili-yong-hu-shao-hou-zai-kan} + + + +:::caution + +用户稍后再看需要 b 站登录后的 Cookie 值,所以只能自建,详情见部署页面的配置模块。 + +::: + + + ### 直播开播 {#bilibili-zhi-bo-kai-bo} 见 [#哔哩哔哩直播](/routes/live#bi-li-bi-li-zhi-bo)