From a8d9bf68427b2d311b5f1c782e90415548b09a9e Mon Sep 17 00:00:00 2001 From: zpcc Date: Tue, 11 Oct 2022 19:32:28 +0800 Subject: [PATCH] fix(route): zhihu daily (#11060) --- lib/v2/zhihu/daily.js | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/lib/v2/zhihu/daily.js b/lib/v2/zhihu/daily.js index 5265bd1847..b6468e6f09 100644 --- a/lib/v2/zhihu/daily.js +++ b/lib/v2/zhihu/daily.js @@ -5,15 +5,38 @@ const { parseDate } = require('@/utils/parse-date'); // 参考:https://github.com/izzyleung/ZhihuDailyPurify/wiki/%E7%9F%A5%E4%B9%8E%E6%97%A5%E6%8A%A5-API-%E5%88%86%E6%9E%90 // 文章给出了v4版 api的信息,包含全文api +async function dohResolve(name) { + const response = await got('https://223.5.5.5/resolve', { + searchParams: { + name, + type: 'A', + edns_client_subnet: '223.5.5.5', // 使用国内的ip地址 + }, + headers: { + accept: 'application/dns-json', + }, + }); + return response.data.Answer.map((item) => item.data); +} + module.exports = async (ctx) => { const api = 'https://news-at.zhihu.com/api/4/news'; + const HOST = 'news-at.zhihu.com'; + let address = HOST; + try { + await got(`${api}/latest`); + } catch (e) { + address = (await dohResolve(HOST)).pop(); // 国外ip证书配置有误,解析到国内地址 + } const listRes = await got({ method: 'get', url: `${api}/latest`, headers: { ...utils.header, Referer: `${api}/latest`, + Host: HOST, }, + host: address, }); // 根据api的说明,过滤掉极个别站外链接 const storyList = listRes.data.stories.filter((el) => el.type === 0); @@ -36,7 +59,9 @@ module.exports = async (ctx) => { url, headers: { Referer: url, + Host: HOST, }, + host: address, }); return utils.ProcessImage(storyDetail.data.body.replace(/
([\s\S]*?)<\/div>/g, '$1').replace(/<\/?h2.*?>/g, '')); });