From 2070264d57563c93296402aa5c156c1c7d5f7a16 Mon Sep 17 00:00:00 2001 From: karasu Date: Wed, 20 Nov 2024 23:58:39 +0800 Subject: [PATCH] =?UTF-8?q?feat(route):=20=E5=86=86=E8=B0=B7=E3=82=B9?= =?UTF-8?q?=E3=83=86=E3=83=BC=E3=82=B7=E3=83=A7=E3=83=B3=20(#17650)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat(route): m-78 * add updated * fix error tips * Update lib/routes/m-78/news.ts --------- --- lib/routes/m-78/namespace.ts | 7 +++ lib/routes/m-78/news.ts | 115 +++++++++++++++++++++++++++++++++++ lib/routes/m-78/types.ts | 13 ++++ 3 files changed, 135 insertions(+) create mode 100644 lib/routes/m-78/namespace.ts create mode 100644 lib/routes/m-78/news.ts create mode 100644 lib/routes/m-78/types.ts diff --git a/lib/routes/m-78/namespace.ts b/lib/routes/m-78/namespace.ts new file mode 100644 index 00000000000000..1b1074074f5fcc --- /dev/null +++ b/lib/routes/m-78/namespace.ts @@ -0,0 +1,7 @@ +import type { Namespace } from '@/types'; + +export const namespace: Namespace = { + name: '円谷ステーション', + url: 'm-78.jp', + lang: 'ja', +}; diff --git a/lib/routes/m-78/news.ts b/lib/routes/m-78/news.ts new file mode 100644 index 00000000000000..5eed7b6b84f1d4 --- /dev/null +++ b/lib/routes/m-78/news.ts @@ -0,0 +1,115 @@ +import { type Data, type Route, ViewType } from '@/types'; +import ofetch from '@/utils/ofetch'; +import type { Context } from 'hono'; +import type { Post } from './types'; +import { parseDate } from '@/utils/parse-date'; +import { load } from 'cheerio'; +import InvalidParameterError from '@/errors/types/invalid-parameter'; + +export const route: Route = { + name: 'ニュース', + categories: ['anime'], + path: '/news/:category?', + example: '/m-78/news', + radar: [ + { + source: ['m-78.jp/news'], + target: '/news', + }, + { + source: ['m-78.jp/news/category/:category'], + target: '/news/:category', + }, + ], + parameters: { + category: { + description: 'news category', + default: 'news', + options: [ + { + value: 'news', + label: 'ニュース', + }, + { + value: 'streaming', + label: '動画配信', + }, + { + value: 'event', + label: 'イベント', + }, + { + value: 'onair', + label: '放送', + }, + { + value: 'broadcast', + label: '放送/配信', + }, + { + value: 'goods', + label: 'グッズ', + }, + { + value: 'ultraman-cardgame', + label: 'ウルトラマン カードゲーム', + }, + { + value: 'shop', + label: 'ショップ', + }, + { + value: 'blu-ray_dvd', + label: 'Blu-ray・DVD', + }, + { + value: 'digital', + label: 'デジタル', + }, + ], + }, + }, + handler, + maintainers: ['KarasuShin'], + features: { + supportRadar: true, + }, + view: ViewType.Articles, +}; + +async function handler(ctx: Context): Promise { + const rootUrl = 'https://m-78.jp'; + const cateAPIUrl = `${rootUrl}/wp-json/wp/v2/categories`; + const postsAPIUrl = `${rootUrl}/wp-json/wp/v2/posts`; + const category = ctx.req.param('category') ?? 'news'; + const limit = ctx.req.query('limit') ? Number.parseInt(ctx.req.query('limit')!, 10) : 20; + + const categories = await ofetch(`${cateAPIUrl}?slug=${category}`); + if (categories.length === 0) { + throw new InvalidParameterError('Category not found'); + } + + const { id: categoryId, link: categoryLink, name: categoryName } = categories[0]; + + const posts = await ofetch(`${postsAPIUrl}?categories=${categoryId}&per_page=${limit}`); + return { + title: `${categoryName} | ニュース`, + link: categoryLink, + item: posts.map((post) => { + const $ = load(post.content.rendered, null, false); + $('#ez-toc-container').remove(); + $('img').each((_, img) => { + if (/wp-content\/uploads/.test(img.attribs.src)) { + img.attribs.src = img.attribs.src.replace(/(-\d+x\d+)/, ''); + } + }); + return { + title: post.title.rendered, + link: post.link, + description: $.html(), + pubDate: parseDate(post.date_gmt), + updated: parseDate(post.modified_gmt), + }; + }), + }; +} diff --git a/lib/routes/m-78/types.ts b/lib/routes/m-78/types.ts new file mode 100644 index 00000000000000..ff84da8ae61683 --- /dev/null +++ b/lib/routes/m-78/types.ts @@ -0,0 +1,13 @@ +export interface Post { + id: number; + content: { + rendered: string; + }; + date_gmt: string; + modified_gmt: string; + link: string; + categories: number[]; + title: { + rendered: string; + }; +}