feat: more readable weibo (#6052)
This commit is contained in:
Binary file not shown.
|
After Width: | Height: | Size: 613 KiB |
+35
-3
@@ -898,9 +898,35 @@ rule
|
||||
|
||||
## 微博
|
||||
|
||||
对于微博内容,在 `routeParams` 参数中以 query string 格式指定选项,可以控制输出的样式
|
||||
|
||||
| 键 | 含义 | 接受的值 | 默认值 |
|
||||
| -------------------------- | -------------------------------------------------------------- | -------------- | ------ |
|
||||
| readable | 是否开启细节排版可读性优化 | 0/1/true/false | false |
|
||||
| authorNameBold | 是否加粗作者名字 | 0/1/true/false | false |
|
||||
| showAuthorInTitle | 是否在标题处显示作者 | 0/1/true/false | false |
|
||||
| showAuthorInDesc | 是否在正文处显示作者 | 0/1/true/false | false |
|
||||
| showAuthorAvatarInDesc | 是否在正文处显示作者头像(若阅读器会提取正文图片,不建议开启) | 0/1/true/false | false |
|
||||
| showEmojiForRetweet | 显示 “🔁” 取代 “转发” 两个字 | 0/1/true/false | false |
|
||||
| showRetweetTextInTitle | 在标题出显示转发评论(置为 false 则在标题只显示被转发微博) | 0/1/true/false | true |
|
||||
| addLinkForPics | 为图片添加可点击的链接 | 0/1/true/false | false |
|
||||
| showTimestampInDescription | 在正文处显示被转发微博的时间戳 | 0/1/true/false | false |
|
||||
| widthOfPics | 微博配图宽(生效取决于阅读器) | 不指定 / 数字 | 不指定 |
|
||||
| heightOfPics | 微博配图高(生效取决于阅读器) | 不指定 / 数字 | 不指定 |
|
||||
| sizeOfAuthorAvatar | 作者头像大小 | 数字 | 48 |
|
||||
| displayVideo | 是否直接显示微博视频,只在博主 RSS 中有效 | 0/1/true/false | true |
|
||||
|
||||
指定更多与默认值不同的参数选项可以改善 RSS 的可读性,如
|
||||
|
||||
https://rsshub.app/weibo/user/1642909335/readable=1&authorNameBold=1&showAuthorInTitle=1&showAuthorInDesc=1&showAuthorAvatarInDesc=1&showEmojiForRetweet=1&showRetweetTextInTitle=0&addLinkForPics=1&showTimestampInDescription=1&showTimestampInDescription=1&heightOfPics=150
|
||||
|
||||
的效果为
|
||||
|
||||
<img src="/readable-weibo.png" alt="微博小秘书的可读微博 RSS">
|
||||
|
||||
### 博主
|
||||
|
||||
<Route author="DIYgod iplusx" example="/weibo/user/1195230310" path="/weibo/user/:uid/:displayVideo?" :paramsDesc="['用户 id, 博主主页打开控制台执行 `$CONFIG.oid` 获取', '是否直接显示微博视频, 缺省 `1` 显示, 若不需要显示则填 `0` ']" anticrawler="1" radar="1">
|
||||
<Route author="DIYgod iplusx" example="/weibo/user/1195230310" path="/weibo/user/:uid/:routeParams?" :paramsDesc="['用户 id, 博主主页打开控制台执行 `$CONFIG.oid` 获取', '额外参数;请参阅上面的说明和表格;特别地,当 `routeParams=1` 时开启微博视频显示']" anticrawler="1" radar="1">
|
||||
|
||||
部分博主仅登录可见,不支持订阅,可以通过打开 `https://m.weibo.cn/u/:uid` 验证
|
||||
|
||||
@@ -908,7 +934,7 @@ rule
|
||||
|
||||
### 关键词
|
||||
|
||||
<Route author="DIYgod" example="/weibo/keyword/DIYgod" path="/weibo/keyword/:keyword" :paramsDesc="['你想订阅的微博关键词']" anticrawler="1" radar="1" rssbud="1"/>
|
||||
<Route author="DIYgod" example="/weibo/keyword/DIYgod" path="/weibo/keyword/:keyword/:routeParams?" :paramsDesc="['你想订阅的微博关键词', '额外参数;请参阅上面的说明和表格']" anticrawler="1" radar="1" rssbud="1"/>
|
||||
|
||||
### 热搜榜
|
||||
|
||||
@@ -916,10 +942,16 @@ rule
|
||||
|
||||
### 超话
|
||||
|
||||
<Route author="zengxs" example="/weibo/super_index/1008084989d223732bf6f02f75ea30efad58a9" path="/weibo/super_index/:id" :paramsDesc="['超话ID']" anticrawler="1" radar="1" rssbud="1"/>
|
||||
<Route author="zengxs" example="/weibo/super_index/1008084989d223732bf6f02f75ea30efad58a9" path="/weibo/super_index/:id/:routeParams?" :paramsDesc="['超话ID', '额外参数;请参阅上面的说明和表格']" anticrawler="1" radar="1" rssbud="1"/>
|
||||
|
||||
### 个人时间线
|
||||
|
||||
::: warning 注意
|
||||
|
||||
个人时间线暂不支持改善可读性。
|
||||
|
||||
:::
|
||||
|
||||
<Route author="zytomorrow DIYgod" example="/weibo/timeline/3306934123" path="/weibo/timeline/:uid/:feature?" :paramsDesc="['用户的uid', ' 过滤类型ID,0:全部、1:原创、2:图片、3:视频、4:音乐,默认为0。']" anticrawler="1">
|
||||
|
||||
::: warning 注意
|
||||
|
||||
+3
-3
@@ -93,10 +93,10 @@ router.get('/bangumi/subject/:id', require('./routes/bangumi/subject'));
|
||||
router.get('/bangumi/user/blog/:id', require('./routes/bangumi/user/blog'));
|
||||
|
||||
// 微博
|
||||
router.get('/weibo/user/:uid/:displayVideo?', require('./routes/weibo/user'));
|
||||
router.get('/weibo/keyword/:keyword', require('./routes/weibo/keyword'));
|
||||
router.get('/weibo/user/:uid/:routeParams?', require('./routes/weibo/user'));
|
||||
router.get('/weibo/keyword/:keyword/:routeParams?', require('./routes/weibo/keyword'));
|
||||
router.get('/weibo/search/hot', require('./routes/weibo/search/hot'));
|
||||
router.get('/weibo/super_index/:id', require('./routes/weibo/super_index'));
|
||||
router.get('/weibo/super_index/:id/:routeParams?', require('./routes/weibo/super_index'));
|
||||
router.get('/weibo/oasis/user/:userid', require('./routes/weibo/oasis/user'));
|
||||
|
||||
// 贴吧
|
||||
|
||||
@@ -22,10 +22,19 @@ module.exports = async (ctx) => {
|
||||
link: `http://s.weibo.com/weibo/${encodeURIComponent(keyword)}&b=1&nodup=1`,
|
||||
description: `又有人在微博提到${keyword}了`,
|
||||
item: data.map((item) => {
|
||||
const title = item.mblog.text.replace(/<img.*?>/g, '[图片]').replace(/<.*?>/g, '');
|
||||
item.mblog.created_at = date(item.mblog.created_at, 8);
|
||||
if (item.mblog.retweeted_status && item.mblog.retweeted_status.created_at) {
|
||||
item.mblog.retweeted_status.created_at = date(item.mblog.retweeted_status.created_at, 8);
|
||||
}
|
||||
const formatExtended = weiboUtils.formatExtended(ctx, item.mblog, {
|
||||
showAuthorInTitle: true,
|
||||
showAuthorInDesc: true,
|
||||
});
|
||||
const title = formatExtended.title;
|
||||
const description = formatExtended.description;
|
||||
return {
|
||||
title: `${item.mblog.user.screen_name}: ${title}`,
|
||||
description: `${item.mblog.user.screen_name}: ${weiboUtils.format(item.mblog)}`,
|
||||
title: title,
|
||||
description: description,
|
||||
pubDate: date(item.mblog.created_at, 8),
|
||||
link: `https://weibo.com/${item.mblog.user.id}/${item.mblog.bid}`,
|
||||
};
|
||||
|
||||
@@ -29,9 +29,11 @@ module.exports = async (ctx) => {
|
||||
for (const mblogCard of card.card_group) {
|
||||
if (mblogCard.card_type === '9' && 'mblog' in mblogCard) {
|
||||
const mblog = mblogCard.mblog;
|
||||
const desc = weiboUtils.format(mblog);
|
||||
const formatExtended = weiboUtils.formatExtended(ctx, mblog);
|
||||
const title = formatExtended.title;
|
||||
const desc = formatExtended.description;
|
||||
resultItems.push({
|
||||
title: desc.replace(/<img.*?>/g, '[图片]').replace(/<.*?>/g, ''),
|
||||
title: title,
|
||||
description: desc,
|
||||
author: mblog.user.screen_name,
|
||||
link: `https://weibo.com/${mblog.user.id}/${mblog.bid}`,
|
||||
|
||||
@@ -1,10 +1,20 @@
|
||||
const querystring = require('querystring');
|
||||
const got = require('@/utils/got');
|
||||
const weiboUtils = require('./utils');
|
||||
const date = require('@/utils/date');
|
||||
const { fallback, queryToBoolean } = require('@/utils/readable-social');
|
||||
|
||||
module.exports = async (ctx) => {
|
||||
const uid = ctx.params.uid;
|
||||
const displayVideo = ctx.params.displayVideo || '1';
|
||||
let displayVideo = '1';
|
||||
if (ctx.params.routeParams) {
|
||||
if (ctx.params.routeParams === '1' || ctx.params.routeParams === '0') {
|
||||
displayVideo = ctx.params.routeParams;
|
||||
} else {
|
||||
const routeParams = querystring.parse(ctx.params.routeParams);
|
||||
displayVideo = fallback(undefined, queryToBoolean(routeParams.displayVideo), true) ? '1' : '0';
|
||||
}
|
||||
}
|
||||
|
||||
const containerResponse = await got({
|
||||
method: 'get',
|
||||
@@ -49,6 +59,10 @@ module.exports = async (ctx) => {
|
||||
const isDataOK = data !== undefined && data.text;
|
||||
if (isDataOK) {
|
||||
item.mblog.text = data.text;
|
||||
item.mblog.created_at = data.created_at;
|
||||
if (item.mblog.retweeted_status && data.retweeted_status) {
|
||||
item.mblog.retweeted_status.created_at = data.retweeted_status.created_at;
|
||||
}
|
||||
}
|
||||
// 转发的长微博处理
|
||||
const retweet = item.mblog.retweeted_status;
|
||||
@@ -60,8 +74,10 @@ module.exports = async (ctx) => {
|
||||
}
|
||||
|
||||
const link = `https://weibo.com/${uid}/${item.mblog.bid}`;
|
||||
let description = weiboUtils.format(item.mblog);
|
||||
const title = description.replace(/<img[\s\S]*?>/g, '[图片]').replace(/<.*?>/g, '');
|
||||
const formatExtended = weiboUtils.formatExtended(ctx, item.mblog);
|
||||
let description = formatExtended.description;
|
||||
const formattedTitle = formatExtended.title;
|
||||
const title = formattedTitle;
|
||||
const pubDate = isDataOK ? new Date(data.created_at).toUTCString() : date(item.mblog.created_at, 8);
|
||||
|
||||
// 视频的处理
|
||||
@@ -74,6 +90,7 @@ module.exports = async (ctx) => {
|
||||
description: description,
|
||||
link: link,
|
||||
pubDate: pubDate,
|
||||
author: item.mblog.user.screen_name,
|
||||
};
|
||||
return Promise.resolve(it);
|
||||
})
|
||||
|
||||
+167
-19
@@ -1,42 +1,190 @@
|
||||
const querystring = require('querystring');
|
||||
const got = require('@/utils/got');
|
||||
const { fallback, queryToBoolean, queryToInteger } = require('@/utils/readable-social');
|
||||
|
||||
const weiboUtils = {
|
||||
format: (status) => {
|
||||
formatExtended: (ctx, status, params = {}, picsPrefixes = []) => {
|
||||
// undefined and strings like "1" is also safely parsed, so no if branch is needed
|
||||
const routeParams = querystring.parse(ctx.params.routeParams);
|
||||
|
||||
const mergedParams = {
|
||||
readable: fallback(params.readable, queryToBoolean(routeParams.readable), false),
|
||||
authorNameBold: fallback(params.authorNameBold, queryToBoolean(routeParams.authorNameBold), false),
|
||||
showAuthorInTitle: fallback(params.showAuthorInTitle, queryToBoolean(routeParams.showAuthorInTitle), false),
|
||||
showAuthorInDesc: fallback(params.showAuthorInDesc, queryToBoolean(routeParams.showAuthorInDesc), false),
|
||||
showAuthorAvatarInDesc: fallback(params.showAuthorAvatarInDesc, queryToBoolean(routeParams.showAuthorAvatarInDesc), false),
|
||||
showAtBeforeAuthor: fallback(params.showAtBeforeAuthor, null, false),
|
||||
showEmojiForRetweet: fallback(params.showEmojiForRetweet, queryToBoolean(routeParams.showEmojiForRetweet), false),
|
||||
showRetweetTextInTitle: fallback(params.showRetweetTextInTitle, queryToBoolean(routeParams.showRetweetTextInTitle), true),
|
||||
addLinkForPics: fallback(params.addLinkForPics, queryToBoolean(routeParams.addLinkForPics), false),
|
||||
showTimestampInDescription: fallback(params.showTimestampInDescription, queryToBoolean(routeParams.showTimestampInDescription), false),
|
||||
|
||||
widthOfPics: fallback(params.widthOfPics, queryToInteger(routeParams.widthOfPics), -1),
|
||||
heightOfPics: fallback(params.heightOfPics, queryToInteger(routeParams.heightOfPics), -1),
|
||||
sizeOfAuthorAvatar: fallback(params.sizeOfAuthorAvatar, queryToInteger(routeParams.sizeOfAuthorAvatar), 48),
|
||||
};
|
||||
|
||||
params = mergedParams;
|
||||
|
||||
const {
|
||||
readable,
|
||||
authorNameBold,
|
||||
showAuthorInTitle,
|
||||
showAuthorInDesc,
|
||||
showAuthorAvatarInDesc,
|
||||
showAtBeforeAuthor,
|
||||
showEmojiForRetweet,
|
||||
showRetweetTextInTitle,
|
||||
addLinkForPics,
|
||||
showTimestampInDescription,
|
||||
|
||||
widthOfPics,
|
||||
heightOfPics,
|
||||
sizeOfAuthorAvatar,
|
||||
} = params;
|
||||
|
||||
let retweeted = '';
|
||||
// 长文章的处理
|
||||
let temp = (status.longText && status.longText.longTextContent.replace(/\n/g, '<br>')) || status.text || '';
|
||||
let htmlNewLineUnreplaced = (status.longText && status.longText.longTextContent) || status.text || '';
|
||||
// 表情图标转换为文字
|
||||
temp = temp.replace(/<span class="url-icon"><img.*?alt="?(.*?)"? [\s\S]*?\/><\/span>/g, '$1');
|
||||
htmlNewLineUnreplaced = htmlNewLineUnreplaced.replace(/<span class="url-icon"><img.*?alt="?(.*?)"? [\s\S]*?\/><\/span>/g, '$1');
|
||||
// 去掉外部链接的图标
|
||||
temp = temp.replace(/<span class=["|']url-icon["|']>.*?<\/span><span class="surl-text">(.*?)<\/span>/g, '$1');
|
||||
htmlNewLineUnreplaced = htmlNewLineUnreplaced.replace(/<span class=["|']url-icon["|']>.*?<\/span><span class="surl-text">(.*?)<\/span>/g, '$1');
|
||||
// 去掉乱七八糟的图标
|
||||
temp = temp.replace(/<span class=["|']url-icon["|']>(.*?)<\/span>/g, '');
|
||||
htmlNewLineUnreplaced = htmlNewLineUnreplaced.replace(/<span class=["|']url-icon["|']>(.*?)<\/span>/g, '');
|
||||
// 去掉全文
|
||||
temp = temp.replace(/全文<br>/g, '<br>');
|
||||
temp = temp.replace(/<a href="(.*?)">全文<\/a>/g, '');
|
||||
htmlNewLineUnreplaced = htmlNewLineUnreplaced.replace(/全文<br>/g, '<br>');
|
||||
htmlNewLineUnreplaced = htmlNewLineUnreplaced.replace(/<a href="(.*?)">全文<\/a>/g, '');
|
||||
|
||||
// 处理外部链接
|
||||
temp = temp.replace(/https:\/\/weibo\.cn\/sinaurl\/.*?&u=(http.*?")/g, function (match, p1) {
|
||||
htmlNewLineUnreplaced = htmlNewLineUnreplaced.replace(/https:\/\/weibo\.cn\/sinaurl\/.*?&u=(http.*?")/g, function (match, p1) {
|
||||
return decodeURIComponent(p1);
|
||||
});
|
||||
|
||||
// 处理转发的微博
|
||||
if (status.retweeted_status) {
|
||||
// 当转发的微博被删除时user为null
|
||||
if (status.retweeted_status.user) {
|
||||
temp += `<br><blockquote> - 转发 <a href="https://weibo.com/${status.retweeted_status.user.id}" target="_blank">@${status.retweeted_status.user.screen_name}</a>: `;
|
||||
let html = htmlNewLineUnreplaced.replace(/\n/g, '<br>');
|
||||
|
||||
// 添加用户名和头像
|
||||
if (showAuthorInDesc) {
|
||||
let usernameAndAvatar = `<a href="https://weibo.com/${status.user.id}" target="_blank">`;
|
||||
if (showAuthorAvatarInDesc) {
|
||||
usernameAndAvatar += `<img width="${sizeOfAuthorAvatar}" height="${sizeOfAuthorAvatar}" src="${status.user.profile_image_url}" ${readable ? 'hspace="8" vspace="8" align="left"' : ''} />`;
|
||||
}
|
||||
// 插入转发的微博
|
||||
temp += weiboUtils.format(status.retweeted_status);
|
||||
temp += '</blockquote>';
|
||||
let name = status.user.screen_name;
|
||||
if (showAtBeforeAuthor) {
|
||||
name = '@' + name;
|
||||
}
|
||||
if (authorNameBold) {
|
||||
usernameAndAvatar += `<strong>${name}</strong></a>: `;
|
||||
} else {
|
||||
usernameAndAvatar += `${name}</a>: `;
|
||||
}
|
||||
html = usernameAndAvatar + html;
|
||||
}
|
||||
|
||||
// 添加微博配图
|
||||
if (status.pics) {
|
||||
if (readable) {
|
||||
html += '<br clear="both" /><div style="clear: both"></div>';
|
||||
}
|
||||
|
||||
// 一些RSS Reader会识别所有<img>标签作为内含图片显示,我们不想要头像也作为内含图片之一
|
||||
// 让所有配图在description的最前面再次出现一次,但宽高设为0
|
||||
let picsPrefix = '';
|
||||
status.pics.forEach(function (item) {
|
||||
temp += '<img src="' + item.large.url + '"><br><br>';
|
||||
picsPrefix += `<img width="0" height="0" hidden="true" src="${item.large.url}">`;
|
||||
});
|
||||
picsPrefixes.push(picsPrefix);
|
||||
|
||||
status.pics.forEach(function (item) {
|
||||
if (addLinkForPics) {
|
||||
html += '<a href="' + item.large.url + '">';
|
||||
}
|
||||
|
||||
let style = '';
|
||||
html += '<img ';
|
||||
html += readable ? 'vspace="8" hspace="4"' : '';
|
||||
if (widthOfPics >= 0) {
|
||||
html += ` width="${widthOfPics}"`;
|
||||
style += `width: ${widthOfPics}px;`;
|
||||
}
|
||||
if (heightOfPics >= 0) {
|
||||
html += ` height="${heightOfPics}"`;
|
||||
style += `height: ${heightOfPics}px;`;
|
||||
}
|
||||
html += ` style="${style}"` + ' src="' + item.large.url + '">';
|
||||
|
||||
if (addLinkForPics) {
|
||||
html += '</a>';
|
||||
}
|
||||
|
||||
if (!readable) {
|
||||
html += '<br><br>';
|
||||
}
|
||||
|
||||
htmlNewLineUnreplaced += '<img src="" />';
|
||||
});
|
||||
}
|
||||
return temp;
|
||||
|
||||
// 处理转发的微博
|
||||
if (status.retweeted_status) {
|
||||
if (readable) {
|
||||
html += `<br clear="both" /><div style="clear: both"></div><blockquote style="background: #80808010;border-top:1px solid #80808030;border-bottom:1px solid #80808030;margin:0;padding:5px 20px;">`;
|
||||
} else {
|
||||
html += `<br><blockquote> - 转发 `;
|
||||
}
|
||||
if (!status.retweeted_status.user) {
|
||||
// 当转发的微博被删除时user为null
|
||||
status.retweeted_status.user = {
|
||||
profile_image_url: '',
|
||||
screen_name: '[原微博不可访问]',
|
||||
id: 'sorry',
|
||||
};
|
||||
}
|
||||
// 插入转发的微博
|
||||
const retweetedParams = Object.assign({}, params);
|
||||
retweetedParams.showAuthorInDesc = true;
|
||||
retweetedParams.showAuthorAvatarInDesc = false;
|
||||
retweetedParams.showAtBeforeAuthor = true;
|
||||
retweeted += weiboUtils.formatExtended(ctx, status.retweeted_status, retweetedParams, picsPrefixes).description;
|
||||
|
||||
html += retweeted;
|
||||
|
||||
if (readable) {
|
||||
html += `<br><small>原博:<a href="https://weibo.com/${status.retweeted_status.user.id}/${status.retweeted_status.bid}" target="_blank" rel="noopener noreferrer">https://weibo.com/${status.retweeted_status.user.id}/${status.retweeted_status.bid}</a></small>`;
|
||||
}
|
||||
if (showTimestampInDescription) {
|
||||
html += `<br><small>` + new Date(status.retweeted_status.created_at).toLocaleString() + `</small>`;
|
||||
}
|
||||
if (readable) {
|
||||
html += `<br clear="both" /><div style="clear: both"></div>`;
|
||||
}
|
||||
|
||||
html += '</blockquote>';
|
||||
}
|
||||
|
||||
if (showAuthorInDesc && showAuthorAvatarInDesc) {
|
||||
html = picsPrefixes.join('') + html;
|
||||
}
|
||||
|
||||
let title = '';
|
||||
if (showAuthorInTitle) {
|
||||
title += status.user.screen_name + ': ';
|
||||
}
|
||||
if (!status.retweeted_status || showRetweetTextInTitle) {
|
||||
title += htmlNewLineUnreplaced
|
||||
.replace(/<img[\s\S]*?>/g, '[图片]')
|
||||
.replace(/<.*?>/g, '')
|
||||
.replace('\n', '');
|
||||
}
|
||||
if (status.retweeted_status) {
|
||||
title += showEmojiForRetweet ? '🔁 ' : ' - 转发 ';
|
||||
title += retweeted
|
||||
.replace(/<img[\s\S]*?>/g, '[图片]')
|
||||
.replace(/<.*?>/g, '')
|
||||
.replace('\n', '');
|
||||
}
|
||||
|
||||
return { description: html, title: title };
|
||||
},
|
||||
getShowData: async (uid, bid) => {
|
||||
const link = `https://m.weibo.cn/statuses/show?id=${bid}`;
|
||||
@@ -58,7 +206,7 @@ const weiboUtils = {
|
||||
const posterUrl = pagePic ? pagePic.url : '';
|
||||
const videoUrl = mediaInfo ? mediaInfo.stream_url_hd || mediaInfo.stream_url || mediaInfo.mp4_hd_url || mediaInfo.mp4_sd_url || mediaInfo.mp4_720p_mp4 : '';
|
||||
if (videoUrl) {
|
||||
const video = `<br><video src="${videoUrl}" controls="controls" poster="${posterUrl}" style="width: 100%"></video>`;
|
||||
const video = `<br clear="both" /><div style="clear: both"></div><video src="${videoUrl}" controls="controls" poster="${posterUrl}" style="width: 100%"></video>`;
|
||||
itemDesc += video;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user