From 04e766d0f5072f360f4edf984150c71b4e3161d8 Mon Sep 17 00:00:00 2001 From: Entropy Date: Mon, 19 Aug 2024 03:29:36 +0800 Subject: [PATCH] fix(route/zhihu): Fix bug that zhihu answers would be truncated (#16473) * fix(route/zhihu): Fix bug that zhihu answers would be truncated * fix(route/zhihu): Fix bug that zhihu answers would be truncated * fix(route/zhihu): Fix bug that zhihu answers would be truncated * fix(route/zhihu): Fix bug that zhihu answers would be truncated --- lib/routes/zhihu/answers.ts | 6 +++++- lib/routes/zhihu/utils.ts | 14 +++++++++----- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/lib/routes/zhihu/answers.ts b/lib/routes/zhihu/answers.ts index e065bff2a..dbf7ec5a5 100644 --- a/lib/routes/zhihu/answers.ts +++ b/lib/routes/zhihu/answers.ts @@ -1,7 +1,7 @@ import { Route } from '@/types'; import cache from '@/utils/cache'; import got from '@/utils/got'; -import { header, processImage } from './utils'; +import { getCookieValueByKey, header, processImage } from './utils'; import { parseDate } from '@/utils/parse-date'; export const route: Route = { @@ -29,9 +29,13 @@ export const route: Route = { async function handler(ctx) { const id = ctx.req.param('id'); + + const zc0 = getCookieValueByKey('z_c0'); + 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 response = await got({ diff --git a/lib/routes/zhihu/utils.ts b/lib/routes/zhihu/utils.ts index c530ab928..24a4115fa 100644 --- a/lib/routes/zhihu/utils.ts +++ b/lib/routes/zhihu/utils.ts @@ -56,6 +56,13 @@ export const processImage = (content: string) => { return $.html(); }; +export const getCookieValueByKey = (key: string) => + config.zhihu.cookies + ?.split(';') + .map((e) => e.trim()) + .find((e) => e.startsWith(key + '=')) + ?.slice(key.length + 1); + export const getSignedHeader = async (url: string, apiPath: string) => { // Because the API of zhihu.com has changed, we must use the value of `d_c0` (extracted from cookies) to calculate // `x-zse-96`. So first get `d_c0`, then get the actual data of a ZhiHu question. In this way, we don't need to @@ -95,11 +102,8 @@ export const getSignedHeader = async (url: string, apiPath: string) => { const f = `${xzse93}+${apiPath}+${dc0}`; const xzse96 = '2.0_' + g_encrypt(md5(f)); - const zc0 = config.zhihu.cookies - ?.split(';') - .map((e) => e.trim()) - .find((e) => e.includes('z_c0')) - ?.slice('z_c0='.length); + const zc0 = getCookieValueByKey('z_c0'); + return { cookie: `d_c0=${dc0}${zc0 ? `;z_c0=${zc0}` : ''}`, 'x-zse-96': xzse96,