From f4bbba1b89ec6b5641d53b2a0ad79b66c70faf41 Mon Sep 17 00:00:00 2001 From: Woshiluo Luo Date: Thu, 30 May 2019 11:59:02 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20Bilibii=20=E5=85=B3=E6=B3=A8=E4=B8=93?= =?UTF-8?q?=E6=A0=8F=20(#2260)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 增加Bilibili专栏 * Docs: Bilibili 关注专栏 * 使用 axios * 使用 axios --- docs/social-media.md | 10 ++++ lib/router.js | 1 + lib/routes/bilibili/followings_article.js | 57 +++++++++++++++++++++++ 3 files changed, 68 insertions(+) create mode 100644 lib/routes/bilibili/followings_article.js diff --git a/docs/social-media.md b/docs/social-media.md index 65b832b1d..810ce8dc7 100644 --- a/docs/social-media.md +++ b/docs/social-media.md @@ -182,6 +182,16 @@ pageClass: routes ::: +### 用户关注专栏 + + +::: warning 注意 + +用户动态需要 b 站登录后的 Cookie 值,所以只能自建,详情见部署页面的配置模块。 + +::: + + ### 直播开播 见 [#哔哩哔哩直播](/live.html#哔哩哔哩直播) diff --git a/lib/router.js b/lib/router.js index 29378c10f..3c0b8f7b2 100644 --- a/lib/router.js +++ b/lib/router.js @@ -41,6 +41,7 @@ router.get('/bilibili/topic/:topic', require('./routes/bilibili/topic')); router.get('/bilibili/audio/:id', require('./routes/bilibili/audio')); router.get('/bilibili/vsearch/:kw/:order?', require('./routes/bilibili/vsearch')); router.get('/bilibili/followings/video/:uid', require('./routes/bilibili/followings_video')); +router.get('/bilibili/followings/article/:uid', require('./routes/bilibili/followings_article')); // bangumi router.get('/bangumi/calendar/today', require('./routes/bangumi/calendar/today')); diff --git a/lib/routes/bilibili/followings_article.js b/lib/routes/bilibili/followings_article.js new file mode 100644 index 000000000..849986ed1 --- /dev/null +++ b/lib/routes/bilibili/followings_article.js @@ -0,0 +1,57 @@ +const axios = require('@/utils/axios'); +const cache = require('./cache'); +const config = require('@/config'); +const cheerio = require('cheerio'); + +async function get_bilibili_article_contain(url) { + const result = await axios.get(url); + + const $ = cheerio.load(result.data); + $('img').each(function(i, e) { + $(e).attr('src', $(e).attr('data-src')); + }); + + return $('.article-holder').html(); +} + +module.exports = async (ctx) => { + const uid = String(ctx.params.uid); + const name = await cache.getUsernameFromUID(ctx, uid); + + const cookie = config.bilibili.cookies[uid]; + if (cookie === undefined) { + throw Error('缺少对应uid的b站用户登录后的Cookie值'); + } + + const response = await axios({ + method: 'get', + url: `https://api.vc.bilibili.com/dynamic_svr/v1/dynamic_svr/dynamic_new?uid=${uid}&type=64`, + headers: { + Referer: `https://space.bilibili.com/${uid}/`, + Cookie: cookie, + }, + }); + const cards = response.data.data.cards; + + const out = await Promise.all( + cards.map(async (card) => { + const card_data = JSON.parse(card.card); + const link = `http://www.bilibili.com/read/cv${card_data.id}`; + + const item = { + title: card_data.title, + description: await ctx.cache.tryGet(link, async () => await get_bilibili_article_contain(link)), + pubDate: new Date(card_data.publish_time * 1000).toUTCString(), + link: link, + author: card.desc.user_profile.info.uname, + }; + return Promise.resolve(item); + }) + ); + + ctx.state.data = { + title: `${name} 关注专栏动态`, + link: `https://t.bilibili.com/?tab=64`, + item: out, + }; +};