fix: 知乎用户文章 (#6760)
* fix 知乎用户文章 * fix a problem reported by DeepScan * [CodeFactor] Apply fixes to commit 487521b [ci skip] [skip ci] Co-authored-by: codefactor-io <support@codefactor.io>
This commit is contained in:
parent
ce1a525fcb
commit
f3e7f55c57
|
|
@ -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',
|
||||
|
|
|
|||
|
|
@ -1106,7 +1106,11 @@ rule
|
|||
|
||||
### 用户文章
|
||||
|
||||
<Route author="whtsky" example="/zhihu/people/posts/dcjanus" path="/zhihu/people/posts/:id" :paramsDesc="['作者 id, 可在用户主页 URL 中找到']" anticrawler="1" radar="1" rssbud="1"/>
|
||||
<Route author="whtsky Colin-XKL" example="/zhihu/posts/people/frederchen" path="/zhihu/posts/:usertype/:id" :paramsDesc="['作者 id, 可在用户主页 URL 中找到', '用户类型usertype,参考用户主页的URL。目前有两种,见下表']" anticrawler="1" radar="1" rssbud="1"/>
|
||||
|
||||
| 普通用户 | 机构用户 |
|
||||
| -------- | -------- |
|
||||
| people | org |
|
||||
|
||||
### 专栏
|
||||
|
||||
|
|
|
|||
|
|
@ -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'));
|
||||
|
|
|
|||
|
|
@ -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(),
|
||||
})),
|
||||
};
|
||||
};
|
||||
|
|
|
|||
Loading…
Reference in New Issue