|
1 | 1 | import { Route } from '@/types'; |
2 | 2 | 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'; |
5 | 5 | import { parseDate } from '@/utils/parse-date'; |
6 | 6 |
|
7 | 7 | // 参考: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 = { |
34 | 34 |
|
35 | 35 | async function handler(ctx) { |
36 | 36 | 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}`, { |
40 | 38 | headers: { |
41 | 39 | ...header, |
42 | 40 | Referer: `https://news-at.zhihu.com/api/7/section/${sectionId}`, |
43 | 41 | }, |
44 | 42 | }); |
45 | 43 | // 根据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/')); |
47 | 45 | const resultItem = await Promise.all( |
48 | | - storyList.map((story) => { |
| 46 | + storyList.map(async (story) => { |
49 | 47 | 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, '')); |
65 | 48 |
|
66 | | - return item; |
| 49 | + const storyJson = await cache.tryGet(url, async () => { |
| 50 | + const response = await ofetch(url); |
| 51 | + return response; |
67 | 52 | }); |
| 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 | + }; |
68 | 63 | }) |
69 | 64 | ); |
70 | 65 |
|
71 | 66 | return { |
72 | | - title: `${listRes.data.name} - 知乎日报`, |
| 67 | + title: `${listRes.name} - 知乎日报`, |
73 | 68 | link: 'https://daily.zhihu.com', |
74 | 69 | description: '每天3次,每次7分钟', |
75 | 70 | image: 'http://static.daily.zhihu.com/img/new_home_v3/mobile_top_logo.png', |
|
0 commit comments