From f1b8eb3cbcc0eb6543dbbc6c155d460238b98fc0 Mon Sep 17 00:00:00 2001 From: Mg Pig Date: Tue, 26 Mar 2024 01:21:57 +0800 Subject: [PATCH] =?UTF-8?q?feat(route):=20Add=20gamer520(=E5=85=A8?= =?UTF-8?q?=E7=90=83=E6=B8=B8=E6=88=8F=E4=BA=A4=E6=B5=81=E4=B8=AD=E5=BF=83?= =?UTF-8?q?)=20(#14909)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat(route): Add gamer520(全球游戏交流中心) * feat(route): gamer520 change to use wordpress api * feat(route): gamer520 load categories via wp api & apply suggestions from code review * fix: typing --------- --- lib/routes/gamer520/index.ts | 85 ++++++++++++++++++++++++++++++++ lib/routes/gamer520/namespace.ts | 6 +++ 2 files changed, 91 insertions(+) create mode 100644 lib/routes/gamer520/index.ts create mode 100644 lib/routes/gamer520/namespace.ts diff --git a/lib/routes/gamer520/index.ts b/lib/routes/gamer520/index.ts new file mode 100644 index 000000000..36ec51d6a --- /dev/null +++ b/lib/routes/gamer520/index.ts @@ -0,0 +1,85 @@ +import { Data, DataItem, Route } from '@/types'; +import cache from '@/utils/cache'; +import got from '@/utils/got'; +import { parseDate } from '@/utils/parse-date'; +import { Context } from 'hono'; + +export const route: Route = { + path: '/:category?/:order?', + categories: ['game'], + example: '/gamer520/switchyouxi', + parameters: { + category: '分类,见下表', + order: '排序,发布日期: date; 修改日期: modified', + }, + features: { + antiCrawler: true, + }, + name: '文章', + maintainers: ['xzzpig'], + handler, + url: 'www.gamer520.com/', + description: `分类 + + | 所有 | Switch 游戏下载 | 金手指 | 3A 巨作 | switch 主题 | PC 游戏 | + | ---- | --------------- | ---------- | ------- | ----------- | ------- | + | all | switchyouxi | jinshouzhi | 3ajuzuo | zhuti | pcgame |`, +}; + +interface Post { + id: number; + guid: { rendered: string }; + title: { rendered: string }; + date_gmt: string; + modified_gmt: string; + categories?: number[]; + content: { rendered: string }; +} + +interface Category { + id: number; + name: string; + link: string; + slug: string; +} + +async function getCategories(baseUrl: string): Promise { + return (await cache.tryGet('gamer520:categories', async () => { + const { data } = await got(`${baseUrl}/wp-json/wp/v2/categories`); + return data.map((category) => ({ slug: category.slug, id: category.id, name: category.name, link: category.link })); + })) as Category[]; +} + +async function handler(ctx: Context): Promise { + const baseUrl = 'https://www.gamer520.com'; + const categories = await getCategories(baseUrl); + + const category = ctx.req.param('category') ?? 'all'; + const order = ctx.req.param('order'); + const categoryId = categories.find((c) => c.slug === category)?.id; + + const { data } = (await got(`${baseUrl}/wp-json/wp/v2/posts`, { + searchParams: { + categories: categoryId, + orderby: order, + per_page: ctx.req.query('limit') ? Number.parseInt(ctx.req.query('limit') as string) : undefined, + }, + })) as unknown as { data: Post[] }; + + const items: DataItem[] = data.map((item) => ({ + guid: `gamer520:${item.id}`, + title: item.title.rendered, + link: item.guid.rendered, + pubDate: parseDate(item.date_gmt), + updated: parseDate(item.modified_gmt), + category: item.categories?.map((c) => categories.find((ca) => ca.id === c)?.name ?? '').filter((c) => c !== '') ?? [], + + description: item.content.rendered, + })); + + return { + title: '全球游戏交流中心-' + (categories.find((c) => c.slug === category)?.name ?? '所有'), + link: categories.find((c) => c.slug === category)?.link ?? baseUrl, + item: items, + }; +} diff --git a/lib/routes/gamer520/namespace.ts b/lib/routes/gamer520/namespace.ts new file mode 100644 index 000000000..0f236a4cf --- /dev/null +++ b/lib/routes/gamer520/namespace.ts @@ -0,0 +1,6 @@ +import type { Namespace } from '@/types'; + +export const namespace: Namespace = { + name: '全球游戏交流中心', + url: 'www.gamer520.com', +};