feat(route): zhihu top topic (#14431)
This commit is contained in:
@@ -13,7 +13,7 @@ module.exports = {
|
||||
'/posts/:usertype/:id': ['whtsky', 'Colin-XKL'],
|
||||
'/question/:questionId': ['xyqfer', 'hacklu'],
|
||||
'/timeline': ['SeanChao'],
|
||||
'/topic/:topicId': ['xyqfer'],
|
||||
'/topic/:topicId/:isTop?': ['xyqfer'],
|
||||
'/weekly': ['LogicJake'],
|
||||
'/xhu/collection/:id': ['JimenezLi'],
|
||||
'/xhu/people/activities/:hexId': ['JimenezLi'],
|
||||
|
||||
@@ -13,7 +13,7 @@ module.exports = (router) => {
|
||||
router.get('/posts/:usertype/:id', require('./posts'));
|
||||
router.get('/question/:questionId/:sortBy?', require('./question'));
|
||||
router.get('/timeline', require('./timeline'));
|
||||
router.get('/topic/:topicId', require('./topic'));
|
||||
router.get('/topic/:topicId/:isTop?', require('./topic'));
|
||||
router.get('/weekly', require('./weekly'));
|
||||
router.get('/xhu/collection/:id', require('./xhu/collection'));
|
||||
router.get('/xhu/people/activities/:hexId', require('./xhu/activities'));
|
||||
|
||||
+96
-53
@@ -1,68 +1,111 @@
|
||||
const got = require('@/utils/got');
|
||||
const utils = require('./utils');
|
||||
const { parseDate } = require('@/utils/parse-date');
|
||||
const g_encrypt = require('./execlib/x-zse-96-v3');
|
||||
const md5 = require('@/utils/md5');
|
||||
|
||||
module.exports = async (ctx) => {
|
||||
const { topicId } = ctx.params;
|
||||
const link = `https://www.zhihu.com/topic/${topicId}/newest`;
|
||||
const url = `https://www.zhihu.com/api/v4/topics/${topicId}/feeds/timeline_activity?include=data%5B%3F%28target.type%3Dtopic_sticky_module%29%5D.target.data%5B%3F%28target.type%3Danswer%29%5D.target.content%2Crelationship.is_authorized%2Cis_author%2Cvoting%2Cis_thanked%2Cis_nothelp%3Bdata%5B%3F%28target.type%3Dtopic_sticky_module%29%5D.target.data%5B%3F%28target.type%3Danswer%29%5D.target.is_normal%2Ccomment_count%2Cvoteup_count%2Ccontent%2Crelevant_info%2Cexcerpt.author.badge%5B%3F%28type%3Dbest_answerer%29%5D.topics%3Bdata%5B%3F%28target.type%3Dtopic_sticky_module%29%5D.target.data%5B%3F%28target.type%3Darticle%29%5D.target.content%2Cvoteup_count%2Ccomment_count%2Cvoting%2Cauthor.badge%5B%3F%28type%3Dbest_answerer%29%5D.topics%3Bdata%5B%3F%28target.type%3Dtopic_sticky_module%29%5D.target.data%5B%3F%28target.type%3Dpeople%29%5D.target.answer_count%2Carticles_count%2Cgender%2Cfollower_count%2Cis_followed%2Cis_following%2Cbadge%5B%3F%28type%3Dbest_answerer%29%5D.topics%3Bdata%5B%3F%28target.type%3Danswer%29%5D.target.annotation_detail%2Ccontent%2Crelationship.is_authorized%2Cis_author%2Cvoting%2Cis_thanked%2Cis_nothelp%3Bdata%5B%3F%28target.type%3Danswer%29%5D.target.author.badge%5B%3F%28type%3Dbest_answerer%29%5D.topics%3Bdata%5B%3F%28target.type%3Darticle%29%5D.target.annotation_detail%2Ccontent%2Cauthor.badge%5B%3F%28type%3Dbest_answerer%29%5D.topics%3Bdata%5B%3F%28target.type%3Dquestion%29%5D.target.annotation_detail%2Ccomment_count&limit=20`;
|
||||
const response = await got({
|
||||
method: 'get',
|
||||
url,
|
||||
const { topicId, isTop = false } = ctx.params;
|
||||
const link = `https://www.zhihu.com/topic/${topicId}/${isTop ? 'top-answers' : 'newest'}`;
|
||||
|
||||
const cookieDc0 = await ctx.cache.tryGet('zhihu:cookies:d_c0', async () => {
|
||||
const { headers } = await got(link, {
|
||||
headers: {
|
||||
...utils.header,
|
||||
},
|
||||
});
|
||||
const cookie = headers['set-cookie'].toString();
|
||||
const match = cookie.match(/d_c0=(\S+?)(?:;|$)/);
|
||||
const cookieMatched = match?.[1];
|
||||
if (!cookieMatched) {
|
||||
throw new Error('Failed to extract `d_c0` from cookies');
|
||||
}
|
||||
return cookieMatched;
|
||||
});
|
||||
const cookie = `d_c0=${cookieDc0}`;
|
||||
|
||||
const topicMeta = await ctx.cache.tryGet(`zhihu:topic:${topicId}`, async () => {
|
||||
const { data: response } = await got(`https://www.zhihu.com/api/v4/topics/${topicId}/intro`, {
|
||||
searchParams: {
|
||||
include: 'content.meta.content.photos',
|
||||
},
|
||||
});
|
||||
return response;
|
||||
});
|
||||
|
||||
const apiPath = `/api/v5.1/topics/${topicId}/feeds/${isTop ? 'top_activity' : 'timeline_activity'}`;
|
||||
const xzse93 = '101_3_3.0';
|
||||
const f = `${xzse93}+${apiPath}+${cookieDc0}`;
|
||||
const xzse96 = '2.0_' + g_encrypt(md5(f));
|
||||
const _header = { cookie, 'x-zse-96': xzse96, 'x-zse-93': xzse93 };
|
||||
|
||||
const { data: response } = await got(`https://www.zhihu.com${apiPath}`, {
|
||||
headers: {
|
||||
...utils.header,
|
||||
..._header,
|
||||
Referer: link,
|
||||
Authorization: 'oauth c3cef7c66a1843f8b3a9e6a1e3160e20', // hard-coded in js
|
||||
},
|
||||
});
|
||||
const listRes = response.data.data;
|
||||
|
||||
const items = response.data.map(({ target: item }) => {
|
||||
const type = item.type;
|
||||
let title = '';
|
||||
let description = '';
|
||||
let link = '';
|
||||
let pubDate = '';
|
||||
let author = '';
|
||||
|
||||
switch (type) {
|
||||
case 'answer':
|
||||
title = `${item.question.title}-${item.author.name}的回答:${item.excerpt}`;
|
||||
description = `<strong>${item.question.title}</strong><br>${item.author.name}的回答<br/>${utils.ProcessImage(item.content)}`;
|
||||
link = `https://www.zhihu.com/question/${item.question.id}/answer/${item.id}`;
|
||||
pubDate = parseDate(item.updated_time * 1000);
|
||||
author = item.author.name;
|
||||
break;
|
||||
|
||||
case 'question':
|
||||
title = item.title;
|
||||
description = item.title;
|
||||
link = `https://www.zhihu.com/question/${item.id}`;
|
||||
pubDate = parseDate(item.created * 1000);
|
||||
break;
|
||||
|
||||
case 'article':
|
||||
title = item.title;
|
||||
description = utils.ProcessImage(item.content);
|
||||
link = item.url;
|
||||
pubDate = parseDate(item.created * 1000);
|
||||
break;
|
||||
|
||||
case 'zvideo':
|
||||
title = item.title;
|
||||
description = `${item.description}<br>
|
||||
<video controls poster="${item.video.thumbnail}" preload="none">
|
||||
<source src="${item.video.playlist.fhd?.url ?? item.video.playlist.hd?.url ?? item.video.playlist.ld?.url ?? item.video.playlist.sd?.url}" type="video/mp4">
|
||||
</video>`;
|
||||
link = item.url;
|
||||
pubDate = parseDate(item.created_at * 1000);
|
||||
break;
|
||||
|
||||
default:
|
||||
description = '未知类型,请点击<a href="https://github.com/DIYgod/RSSHub/issues">链接</a>提交issue';
|
||||
}
|
||||
|
||||
return {
|
||||
title,
|
||||
description,
|
||||
author,
|
||||
pubDate,
|
||||
guid: item.id.toString(),
|
||||
link,
|
||||
};
|
||||
});
|
||||
|
||||
ctx.state.data = {
|
||||
title: `知乎话题-${topicId}`,
|
||||
title: `知乎话题-${topicId}-${isTop ? '精华' : '讨论'}`,
|
||||
description: topicMeta.content.introduction,
|
||||
link,
|
||||
item: listRes.map(({ target: item }) => {
|
||||
const type = item.type;
|
||||
let title = '';
|
||||
let description = '';
|
||||
let link = '';
|
||||
let pubDate = '';
|
||||
let author = '';
|
||||
|
||||
switch (type) {
|
||||
case 'answer':
|
||||
title = `${item.question.title}-${item.author.name}的回答:${item.excerpt}`;
|
||||
description = `<strong>${item.question.title}</strong><br>${item.author.name}的回答<br/>${utils.ProcessImage(item.content)}`;
|
||||
link = `https://www.zhihu.com/question/${item.question.id}/answer/${item.id}`;
|
||||
pubDate = parseDate(item.updated_time * 1000);
|
||||
author = item.author.name;
|
||||
break;
|
||||
|
||||
case 'question':
|
||||
title = item.title;
|
||||
description = item.title;
|
||||
link = `https://www.zhihu.com/question/${item.id}`;
|
||||
pubDate = parseDate(item.created * 1000);
|
||||
break;
|
||||
|
||||
case 'article':
|
||||
title = item.title;
|
||||
description = item.content;
|
||||
link = item.url;
|
||||
pubDate = parseDate(item.created * 1000);
|
||||
break;
|
||||
|
||||
default:
|
||||
description = '未知类型,请点击<a href="https://github.com/DIYgod/RSSHub/issues">链接</a>提交issue';
|
||||
}
|
||||
|
||||
return {
|
||||
title,
|
||||
description,
|
||||
author,
|
||||
pubDate,
|
||||
guid: item.id.toString(),
|
||||
link,
|
||||
};
|
||||
}),
|
||||
item: items,
|
||||
};
|
||||
};
|
||||
|
||||
@@ -3,7 +3,6 @@ const cheerio = require('cheerio');
|
||||
module.exports = {
|
||||
header: {
|
||||
'x-api-version': '3.0.91',
|
||||
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.135 Safari/537.36',
|
||||
},
|
||||
ProcessImage(content) {
|
||||
const $ = cheerio.load(content, null, false);
|
||||
|
||||
@@ -1596,19 +1596,19 @@ YouTube provides official RSS feeds for channels, for instance [https://www.yout
|
||||
|
||||
### 收藏夹 {#zhi-hu-shou-cang-jia}
|
||||
|
||||
<Route author="huruji Colin-XKL Fatpandac" example="/zhihu/collection/26444956" path="/zhihu/collection/:id/:getAll?" paramsDesc={['收藏夹 id, 可在收藏夹页面 URL 中找到', '获取全部收藏内容,任意值为打开']} anticrawler="1" radar="1" />
|
||||
<Route author="huruji Colin-XKL Fatpandac" example="/zhihu/collection/26444956" path="/zhihu/collection/:id/:getAll?" paramsDesc={['收藏夹 id,可在收藏夹页面 URL 中找到', '获取全部收藏内容,任意值为打开']} anticrawler="1" radar="1" />
|
||||
|
||||
### 用户动态 {#zhi-hu-yong-hu-dong-tai}
|
||||
|
||||
<Route author="DIYgod" example="/zhihu/people/activities/diygod" path="/zhihu/people/activities/:id" paramsDesc={['作者 id, 可在用户主页 URL 中找到']} anticrawler="1" radar="1" />
|
||||
<Route author="DIYgod" example="/zhihu/people/activities/diygod" path="/zhihu/people/activities/:id" paramsDesc={['作者 id,可在用户主页 URL 中找到']} anticrawler="1" radar="1" />
|
||||
|
||||
### 用户回答 {#zhi-hu-yong-hu-hui-da}
|
||||
|
||||
<Route author="DIYgod prnake" example="/zhihu/people/answers/diygod" path="/zhihu/people/answers/:id" paramsDesc={['作者 id, 可在用户主页 URL 中找到']} anticrawler="1" radar="1" />
|
||||
<Route author="DIYgod prnake" example="/zhihu/people/answers/diygod" path="/zhihu/people/answers/:id" paramsDesc={['作者 id,可在用户主页 URL 中找到']} anticrawler="1" radar="1" />
|
||||
|
||||
### 用户文章 {#zhi-hu-yong-hu-wen-zhang}
|
||||
|
||||
<Route author="whtsky Colin-XKL" example="/zhihu/posts/people/frederchen" path="/zhihu/posts/:usertype/:id" paramsDesc={['作者 id, 可在用户主页 URL 中找到', '用户类型usertype,参考用户主页的URL。目前有两种,见下表']} anticrawler="1" radar="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" />
|
||||
|
||||
| 普通用户 | 机构用户 |
|
||||
| -------- | -------- |
|
||||
@@ -1616,7 +1616,7 @@ YouTube provides official RSS feeds for channels, for instance [https://www.yout
|
||||
|
||||
### 专栏 {#zhi-hu-zhuan-lan}
|
||||
|
||||
<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" />
|
||||
|
||||
### 知乎日报 {#zhi-hu-zhi-hu-ri-bao}
|
||||
|
||||
@@ -1624,7 +1624,7 @@ YouTube provides official RSS feeds for channels, for instance [https://www.yout
|
||||
|
||||
### 知乎日报 - 合集 {#zhi-hu-zhi-hu-ri-bao-he-ji}
|
||||
|
||||
<Route author="ccbikai" example="/zhihu/daily/section/2" path="/zhihu/daily/section/:sectionId" paramsDesc={['合集 id, 可在 https://news-at.zhihu.com/api/7/sections 找到']} anticrawler="1" />
|
||||
<Route author="ccbikai" example="/zhihu/daily/section/2" path="/zhihu/daily/section/:sectionId" paramsDesc={['合集 id,可在 https://news-at.zhihu.com/api/7/sections 找到']} anticrawler="1" />
|
||||
|
||||
### 知乎热榜 {#zhi-hu-zhi-hu-re-bang}
|
||||
|
||||
@@ -1648,11 +1648,11 @@ YouTube provides official RSS feeds for channels, for instance [https://www.yout
|
||||
|
||||
### 话题 {#zhi-hu-hua-ti}
|
||||
|
||||
<Route author="xyqfer" example="/zhihu/topic/19828946" path="/zhihu/topic/:topicId" paramsDesc={['话题 id']} anticrawler="1" radar="1" />
|
||||
<Route author="xyqfer" example="/zhihu/topic/19828946" path="/zhihu/topic/:topicId/:isTop?" paramsDesc={['话题 id', '仅精华,默认为否,其他值为是']} anticrawler="1" radar="1" />
|
||||
|
||||
### 用户想法 {#zhi-hu-yong-hu-xiang-fa}
|
||||
|
||||
<Route author="xyqfer" example="/zhihu/people/pins/kan-dan-45" path="/zhihu/people/pins/:id" paramsDesc={['作者 id, 可在用户主页 URL 中找到']} anticrawler="1" radar="1" />
|
||||
<Route author="xyqfer" example="/zhihu/people/pins/kan-dan-45" path="/zhihu/people/pins/:id" paramsDesc={['作者 id,可在用户主页 URL 中找到']} anticrawler="1" radar="1" />
|
||||
|
||||
### 知乎书店 - 新书 {#zhi-hu-zhi-hu-shu-dian-xin-shu}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user