|
1 | 1 | import { Route } from '@/types'; |
2 | | -import cache from '@/utils/cache'; |
3 | 2 | import got from '@/utils/got'; |
4 | | -import { getCookieValueByKey, header, processImage } from './utils'; |
| 3 | +import { header, getSignedHeader } from './utils'; |
5 | 4 | import { parseDate } from '@/utils/parse-date'; |
6 | 5 |
|
7 | 6 | export const route: Route = { |
@@ -30,66 +29,39 @@ export const route: Route = { |
30 | 29 | async function handler(ctx) { |
31 | 30 | const id = ctx.req.param('id'); |
32 | 31 |
|
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`; |
34 | 34 |
|
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); |
40 | 36 |
|
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 | + }, |
45 | 44 | }); |
46 | 45 |
|
47 | 46 | 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; |
53 | 53 |
|
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 | + }); |
90 | 62 |
|
91 | 63 | return { |
92 | | - title: `${name}的知乎回答`, |
| 64 | + title: `${data[0].author.name}的知乎回答`, |
93 | 65 | link: `https://www.zhihu.com/people/${id}/answers`, |
94 | 66 | item: items, |
95 | 67 | }; |
|
0 commit comments