feat(route): add xhu people activities and answers (#14063)

This commit is contained in:
JimenezLi
2023-12-17 22:38:44 +08:00
committed by GitHub
parent c576d1a931
commit 930f09c174
6 changed files with 187 additions and 8 deletions

View File

@@ -16,6 +16,8 @@ module.exports = {
'/topic/:topicId': ['xyqfer'],
'/weekly': ['LogicJake'],
'/xhu/collection/:id': ['JimenezLi'],
'/xhu/people/activities/:hexId': ['JimenezLi'],
'/xhu/people/answers/:hexId': ['JimenezLi'],
'/xhu/question/:questionId/:sortBy?': ['JimenezLi'],
'/xhu/topic/:topicId': ['JimenezLi'],
'/xhu/zhuanlan/:id': ['JimenezLi'],

View File

@@ -80,6 +80,24 @@ module.exports = {
source: '/column/:id',
target: '/zhihu/zhuanlan/:id',
},
{
title: 'xhu - 用户动态',
docs: 'https://docs.rsshub.app/routes/social-media#zhi-hu',
source: '/people/:id',
target: (params, url, document) => {
const hexId = /"id":"(.*?)"/.exec(document.getElementById('js-initialData').innerHTML)[1];
return `/zhihu/xhu/people/activities/${hexId}`;
},
},
{
title: 'xhu - 用户回答',
docs: 'https://docs.rsshub.app/routes/social-media#zhi-hu',
source: '/people/:id/answers',
target: (params, url, document) => {
const hexId = /"id":"(.*?)"/.exec(document.getElementById('js-initialData').innerHTML)[1];
return `/zhihu/xhu/people/answers/${hexId}`;
},
},
{
title: 'xhu - 收藏夹',
docs: 'https://docs.rsshub.app/routes/social-media#zhi-hu',

View File

@@ -16,6 +16,8 @@ module.exports = (router) => {
router.get('/topic/:topicId', require('./topic'));
router.get('/weekly', require('./weekly'));
router.get('/xhu/collection/:id', require('./xhu/collection'));
router.get('/xhu/people/activities/:hexId', require('./xhu/activities'));
router.get('/xhu/people/answers/:hexId', require('./xhu/answers'));
router.get('/xhu/question/:questionId/:sortBy?', require('./xhu/question'));
router.get('/xhu/topic/:topicId', require('./xhu/topic'));
router.get('/xhu/zhuanlan/:id', require('./xhu/zhuanlan'));

View File

@@ -0,0 +1,112 @@
const got = require('@/utils/got');
const auth = require('./auth');
const utils = require('../utils');
const { parseDate } = require('@/utils/parse-date');
module.exports = async (ctx) => {
const xhuCookie = await auth.getCookie(ctx);
const { hexId } = ctx.params;
const link = `https://www.zhihu.com/people/${hexId}`;
const url = `https://api.zhihuvvv.workers.dev/people/${hexId}/activities?before_id=0&limit=20`;
const response = await got({
method: 'get',
url,
headers: {
Referer: 'https://api.zhihuvvv.workers.dev',
Cookie: xhuCookie,
},
});
const data = response.data.data;
ctx.state.data = {
title: `${data[0].actor.name}的知乎动态`,
link,
image: data[0].actor.avatar_url,
description: data[0].actor.headline || data[0].actor.description,
item: data.map((item) => {
const detail = item.target;
let title;
let description;
let url;
const images = [];
let text = '';
let link = '';
let author = '';
switch (item.target.type) {
case 'answer':
title = detail.question.title;
author = detail.author.name;
description = utils.ProcessImage(detail.content);
url = `https://www.zhihu.com/question/${detail.question.id}/answer/${detail.id}`;
break;
case 'article':
title = detail.title;
author = detail.author.name;
description = utils.ProcessImage(detail.content);
url = `https://zhuanlan.zhihu.com/p/${detail.id}`;
break;
case 'pin':
title = detail.excerpt_title;
author = detail.author.name;
detail.content.forEach((contentItem) => {
if (contentItem.type === 'text') {
text = `<p>${contentItem.own_text}</p>`;
} else if (contentItem.type === 'image') {
images.push(`<p><img src="${contentItem.url.replace('xl', 'r')}"/></p>`);
} else if (contentItem.type === 'link') {
link = `<p><a href="${contentItem.url}" target="_blank">${contentItem.title}</a></p>`;
} else if (contentItem.type === 'video') {
link = `<p><video
controls="controls"
width="${contentItem.playlist[1].width}"
height="${contentItem.playlist[1].height}"
src="${contentItem.playlist[1].url}"></video></p>`;
}
});
description = `${text}${link}${images.join('')}`;
url = `https://www.zhihu.com/pin/${detail.id}`;
break;
case 'question':
title = detail.title;
author = detail.author.name;
description = utils.ProcessImage(detail.detail);
url = `https://www.zhihu.com/question/${detail.id}`;
break;
case 'collection':
title = detail.title;
url = `https://www.zhihu.com/collection/${detail.id}`;
break;
case 'column':
title = detail.title;
description = `<p>${detail.intro}</p><p><img src="${detail.image_url}"/></p>`;
url = `https://zhuanlan.zhihu.com/${detail.id}`;
break;
case 'topic':
title = detail.name;
description = `<p>${detail.introduction}</p><p>话题关注者人数:${detail.followers_count}</p>`;
url = `https://www.zhihu.com/topic/${detail.id}`;
break;
case 'live':
title = detail.subject;
description = detail.description.replace(/\n|\r/g, '<br>');
url = `https://www.zhihu.com/lives/${detail.id}`;
break;
case 'roundtable':
title = detail.name;
description = detail.description;
url = `https://www.zhihu.com/roundtable/${detail.id}`;
break;
}
return {
title: `${data[0].actor.name}${item.action_text}: ${title}`,
author,
description,
pubDate: parseDate(item.created_time * 1000),
link: url,
};
}),
};
};

View File

@@ -0,0 +1,31 @@
const got = require('@/utils/got');
const auth = require('./auth');
const { parseDate } = require('@/utils/parse-date');
module.exports = async (ctx) => {
const xhuCookie = await auth.getCookie(ctx);
const { hexId } = ctx.params;
const link = `https://www.zhihu.com/people/${hexId}/answers`;
const url = `https://api.zhihuvvv.workers.dev/people/${hexId}/answers?limit=20&offset=0`;
const response = await got({
method: 'get',
url,
headers: {
Referer: 'https://api.zhihuvvv.workers.dev',
Cookie: xhuCookie,
},
});
const data = response.data.data;
ctx.state.data = {
title: `${data[0].author.name}的知乎回答`,
link,
item: data.map((item) => ({
title: item.question.title,
description: item.excerpt,
pubDate: parseDate(item.created_time * 1000),
link: `https://www.zhihu.com/question/${item.question.id}/answer/${item.id}`,
})),
};
};

View File

@@ -1746,18 +1746,32 @@ YouTube provides official RSS feeds for channels, for instance [https://www.yout
:::
</Route>
### [xhu](https://github.com/REToys/xhu) - 收藏夹 {#zhi-hu-shou-cang-jia}
### [xhu](https://github.com/REToys/xhu) - 用户动态 {#zhi-hu-xhu-yong-hu-dong-tai}
<Route author="JimenezLi" example="/zhihu/xhu/collection/26444956" path="/zhihu/xhu/collection/:id" paramsDesc={['收藏夹 id, 可在收藏夹页面 URL 中找到']} />
<Route author="JimenezLi" example="/zhihu/xhu/people/activities/246e6cf44e94cefbf4b959cb5042bc91" path="/zhihu/xhu/people/activities/:hexId" paramsDesc={['用户的 16 进制 id获取方式见下方说明']} radar="1">
:::tip
用户的 16 进制 id 获取方式:
1. 可以通过 RSSHub Radar 扩展获取;
2. 或者在用户主页打开 F12 控制台,执行以下代码:`console.log(/"id":"(.*?)"/.exec(document.getElementById("js-initialData").innerHTML)[1]);` 即可获取用户的 16 进制 id。
:::
</Route>
### [xhu](https://github.com/REToys/xhu) - 专栏 {#zhi-hu-zhuan-lan}
### [xhu](https://github.com/REToys/xhu) - 用户回答{#zhi-hu-xhu-yong-hu-hui-da}
<Route author="JimenezLi" example="/zhihu/xhu/zhuanlan/githubdaily" path="/zhihu/xhu/zhuanlan/:id" paramsDesc={['专栏 id, 可在专栏主页 URL 中找到']} />
<Route author="JimenezLi" example="/zhihu/xhu/people/answers/246e6cf44e94cefbf4b959cb5042bc91" path="/zhihu/xhu/people/answers/:hexId" paramsDesc={['用户的 16 进制 id获取方式同 [xhu - 用户动态](#zhi-hu-xhu-yong-hu-dong-tai)']} radar="1" />
### [xhu](https://github.com/REToys/xhu) - 问题 {#zhi-hu-wen-ti}
### [xhu](https://github.com/REToys/xhu) - 收藏夹 {#zhi-hu-xhu-shou-cang-jia}
<Route author="JimenezLi" example="/zhihu/xhu/question/264051433" path="/zhihu/xhu/question/:questionId/:sortBy?" paramsDesc={['问题 id', '排序方式:`default`, `created`, `updated`。默认为 `default`']} />
<Route author="JimenezLi" example="/zhihu/xhu/collection/26444956" path="/zhihu/xhu/collection/:id" paramsDesc={['收藏夹 id, 可在收藏夹页面 URL 中找到']} radar="1" />
### [xhu](https://github.com/REToys/xhu) - 话题 {#zhi-hu-hua-ti}
### [xhu](https://github.com/REToys/xhu) - 专栏 {#zhi-hu-xhu-zhuan-lan}
<Route author="JimenezLi" example="/zhihu/xhu/topic/19566035" path="/zhihu/xhu/topic/:topicId" paramsDesc={['话题ID']} />
<Route author="JimenezLi" example="/zhihu/xhu/zhuanlan/githubdaily" path="/zhihu/xhu/zhuanlan/:id" paramsDesc={['专栏 id, 可在专栏主页 URL 中找到']} radar="1" />
### [xhu](https://github.com/REToys/xhu) - 问题 {#zhi-hu-xhu-wen-ti}
<Route author="JimenezLi" example="/zhihu/xhu/question/264051433" path="/zhihu/xhu/question/:questionId/:sortBy?" paramsDesc={['问题 id', '排序方式:`default`, `created`, `updated`。默认为 `default`']} radar="1" />
### [xhu](https://github.com/REToys/xhu) - 话题 {#zhi-hu-xhu-hua-ti}
<Route author="JimenezLi" example="/zhihu/xhu/topic/19566035" path="/zhihu/xhu/topic/:topicId" paramsDesc={['话题ID']} radar="1" />