From 13c1eb02fdd43c6cee1b50d5c2cd64b87231b61d Mon Sep 17 00:00:00 2001 From: equt <17521736+equt@users.noreply.github.com> Date: Tue, 2 Jul 2024 22:57:46 +0800 Subject: [PATCH] feat(route): add `/react/blog` (#16055) * feat(route): add `/react/blog` * fix: remove unnecessary cache according to #16038 * fix: cache individual post * fix: cache item * Update lib/routes/react/blog.ts --------- --- lib/routes/react/blog.ts | 57 +++++++++++++++++++++++++++++++++++ lib/routes/react/namespace.ts | 6 ++++ 2 files changed, 63 insertions(+) create mode 100644 lib/routes/react/blog.ts create mode 100644 lib/routes/react/namespace.ts diff --git a/lib/routes/react/blog.ts b/lib/routes/react/blog.ts new file mode 100644 index 000000000..a1d3f2f01 --- /dev/null +++ b/lib/routes/react/blog.ts @@ -0,0 +1,57 @@ +import type { DataItem, Route } from '@/types'; +import { parseDate } from '@/utils/parse-date'; +import cache from '@/utils/cache'; +import { load } from 'cheerio'; +import ofetch from '@/utils/ofetch'; + +const handler: Route['handler'] = async () => { + const data = await ofetch('https://react.dev/blog'); + + const $ = load(data); + + const item = (await Promise.all( + $('a[href^="/blog/"]') + .toArray() + .slice(0, 20) + .map((item) => { + const link = `https://react.dev${item.attribs.href}`; + + return cache.tryGet(`react:blog:${link}`, async () => { + const data = await ofetch(link); + + const $ = load(data); + + return { + title: $('h1').first().text().trim(), + link, + description: $('article div:nth-child(2)').html() ?? '', + pubDate: parseDate($('p.whitespace-pre-wrap').first().text().split(/\s+by/)[0]), + }; + }); + }) + )) as DataItem[]; + + return { + title: 'React Blog', + link: 'https://react.dev/blog', + language: 'en-US', + item, + }; +}; + +export const route: Route = { + path: '/blog', + name: 'Blog', + categories: ['blog'], + maintainers: ['equt'], + example: '/react/blog', + features: { + requireConfig: false, + requirePuppeteer: false, + antiCrawler: false, + supportBT: false, + supportPodcast: false, + supportScihub: false, + }, + handler, +}; diff --git a/lib/routes/react/namespace.ts b/lib/routes/react/namespace.ts new file mode 100644 index 000000000..83a673fa4 --- /dev/null +++ b/lib/routes/react/namespace.ts @@ -0,0 +1,6 @@ +import type { Namespace } from '@/types'; + +export const namespace: Namespace = { + name: 'React', + url: 'react.dev', +};