diff --git a/assets/radar-rules.js b/assets/radar-rules.js index 5f80e7b5a..4c1af4c62 100644 --- a/assets/radar-rules.js +++ b/assets/radar-rules.js @@ -226,6 +226,12 @@ source: '/people/:id/pins', target: '/zhihu/people/pins/:id', }, + { + title: '用户文章', + docs: 'https://docs.rsshub.app/social-media.html#zhi-hu', + source: '/people/:id/posts', + target: '/zhihu/people/posts/:id', + }, { title: '热榜', docs: 'https://docs.rsshub.app/social-media.html#zhi-hu', diff --git a/docs/social-media.md b/docs/social-media.md index aa2f05b20..a9b2e85fb 100644 --- a/docs/social-media.md +++ b/docs/social-media.md @@ -630,6 +630,10 @@ pageClass: routes +### 用户文章 + + + ### 专栏 diff --git a/lib/router.js b/lib/router.js index 1b3acd60b..3285bc5e4 100644 --- a/lib/router.js +++ b/lib/router.js @@ -93,6 +93,7 @@ router.get('/jianshu/user/:id', require('./routes/jianshu/user')); router.get('/zhihu/collection/:id', require('./routes/zhihu/collection')); router.get('/zhihu/people/activities/:id', require('./routes/zhihu/activities')); router.get('/zhihu/people/answers/:id', require('./routes/zhihu/answers')); +router.get('/zhihu/people/posts/:id', require('./routes/zhihu/posts')); router.get('/zhihu/zhuanlan/:id', require('./routes/zhihu/zhuanlan')); router.get('/zhihu/daily', require('./routes/zhihu/daily')); router.get('/zhihu/hotlist', require('./routes/zhihu/hotlist')); diff --git a/lib/routes/zhihu/posts.js b/lib/routes/zhihu/posts.js new file mode 100644 index 000000000..852ff0a2e --- /dev/null +++ b/lib/routes/zhihu/posts.js @@ -0,0 +1,69 @@ +const got = require('@/utils/got'); +const utils = require('./utils'); +const cheerio = require('cheerio'); + +module.exports = async (ctx) => { + const id = ctx.params.id; + + const response = await got({ + method: 'get', + url: `https://www.zhihu.com/api/v4/members/${id}/articles?limit=5`, + headers: { + ...utils.header, + Referer: `https://www.zhihu.com/people/${id}/posts`, + Authorization: 'oauth c3cef7c66a1843f8b3a9e6a1e3160e20', // hard-coded in js + }, + }); + + const data = response.data.data; + + const articles = await Promise.all( + data.map(async (article) => { + const url = article.url; + const item = { + title: article.title, + description: '', + link: article.url, + }; + const key = 'zhuanlan' + article.url; + const value = await ctx.cache.get(key); + + if (value) { + item.description = value; + } else { + const articleOriginal = await got({ + method: 'get', + url: url, + headers: { + Referer: url, + }, + }); + + const articleContent = cheerio + .load(articleOriginal.data)('.Post-RichText') + .html(); + + if (articleContent !== null) { + item.description = utils.ProcessImage(articleContent); + } else { + item.description = utils.ProcessImage( + cheerio + .load(articleOriginal.data)('.PostIndex-warning') + .html() + ); + } + + ctx.cache.set(key, item.description); + } + + return Promise.resolve(item); + }) + ); + + ctx.state.data = { + title: `${data[0].author.name}的知乎文章`, + link: `https://www.zhihu.com/people/${id}/posts`, + description: data[0].author.headline || data[0].author.description, + item: articles, + }; +};