From 6fea921aa4e23941d5d5bb729e6cddebdd40c0e5 Mon Sep 17 00:00:00 2001 From: Rongrong <15956627+Rongronggg9@users.noreply.github.com> Date: Sat, 26 Mar 2022 22:46:39 +0800 Subject: [PATCH] feat(route)(weibo): display comments Signed-off-by: Rongrong <15956627+Rongronggg9@users.noreply.github.com> --- docs/social-media.md | 1 + lib/routes/weibo/timeline.js | 7 +++++++ lib/routes/weibo/user.js | 7 +++++++ lib/routes/weibo/utils.js | 39 ++++++++++++++++++++++++++++++++++++ 4 files changed, 54 insertions(+) diff --git a/docs/social-media.md b/docs/social-media.md index 01358d6d672d46..22dbd7689b1a4b 100644 --- a/docs/social-media.md +++ b/docs/social-media.md @@ -1189,6 +1189,7 @@ rule | sizeOfAuthorAvatar | 作者头像大小 | 数字 | 48 | | displayVideo | 是否直接显示微博视频,只在博主或个人时间线 RSS 中有效 | 0/1/true/false | true | | displayArticle | 是否直接显示微博文章,只在博主或个人时间线 RSS 中有效 | 0/1/true/false | false | +| displayComments | 是否直接显示热门评论,只在博主或个人时间线 RSS 中有效 | 0/1/true/false | false | | showEmojiInDescription | 是否展示正文中的 emoji 表情 | 0/1/true/false | true | 指定更多与默认值不同的参数选项可以改善 RSS 的可读性,如 diff --git a/lib/routes/weibo/timeline.js b/lib/routes/weibo/timeline.js index 13866f12f9bb0e..15c0aa08994bb9 100644 --- a/lib/routes/weibo/timeline.js +++ b/lib/routes/weibo/timeline.js @@ -12,6 +12,7 @@ module.exports = async (ctx) => { const token = await ctx.cache.get('weibotimelineuid' + uid, false); let displayVideo = '1'; let displayArticle = '0'; + let displayComments = '0'; if (routeParams) { if (routeParams === '1' || routeParams === '0') { displayVideo = routeParams; @@ -19,6 +20,7 @@ module.exports = async (ctx) => { const routeParams = querystring.parse(ctx.params.routeParams); displayVideo = fallback(undefined, queryToBoolean(routeParams.displayVideo), true) ? '1' : '0'; displayArticle = fallback(undefined, queryToBoolean(routeParams.displayArticle), false) ? '1' : '0'; + displayComments = fallback(undefined, queryToBoolean(routeParams.displayComments), false) ? '1' : '0'; } } @@ -86,6 +88,11 @@ module.exports = async (ctx) => { } } + // 评论的处理 + if (displayComments === '1') { + description = await weiboUtils.formatComments(ctx, description, item); + } + // 文章的处理 if (displayArticle === '1') { // 含被转发微博时需要从被转发微博中获取文章 diff --git a/lib/routes/weibo/user.js b/lib/routes/weibo/user.js index b0557914f08714..148afc02cccf6a 100644 --- a/lib/routes/weibo/user.js +++ b/lib/routes/weibo/user.js @@ -9,6 +9,7 @@ module.exports = async (ctx) => { const uid = ctx.params.uid; let displayVideo = '1'; let displayArticle = '0'; + let displayComments = '0'; if (ctx.params.routeParams) { if (ctx.params.routeParams === '1' || ctx.params.routeParams === '0') { displayVideo = ctx.params.routeParams; @@ -16,6 +17,7 @@ module.exports = async (ctx) => { const routeParams = querystring.parse(ctx.params.routeParams); displayVideo = fallback(undefined, queryToBoolean(routeParams.displayVideo), true) ? '1' : '0'; displayArticle = fallback(undefined, queryToBoolean(routeParams.displayArticle), false) ? '1' : '0'; + displayComments = fallback(undefined, queryToBoolean(routeParams.displayComments), false) ? '1' : '0'; } } @@ -107,6 +109,11 @@ module.exports = async (ctx) => { } } + // 评论的处理 + if (displayComments === '1') { + description = await weiboUtils.formatComments(ctx, description, item.mblog); + } + // 文章的处理 if (displayArticle === '1') { // 含被转发微博时需要从被转发微博中获取文章 diff --git a/lib/routes/weibo/utils.js b/lib/routes/weibo/utils.js index 604b724e2280c4..8c29f5138fc6ae 100644 --- a/lib/routes/weibo/utils.js +++ b/lib/routes/weibo/utils.js @@ -348,6 +348,45 @@ const weiboUtils = { } return itemDesc; }, + formatComments: async (ctx, itemDesc, status) => { + if (status && status.comments_count && status.id && status.mid) { + const id = status.id; + const mid = status.mid; + const link = `https://m.weibo.cn/comments/hotflow?id=${id}&mid=${mid}&max_id_type=0`; + const response = await ctx.cache.tryGet(link, async () => { + const _response = await got.get(link, { + headers: { + Referer: `https://m.weibo.cn/detail/${id}`, + 'MWeibo-Pwa': 1, + 'X-Requested-With': 'XMLHttpRequest', + 'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1', + }, + }); + return _response.data; + }); + if (response.data && response.data.data) { + const comments = response.data.data; + itemDesc += `
`; + itemDesc += '

热门评论

'; + comments.forEach((comment) => { + itemDesc += '

'; + itemDesc += `${comment.user.screen_name}: ${comment.text}`; + if (comment.comments) { + itemDesc += '

'; + comment.comments.forEach((comment) => { + itemDesc += '
'; + itemDesc += `${comment.user.screen_name}: ${comment.text}`; + itemDesc += '
'; + }); + itemDesc += '
'; + } + itemDesc += '

'; + }); + itemDesc += '
'; + } + } + return itemDesc; + }, }; module.exports = weiboUtils;