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
This commit is contained in:
Entropy
2024-08-19 03:29:36 +08:00
committed by GitHub
parent 7447f446e0
commit 04e766d0f5
2 changed files with 14 additions and 6 deletions
+5 -1
View File
@@ -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({
+9 -5
View File
@@ -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,