* style: add eslint-unicorn * style: fix unicorn/no-useless-spread * style: fix unicorn/no-useless-promise-resolve-reject * style: fix unicorn/no-for-loop * fix: codeql bad HTML filtering regexp * fix: codeql incomplete replace * fix: unicorn/no-abusive-eslint-disable * style: fix unicorn/no-new-array * style: fix unicorn/no-typeof-undefined * style: fix unicorn/no-zero-fractions * style: fix unicorn/no-empty-file * style: fix unicorn/prefer-date-now * revert: auto fix unicorn/prefer-switch on lib/v2/kuaidi100/utils.js * style: fix unicorn/prefer-array-find * style: fix unicorn/prefer-array-flat * style: fix unicorn/prefer-array-flat-map * style: fix unicorn/prefer-at * style: fix unicorn/prefer-string-starts-ends-with * style: fix unicorn/prefer-includes * fix: codeql URL substring sanitization * style: fix unicorn/prefer-optional-catch-binding * style: fix unicorn/catch-error-name * style: fix unicorn/escape-case * style: fix unicorn/prefer-native-coercion-functions * style: fix unicorn/prefer-regexp-test * style: fix unicorn/require-array-join-separator * style: fix unicorn/prefer-math-trunc * style: fix unicorn/prefer-negative-index * style: fix unicorn/prefer-dom-node-dataset * style: fix unicorn/prefer-dom-node-text-content * style: fix unicorn/prefer-query-selector * style: fix unicorn/no-array-for-each * style: fix unicorn/no-negated-condition * style: fix unicorn/prefer-add-event-listener * style: fix unicorn/import-style * style: fix prefer-regex-literals * style: disable unicorn/no-useless-switch-case * style: disable unicorn/text-encoding-identifier-case * style: fix unicorn/prefer-set-has * style: fix unicorn/prefer-spread * revert: auto fix on lib/routes/universities/ynnu/edu/base64.js * style: fix unicorn/no-useless-undefined * style: fix unicorn/no-array-push-push * style: fix unicorn/no-useless-undefined again * style: fix unicorn/no-lonely-if * style: fix unicorn/prefer-reflect-apply * style: fix unicorn/switch-case-braces * style: fix unicorn/prefer-switch * style: fix unicorn/prefer-array-some * fix: deepscan UNUSED_VAR_ASSIGN * style: fix unicorn/prefer-ternary * fix: follow-up of unicorn/prefer-ternary * revert: auto fix of unicorn/prefer-string-slice for substring() * style: disable unicorn/prefer-string-slice fix: auto fix slice over deprecated substr * style: fix unicorn/throw-new-error * style: fix unicorn/filename-case * test: fix dateParser renaming * style: fix unicorn/better-regex * style: fix unicorn/prefer-string-replace-all * fix(deps): add sanitize-html * style: fix no-prototype-builtins * style: fix unicorn/consistent-destructuring * style: fix unicorn/consistent-function-scoping * style: fix unicorn/prefer-regexp-test * style: fix unicorn/prefer-logical-operator-over-ternary * style: fix unicorn/no-array-callback-reference * style: add prefer-object-has-own * style: warn unicorn/no-empty-file * style: fix unicorn/prefer-number-properties * style: fix no-useless-undefined again * style: fix unicorn/numeric-separators-style * style: disable unicorn/no-array-callback-reference false postive with cheerio
81 lines
3.7 KiB
JavaScript
81 lines
3.7 KiB
JavaScript
const got = require('@/utils/got');
|
|
const utils = require('./utils');
|
|
const md5 = require('@/utils/md5');
|
|
const g_encrypt = require('./execlib/x-zse-96-v3');
|
|
const { parseDate } = require('@/utils/parse-date');
|
|
|
|
module.exports = async (ctx) => {
|
|
const {
|
|
questionId,
|
|
sortBy = 'default', // default,created,updated
|
|
} = ctx.params;
|
|
// 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
|
|
// require users to set the cookie in environmental variables anymore.
|
|
|
|
// fisrt: get cookie(dc_0) from zhihu.com
|
|
const cookie_mes = await ctx.cache.tryGet('zhihu:cookies:d_c0', async () => {
|
|
const response = await got({
|
|
method: 'get',
|
|
url: `https://www.zhihu.com/question/${questionId}`,
|
|
headers: {
|
|
...utils.header,
|
|
},
|
|
});
|
|
|
|
const cookie_org = response.headers['set-cookie'];
|
|
const cookie = cookie_org.toString();
|
|
const match = cookie.match(/d_c0=(\S+?)(?:;|$)/);
|
|
const cookie_mes = match && match[1];
|
|
if (!cookie_mes) {
|
|
throw new Error('Failed to extract `d_c0` from cookies');
|
|
}
|
|
return cookie_mes;
|
|
});
|
|
const cookie = `d_c0=${cookie_mes}`;
|
|
|
|
// second: get real data from zhihu
|
|
const limit = 20;
|
|
const include =
|
|
'data%5B*%5D.is_normal%2Cadmin_closed_comment%2Creward_info%2Cis_collapsed%2Cannotation_action%2Cannotation_detail%2Ccollapse_reason%2Cis_sticky%2Ccollapsed_by%2Csuggest_edit%2Ccomment_count%2Ccan_comment%2Ccontent%2Ceditable_content%2Cattachment%2Cvoteup_count%2Creshipment_settings%2Ccomment_permission%2Ccreated_time%2Cupdated_time%2Creview_info%2Crelevant_info%2Cquestion%2Cexcerpt%2Cis_labeled%2Cpaid_info%2Cpaid_info_content%2Crelationship.is_authorized%2Cis_author%2Cvoting%2Cis_thanked%2Cis_nothelp%2Cis_recognized%3Bdata%5B*%5D.mark_infos%5B*%5D.url%3Bdata%5B*%5D.author.follower_count%2Cbadge%5B*%5D.topics%3Bdata%5B*%5D.settings.table_of_content.enabled&offset=0';
|
|
const rootUrl = 'https://www.zhihu.com';
|
|
const apiPath = `/api/v4/questions/${questionId}/answers?include=${include}&limit=${limit}&sort_by=${sortBy}&platform=desktop`;
|
|
const url = rootUrl + apiPath;
|
|
|
|
// calculate x-zse-96, refer to https://github.com/srx-2000/spider_collection/issues/18
|
|
const f = `101_3_3.0+${apiPath}+${cookie_mes}`;
|
|
const xzse96 = '2.0_' + g_encrypt(md5(f));
|
|
const _header = { cookie, 'x-zse-96': xzse96, 'x-app-za': 'OS=Web', 'x-zse-93': '101_3_3.0' };
|
|
|
|
const response = await got({
|
|
method: 'get',
|
|
url,
|
|
headers: {
|
|
...utils.header,
|
|
..._header,
|
|
Referer: `https://www.zhihu.com/question/${questionId}`,
|
|
// Authorization: 'oauth c3cef7c66a1843f8b3a9e6a1e3160e20', // previously hard-coded in js, outdated
|
|
},
|
|
});
|
|
|
|
const listRes = response.data.data;
|
|
|
|
ctx.state.data = {
|
|
title: `知乎-${listRes[0].question.title}`,
|
|
link: `https://www.zhihu.com/question/${questionId}`,
|
|
item: listRes.map((item) => {
|
|
const title = `${item.author.name}的回答:${item.excerpt}`;
|
|
const description = `${item.author.name}的回答<br/><br/>${utils.ProcessImage(item.content)}`;
|
|
|
|
return {
|
|
title,
|
|
description,
|
|
author: item.author.name,
|
|
pubDate: parseDate(item.updated_time * 1000),
|
|
guid: item.id.toString(),
|
|
link: `https://www.zhihu.com/question/${questionId}/answer/${item.id}`,
|
|
};
|
|
}),
|
|
};
|
|
};
|