feat: enhance video match

This commit is contained in:
shinya
2025-06-26 00:58:56 +08:00
parent c14cd4022b
commit 41c3e4ec85
14 changed files with 317 additions and 177 deletions

View File

@@ -17,6 +17,7 @@
export interface PlayRecord {
title: string;
source_name: string;
year: string;
cover: string;
index: number; // 第几集
total_episodes: number; // 总集数
@@ -270,6 +271,7 @@ export async function clearSearchHistory(): Promise<void> {
export interface Favorite {
title: string;
source_name: string;
year: string;
cover: string;
total_episodes: number;
save_time: number;

View File

@@ -0,0 +1,73 @@
export interface VideoDetail {
id: string;
title: string;
poster: string;
episodes: string[];
source: string;
source_name: string;
class?: string;
year: string;
desc?: string;
type_name?: string;
}
interface FetchVideoDetailOptions {
source: string;
id: string;
fallbackTitle?: string;
fallbackYear?: string;
}
/**
* 根据 source 与 id 获取视频详情。
* 1. 若传入 fallbackTitle则先调用 /api/search 搜索精确匹配。
* 2. 若搜索未命中或未提供 fallbackTitle则直接调用 /api/detail。
*/
export async function fetchVideoDetail({
source,
id,
fallbackTitle = '',
fallbackYear = '',
}: FetchVideoDetailOptions): Promise<VideoDetail> {
// 优先通过搜索接口查找精确匹配
if (fallbackTitle) {
try {
const searchResp = await fetch(
`/api/search?q=${encodeURIComponent(fallbackTitle)}`
);
if (searchResp.ok) {
const searchData = await searchResp.json();
const exactMatch = searchData.results.find(
(item: VideoDetail) =>
item.source.toString() === source.toString() &&
item.id.toString() === id.toString()
);
if (exactMatch) {
return exactMatch as VideoDetail;
}
}
} catch (error) {
// do nothing
}
}
// 调用 /api/detail 接口
const response = await fetch(`/api/detail?source=${source}&id=${id}`);
if (!response.ok) {
throw new Error('获取详情失败');
}
const data = await response.json();
return {
id: data?.videoInfo?.id || id,
title: data?.videoInfo?.title || fallbackTitle,
poster: data?.videoInfo?.cover || '',
episodes: data?.episodes || [],
source: data?.videoInfo?.source || source,
source_name: data?.videoInfo?.source_name || '',
class: data?.videoInfo?.remarks || '',
year: data?.videoInfo?.year || fallbackYear || '',
desc: data?.videoInfo?.desc || '',
type_name: data?.videoInfo?.type || '',
} as VideoDetail;
}

View File

@@ -1,7 +1,10 @@
import clsx, { ClassValue } from 'clsx';
import { twMerge } from 'tailwind-merge';
/** Merge classes with tailwind-merge with clsx full feature */
export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs));
export function cleanHtmlTags(text: string): string {
if (!text) return '';
return text
.replace(/<[^>]+>/g, '\n') // 将 HTML 标签替换为换行
.replace(/\n+/g, '\n') // 将多个连续换行合并为一个
.replace(/[ \t]+/g, ' ') // 将多个连续空格和制表符合并为一个空格,但保留换行符
.replace(/^\n+|\n+$/g, '') // 去掉首尾换行
.replace(/&nbsp;/g, ' ') // 将 &nbsp; 替换为空格
.trim(); // 去掉首尾空格
}