Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(route)(weibo): display comments #9394

Merged
merged 1 commit into from
Mar 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/social-media.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 的可读性,如
Expand Down
7 changes: 7 additions & 0 deletions lib/routes/weibo/timeline.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ 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;
} else {
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';
}
}

Expand Down Expand Up @@ -86,6 +88,11 @@ module.exports = async (ctx) => {
}
}

// 评论的处理
if (displayComments === '1') {
description = await weiboUtils.formatComments(ctx, description, item);
}

// 文章的处理
if (displayArticle === '1') {
// 含被转发微博时需要从被转发微博中获取文章
Expand Down
7 changes: 7 additions & 0 deletions lib/routes/weibo/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ 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;
} else {
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';
}
}

Expand Down Expand Up @@ -107,6 +109,11 @@ module.exports = async (ctx) => {
}
}

// 评论的处理
if (displayComments === '1') {
description = await weiboUtils.formatComments(ctx, description, item.mblog);
}

// 文章的处理
if (displayArticle === '1') {
// 含被转发微博时需要从被转发微博中获取文章
Expand Down
39 changes: 39 additions & 0 deletions lib/routes/weibo/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 += `<br clear="both" /><div style="clear: both"></div><div style="background: #80808010;border-top:1px solid #80808030;border-bottom:1px solid #80808030;margin:0;padding:5px 20px;">`;
itemDesc += '<h3>热门评论</h3>';
comments.forEach((comment) => {
itemDesc += '<p style="margin-bottom: 0.5em;margin-top: 0.5em">';
itemDesc += `<a href="https://weibo.com/${comment.user.id}" target="_blank">${comment.user.screen_name}</a>: ${comment.text}`;
if (comment.comments) {
itemDesc += '<blockquote style="border-left:0.2em solid #80808080; margin-left: 0.3em; padding-left: 0.5em; margin-bottom: 0.5em; margin-top: 0.25em">';
comment.comments.forEach((comment) => {
itemDesc += '<div style="font-size: 0.9em">';
itemDesc += `<a href="https://weibo.com/${comment.user.id}" target="_blank">${comment.user.screen_name}</a>: ${comment.text}`;
itemDesc += '</div>';
});
itemDesc += '</blockquote>';
}
itemDesc += '</p>';
});
itemDesc += '</div>';
}
}
return itemDesc;
},
};

module.exports = weiboUtils;