fix(route/zhihu): Use Web API for zhihu answers (#18550)

This commit is contained in:
Andvari
2025-03-12 00:33:46 +08:00
committed by GitHub
parent 333ddabba9
commit 24f7bbe481
+26 -54
View File
@@ -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 = `<a href="${link}" target="_blank">${title}</a>`;
}
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,
};