From 2a90282da9154cd702f1f5d5c1bb084569cd7d48 Mon Sep 17 00:00:00 2001 From: Geraldxm <115340172+Geraldxm@users.noreply.github.com> Date: Thu, 12 Dec 2024 18:52:24 +0800 Subject: [PATCH] =?UTF-8?q?feat(route):=20=E5=85=85=E7=94=B5=E5=A4=B4?= =?UTF-8?q?=E7=BD=91=20(#17860)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat(route): 充电头网 * use REST API * remove cache; use date_gmt * Update lib/routes/chongdiantou/index.ts --- lib/routes/chongdiantou/index.ts | 54 ++++++++++++++++++++++++++++ lib/routes/chongdiantou/namespace.ts | 9 +++++ 2 files changed, 63 insertions(+) create mode 100644 lib/routes/chongdiantou/index.ts create mode 100644 lib/routes/chongdiantou/namespace.ts diff --git a/lib/routes/chongdiantou/index.ts b/lib/routes/chongdiantou/index.ts new file mode 100644 index 00000000000000..3ea95397d04ee5 --- /dev/null +++ b/lib/routes/chongdiantou/index.ts @@ -0,0 +1,54 @@ +import { Route } from '@/types'; +import { namespace } from './namespace'; +import ofetch from '@/utils/ofetch'; +import logger from '@/utils/logger'; + +async function getPosts() { + try { + // Fetch data directly from the API without caching + const response = await ofetch('https://www.chongdiantou.com/wp-json/wp/v2/posts?_embed&per_page=10', { + headers: { + method: 'GET', + }, + }); + return response.map((post) => ({ + title: post.title.rendered, + link: post.link, + pubDate: new Date(post.date_gmt), // Use date_gmt instead of date + category: post._embedded['wp:term'][0].map((term) => term.name).join(', '), + description: post.content.rendered, + author: post._embedded.author[0].name, + image: post._embedded['wp:featuredmedia'] ? post._embedded['wp:featuredmedia'][0].source_url : '', + })); + } catch (error) { + logger.error('Error fetching posts:', error); + return []; + } +} + +export const route: Route = { + path: '/', + categories: namespace.categories, + example: '/chongdiantou', + radar: [ + { + source: ['www.chongdiantou.com'], + }, + ], + name: '最新资讯', + maintainers: ['Geraldxm'], + handler, + url: 'www.chongdiantou.com', +}; + +async function handler() { + const items = await getPosts(); + + return { + title: '充电头网 - 最新资讯', + description: '充电头网新闻资讯', + link: 'https://www.chongdiantou.com/', + image: 'https://static.chongdiantou.com/wp-content/uploads/2021/02/2021021806172389.png', + item: items, + }; +} diff --git a/lib/routes/chongdiantou/namespace.ts b/lib/routes/chongdiantou/namespace.ts new file mode 100644 index 00000000000000..b67d38e1b65017 --- /dev/null +++ b/lib/routes/chongdiantou/namespace.ts @@ -0,0 +1,9 @@ +import type { Namespace } from '@/types'; + +export const namespace: Namespace = { + name: '充电头网', + url: 'www.chongdiantou.com', + categories: ['new-media'], + lang: 'zh-CN', + description: '充电头网是国内最早进行消费类电源技术及其周边配件(快充、充电头、充电器、无线充、车充、车载充电器、数据线、充电线材、移动电源及电芯、USB插排)评测、拆解的专业机构。', +};