Skip to content

Commit

Permalink
feat(route): Bilibili UP主动态 参数格式更新 & 增加显示专栏全文参数 (DIYgod#13109)
Browse files Browse the repository at this point in the history
* feat(route): /bilibili/user(followings)/dynamic change to routeParams

* feat(route): Add displayArticle parameter

* small change of uid

* Original link can click to jump
  • Loading branch information
JimenezLi authored Aug 25, 2023
1 parent 39ffd34 commit b08045c
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 58 deletions.
19 changes: 2 additions & 17 deletions lib/v2/bilibili/article.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const got = require('@/utils/got');
const cache = require('./cache');
const cheerio = require('cheerio');

const { parseDate } = require('@/utils/parse-date');
module.exports = async (ctx) => {
const uid = ctx.params.uid;
Expand All @@ -18,22 +18,7 @@ module.exports = async (ctx) => {
const description = `${name} 的 bilibili 专栏`;
const item = await Promise.all(
data.articles.map(async (item) => {
const art_url = `https://www.bilibili.com/read/cv${item.id}`;
const itemData = await ctx.cache.tryGet(
art_url,
async () =>
(
await got({
method: 'get',
url: art_url,
headers: {
Referer: `https://space.bilibili.com/${uid}/`,
},
})
).data
);
const content = cheerio.load(itemData);
const eDescription = content('#read-article-holder').html();
const { url: art_url, description: eDescription } = await cache.getArticleDataFromCvid(ctx, item.id, uid);
const publishDate = parseDate(item.publish_time * 1000);
const single = {
title: item.title,
Expand Down
20 changes: 20 additions & 0 deletions lib/v2/bilibili/cache.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const got = require('@/utils/got');
const utils = require('./utils');
const cheerio = require('cheerio');

module.exports = {
getCookie: (ctx) => {
Expand Down Expand Up @@ -164,4 +165,23 @@ module.exports = {
}
return aid;
},
getArticleDataFromCvid: async (ctx, cvid, uid) => {
const url = `https://www.bilibili.com/read/cv${cvid}`;
const data = await ctx.cache.tryGet(
url,
async () =>
(
await got({
method: 'get',
url,
headers: {
Referer: `https://space.bilibili.com/${uid}/`,
},
})
).data
);
const $ = cheerio.load(data);
const description = $('#read-article-holder').html();
return { url, description };
},
};
53 changes: 37 additions & 16 deletions lib/v2/bilibili/dynamic.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ const got = require('@/utils/got');
const JSONbig = require('json-bigint');
const utils = require('./utils');
const { parseDate } = require('@/utils/parse-date');
const { fallback, queryToBoolean } = require('@/utils/readable-social');
const querystring = require('querystring');
const cache = require('./cache');

/**
@by CaoMeiYouRen 2020-05-05 添加注释
Expand Down Expand Up @@ -66,8 +69,11 @@ const { parseDate } = require('@/utils/parse-date');

module.exports = async (ctx) => {
const uid = ctx.params.uid;
const showEmoji = ctx.params.showEmoji || '0';
const disableEmbed = ctx.params.disableEmbed;
const routeParams = querystring.parse(ctx.params.routeParams);

const showEmoji = fallback(undefined, queryToBoolean(routeParams.showEmoji), false);
const disableEmbed = fallback(undefined, queryToBoolean(routeParams.disableEmbed), false);
const displayArticle = fallback(undefined, queryToBoolean(routeParams.displayArticle), false);

const response = await got({
method: 'get',
Expand All @@ -79,12 +85,8 @@ module.exports = async (ctx) => {
});
const data = JSONbig.parse(response.body).data.cards;

ctx.state.data = {
title: `${data[0].desc.user_profile.info.uname} 的 bilibili 动态`,
link: `https://space.bilibili.com/${uid}/dynamic`,
description: `${data[0].desc.user_profile.info.uname} 的 bilibili 动态`,
image: data[0].desc.user_profile.info.face,
item: data.map((item) => {
const items = await Promise.all(
data.map(async (item) => {
const getTitle = (data) => (data ? data.title || data.description || data.content || (data.vest && data.vest.content) || '' : '');
const getDes = (data) =>
data.dynamic || data.desc || data.description || data.content || data.summary || (data.vest && data.vest.content) + (data.sketch && `<br>${data.sketch.title}<br>${data.sketch.desc_text}`) || data.intro || '';
Expand Down Expand Up @@ -159,7 +161,7 @@ module.exports = async (ctx) => {
let data_content = getDes(data);
// 换行处理
data_content = data_content.replace(new RegExp('\r\n', 'g'), '<br>').replace(new RegExp('\n', 'g'), '<br>');
if (item.display.emoji_info && showEmoji !== '0') {
if (item.display.emoji_info && showEmoji) {
const emoji = item.display.emoji_info.emoji_details;
emoji.forEach((item) => {
data_content = data_content.replace(
Expand All @@ -172,27 +174,38 @@ module.exports = async (ctx) => {
if (!data) {
return '';
}
let url;
if (data.aid) {
return `<br>视频地址:https://www.bilibili.com/video/av${data.aid}`;
url = `https://www.bilibili.com/video/av${data.aid}`;
return `<br>视频地址:<a href=${url}>${url}</a>`;
}
if (data.image_urls) {
return `<br>专栏地址:https://www.bilibili.com/read/cv${data.id}`;
url = `https://www.bilibili.com/read/cv${data.id}`;
return `<br>专栏地址:<a href=${url}>${url}</a>`;
}
if (data.upper) {
return `<br>音频地址:https://www.bilibili.com/audio/au${data.id}`;
url = `https://www.bilibili.com/audio/au${data.id}`;
return `<br>音频地址:<a href=${url}>${url}</a>`;
}
if (data.roomid) {
return `<br>直播间地址:https://live.bilibili.com/${data.roomid}`;
url = `https://live.bilibili.com/${data.roomid}`;
return `<br>直播间地址:<a href=${url}>${url}</a>`;
}
if (data.sketch) {
return `<br>活动地址:${data.sketch.target_url}`;
url = data.sketch.target_url;
return `<br>活动地址:<a href=${url}>${url}</a>`;
}
if (data.url) {
return `<br>地址:${data.url}`;
url = data.url;
return `<br>地址:<a href=${url}>${url}</a>`;
}
return '';
};

if (data.image_urls && displayArticle) {
data_content = (await cache.getArticleDataFromCvid(ctx, data.id, uid)).description;
}

return {
title: getTitle(data),
description: (() => {
Expand All @@ -205,6 +218,14 @@ module.exports = async (ctx) => {
pubDate: item.desc?.timestamp ? parseDate(item.desc.timestamp, 'X') : null,
link,
};
}),
})
);

ctx.state.data = {
title: `${data[0].desc.user_profile.info.uname} 的 bilibili 动态`,
link: `https://space.bilibili.com/${uid}/dynamic`,
description: `${data[0].desc.user_profile.info.uname} 的 bilibili 动态`,
image: data[0].desc.user_profile.info.face,
item: items,
};
};
11 changes: 1 addition & 10 deletions lib/v2/bilibili/followings_article.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const got = require('@/utils/got');
const cache = require('./cache');
const config = require('@/config').value;
const cheerio = require('cheerio');

module.exports = async (ctx) => {
const uid = String(ctx.params.uid);
Expand All @@ -28,15 +27,7 @@ module.exports = async (ctx) => {
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 description = await ctx.cache.tryGet(link, async () => {
const result = await got.get(link);

const $ = cheerio.load(result.data);

return $('.article-holder').html();
});
const { url: link, description } = await cache.getArticleDataFromCvid(ctx, card_data.id, uid);

const item = {
title: card_data.title,
Expand Down
32 changes: 23 additions & 9 deletions lib/v2/bilibili/followings_dynamic.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,17 @@ const cache = require('./cache');
const config = require('@/config').value;
const utils = require('./utils');
const JSONbig = require('json-bigint');
const { fallback, queryToBoolean } = require('@/utils/readable-social');
const querystring = require('querystring');

module.exports = async (ctx) => {
const uid = String(ctx.params.uid);
const showEmoji = ctx.params.showEmoji || '0';
const disableEmbed = ctx.params.disableEmbed;
const routeParams = querystring.parse(ctx.params.routeParams);

const showEmoji = fallback(undefined, queryToBoolean(routeParams.showEmoji), false);
const disableEmbed = fallback(undefined, queryToBoolean(routeParams.disableEmbed), false);
const displayArticle = fallback(undefined, queryToBoolean(routeParams.displayArticle), false);

const name = await cache.getUsernameFromUID(ctx, uid);

const cookie = config.bilibili.cookies[uid];
Expand All @@ -28,11 +34,8 @@ module.exports = async (ctx) => {
}
const data = JSONbig.parse(response.body).data.cards;

ctx.state.data = {
title: `${name} 关注的动态`,
link: `https://t.bilibili.com`,
description: `${name} 关注的动态`,
item: data.map((item) => {
const items = await Promise.all(
data.map(async (item) => {
const getTitle = (data) => (data ? data.title || data.description || data.content || (data.vest && data.vest.content) || '' : '');
const getDes = (data) =>
data.dynamic || data.desc || data.description || data.content || data.summary || (data.vest && data.vest.content) + (data.sketch && `<br>${data.sketch.title}<br>${data.sketch.desc_text}`) || data.intro || '';
Expand Down Expand Up @@ -104,7 +107,7 @@ module.exports = async (ctx) => {

// emoji
let data_content = getDes(data);
if (item.display && item.display.emoji_info && showEmoji !== '0') {
if (item.display && item.display.emoji_info && showEmoji) {
const emoji = item.display.emoji_info.emoji_details;
emoji.forEach((item) => {
data_content = data_content.replace(
Expand All @@ -119,6 +122,10 @@ module.exports = async (ctx) => {
author = item.desc.user_profile.info.uname;
}

if (data.image_urls && displayArticle) {
data_content = (await cache.getArticleDataFromCvid(ctx, data.id, uid)).description;
}

return {
title: getTitle(data),
author,
Expand All @@ -133,6 +140,13 @@ module.exports = async (ctx) => {
pubDate: new Date(item.desc?.timestamp * 1000).toUTCString(),
link,
};
}),
})
);

ctx.state.data = {
title: `${name} 关注的动态`,
link: `https://t.bilibili.com`,
description: `${name} 关注的动态`,
item: items,
};
};
4 changes: 2 additions & 2 deletions lib/v2/bilibili/maintainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module.exports = {
'/channel/:channelid/hot/:disableEmbed?': ['3401797899'],
'/fav/:uid/:fid/:disableEmbed?': ['Qixingchen'],
'/followings/article/:uid': ['woshiluo'],
'/followings/dynamic/:uid/:showEmoji?/:disableEmbed?': ['TigerCubDen'],
'/followings/dynamic/:uid/:routeParams?': ['TigerCubDen', 'JimenezLi'],
'/followings/video/:uid/:disableEmbed?': ['LogicJake'],
'/hot-search': ['CaoMeiYouRen'],
'/link/news/:product': ['Qixingchen'],
Expand All @@ -30,7 +30,7 @@ module.exports = {
'/user/channel/:uid/:sid/:disableEmbed?': ['weirongxu'],
'/user/coin/:uid/:disableEmbed?': ['DIYgod'],
'/user/collection/:uid/:sid/:disableEmbed?': ['shininome'],
'/user/dynamic/:uid/:showEmoji?/:disableEmbed?': ['DIYgod', 'zytomorrow'],
'/user/dynamic/:uid/:routeParams?': ['DIYgod', 'zytomorrow', 'JimenezLi'],
'/user/fav/:uid/:disableEmbed?': ['DIYgod'],
'/user/followers/:uid/:loginUid': ['Qixingchen'],
'/user/followings/:uid/:loginUid': ['Qixingchen'],
Expand Down
4 changes: 2 additions & 2 deletions lib/v2/bilibili/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module.exports = (router) => {
router.get('/channel/:channelid/hot/:disableEmbed?', require('./channel'));
router.get('/fav/:uid/:fid/:disableEmbed?', require('./fav'));
router.get('/followings/article/:uid', require('./followings_article'));
router.get('/followings/dynamic/:uid/:showEmoji?/:disableEmbed?', require('./followings_dynamic'));
router.get('/followings/dynamic/:uid/:routeParams?', require('./followings_dynamic'));
router.get('/followings/video/:uid/:disableEmbed?', require('./followings_video'));
router.get('/link/news/:product', require('./linkNews'));
router.get('/hot-search', require('./hotSearch'));
Expand All @@ -30,7 +30,7 @@ module.exports = (router) => {
router.get('/user/channel/:uid/:sid/:disableEmbed?', require('./userChannel'));
router.get('/user/coin/:uid/:disableEmbed?', require('./coin'));
router.get('/user/collection/:uid/:sid/:disableEmbed?', require('./userCollection'));
router.get('/user/dynamic/:uid/:showEmoji?/:disableEmbed?', require('./dynamic'));
router.get('/user/dynamic/:uid/:routeParams?', require('./dynamic'));
router.get('/user/fav/:uid/:disableEmbed?', require('./userFav'));
router.get('/user/followers/:uid/:loginUid', require('./followers'));
router.get('/user/followings/:uid/:loginUid', require('./followings'));
Expand Down
14 changes: 12 additions & 2 deletions website/docs/routes/social-media.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,17 @@ Tiny Tiny RSS 会给所有 iframe 元素添加 `sandbox="allow-scripts"` 属性

### UP 主动态 {#bilibili-up-zhu-dong-tai}

<Route author="DIYgod zytomorrow" example="/bilibili/user/dynamic/2267573" path="/bilibili/user/dynamic/:uid/:showEmoji?/:disableEmbed?" paramsDesc={['用户 id, 可在 UP 主主页中找到', '显示或隐藏表情图片,默认值为 0 隐藏,其他值为显示', '默认为开启内嵌视频, 任意值为关闭']} radar="1" rssbud="1"/>
<Route author="DIYgod zytomorrow JimenezLi" example="/bilibili/user/dynamic/2267573" path="/bilibili/user/dynamic/:uid/:routeParams?" paramsDesc={['用户 id, 可在 UP 主主页中找到', '额外参数;请参阅以下说明和表格']} radar="1" rssbud="1">

|| 含义 | 接受的值 | 默认值 |
| -- | ---- | ------- | ------ |
| showEmoji | 显示或隐藏表情图片 | 0/1/true/false | false |
| disableEmbed | 关闭内嵌视频 | 0/1/true/false | false |
| displayArticle | 专栏显示全文 | 0/1/true/false | false |

用例:`/bilibili/user/dynamic/2267573/showEmoji=1&disableEmbed=1&displayArticle=1`

</Route>

### UP 主频道的合集 {#bilibili-up-zhu-pin-dao-de-he-ji}

Expand Down Expand Up @@ -246,7 +256,7 @@ UP 主关注用户现在需要 b 站登录后的 Cookie 值,所以只能自建

### 用户关注动态 {#bilibili-yong-hu-guan-zhu-dong-tai}

<Route author="TigerCubDen" example="/bilibili/followings/dynamic/109937383" path="/bilibili/followings/dynamic/:uid/:showEmoji?/:disableEmbed?" paramsDesc={['用户 id', '显示或隐藏表情图片,默认值为 0 隐藏,其他值为显示', '默认为开启内嵌视频, 任意值为关闭']} selfhost="1">
<Route author="TigerCubDen JimenezLi" example="/bilibili/followings/dynamic/109937383" path="/bilibili/followings/dynamic/:uid/:routeParams?" paramsDesc={['用户 id', '额外参数;请参阅 [#UP 主动态](#bilibili-up-zhu-dong-tai) 的说明和表格']} selfhost="1">

:::caution

Expand Down

0 comments on commit b08045c

Please sign in to comment.