Skip to content

Commit

Permalink
fix(route/smzdm): update handler to use ofetch and improve data struc…
Browse files Browse the repository at this point in the history
…ture
  • Loading branch information
pseudoyu committed Dec 17, 2024
1 parent 0bc647c commit 6a06261
Showing 1 changed file with 23 additions and 17 deletions.
40 changes: 23 additions & 17 deletions lib/routes/smzdm/haowen.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -19,50 +19,56 @@ export const route: Route = {
supportScihub: false,
},
name: '好文',
maintainers: ['LogicJake'],
maintainers: ['LogicJake', 'pseudoyu'],
handler,
};

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;
})
)
);

return {
title: `${title}-什么值得买好文`,
link,
item: out,
item: out.filter((item): item is DataItem => item !== null),
};
}

0 comments on commit 6a06261

Please sign in to comment.