Files
RSSHub/lib/routes/zhihu/posts.js
T
Colin-XKLandcodefactor-io f3e7f55c57 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>
2021-01-22 18:14:10 -05:00

45 lines
1.4 KiB
JavaScript

const got = require('@/utils/got');
const utils = require('./utils');
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/${usertype}/${id}/posts`,
headers: {
...utils.header,
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 parsed = JSON.parse(jsondata.html());
const articlesdata = parsed.initialState.entities.articles;
const list = [];
Object.keys(articlesdata).forEach((v) => {
list.push(articlesdata[v]);
});
ctx.state.data = {
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(),
})),
};
};