fix(route/zhihu): fix daily section (#20179)
This commit is contained in:
parent
e3c86d8a62
commit
519acae24c
|
|
@ -1,7 +1,7 @@
|
|||
import { Route } from '@/types';
|
||||
import cache from '@/utils/cache';
|
||||
import got from '@/utils/got';
|
||||
import { header, processImage } from './utils';
|
||||
import ofetch from '@/utils/ofetch';
|
||||
import { header } 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
|
||||
|
|
@ -34,42 +34,37 @@ export const route: Route = {
|
|||
|
||||
async function handler(ctx) {
|
||||
const sectionId = ctx.req.param('sectionId');
|
||||
const listRes = await got({
|
||||
method: 'get',
|
||||
url: `https://news-at.zhihu.com/api/7/section/${sectionId}`,
|
||||
const listRes = await ofetch(`https://news-at.zhihu.com/api/7/section/${sectionId}`, {
|
||||
headers: {
|
||||
...header,
|
||||
Referer: `https://news-at.zhihu.com/api/7/section/${sectionId}`,
|
||||
},
|
||||
});
|
||||
// 根据api的说明,过滤掉极个别站外链接
|
||||
const storyList = listRes.data.stories.filter((el) => el.url.startsWith('https://daily.zhihu.com/'));
|
||||
const storyList = listRes.stories.filter((el) => el.url.startsWith('https://daily.zhihu.com/'));
|
||||
const resultItem = await Promise.all(
|
||||
storyList.map((story) => {
|
||||
storyList.map(async (story) => {
|
||||
const url = 'https://news-at.zhihu.com/api/7/news/' + story.id;
|
||||
const item = {
|
||||
title: story.title,
|
||||
pubDate: parseDate(story.date, 'YYYYMMDD'),
|
||||
description: '',
|
||||
link: 'https://daily.zhihu.com/story/' + story.id,
|
||||
};
|
||||
return cache.tryGet(`https://daily.zhihu.com/story/${story.id}`, async () => {
|
||||
const storyDetail = await got({
|
||||
method: 'get',
|
||||
url,
|
||||
headers: {
|
||||
Referer: url,
|
||||
},
|
||||
});
|
||||
item.description = processImage(storyDetail.data.body.replaceAll(/<div class="meta">([\S\s]*?)<\/div>/g, '<strong>$1</strong>').replaceAll(/<\/?h2.*?>/g, ''));
|
||||
|
||||
return item;
|
||||
const storyJson = await cache.tryGet(url, async () => {
|
||||
const response = await ofetch(url);
|
||||
return response;
|
||||
});
|
||||
|
||||
const storyTitle = storyJson.title;
|
||||
const storyContent = storyJson.body;
|
||||
|
||||
return {
|
||||
title: storyTitle,
|
||||
description: storyContent,
|
||||
link: storyJson.url,
|
||||
pubDate: parseDate(storyJson.publish_time, 'X'),
|
||||
};
|
||||
})
|
||||
);
|
||||
|
||||
return {
|
||||
title: `${listRes.data.name} - 知乎日报`,
|
||||
title: `${listRes.name} - 知乎日报`,
|
||||
link: 'https://daily.zhihu.com',
|
||||
description: '每天3次,每次7分钟',
|
||||
image: 'http://static.daily.zhihu.com/img/new_home_v3/mobile_top_logo.png',
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ import { Route } from '@/types';
|
|||
import ofetch from '@/utils/ofetch';
|
||||
import { load } from 'cheerio';
|
||||
import cache from '@/utils/cache';
|
||||
import { parseDate } from '@/utils/parse-date';
|
||||
|
||||
export const route: Route = {
|
||||
path: '/daily',
|
||||
|
|
@ -53,6 +54,7 @@ async function handler() {
|
|||
title: storyTitle,
|
||||
description: storyContent,
|
||||
link: storyJson.url,
|
||||
pubDate: parseDate(storyJson.publish_time, 'X'),
|
||||
};
|
||||
})
|
||||
);
|
||||
|
|
|
|||
Loading…
Reference in New Issue