Skip to content

Commit 24f7bbe

Browse files
authored
fix(route/zhihu): Use Web API for zhihu answers (#18550)
1 parent 333ddab commit 24f7bbe

File tree

1 file changed

+26
-54
lines changed

1 file changed

+26
-54
lines changed

lib/routes/zhihu/answers.ts

Lines changed: 26 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { Route } from '@/types';
2-
import cache from '@/utils/cache';
32
import got from '@/utils/got';
4-
import { getCookieValueByKey, header, processImage } from './utils';
3+
import { header, getSignedHeader } from './utils';
54
import { parseDate } from '@/utils/parse-date';
65

76
export const route: Route = {
@@ -30,66 +29,39 @@ export const route: Route = {
3029
async function handler(ctx) {
3130
const id = ctx.req.param('id');
3231

33-
const zc0 = getCookieValueByKey('z_c0');
32+
// second: get real data from zhihu
33+
const apiPath = `/api/v4/members/${id}/answers?limit=7&include=data[*].is_normal,content`;
3434

35-
const headers = {
36-
'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',
37-
Referer: `https://www.zhihu.com/people/${id}/answers`,
38-
Cookie: zc0 ? `z_c0=${zc0}` : '',
39-
};
35+
const signedHeader = await getSignedHeader(`https://www.zhihu.com/people/${id}`, apiPath);
4036

41-
const response = await got({
42-
method: 'get',
43-
url: `https://api.zhihu.com/people/${id}/answers?order_by=created&offset=0&limit=10`,
44-
headers,
37+
const response = await got(`https://www.zhihu.com${apiPath}`, {
38+
headers: {
39+
...header,
40+
...signedHeader,
41+
Referer: `https://www.zhihu.com/people/${id}/activities`,
42+
// Authorization: 'oauth c3cef7c66a1843f8b3a9e6a1e3160e20', // hard-coded in js
43+
},
4544
});
4645

4746
const data = response.data.data;
48-
let name = data[0].author.name;
49-
50-
if (name === '知乎用户') {
51-
const userProfile = await cache.tryGet(`zhihu:profile:${id}`, async () => {
52-
const apiPath = `/api/v4/members/${id}`;
47+
const items = data.map((item) => {
48+
const title = item.question.title;
49+
// let description = processImage(detail.content);
50+
const url = `https://www.zhihu.com/question/${item.question.id}/answer/${item.id}`;
51+
const author = item.author.name;
52+
const description = item.content;
5353

54-
const { data } = await got({
55-
method: 'get',
56-
url: `https://www.zhihu.com${apiPath}`,
57-
headers: {
58-
...header,
59-
Referer: `https://www.zhihu.com/people/${id}`,
60-
},
61-
});
62-
return data;
63-
});
64-
name = userProfile.name;
65-
}
66-
67-
const items = await Promise.all(
68-
data.map(async (item) => {
69-
let description;
70-
const link = `https://www.zhihu.com/question/${item.question.id}/answer/${item.id}`;
71-
const title = item.question.title;
72-
try {
73-
const detail = await got({
74-
method: 'get',
75-
url: `https://api.zhihu.com/appview/api/v4/answers/${item.id}?include=content&is_appview=true`,
76-
headers,
77-
});
78-
description = processImage(detail.data.content);
79-
} catch {
80-
description = `<a href="${link}" target="_blank">${title}</a>`;
81-
}
82-
return {
83-
title,
84-
description,
85-
pubDate: parseDate(item.created_time * 1000),
86-
link,
87-
};
88-
})
89-
);
54+
return {
55+
title,
56+
author,
57+
description,
58+
pubDate: parseDate(item.created_time * 1000),
59+
link: url,
60+
};
61+
});
9062

9163
return {
92-
title: `${name}的知乎回答`,
64+
title: `${data[0].author.name}的知乎回答`,
9365
link: `https://www.zhihu.com/people/${id}/answers`,
9466
item: items,
9567
};

0 commit comments

Comments
 (0)