From 09d311b01236f05115cc68e8ab96e9c1af8ed536 Mon Sep 17 00:00:00 2001 From: Marshall <57112423+Xy2002@users.noreply.github.com> Date: Thu, 19 Sep 2024 10:47:25 +0800 Subject: [PATCH] feat(route): add route of devtrium (#16794) * feat(route): add route of devtrium * Update posts.ts remove useless comments * Update lib/routes/devtrium/posts.ts Co-authored-by: Tony * refactor(route/devtrium): use embedded JSON data instead of DOM parsing * remove useless character --------- --- lib/routes/devtrium/namespace.ts | 6 ++++ lib/routes/devtrium/posts.ts | 55 ++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 lib/routes/devtrium/namespace.ts create mode 100644 lib/routes/devtrium/posts.ts diff --git a/lib/routes/devtrium/namespace.ts b/lib/routes/devtrium/namespace.ts new file mode 100644 index 0000000000000..5a2ff90d0e706 --- /dev/null +++ b/lib/routes/devtrium/namespace.ts @@ -0,0 +1,6 @@ +import type { Namespace } from '@/types'; + +export const namespace: Namespace = { + name: 'Devtrium', + url: 'devtrium.com', +}; diff --git a/lib/routes/devtrium/posts.ts b/lib/routes/devtrium/posts.ts new file mode 100644 index 0000000000000..1a842c14be0b3 --- /dev/null +++ b/lib/routes/devtrium/posts.ts @@ -0,0 +1,55 @@ +import { DataItem, Route } from '@/types'; +import { load } from 'cheerio'; +import { ofetch } from 'ofetch'; +import { parseDate } from '@/utils/parse-date'; + +export const route: Route = { + path: '/', + categories: ['programming'], + example: '/devtrium', + parameters: {}, + features: { + requireConfig: false, + requirePuppeteer: false, + antiCrawler: false, + supportBT: false, + supportPodcast: false, + supportScihub: false, + }, + radar: [ + { + source: ['devtrium.com'], + }, + ], + name: 'Official Blogs', + maintainers: ['Xy2002'], + handler, + url: 'devtrium.com', +}; + +async function handler() { + const items = await fetchPage(); + + return { + title: 'Devtrium', + language: 'en-us', + item: items, + link: 'https://devtrium.com', + }; +} + +async function fetchPage() { + const baseUrl = 'https://devtrium.com'; + const response = await ofetch(baseUrl); + const $ = load(response, { scriptingEnabled: false }); + + // Extract all posts of this page + const data = JSON.parse($('script#__NEXT_DATA__').text()); + const items: DataItem[] = data.props.pageProps.posts.map((post) => ({ + title: post.title, + link: `${baseUrl}/posts/${post.slug}`, + description: post.description, + pubDate: parseDate(post.date), + })); + return items; +}