diff --git a/docs/social-media.md b/docs/social-media.md index 27eb2b99ee..2a3f020155 100644 --- a/docs/social-media.md +++ b/docs/social-media.md @@ -688,6 +688,26 @@ Tiny Tiny RSS 会给所有 iframe 元素添加 `sandbox="allow-scripts"` 属性 +## 即刻 + +### 圈子 + + + +::: tip 提示 + +部分圈子如 `一觉醒来发生了什么: 553870e8e4b0cafb0a1bef68` 提供纯文字内容,圈子 - 纯文字 /jike/topic/text/:id 可能会提供更好的体验. + +::: + +### 圈子 - 纯文字 + + + +### 用户动态 + + + ## 简书 ### 首页 diff --git a/lib/router.js b/lib/router.js index 57f03941c9..dfce4f3961 100644 --- a/lib/router.js +++ b/lib/router.js @@ -2853,4 +2853,9 @@ router.get('/futunn/highlights', require('./routes/futunn/highlights')); // 外接大脑 router.get('/waijiedanao/article/:caty', require('./routes/waijiedanao/article')); +// 即刻 +router.get('/jike/topic/:id', require('./routes/jike/topic')); +router.get('/jike/topic/text/:id', require('./routes/jike/topicText')); +router.get('/jike/user/:id', require('./routes/jike/user')); + module.exports = router; diff --git a/lib/routes/jike/common.js b/lib/routes/jike/common.js new file mode 100644 index 0000000000..a3dd8eb5db --- /dev/null +++ b/lib/routes/jike/common.js @@ -0,0 +1,135 @@ +const url = require('url'); + +module.exports = { + emptyResponseCheck: (ctx, data) => { + if (data.length === 0) { + ctx.state.data = { + title: '主题 ID 不存在,或该主题暂无内容', + }; + return true; + } + }, + topicDataHanding: (data) => + data.posts.map((item) => { + let audioName, videoName, linkName; + + // 获取纯文字内容 和 即刻原文链接 + let content, link; + switch (item.type) { + case 'ORIGINAL_POST': + content = item.content; + link = `https://m.okjike.com/originalPosts/${item.id}`; + break; + + /* case 'QUESTION': + content = item.title; + link = `https://m.okjike.com/questions/${item.id}`; + break; + case 'OFFICIAL_MESSAGE': + content = item.content; + link = `https://web.okjike.com/message-detail/${item.id}/officialMessage`; + 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) => { + if (pic.format === 'gif') { + description += ``; + } else { + // jpeg, bmp, png, gif, webp + // https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types + let type = 'jpeg'; + switch (pic.format) { + case 'bmp': + type = 'bmp'; + break; + case 'png': + type = 'png'; + break; + default: + break; + } + const imgUrl = pic.picUrl.match(/\.[a-z0-9]+?\?imageMogr2/) ? pic.picUrl.split('?imageMogr2/')[0] : pic.picUrl.replace(/thumbnail\/.+/, ''); + 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/lib/routes/jike/topic.js b/lib/routes/jike/topic.js new file mode 100644 index 0000000000..0db3148787 --- /dev/null +++ b/lib/routes/jike/topic.js @@ -0,0 +1,65 @@ +const got = require('@/utils/got'); +const common = require('./common'); +const cheerio = require('cheerio'); +const dayjs = require('dayjs'); + +module.exports = async (ctx) => { + const id = ctx.params.id; + + const response = await got({ + method: 'get', + url: `https://m.okjike.com/topics/${id}`, + headers: { + Referer: `https://m.okjike.com/topics/${id}`, + }, + }); + + const html = response.data; + const $ = cheerio.load(html); + const raw = $('[type = "application/json"]').html(); + const data = JSON.parse(raw).props.pageProps; + + if (common.emptyResponseCheck(ctx, data)) { + return; + } + + const topic = data.topic; + ctx.state.data = { + title: `${topic.content} - 即刻圈子`, + link: `https://m.okjike.com/topics/${id}`, + description: topic.briefIntro, + image: topic.squarePicture.picUrl || topic.squarePicture.middlePicUrl || topic.squarePicture.thumbnailUrl, + item: common.topicDataHanding(data), + }; + if (id === '553870e8e4b0cafb0a1bef68' || id === '55963702e4b0d84d2c30ce6f') { + const promises = ctx.state.data.item.map(async (one) => { + const item = { ...one }; + const regResult = /https:\/\/www.okjike.com\/medium\/[a-zA-Z0-9]*/.exec(item.description); + if (regResult) { + const newsUrl = regResult[0]; + const cache = await ctx.cache.get(newsUrl); + if (cache) { + item.description = cache; + } else { + const { data } = await got.get(newsUrl); + const $ = cheerio.load(data); + const upper = $('ul.main > li.item'); + const links = upper.find('a').map((_, ele) => $(ele).attr('href')); + const texts = upper.find('span.text').map((_, ele) => $(ele).text()); + let description = ''; + for (let i = 0; i < links.length; i++) { + description += `${i + 1}、${texts[i]}
`; + } + if (description) { + item.description = description; + await ctx.cache.set(newsUrl, description); + } + } + } + item.title = `${topic.content} ${dayjs(new Date(one.pubDate)).format('MM月DD日')}`; + return item; + }); + + ctx.state.data.item = await Promise.all(promises); + } +}; diff --git a/lib/routes/jike/topicText.js b/lib/routes/jike/topicText.js new file mode 100644 index 0000000000..bb7410b001 --- /dev/null +++ b/lib/routes/jike/topicText.js @@ -0,0 +1,42 @@ +const got = require('@/utils/got'); +const dayjs = require('dayjs'); +const common = require('./common'); +const cheerio = require('cheerio'); + +module.exports = async (ctx) => { + const id = ctx.params.id; + + const response = await got({ + method: 'get', + url: `https://m.okjike.com/topics/${id}`, + headers: { + Referer: `https://m.okjike.com/topics/${id}`, + }, + }); + + const html = response.data; + const $ = cheerio.load(html); + const raw = $('[type = "application/json"]').html(); + const data = JSON.parse(raw).props.pageProps; + + if (common.emptyResponseCheck(ctx, data)) { + return; + } + + const title = data.topic.content; + + ctx.state.data = { + title: `${title} - 即刻`, + link: `https://m.okjike.com/topics/${id}`, + description: `${title} - 即刻`, + item: data.posts.map((item) => { + const date = new Date(item.createdAt); + return { + title: `${title} ${dayjs(date).format('MM月DD日')}`, + description: item.content.replace(new RegExp('\n', 'g'), '
'), + pubDate: date.toUTCString(), + link: `https://m.okjike.com/originalPosts/${item.id}`, + }; + }), + }; +}; diff --git a/lib/routes/jike/user.js b/lib/routes/jike/user.js new file mode 100644 index 0000000000..3028f3626d --- /dev/null +++ b/lib/routes/jike/user.js @@ -0,0 +1,112 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); + +module.exports = async (ctx) => { + const id = ctx.params.id; + + const response = await got({ + method: 'get', + url: `https://m.okjike.com/users/${id}`, + headers: { + Referer: `https://m.okjike.com/users/${id}`, + }, + }); + + const html = response.data; + const $ = cheerio.load(html); + const raw = $('[type = "application/json"]').html(); + const data = JSON.parse(raw).props.pageProps; + + ctx.state.data = { + title: `${data.user.screenName}的即刻动态`, + link: `https://m.okjike.com/users/${id}`, + image: data.user.avatarImage.picUrl, + item: data.posts.map((item) => { + const typeMap = { + ORIGINAL_POST: '发布', + REPOST: '转发', + ANSWER: '回答', + QUESTION: '提问', + PERSONAL_UPDATE: '创建新主题', + }; + + const linkMap = { + ORIGINAL_POST: `https://m.okjike.com/originalPosts/${item.id}`, + REPOST: `https://m.okjike.com/reposts/${item.id}`, + // OFFICIAL_MESSAGE: `https://m.okjike.com/originalPosts/${item.id}`, + // ANSWER: `https://m.okjike.com/answer/${item.id}`, + // QUESTION: `https://m.okjike.com/originalPosts/${item.id}`, + // PERSONAL_UPDATE: `https://m.okjike.com/topic/${item.id}${item.topic && item.topic.id}`, + }; + + let linkTemplate = ''; + if (item.linkInfo && item.linkInfo.linkUrl) { + linkTemplate = `${item.linkInfo.title}
`; + } + + let imgTemplate = ''; + item.pictures && + item.pictures.forEach((item) => { + imgTemplate += `
`; + }); + + let content = item.content || (item.linkInfo && item.linkInfo.title) || (item.question && item.question.title) || item.title || ''; + + let shortenTitle = '一条动态'; + if (content) { + shortenTitle = content.length > 75 ? `${content.substr(0, 75)}...` : content; + content = `${content}

