From 930f09c17487a032192decd687cbd221bb1df1d4 Mon Sep 17 00:00:00 2001 From: JimenezLi <75196426+JimenezLi@users.noreply.github.com> Date: Sun, 17 Dec 2023 22:38:44 +0800 Subject: [PATCH] feat(route): add xhu people activities and answers (#14063) --- lib/v2/zhihu/maintainer.js | 2 + lib/v2/zhihu/radar.js | 18 +++++ lib/v2/zhihu/router.js | 2 + lib/v2/zhihu/xhu/activities.js | 112 +++++++++++++++++++++++++++ lib/v2/zhihu/xhu/answers.js | 31 ++++++++ website/docs/routes/social-media.mdx | 30 +++++-- 6 files changed, 187 insertions(+), 8 deletions(-) create mode 100644 lib/v2/zhihu/xhu/activities.js create mode 100644 lib/v2/zhihu/xhu/answers.js diff --git a/lib/v2/zhihu/maintainer.js b/lib/v2/zhihu/maintainer.js index b7071060b1..8c2cdc42ac 100644 --- a/lib/v2/zhihu/maintainer.js +++ b/lib/v2/zhihu/maintainer.js @@ -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'], diff --git a/lib/v2/zhihu/radar.js b/lib/v2/zhihu/radar.js index ed051f7cb1..d0833cb803 100644 --- a/lib/v2/zhihu/radar.js +++ b/lib/v2/zhihu/radar.js @@ -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', diff --git a/lib/v2/zhihu/router.js b/lib/v2/zhihu/router.js index 4e09cd9f79..bdeea3fe3d 100644 --- a/lib/v2/zhihu/router.js +++ b/lib/v2/zhihu/router.js @@ -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')); diff --git a/lib/v2/zhihu/xhu/activities.js b/lib/v2/zhihu/xhu/activities.js new file mode 100644 index 0000000000..4e44f6e7a0 --- /dev/null +++ b/lib/v2/zhihu/xhu/activities.js @@ -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 = `
${contentItem.own_text}
`; + } else if (contentItem.type === 'image') { + images.push(`${detail.intro}
${detail.introduction}
话题关注者人数:${detail.followers_count}
`; + url = `https://www.zhihu.com/topic/${detail.id}`; + break; + case 'live': + title = detail.subject; + description = detail.description.replace(/\n|\r/g, '