diff --git a/lib/routes/yicai/carousel.ts b/lib/routes/yicai/carousel.ts new file mode 100644 index 00000000000000..ef8f9b99a6bfa9 --- /dev/null +++ b/lib/routes/yicai/carousel.ts @@ -0,0 +1,48 @@ +import { Route } from '@/types'; +import cache from '@/utils/cache'; +import { rootUrl, fetchFullArticles } from './utils'; +import ofetch from '@/utils/ofetch'; +import { load } from 'cheerio'; + +export const route: Route = { + path: '/carousel', + categories: ['traditional-media'], + example: '/yicai/carousel', + parameters: {}, + features: { + requireConfig: false, + requirePuppeteer: false, + antiCrawler: false, + supportBT: false, + supportPodcast: false, + supportScihub: false, + }, + radar: [ + { + source: ['yicai.com/'], + }, + ], + name: '轮播', + maintainers: ['nczitzk'], + handler, + url: 'yicai.com/', +}; + +async function handler() { + const res = await ofetch(rootUrl); + const $ = load(res); + const items = await Promise.all( + fetchFullArticles( + $('#breaknews a') + .toArray() + .map((e) => ({ link: new URL($(e).attr('href'), rootUrl).href, title: $(e).text() })), + cache.tryGet + ) + ); + + return { + title: '第一财经 - 轮播', + link: rootUrl, + item: items, + }; +} diff --git a/lib/routes/yicai/utils.ts b/lib/routes/yicai/utils.ts index 779c7790459fc5..d1f58a7be4b2bf 100644 --- a/lib/routes/yicai/utils.ts +++ b/lib/routes/yicai/utils.ts @@ -35,25 +35,31 @@ const ProcessItems = async (apiUrl, tryGet) => { }), })); - return Promise.all( - items.map((item) => - tryGet(item.link, async () => { - const detailResponse = await got({ - method: 'get', - url: item.link, - }); + return Promise.all(fetchFullArticles(items, tryGet)); +}; +function fetchFullArticles(items, tryGet) { + return items.map((item) => + tryGet(item.link, async () => { + const detailResponse = await got({ + method: 'get', + url: item.link, + }); - const content = load(detailResponse.data); + const content = load(detailResponse.data); - content('h1').remove(); - content('.u-btn6, .m-smallshare, .topic-hot').remove(); + if (!item.pubDate) { + const dataScript = content("script[src='/js/alert.min.js']").next().text() || content('title').next().text(); + const pb = new Map(JSON.parse(dataScript.match(/_pb = (\[.*?]);/)[1].replaceAll("'", '"'))); + item.pubDate = parseDate(`${pb.get('actime')}:00`); + } - item.description += content('.multiText, #multi-text, .txt').html() ?? ''; + content('h1').remove(); + content('.u-btn6, .m-smallshare, .topic-hot').remove(); - return item; - }) - ) - ); -}; + item.description = (item.description ?? '') + (content('.multiText, #multi-text, .txt').html() ?? ''); -export { rootUrl, ProcessItems }; + return item; + }) + ); +} +export { rootUrl, ProcessItems, fetchFullArticles };