From 0e9ef88ba8361f811ed154d775069eebb6e78fe0 Mon Sep 17 00:00:00 2001 From: Stephen Zhou <38493346+hyoban@users.noreply.github.com> Date: Mon, 15 Sep 2025 22:40:24 +0800 Subject: [PATCH] feat(route/hupu)!: index support (#20062) * feat(route/hupu)!: index support * add type * Apply suggestions from code review --------- --- lib/routes/hupu/index.ts | 202 +++++++++++++++++++++++++-------------- lib/routes/hupu/types.ts | 163 +++++++++++++++++++++++++++++++ 2 files changed, 293 insertions(+), 72 deletions(-) create mode 100644 lib/routes/hupu/types.ts diff --git a/lib/routes/hupu/index.ts b/lib/routes/hupu/index.ts index 99fa3d88d..73752dbdb 100644 --- a/lib/routes/hupu/index.ts +++ b/lib/routes/hupu/index.ts @@ -1,9 +1,10 @@ -import { Route } from '@/types'; +import { Route, Data, DataItem } from '@/types'; import cache from '@/utils/cache'; import got from '@/utils/got'; import { load } from 'cheerio'; import timezone from '@/utils/timezone'; import { parseDate } from '@/utils/parse-date'; +import { type HupuApiResponse, type HomePostItem, type NewsDataItem, isHomePostItem } from './types'; const categories = { nba: { @@ -18,85 +19,142 @@ const categories = { title: '足球', data: 'news', }, -}; + '': { + title: '首页', + data: 'res', + }, +} as const; export const route: Route = { path: ['/dept/:category?', '/:category?'], + name: '手机虎扑网', + url: 'm.hupu.com', + maintainers: ['nczitzk', 'hyoban'], + example: 'hupu/nba', + parameters: { + category: { + description: '分类,可选值:nba、cba、soccer,默认为空(首页)', + default: '', + options: Object.entries(categories).map(([key, value]) => ({ + label: value.title, + value: key, + })), + }, + }, + description: `::: tip +电竞分类参见 [游戏热帖](https://bbs.hupu.com/all-gg) 的对应路由 [\`/hupu/all/all-gg\`](https://rsshub.app/hupu/all/all-gg)。 + :::`, + categories: ['bbs'], radar: [ { source: ['m.hupu.com/:category', 'm.hupu.com/'], target: '/:category', }, ], - name: 'Unknown', - maintainers: ['nczitzk'], - handler, - description: `| NBA | CBA | 足球 | -| --- | --- | ------ | -| nba | cba | soccer | + handler: async (ctx): Promise => { + const c = ctx.req.param('category') || ''; + if (!(c in categories)) { + throw new Error('Invalid category. Valid options are: ' + Object.keys(categories).filter(Boolean).join(', ')); + } + const category = c as keyof typeof categories; -::: tip - 电竞分类参见 [游戏热帖](https://bbs.hupu.com/all-gg) 的对应路由 [\`/hupu/all/all-gg\`](https://rsshub.app/hupu/all/all-gg)。 -:::`, + const rootUrl = 'https://m.hupu.com'; + const currentUrl = `${rootUrl}/${category}`; + + const response = await got({ + method: 'get', + url: currentUrl, + }); + + const scriptMatch = response.data.match(/