From 24f7bbe4817c0fbf43c44f03c4bb6dc1165f0eb3 Mon Sep 17 00:00:00 2001 From: Andvari <31068367+dzx-dzx@users.noreply.github.com> Date: Wed, 12 Mar 2025 00:33:46 +0800 Subject: [PATCH] fix(route/zhihu): Use Web API for zhihu answers (#18550) --- lib/routes/zhihu/answers.ts | 80 ++++++++++++------------------------- 1 file changed, 26 insertions(+), 54 deletions(-) diff --git a/lib/routes/zhihu/answers.ts b/lib/routes/zhihu/answers.ts index dbf7ec5a5..2ec0f9de3 100644 --- a/lib/routes/zhihu/answers.ts +++ b/lib/routes/zhihu/answers.ts @@ -1,7 +1,6 @@ import { Route } from '@/types'; -import cache from '@/utils/cache'; import got from '@/utils/got'; -import { getCookieValueByKey, header, processImage } from './utils'; +import { header, getSignedHeader } from './utils'; import { parseDate } from '@/utils/parse-date'; export const route: Route = { @@ -30,66 +29,39 @@ export const route: Route = { async function handler(ctx) { const id = ctx.req.param('id'); - const zc0 = getCookieValueByKey('z_c0'); + // second: get real data from zhihu + const apiPath = `/api/v4/members/${id}/answers?limit=7&include=data[*].is_normal,content`; - const headers = { - 'User-Agent': 'ZhihuHybrid com.zhihu.android/Futureve/6.59.0 Mozilla/5.0 (Linux; Android 10; GM1900 Build/QKQ1.190716.003; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/85.0.4183.127 Mobile Safari/537.36', - Referer: `https://www.zhihu.com/people/${id}/answers`, - Cookie: zc0 ? `z_c0=${zc0}` : '', - }; + const signedHeader = await getSignedHeader(`https://www.zhihu.com/people/${id}`, apiPath); - const response = await got({ - method: 'get', - url: `https://api.zhihu.com/people/${id}/answers?order_by=created&offset=0&limit=10`, - headers, + const response = await got(`https://www.zhihu.com${apiPath}`, { + headers: { + ...header, + ...signedHeader, + Referer: `https://www.zhihu.com/people/${id}/activities`, + // Authorization: 'oauth c3cef7c66a1843f8b3a9e6a1e3160e20', // hard-coded in js + }, }); const data = response.data.data; - let name = data[0].author.name; + const items = data.map((item) => { + const title = item.question.title; + // let description = processImage(detail.content); + const url = `https://www.zhihu.com/question/${item.question.id}/answer/${item.id}`; + const author = item.author.name; + const description = item.content; - if (name === '知乎用户') { - const userProfile = await cache.tryGet(`zhihu:profile:${id}`, async () => { - const apiPath = `/api/v4/members/${id}`; - - const { data } = await got({ - method: 'get', - url: `https://www.zhihu.com${apiPath}`, - headers: { - ...header, - Referer: `https://www.zhihu.com/people/${id}`, - }, - }); - return data; - }); - name = userProfile.name; - } - - const items = await Promise.all( - data.map(async (item) => { - let description; - const link = `https://www.zhihu.com/question/${item.question.id}/answer/${item.id}`; - const title = item.question.title; - try { - const detail = await got({ - method: 'get', - url: `https://api.zhihu.com/appview/api/v4/answers/${item.id}?include=content&is_appview=true`, - headers, - }); - description = processImage(detail.data.content); - } catch { - description = `${title}`; - } - return { - title, - description, - pubDate: parseDate(item.created_time * 1000), - link, - }; - }) - ); + return { + title, + author, + description, + pubDate: parseDate(item.created_time * 1000), + link: url, + }; + }); return { - title: `${name}的知乎回答`, + title: `${data[0].author.name}的知乎回答`, link: `https://www.zhihu.com/people/${id}/answers`, item: items, };