feat: 增加知乎文章 (#3157)

This commit is contained in:
Wu Haotian
2019-09-27 18:28:29 +08:00
committed by DIYgod
parent f9dfb9683d
commit 4202942628
4 changed files with 80 additions and 0 deletions

View File

@@ -226,6 +226,12 @@
source: '/people/:id/pins', source: '/people/:id/pins',
target: '/zhihu/people/pins/:id', 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: '热榜', title: '热榜',
docs: 'https://docs.rsshub.app/social-media.html#zhi-hu', docs: 'https://docs.rsshub.app/social-media.html#zhi-hu',

View File

@@ -630,6 +630,10 @@ pageClass: routes
<Route author="DIYgod" example="/zhihu/people/answers/diygod" path="/zhihu/people/answers/:id" :paramsDesc="['作者 id, 可在用户主页 URL 中找到']" anticrawler="1" radar="1"/> <Route author="DIYgod" example="/zhihu/people/answers/diygod" path="/zhihu/people/answers/:id" :paramsDesc="['作者 id, 可在用户主页 URL 中找到']" anticrawler="1" radar="1"/>
### 用户文章
<Route author="whtsky" example="/zhihu/people/posts/dcjanus" path="/zhihu/people/posts/:id" :paramsDesc="['作者 id, 可在用户主页 URL 中找到']" anticrawler="1" radar="1"/>
### 专栏 ### 专栏
<Route author="DIYgod" example="/zhihu/zhuanlan/googledevelopers" path="/zhihu/zhuanlan/:id" :paramsDesc="['专栏 id, 可在专栏主页 URL 中找到']" anticrawler="1" radar="1"/> <Route author="DIYgod" example="/zhihu/zhuanlan/googledevelopers" path="/zhihu/zhuanlan/:id" :paramsDesc="['专栏 id, 可在专栏主页 URL 中找到']" anticrawler="1" radar="1"/>

View File

@@ -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/collection/:id', require('./routes/zhihu/collection'));
router.get('/zhihu/people/activities/:id', require('./routes/zhihu/activities')); router.get('/zhihu/people/activities/:id', require('./routes/zhihu/activities'));
router.get('/zhihu/people/answers/:id', require('./routes/zhihu/answers')); 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/zhuanlan/:id', require('./routes/zhihu/zhuanlan'));
router.get('/zhihu/daily', require('./routes/zhihu/daily')); router.get('/zhihu/daily', require('./routes/zhihu/daily'));
router.get('/zhihu/hotlist', require('./routes/zhihu/hotlist')); router.get('/zhihu/hotlist', require('./routes/zhihu/hotlist'));

69
lib/routes/zhihu/posts.js Normal file
View File

@@ -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,
};
};