From a50a51286bfc84ee9bc99201efc47d364087d828 Mon Sep 17 00:00:00 2001 From: pseudoyu Date: Tue, 29 Oct 2024 20:44:36 +0700 Subject: [PATCH] fix(route/zhihu): get rid of outdated zhihu daily api and use web html --- lib/routes/zhihu/daily.ts | 94 +++++++++++++-------------------------- 1 file changed, 30 insertions(+), 64 deletions(-) diff --git a/lib/routes/zhihu/daily.ts b/lib/routes/zhihu/daily.ts index 342664357..5cd788ed7 100644 --- a/lib/routes/zhihu/daily.ts +++ b/lib/routes/zhihu/daily.ts @@ -1,25 +1,7 @@ import { Route } from '@/types'; +import ofetch from '@/utils/ofetch'; +import { load } from 'cheerio'; import cache from '@/utils/cache'; -import got from '@/utils/got'; -import { header, processImage } from './utils'; -import { parseDate } from '@/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); -} export const route: Route = { path: '/daily', @@ -40,60 +22,44 @@ export const route: Route = { }, ], name: '知乎日报', - maintainers: ['DHPO'], + maintainers: ['DHPO', 'pseudoyu'], handler, url: 'daily.zhihu.com/*', }; async function handler() { - 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 { - address = (await dohResolve(HOST)).pop(); // 国外ip证书配置有误,解析到国内地址 - } - const listRes = await got({ - method: 'get', - url: `${api}/latest`, - headers: { - ...header, - Referer: `${api}/latest`, - Host: address, - }, - // host: address, + const key = 'zhihu:daily'; + const response = await cache.tryGet(key, async () => { + const response = await ofetch('https://daily.zhihu.com/'); + return response; }); - // 根据api的说明,过滤掉极个别站外链接 - const storyList = listRes.data.stories.filter((el) => el.type === 0); - const date = listRes.data.date; - const articleList = storyList.map((item) => ({ - title: item.title, - pubDate: parseDate(date, 'YYYYMMDD'), - link: item.url, - guid: item.url, - storyId: item.id, - })); + const $ = load(response); const items = await Promise.all( - articleList.map(async (item) => { - const url = `${api}/${item.storyId}`; - const description = await cache.tryGet(item.link, async () => { - const storyDetail = await got({ - method: 'get', - url, - headers: { - Referer: url, - Host: address, - }, - // host: address, + $('.box') + .toArray() + .map(async (item) => { + item = $(item); + const linkElem = item.find('.link-button'); + const storyUrl = 'https://daily.zhihu.com' + linkElem.attr('href'); + + // Fetch full story content + const storyResponse = await cache.tryGet(storyUrl, async () => { + const response = await ofetch(storyUrl); + return response; }); - return processImage(storyDetail.data.body.replaceAll(/
([\S\s]*?)<\/div>/g, '$1').replaceAll(/<\/?h2.*?>/g, '')); - }); - item.description = description; - return item; - }) + + const $story = load(storyResponse); + const storyTitle = $story('.DailyHeader-title').text(); + const storyContent = $story('.DailyRichText').html(); + + return { + title: storyTitle, + description: storyContent, + link: storyUrl, + }; + }) ); return {