diff --git a/routes/jike/common.js b/routes/jike/common.js new file mode 100644 index 0000000000..0d90d03901 --- /dev/null +++ b/routes/jike/common.js @@ -0,0 +1,109 @@ +const url = require('url'); + +module.exports = { + topicDataHanding: (data) => + data.map((item) => { + 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'; + } + + // 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, + description: description.replace(new RegExp('\n', 'g'), '
'), + pubDate: new Date(item.createdAt).toUTCString(), + link: link, + }; + }), +}; diff --git a/routes/jike/topic.js b/routes/jike/topic.js index 0c2841fd8a..21fcd3c42d 100644 --- a/routes/jike/topic.js +++ b/routes/jike/topic.js @@ -1,5 +1,5 @@ const axios = require('../../utils/axios'); -const url = require('url'); +const common = require('./common'); module.exports = async (ctx) => { const id = ctx.params.id; @@ -26,109 +26,6 @@ module.exports = async (ctx) => { link: `https://web.okjike.com/topic/${id}/official`, description: topic.content, image: topic.squarePicture.picUrl || topic.squarePicture.middlePicUrl || topic.squarePicture.thumbnailUrl, - item: data.map((item) => { - 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'; - } - - // 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, - description: description.replace(new RegExp('\n', 'g'), '
'), - pubDate: new Date(item.createdAt).toUTCString(), - link: link, - }; - }), + item: common.topicDataHanding(data), }; }; diff --git a/routes/jike/topicSquare.js b/routes/jike/topicSquare.js index 7a2b17c3cf..05af172c2b 100644 --- a/routes/jike/topicSquare.js +++ b/routes/jike/topicSquare.js @@ -1,4 +1,5 @@ const axios = require('../../utils/axios'); +const common = require('./common'); module.exports = async (ctx) => { const id = ctx.params.id; @@ -25,24 +26,6 @@ module.exports = async (ctx) => { link: `https://web.okjike.com/topic/${id}/user`, description: topic.content, image: topic.squarePicture.picUrl || topic.squarePicture.middlePicUrl || topic.squarePicture.thumbnailUrl, - item: data.map((item) => { - let contentTemplate = item.content; - if (item.linkInfo && (item.linkInfo.originalLinkUrl || item.linkInfo.linkUrl)) { - contentTemplate = `${item.content}`; - } - - let imgTemplate = ''; - item.pictures && - item.pictures.forEach((item) => { - imgTemplate += `
`; - }); - - return { - title: item.content, - description: `${item.user.screenName}: ${contentTemplate}${imgTemplate}`, - pubDate: new Date(item.createdAt).toUTCString(), - link: `https://web.okjike.com/post-detail/${item.id}/originalPost`, - }; - }), + item: common.topicDataHanding(data), }; };