feat(route/hupu)!: index support (#20062)

* feat(route/hupu)!: index support

* add type

* Apply suggestions from code review

---------
This commit is contained in:
Stephen Zhou
2025-09-15 22:40:24 +08:00
committed by GitHub
parent a59a561a72
commit 0e9ef88ba8
2 changed files with 293 additions and 72 deletions
+130 -72
View File
@@ -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<Data> => {
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(/<script id="__NEXT_DATA__" type="application\/json">(.*?)<\/script>/);
if (!scriptMatch || !scriptMatch[1]) {
throw new Error(`Failed to find __NEXT_DATA__ script tag in page: ${currentUrl}`);
}
const fullJsonString = scriptMatch[1];
let fullData;
try {
fullData = JSON.parse(fullJsonString);
} catch (error) {
throw new Error(`Failed to parse full JSON data: ${error instanceof Error ? error.message : String(error)}`);
}
const data: HupuApiResponse = fullData;
const { pageProps } = data.props;
const dataKey = categories[category].data;
if (!(dataKey in pageProps)) {
throw new Error(`Expected '${dataKey}' property not found in pageProps for category: ${category || 'home'}`);
}
const rawDataArray: (HomePostItem | NewsDataItem)[] = (() => {
const data = (pageProps as any)[dataKey];
return Array.isArray(data) ? data : [];
})();
let items: DataItem[] = rawDataArray.map((item) =>
isHomePostItem(item)
? ({
title: item.title,
link: item.url.replace(/bbs\.hupu.com/, 'm.hupu.com/bbs'),
guid: item.tid,
category: item.label ? [item.label] : undefined,
} satisfies DataItem)
: ({
title: item.title,
pubDate: timezone(parseDate(item.publishTime), +8),
link: item.link.replace(/bbs\.hupu.com/, 'm.hupu.com/bbs'),
guid: item.tid,
} satisfies DataItem)
);
items = await Promise.all(
items
.filter((item) => item.link && !/subject/.test(item.link))
.map((item) =>
cache.tryGet(item.link!, async () => {
try {
const detailResponse = await got({
method: 'get',
url: item.link,
});
const content = load(detailResponse.data);
const author = content('.bbs-user-info-name, .bbs-user-wrapper-content-name-span').text();
const categories = content('.basketballTobbs_tag > a, .tag-player-team')
.toArray()
.map((c) => content(c).text())
.filter(Boolean);
content('.basketballTobbs_tag').remove();
content('.hupu-img').each(function () {
content(this)
.parent()
.html(`<img src="${content(this).attr('data-origin')}">`);
});
const description = content('#bbs-thread-content, .bbs-content-font').html();
return {
...item,
author: author || undefined,
category: categories.length > 0 ? categories : item.category,
description: description || undefined,
};
} catch {
// no-empty
return item;
}
})
)
);
return {
title: `虎扑 - ${categories[category].title}`,
link: currentUrl,
item: items,
} as Data;
},
};
async function handler(ctx) {
const category = ctx.req.param('category') ?? 'soccer';
const rootUrl = 'https://m.hupu.com';
const currentUrl = `${rootUrl}/${category}`;
const response = await got({
method: 'get',
url: currentUrl,
});
const data = JSON.parse(response.data.match(/"props":(.*),"page":"\//)[1]);
let items = data.pageProps[categories[category].data].map((item) => ({
title: item.title,
pubDate: timezone(parseDate(item.publishTime), +8),
link: item.link.replace(/bbs\.hupu.com/, 'm.hupu.com/bbs'),
}));
items = await Promise.all(
items
.filter((item) => !/subject/.test(item.link))
.map((item) =>
cache.tryGet(item.link, async () => {
try {
const detailResponse = await got({
method: 'get',
url: item.link,
});
const content = load(detailResponse.data);
item.author = content('.bbs-user-info-name, .bbs-user-wrapper-content-name-span').text();
item.category = content('.basketballTobbs_tag > a, .tag-player-team')
.toArray()
.map((c) => content(c).text());
content('.basketballTobbs_tag').remove();
content('.hupu-img').each(function () {
content(this)
.parent()
.html(`<img src="${content(this).attr('data-origin')}">`);
});
item.description = content('#bbs-thread-content, .bbs-content-font').html();
} catch {
// no-empty
}
return item;
})
)
);
return {
title: `虎扑 - ${categories[category].title}`,
link: currentUrl,
item: items,
};
}
+163
View File
@@ -0,0 +1,163 @@
// 虎扑 API 响应数据类型定义
/**
* 徽章信息
*/
interface Badge {
name: string;
color: string;
v2DayColor: string;
v2NightColor: string;
relationUrl: string | null;
colorBg: string;
v2DayColorBg: string;
v2NightColorBg: string;
iconDay: string;
iconNight: string;
}
/**
* 首页帖子数据项(res 数组中的项)
*/
interface HomePostItem {
tid: string;
title: string;
url: string;
label: string;
lights: string;
replies: string;
type: string; // e.g., "3_pic", "1_pic", "video"
source: string[];
isNews: boolean;
badge: Badge[];
}
/**
* 新闻/分类页面数据项(newsData 数组中的项)
*/
interface NewsDataItem {
nid: string;
title: string;
img: string;
link: string;
badge: Badge[];
type: string; // e.g., "LINK", "IMG_TEXT"
lights: number;
replies: number;
publishTime: string;
tid: string;
}
/**
* 推荐比赛 Toast 信息
*/
interface RecommendMatchToast {
date: string;
matchCount: number;
title: string;
toastTitle: string | null;
foldTitle: string;
matchListLink: string | null;
matchCountText: string;
}
/**
* 推荐比赛信息
*/
interface RecommendMatch {
projectId: string | null;
version: string | null;
traceId: string | null;
matchList: any[];
events: any[];
toast: RecommendMatchToast;
success: boolean;
is_login: number;
is_jrs: boolean;
night: number;
client: string | null;
}
/**
* 首页页面属性
*/
interface HomePageProps {
res: HomePostItem[];
totalNum: number;
supportHydrate: boolean;
}
/**
* 篮球分类页面属性 (NBA/CBA)
*/
interface BasketballPageProps {
leagueType: string;
newsData: NewsDataItem[];
recommendMatch: RecommendMatch;
supportHydrate: boolean;
}
/**
* 足球分类页面属性
*/
interface SoccerPageProps {
schedules: any[];
news: NewsDataItem[];
supportHydrate: boolean;
}
/**
* 所有分类页面属性的联合类型
*/
type CategoryPageProps = BasketballPageProps | SoccerPageProps;
/**
* API 响应的 props 结构
*/
interface ApiResponseProps {
pageProps: HomePageProps | CategoryPageProps;
__N_SSP: boolean;
}
/**
* 完整的 API 响应结构
*/
export interface HupuApiResponse {
props: ApiResponseProps;
page: string;
query: Record<string, any>;
buildId: string;
assetPrefix: string;
isFallback: boolean;
gssp: boolean;
scriptLoader: any[];
}
/**
* 用于区分不同页面类型的类型守卫
*/
export function isHomePageProps(pageProps: HomePageProps | CategoryPageProps): pageProps is HomePageProps {
return 'res' in pageProps;
}
export function isBasketballPageProps(pageProps: CategoryPageProps): pageProps is BasketballPageProps {
return 'newsData' in pageProps && 'leagueType' in pageProps;
}
export function isSoccerPageProps(pageProps: CategoryPageProps): pageProps is SoccerPageProps {
return 'news' in pageProps && 'schedules' in pageProps;
}
/**
* 用于区分不同数据项类型的类型守卫
*/
export function isHomePostItem(item: HomePostItem | NewsDataItem): item is HomePostItem {
return 'url' in item && 'isNews' in item;
}
export function isNewsDataItem(item: HomePostItem | NewsDataItem): item is NewsDataItem {
return 'nid' in item && 'link' in item && 'publishTime' in item;
}
// 导出具体类型以供外部使用
export type { Badge, HomePostItem, NewsDataItem, RecommendMatch, HomePageProps, BasketballPageProps, SoccerPageProps, CategoryPageProps, ApiResponseProps };