diff --git a/docs/.vuepress/public/readable-weibo.png b/docs/.vuepress/public/readable-weibo.png new file mode 100644 index 000000000..56dc9104c Binary files /dev/null and b/docs/.vuepress/public/readable-weibo.png differ diff --git a/docs/social-media.md b/docs/social-media.md index 71d1a1bcf..92556d388 100644 --- a/docs/social-media.md +++ b/docs/social-media.md @@ -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 + +的效果为 + +微博小秘书的可读微博 RSS + ### 博主 - + 部分博主仅登录可见,不支持订阅,可以通过打开 `https://m.weibo.cn/u/:uid` 验证 @@ -908,7 +934,7 @@ rule ### 关键词 - + ### 热搜榜 @@ -916,10 +942,16 @@ rule ### 超话 - + ### 个人时间线 +::: warning 注意 + +个人时间线暂不支持改善可读性。 + +::: + ::: warning 注意 diff --git a/lib/router.js b/lib/router.js index 468d2f62a..65670ed64 100644 --- a/lib/router.js +++ b/lib/router.js @@ -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')); // 贴吧 diff --git a/lib/routes/weibo/keyword.js b/lib/routes/weibo/keyword.js index 5d6a83e14..30802d1a0 100644 --- a/lib/routes/weibo/keyword.js +++ b/lib/routes/weibo/keyword.js @@ -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(//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}`, }; diff --git a/lib/routes/weibo/super_index.js b/lib/routes/weibo/super_index.js index 89c824993..2cb96fec7 100644 --- a/lib/routes/weibo/super_index.js +++ b/lib/routes/weibo/super_index.js @@ -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(//g, '[图片]').replace(/<.*?>/g, ''), + title: title, description: desc, author: mblog.user.screen_name, link: `https://weibo.com/${mblog.user.id}/${mblog.bid}`, diff --git a/lib/routes/weibo/user.js b/lib/routes/weibo/user.js index ed3e2a19c..c353f3096 100644 --- a/lib/routes/weibo/user.js +++ b/lib/routes/weibo/user.js @@ -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(//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); }) diff --git a/lib/routes/weibo/utils.js b/lib/routes/weibo/utils.js index 02fac9c5a..7fdf6fef3 100644 --- a/lib/routes/weibo/utils.js +++ b/lib/routes/weibo/utils.js @@ -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, '
')) || status.text || ''; + let htmlNewLineUnreplaced = (status.longText && status.longText.longTextContent) || status.text || ''; // 表情图标转换为文字 - temp = temp.replace(/<\/span>/g, '$1'); + htmlNewLineUnreplaced = htmlNewLineUnreplaced.replace(/<\/span>/g, '$1'); // 去掉外部链接的图标 - temp = temp.replace(/.*?<\/span>(.*?)<\/span>/g, '$1'); + htmlNewLineUnreplaced = htmlNewLineUnreplaced.replace(/.*?<\/span>(.*?)<\/span>/g, '$1'); // 去掉乱七八糟的图标 - temp = temp.replace(/(.*?)<\/span>/g, ''); + htmlNewLineUnreplaced = htmlNewLineUnreplaced.replace(/(.*?)<\/span>/g, ''); // 去掉全文 - temp = temp.replace(/全文
/g, '
'); - temp = temp.replace(/全文<\/a>/g, ''); + htmlNewLineUnreplaced = htmlNewLineUnreplaced.replace(/全文
/g, '
'); + htmlNewLineUnreplaced = htmlNewLineUnreplaced.replace(/
全文<\/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 += `
- 转发 @${status.retweeted_status.user.screen_name}: `; + let html = htmlNewLineUnreplaced.replace(/\n/g, '
'); + + // 添加用户名和头像 + if (showAuthorInDesc) { + let usernameAndAvatar = ``; + if (showAuthorAvatarInDesc) { + usernameAndAvatar += ``; } - // 插入转发的微博 - temp += weiboUtils.format(status.retweeted_status); - temp += '
'; + let name = status.user.screen_name; + if (showAtBeforeAuthor) { + name = '@' + name; + } + if (authorNameBold) { + usernameAndAvatar += `${name}: `; + } else { + usernameAndAvatar += `${name}: `; + } + html = usernameAndAvatar + html; } // 添加微博配图 if (status.pics) { + if (readable) { + html += '
'; + } + + // 一些RSS Reader会识别所有标签作为内含图片显示,我们不想要头像也作为内含图片之一 + // 让所有配图在description的最前面再次出现一次,但宽高设为0 + let picsPrefix = ''; status.pics.forEach(function (item) { - temp += '

'; + picsPrefix += ``; + }); + picsPrefixes.push(picsPrefix); + + status.pics.forEach(function (item) { + if (addLinkForPics) { + html += ''; + } + + let style = ''; + html += '= 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 += ''; + } + + if (!readable) { + html += '

'; + } + + htmlNewLineUnreplaced += ''; }); } - return temp; + + // 处理转发的微博 + if (status.retweeted_status) { + if (readable) { + html += `
`; + } else { + html += `
- 转发 `; + } + 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 += `
原博:https://weibo.com/${status.retweeted_status.user.id}/${status.retweeted_status.bid}`; + } + if (showTimestampInDescription) { + html += `
` + new Date(status.retweeted_status.created_at).toLocaleString() + ``; + } + if (readable) { + html += `
`; + } + + html += '
'; + } + + if (showAuthorInDesc && showAuthorAvatarInDesc) { + html = picsPrefixes.join('') + html; + } + + let title = ''; + if (showAuthorInTitle) { + title += status.user.screen_name + ': '; + } + if (!status.retweeted_status || showRetweetTextInTitle) { + title += htmlNewLineUnreplaced + .replace(//g, '[图片]') + .replace(/<.*?>/g, '') + .replace('\n', ''); + } + if (status.retweeted_status) { + title += showEmojiForRetweet ? '🔁 ' : ' - 转发 '; + title += retweeted + .replace(//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 = `
`; + const video = `
`; itemDesc += video; } }