From 33336a95d59041b6dc2f290d2222b3c426f8ea60 Mon Sep 17 00:00:00 2001 From: Yuan Xu Date: Wed, 28 May 2025 02:46:52 +0800 Subject: [PATCH] fix: repair some zhihu routes (#19183) --- lib/routes/zhihu/all-collections.ts | 11 ++++++++++- lib/routes/zhihu/collection.ts | 10 +++++++++- lib/routes/zhihu/topic.ts | 16 +++++++++++----- lib/routes/zhihu/zhuanlan.ts | 9 +++++++-- 4 files changed, 37 insertions(+), 9 deletions(-) diff --git a/lib/routes/zhihu/all-collections.ts b/lib/routes/zhihu/all-collections.ts index 31d0e0c38..4020de3ed 100644 --- a/lib/routes/zhihu/all-collections.ts +++ b/lib/routes/zhihu/all-collections.ts @@ -3,6 +3,7 @@ import got from '@/utils/got'; import { header } from './utils'; import { parseDate } from '@/utils/parse-date'; import cache from '@/utils/cache'; +import { config } from '@/config'; export const route: Route = { path: '/people/allCollections/:id', @@ -11,7 +12,12 @@ export const route: Route = { example: '/zhihu/people/allCollections/87-44-49-67', parameters: { id: '作者 id,可在用户主页 URL 中找到' }, features: { - requireConfig: false, + requireConfig: [ + { + name: 'ZHIHU_COOKIES', + description: '', + }, + ], requirePuppeteer: false, antiCrawler: true, supportBT: false, @@ -35,6 +41,7 @@ async function handler(ctx) { const response = await got(apiPath, { headers: { + cookie: config.zhihu.cookies, Referer: `https://www.zhihu.com/people/${id}/collections`, }, }); @@ -46,6 +53,7 @@ async function handler(ctx) { const firstPageResponse = await got(`https://www.zhihu.com/api/v4/collections/${collection.id}/items?offset=0&limit=20`, { headers: { ...header, + cookie: config.zhihu.cookies, Referer: `https://www.zhihu.com/collection/${collection.id}`, }, }); @@ -64,6 +72,7 @@ async function handler(ctx) { const response = await got(`https://www.zhihu.com/api/v4/collections/${collection.id}/items?offset=${offset}&limit=20`, { headers: { ...header, + cookie: config.zhihu.cookies, Referer: `https://www.zhihu.com/collection/${collection.id}`, }, }); diff --git a/lib/routes/zhihu/collection.ts b/lib/routes/zhihu/collection.ts index 43489c3ba..34ec64321 100644 --- a/lib/routes/zhihu/collection.ts +++ b/lib/routes/zhihu/collection.ts @@ -5,6 +5,7 @@ import { load } from 'cheerio'; import { header } from './utils'; import { generateData } from './pin/utils'; import { parseDate } from '@/utils/parse-date'; +import { config } from '@/config'; export const route: Route = { path: '/collection/:id/:getAll?', @@ -12,7 +13,12 @@ export const route: Route = { example: '/zhihu/collection/26444956', parameters: { id: '收藏夹 id,可在收藏夹页面 URL 中找到', getAll: '获取全部收藏内容,任意值为打开' }, features: { - requireConfig: false, + requireConfig: [ + { + name: 'ZHIHU_COOKIES', + description: '', + }, + ], requirePuppeteer: false, antiCrawler: true, supportBT: false, @@ -39,6 +45,7 @@ async function handler(ctx) { url: `https://www.zhihu.com/api/v4/collections/${id}/items?offset=0&limit=20`, headers: { ...header, + cookie: config.zhihu.cookies, Referer: `https://www.zhihu.com/collection/${id}`, }, }); @@ -57,6 +64,7 @@ async function handler(ctx) { url: `https://www.zhihu.com/api/v4/collections/${id}/items?offset=${offset}&limit=20`, headers: { ...header, + cookie: config.zhihu.cookies, Referer: `https://www.zhihu.com/collection/${id}`, }, }); diff --git a/lib/routes/zhihu/topic.ts b/lib/routes/zhihu/topic.ts index 190b1e4df..29bd7e19d 100644 --- a/lib/routes/zhihu/topic.ts +++ b/lib/routes/zhihu/topic.ts @@ -10,7 +10,12 @@ export const route: Route = { example: '/zhihu/topic/19828946', parameters: { topicId: '话题 id', isTop: '仅精华,默认为否,其他值为是' }, features: { - requireConfig: false, + requireConfig: [ + { + name: 'ZHIHU_COOKIES', + description: '', + }, + ], requirePuppeteer: false, antiCrawler: true, supportBT: false, @@ -33,10 +38,11 @@ async function handler(ctx) { const link = `https://www.zhihu.com/topic/${topicId}/${isTop ? 'top-answers' : 'newest'}`; const topicMeta = await 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', - }, + const url = `https://www.zhihu.com/topic/${topicId}`; + const apiPath = `/api/v4/topics/${topicId}/intro?include=content.meta.content.photos`; + const signedHeader = await getSignedHeader(url, apiPath); + const { data: response } = await got('https://www.zhihu.com' + apiPath, { + headers: signedHeader, }); return response; }); diff --git a/lib/routes/zhihu/zhuanlan.ts b/lib/routes/zhihu/zhuanlan.ts index 0088997fb..a7cfbf107 100644 --- a/lib/routes/zhihu/zhuanlan.ts +++ b/lib/routes/zhihu/zhuanlan.ts @@ -10,7 +10,12 @@ export const route: Route = { example: '/zhihu/zhuanlan/googledevelopers', parameters: { id: '专栏 id,可在专栏主页 URL 中找到' }, features: { - requireConfig: false, + requireConfig: [ + { + name: 'ZHIHU_COOKIES', + description: '', + }, + ], requirePuppeteer: false, antiCrawler: true, supportBT: false, @@ -47,7 +52,7 @@ async function handler(ctx) { const pinnedRes = await got({ method: 'get', - url: `https://www.zhihu.com/api/v4/columns/${id}/pinned-items`, + url: `https://www.zhihu.com/api/v4/columns/${id}/pinned-items/v2`, headers: { ...header, ...signedHeader,