`; + } + + if (item.type === 'REPOST') { + const screenNameTemplate = item.target.user ? `@${item.target.user.screenName}` : ''; + + let repostImgTemplate = ''; + item.target.pictures && + item.target.pictures.forEach((item) => { + repostImgTemplate += `
`; + }); + + const repostContent = `转发 ${screenNameTemplate}: ${item.target.content}${repostImgTemplate}`; + content = `${content}${repostContent}`.replace(/\n|\r/g, '
'); + } + // 部分功能未知 + /* else if (item.type === 'ANSWER') { + let answerTextTemplate = ''; + let answerImgTemplate = ''; + let answerImgKeys = []; + item.richtextContent.blocks && + item.richtextContent.blocks.forEach((item) => { + if (item.entityRanges.length && item.text === '[图片]') { + answerImgKeys = [...answerImgKeys, ...Object.keys(item.entityRanges)]; + } else { + answerTextTemplate += item.text; + } + }); + + if (answerImgKeys.length) { + answerImgKeys.forEach((key) => { + const entity = item.richtextContent.entityMap[key]; + if (entity.type.toUpperCase() === 'IMAGE') { + answerImgTemplate += `
`; + } + }); + } + const answerContent = `回答: ${answerTextTemplate}${answerImgTemplate}`; + content = `${content}${answerContent}`.replace(/\n|\r/g, '
'); + } else if (item.type === 'QUESTION') { + content = `在主题 ${item.topic.content} 提出了一个问题:

${content}`; + } else if (item.type === 'PERSONAL_UPDATE') { + shortenTitle = item.topic.content; + content = ` 主题简介:
${item.topic.briefIntro.replace(/(?:\r\n|\r|\n)/g, '
')}`; + }*/ + + return { + title: `${typeMap[item.type]}了: ${shortenTitle}`, + description: `${content}${linkTemplate}${imgTemplate}`, + pubDate: new Date(item.createdAt).toUTCString(), + link: `${linkMap[item.type]}`, + }; + }), + }; +};