diff --git a/assets/radar-rules.js b/assets/radar-rules.js index a9ecbe283..0fc904ae9 100644 --- a/assets/radar-rules.js +++ b/assets/radar-rules.js @@ -322,9 +322,10 @@ { title: '用户文章', docs: 'https://docs.rsshub.app/social-media.html#zhi-hu', - source: '/people/:id/posts', - target: '/zhihu/people/posts/:id', + source: '/:usertype/:id/posts', + target: '/zhihu/posts/:usertype/: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 0d4578571..cd624aa97 100644 --- a/docs/social-media.md +++ b/docs/social-media.md @@ -1106,7 +1106,11 @@ rule ### 用户文章 - + + +| 普通用户 | 机构用户 | +| -------- | -------- | +| people | org | ### 专栏 diff --git a/lib/router.js b/lib/router.js index 2f5546092..7ee8df841 100644 --- a/lib/router.js +++ b/lib/router.js @@ -146,7 +146,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/posts/:usertype/: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/daily/section/:sectionId', require('./routes/zhihu/daily_section')); diff --git a/lib/routes/zhihu/posts.js b/lib/routes/zhihu/posts.js index 30220d960..470f5e0f6 100644 --- a/lib/routes/zhihu/posts.js +++ b/lib/routes/zhihu/posts.js @@ -4,60 +4,41 @@ const cheerio = require('cheerio'); module.exports = async (ctx) => { const id = ctx.params.id; + const usertype = ctx.params.usertype; const response = await got({ method: 'get', - url: `https://www.zhihu.com/api/v4/members/${id}/articles?limit=5`, + url: `https://www.zhihu.com/${usertype}/${id}/posts`, headers: { ...utils.header, - Referer: `https://www.zhihu.com/people/${id}/posts`, - Authorization: 'oauth c3cef7c66a1843f8b3a9e6a1e3160e20', // hard-coded in js + Referer: `https://www.zhihu.com/${usertype}/${id}/`, }, }); + const data = response.data; + const $ = cheerio.load(data); + const jsondata = $('#js-initialData'); + const authorname = $('.ProfileHeader-name').text(); + const authordescription = $('.ProfileHeader-headline').text(); - const data = response.data.data; + const parsed = JSON.parse(jsondata.html()); + const articlesdata = parsed.initialState.entities.articles; - 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); - }) - ); + const list = []; + Object.keys(articlesdata).forEach((v) => { + list.push(articlesdata[v]); + }); 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, + title: `${authorname}的知乎文章`, + link: `https://www.zhihu.com/${usertype}/${id}/posts`, + description: `${authordescription}`, + item: + list.length > 0 && + list.map((item) => ({ + title: String(item.title), + description: `${item.content}`, + link: `${item.url}`, + pubDate: new Date(item.updated).toUTCString(), + })), }; };