From 0a0d378ff039682d76ac08a04a68d6c755bd8a8f Mon Sep 17 00:00:00 2001 From: Andie Date: Sun, 16 Sep 2018 23:21:52 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8D=B3=E5=88=BB=E4=B8=BB=E9=A2=98=E7=B2=BE?= =?UTF-8?q?=E9=80=89=20All=20in=20one=20(#697)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 在测试主题: - [即刻夜谈会](https://web.okjike.com/topic/58a5773cae113d0015bb7ef6/official)和[产品经理日常](https://web.okjike.com/topic/563a2995306dab1300a32227/official) - [小姐姐Cosplay摄影](https://web.okjike.com/topic/58c361475217740015d3f701/official) - [晚安歌曲](https://web.okjike.com/topic/55483ddee4b03ccbae843925/official) - [值得一看的短视频](https://web.okjike.com/topic/56fd0b78bdc8981200998418/official) - [值得一看的长文章](https://web.okjike.com/topic/56d2fabe7cb3331100467e2b/official) - [产品经理日常](https://web.okjike.com/topic/563a2995306dab1300a32227/official) - [一觉醒来世界发生了什么](https://web.okjike.com/topic/553870e8e4b0cafb0a1bef68/official) 有良好的表现,可以做到一个路由适应目前已知的所有消息格式 且会根据主题内容选择较为合适的标题 并 长度控制 --- routes/jike/topic.js | 110 ++++++++++++++++++++++++++++++++++++++----- 1 file changed, 97 insertions(+), 13 deletions(-) diff --git a/routes/jike/topic.js b/routes/jike/topic.js index d4426b12f..1c7bfe875 100644 --- a/routes/jike/topic.js +++ b/routes/jike/topic.js @@ -1,4 +1,5 @@ const axios = require('../../utils/axios'); +const url = require('url'); module.exports = async (ctx) => { const id = ctx.params.id; @@ -13,7 +14,7 @@ module.exports = async (ctx) => { data: { loadMoreKey: null, topic: id, - limit: 20, + limit: 10, }, }); @@ -26,22 +27,105 @@ module.exports = async (ctx) => { description: topic.content, image: topic.squarePicture.picUrl || topic.squarePicture.middlePicUrl || topic.squarePicture.thumbnailUrl, item: data.map((item) => { - let content = item.content; - let link = `https://web.okjike.com/message-detail/${item.id}/officialMessage`; - if (item.linkInfo && (item.linkInfo.originalLinkUrl || item.linkInfo.linkUrl)) { - const source = item.linkInfo.originalLinkUrl || item.linkInfo.linkUrl; - content = `${item.content} - 即刻`; - link = source; + let audioName, videoName, linkName; + + // 获取纯文字内容 和 即刻原文链接 + let content, link; + switch (item.type) { + case 'OFFICIAL_MESSAGE': + content = item.content; + link = `https://web.okjike.com/message-detail/${item.id}/officialMessage`; + break; + case 'ORIGINAL_POST': + content = item.content; + link = `https://web.okjike.com/post-detail/${item.id}/originalPost`; + break; + case 'QUESTION': + content = item.title; + link = `https://m.okjike.com/questions/${item.id}`; + break; + default: + content = '未知类型,请前往GitHub提交issue'; + link = 'https://github.com/DIYgod/RSSHub/issues'; } - if (item.pictures) { - item.pictures.forEach((item) => { - content += `
`; - }); + // rss内容 + let description = ''; + + if (item.linkInfo) { + const linkUrl = item.linkInfo.originalLinkUrl || item.linkInfo.linkUrl; + + // 对于即刻抓取的微信公众号文章 特殊处理 + // 此时 Rss原文链接 变为 微信公众号链接 + if (url.parse(linkUrl).host === 'mp.weixin.qq.com') { + link = linkUrl; + } + + // 1. 音频 + const audioObject = item.linkInfo.audio || item.audio; + if (audioObject) { + const audioImage = audioObject.image.picUrl || audioObject.image.thumbnailUrl; + const audioLink = linkUrl; + const audioTitle = audioObject.title; + const audioAuthor = audioObject.author; + audioName = `${audioTitle} - ${audioAuthor}`; + description += ` + + ${audioName} + `; + } + + // 2. 视频 + const videoObject = item.linkInfo.video || item.video; + if (videoObject) { + const videoImage = videoObject.image.picUrl || videoObject.image.thumbnailUrl; + const videoLink = linkUrl; + const videoDuration = Math.floor(videoObject.duration / 60000); + videoName = item.linkInfo.title; + description += ` + + ${videoName || '观看视频'} - 约${videoDuration}分钟 + `; + } + + // 3. 链接 + if (!audioObject && !videoObject && linkUrl) { + // 部分链接有标题 + linkName = item.linkInfo.title; + const linkTitle = linkName || '访问原文'; + // 部分链接有缩略图 + const linkImage = item.linkInfo.pictureUrl; + const imageTag = ``; + description += ` + ${linkImage ? imageTag : ''} + ${linkTitle} + `; + } } + + // 4. 文字内容 + description += `
${content}`; + + // 5. 图片 + if (item.pictures) { + item.pictures.forEach( + (pic) => + (description += ``) + ); + } + + // rss标题 + // 优先将音频和视频名作为标题 + // 其次将正文内容作为标题 + // 若都没有 则是推送型消息,将连接标题作为主题 + // “无题” fallback + const title = audioName || videoName || content || linkName || '无题'; + return { - title: item.content, - description: content, + title, + description: description.replace(new RegExp('\n', 'g'), '
'), pubDate: new Date(item.createdAt).toUTCString(), link: link, };