From ff3a9ce0511be9f388f6d591e109732a70d62fb6 Mon Sep 17 00:00:00 2001 From: Tony Date: Sun, 15 Jun 2025 05:30:51 +0800 Subject: [PATCH] feat(route/warhammer-community): add news (#19379) --- lib/routes/warhammer-community/namespace.ts | 7 ++ lib/routes/warhammer-community/news.ts | 73 +++++++++++++++++++++ 2 files changed, 80 insertions(+) create mode 100644 lib/routes/warhammer-community/namespace.ts create mode 100644 lib/routes/warhammer-community/news.ts diff --git a/lib/routes/warhammer-community/namespace.ts b/lib/routes/warhammer-community/namespace.ts new file mode 100644 index 0000000000..10af664708 --- /dev/null +++ b/lib/routes/warhammer-community/namespace.ts @@ -0,0 +1,7 @@ +import { Namespace } from '@/types'; + +export const namespace: Namespace = { + name: 'Warhammer Community', + url: 'www.warhammer-community.com/en-gb/', + lang: 'en', +}; diff --git a/lib/routes/warhammer-community/news.ts b/lib/routes/warhammer-community/news.ts new file mode 100644 index 0000000000..362dc4fe16 --- /dev/null +++ b/lib/routes/warhammer-community/news.ts @@ -0,0 +1,73 @@ +import { Route } from '@/types'; +import cache from '@/utils/cache'; + +import ofetch from '@/utils/ofetch'; +import { parseDate } from '@/utils/parse-date'; +import { load } from 'cheerio'; + +export const route: Route = { + path: '/news', + categories: ['game'], + example: '/warhammer-community/news', + radar: [ + { + source: ['www.warhammer-community.com/en-gb/all-news-and-features/', 'www.warhammer-community.com/en-gb/'], + }, + ], + name: 'News', + maintainers: ['TonyRL'], + handler, + url: 'www.warhammer-community.com/en-gb/all-news-and-features/', +}; + +async function handler(ctx) { + const baseUrl = 'https://www.warhammer-community.com'; + const limit = Number.parseInt(ctx.req.query('limit') || '16', 10); + + const response = await ofetch(`${baseUrl}/api/search/news/`, { + method: 'POST', + body: { + sortBy: 'date_desc', + category: '', + collections: ['articles'], + game_systems: [], + index: 'news', + locale: 'en-gb', + page: 0, + perPage: limit, + topics: [], + }, + }); + + const list = response.news.map((item) => ({ + title: item.title, + link: `${baseUrl}/en-gb${item.uri}/`, + description: item.excerpt, + image: `https://assets.warhammer-community.com/${item.image.path}`, + pubDate: parseDate(item.date), + category: item.topics.map((topic) => topic.title), + })); + + const items = await Promise.all( + list.map((item) => + cache.tryGet(item.link, async () => { + const response = await ofetch(item.link); + const $ = load(response); + + const content = $('.article-content'); + content.find('button').remove(); + + item.description = content.html() || item.description; + + return item; + }) + ) + ); + + return { + title: 'All News and Features - Warhammer Community', + link: `${baseUrl}/en-gb/all-news-and-features/`, + image: `${baseUrl}/images/apple-favicon.png`, + item: items, + }; +}