Skip to content

Commit 519acae

Browse files
authored
fix(route/zhihu): fix daily section (DIYgod#20179)
1 parent e3c86d8 commit 519acae

File tree

2 files changed

+21
-24
lines changed

2 files changed

+21
-24
lines changed

lib/routes/zhihu/daily-section.ts

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Route } from '@/types';
22
import cache from '@/utils/cache';
3-
import got from '@/utils/got';
4-
import { header, processImage } from './utils';
3+
import ofetch from '@/utils/ofetch';
4+
import { header } from './utils';
55
import { parseDate } from '@/utils/parse-date';
66

77
// 参考: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 = {
3434

3535
async function handler(ctx) {
3636
const sectionId = ctx.req.param('sectionId');
37-
const listRes = await got({
38-
method: 'get',
39-
url: `https://news-at.zhihu.com/api/7/section/${sectionId}`,
37+
const listRes = await ofetch(`https://news-at.zhihu.com/api/7/section/${sectionId}`, {
4038
headers: {
4139
...header,
4240
Referer: `https://news-at.zhihu.com/api/7/section/${sectionId}`,
4341
},
4442
});
4543
// 根据api的说明,过滤掉极个别站外链接
46-
const storyList = listRes.data.stories.filter((el) => el.url.startsWith('https://daily.zhihu.com/'));
44+
const storyList = listRes.stories.filter((el) => el.url.startsWith('https://daily.zhihu.com/'));
4745
const resultItem = await Promise.all(
48-
storyList.map((story) => {
46+
storyList.map(async (story) => {
4947
const url = 'https://news-at.zhihu.com/api/7/news/' + story.id;
50-
const item = {
51-
title: story.title,
52-
pubDate: parseDate(story.date, 'YYYYMMDD'),
53-
description: '',
54-
link: 'https://daily.zhihu.com/story/' + story.id,
55-
};
56-
return cache.tryGet(`https://daily.zhihu.com/story/${story.id}`, async () => {
57-
const storyDetail = await got({
58-
method: 'get',
59-
url,
60-
headers: {
61-
Referer: url,
62-
},
63-
});
64-
item.description = processImage(storyDetail.data.body.replaceAll(/<div class="meta">([\S\s]*?)<\/div>/g, '<strong>$1</strong>').replaceAll(/<\/?h2.*?>/g, ''));
6548

66-
return item;
49+
const storyJson = await cache.tryGet(url, async () => {
50+
const response = await ofetch(url);
51+
return response;
6752
});
53+
54+
const storyTitle = storyJson.title;
55+
const storyContent = storyJson.body;
56+
57+
return {
58+
title: storyTitle,
59+
description: storyContent,
60+
link: storyJson.url,
61+
pubDate: parseDate(storyJson.publish_time, 'X'),
62+
};
6863
})
6964
);
7065

7166
return {
72-
title: `${listRes.data.name} - 知乎日报`,
67+
title: `${listRes.name} - 知乎日报`,
7368
link: 'https://daily.zhihu.com',
7469
description: '每天3次,每次7分钟',
7570
image: 'http://static.daily.zhihu.com/img/new_home_v3/mobile_top_logo.png',

lib/routes/zhihu/daily.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Route } from '@/types';
22
import ofetch from '@/utils/ofetch';
33
import { load } from 'cheerio';
44
import cache from '@/utils/cache';
5+
import { parseDate } from '@/utils/parse-date';
56

67
export const route: Route = {
78
path: '/daily',
@@ -53,6 +54,7 @@ async function handler() {
5354
title: storyTitle,
5455
description: storyContent,
5556
link: storyJson.url,
57+
pubDate: parseDate(storyJson.publish_time, 'X'),
5658
};
5759
})
5860
);

0 commit comments

Comments
 (0)