diff --git a/lib/routes/smzdm/haowen.ts b/lib/routes/smzdm/haowen.ts index a7732cc650cfae..3634f244a32a01 100644 --- a/lib/routes/smzdm/haowen.ts +++ b/lib/routes/smzdm/haowen.ts @@ -1,6 +1,6 @@ -import { Route } from '@/types'; +import { DataItem, Route } from '@/types'; import cache from '@/utils/cache'; -import got from '@/utils/got'; +import ofetch from '@/utils/ofetch'; import { load } from 'cheerio'; import { parseDate } from '@/utils/parse-date'; import timezone from '@/utils/timezone'; @@ -19,7 +19,7 @@ export const route: Route = { supportScihub: false, }, name: '好文', - maintainers: ['LogicJake'], + maintainers: ['LogicJake', 'pseudoyu'], handler, }; @@ -27,35 +27,41 @@ async function handler(ctx) { const day = ctx.req.param('day') ?? 'all'; const link = `https://post.smzdm.com/hot_${day}/`; - const response = await got(link); - const $ = load(response.data); + const response = await ofetch(link); + const $ = load(response); const title = $('li.filter-tab.active').text(); const list = $('li.feed-row-wide') .toArray() .map((item) => { - item = $(item); + const $item = $(item); return { - title: item.find('h5.z-feed-title a').text(), - link: item.find('h5.z-feed-title a').attr('href'), - pubDate: timezone(parseDate(item.find('span.z-publish-time').text()), 8), + title: $item.find('h5.z-feed-title a').text(), + link: $item.find('h5.z-feed-title a').attr('href'), + pubDate: timezone(parseDate($item.find('span.z-publish-time').text()), 8), }; }); const out = await Promise.all( list.map((item) => - cache.tryGet(item.link, async () => { - const response = await got(item.link); - const $ = load(response.data); + cache.tryGet(item.link ?? '', async () => { + const response = await ofetch(item.link ?? ''); + const $ = load(response); const content = $('#articleId'); content.find('.item-name').remove(); content.find('.recommend-tab').remove(); - item.description = content.html(); - item.pubDate = timezone(parseDate($('meta[property="og:release_date"]').attr('content')), 8); - item.author = $('meta[property="og:author"]').attr('content'); + const releaseDate = $('meta[property="og:release_date"]').attr('content'); - return item; + const outItem: DataItem = { + title: item.title, + link: item.link, + description: content.html() || '', + pubDate: releaseDate ? timezone(parseDate(releaseDate), 8) : item.pubDate, + author: $('meta[property="og:author"]').attr('content') || '', + }; + + return outItem; }) ) ); @@ -63,6 +69,6 @@ async function handler(ctx) { return { title: `${title}-什么值得买好文`, link, - item: out, + item: out.filter((item): item is DataItem => item !== null), }; }