Skip to content

Commit

Permalink
fix(route): 修复 Bilibili 频道链接,并支持 ctype=1 (#7913)
Browse files Browse the repository at this point in the history
  • Loading branch information
weirongxu authored Aug 12, 2021
1 parent ce15d05 commit a7c483c
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 13 deletions.
2 changes: 1 addition & 1 deletion docs/social-media.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ Tiny Tiny RSS 会给所有 iframe 元素添加 `sandbox="allow-scripts"` 属性

### UP 主频道

<Route author="HenryQW" example="/bilibili/user/channel/142821407/49017" path="/bilibili/user/channel/:uid/:cid/:disableEmbed?" :paramsDesc="['用户 id, 可在 UP 主主页中找到', '频道 id, 可在频道的 URL 中找到', '默认为开启内嵌视频, 任意值为关闭']" anticrawler="1" radar="1" rssbud="1"/>
<Route author="HenryQW" example="/bilibili/user/channel/142821407/49017" path="/bilibili/user/channel/:uid/:cid/:disableEmbed?" :paramsDesc="['用户 id, 可在 UP 主主页中找到', '频道 id, 可在频道的 URL 中找到', '默认为开启内嵌视频, 任意值为关闭']" anticrawler="1"/>

### UP 主默认收藏夹

Expand Down
52 changes: 40 additions & 12 deletions lib/routes/bilibili/userChannel.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,35 @@ const cache = require('./cache');
const utils = require('./utils');

module.exports = async (ctx) => {
const cid = ctx.params.cid;
const uid = ctx.params.uid;
const uid = Number.parseInt(ctx.params.uid);
const cid = Number.parseInt(ctx.params.cid);
const disableEmbed = ctx.params.disableEmbed;

// 获取频道的 ctype
const channelsLink = `https://api.bilibili.com/x/space/channel/list?mid=${uid}`;
const channelsResponse = await ctx.cache.tryGet(channelsLink, async () => {
const response = await got.get(channelsLink, {
headers: {
Referer: `https://space.bilibili.com/${uid}/`,
},
});
return response.data;
});
const channelInfo = channelsResponse.data.list.find((channel) => channel.cid === cid);
if (!channelInfo) {
throw new Error('无法获取该频道信息,请报告这个问题');
}
const ctype = channelInfo.is_live_playback ? 1 : 0;

const link = `https://space.bilibili.com/${uid}/detail?cid=${cid}&ctype=${ctype}`;
const userName = await cache.getUsernameFromUID(ctx, uid);
const host = `https://api.bilibili.com/x/space/channel/video?mid=${uid}&cid=${cid}&pn=1&ps=10&order=0`;
const host = `https://api.bilibili.com/x/space/channel/video?mid=${uid}&cid=${cid}&pn=1&ps=10&order=0&ctype=${ctype}`;

const response = await got({
method: 'get',
url: host,
headers: {
Referer: `https://space.bilibili.com/${uid}/`,
Referer: link,
},
});

Expand All @@ -31,15 +49,25 @@ module.exports = async (ctx) => {

ctx.state.data = {
title: `${userName} 的 bilibili 频道 ${channelName}`,
link: `https://space.bilibili.com/${uid}/#/channel/detail?cid=${cid}`,
link,
description: data.name,

item: data.archives.map((item) => ({
title: item.title,
description: `${item.desc}${!disableEmbed ? `<br><br>${utils.iframe(item.aid)}` : ''}<br><img src="${item.pic}">`,
pubDate: new Date(item.pubdate * 1000).toUTCString(),
link: item.pubdate > utils.bvidTime && item.bvid ? `https://www.bilibili.com/video/${item.bvid}` : `https://www.bilibili.com/video/av${item.aid}`,
author: userName,
})),
item: data.archives.map((item) => {
const descList = [];
if (item.desc) {
descList.push(item.desc);
}
if (!disableEmbed) {
descList.push(`${item.desc ? '<br>' : ''}${utils.iframe(item.aid)}`);
}
descList.push(`<img src="${item.pic}">`);
return {
title: item.title,
description: descList.join('<br>'),
pubDate: new Date(item.pubdate * 1000),
link: item.pubdate > utils.bvidTime && item.bvid ? `https://www.bilibili.com/video/${item.bvid}` : `https://www.bilibili.com/video/av${item.aid}`,
author: userName,
};
}),
};
};

1 comment on commit a7c483c

@vercel
Copy link

@vercel vercel bot commented on a7c483c Aug 12, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.