mirror of
https://github.com/MoonTechLab/LunaTV.git
synced 2026-02-27 23:34:48 +08:00
feat: add douban url, optimize source aggregate
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
|
||||
'use client';
|
||||
|
||||
import { LinkIcon } from 'lucide-react';
|
||||
import Image from 'next/image';
|
||||
import { useRouter, useSearchParams } from 'next/navigation';
|
||||
import { Suspense, useEffect, useState } from 'react';
|
||||
@@ -15,6 +16,7 @@ function AggregatePageClient() {
|
||||
const query = searchParams.get('q')?.trim() || '';
|
||||
const title = searchParams.get('title')?.trim() || '';
|
||||
const year = searchParams.get('year')?.trim() || '';
|
||||
const type = searchParams.get('type')?.trim() || '';
|
||||
|
||||
const [results, setResults] = useState<SearchResult[]>([]);
|
||||
const [loading, setLoading] = useState(true);
|
||||
@@ -46,11 +48,32 @@ function AggregatePageClient() {
|
||||
if (!titleMatch || !yearMatch) {
|
||||
return;
|
||||
}
|
||||
// 如果还传入了 type,则按 type 精确匹配
|
||||
if (type === 'tv' && r.episodes.length === 1) {
|
||||
return;
|
||||
}
|
||||
if (type === 'movie' && r.episodes.length !== 1) {
|
||||
return;
|
||||
}
|
||||
const key = `${r.title}-${r.year}`;
|
||||
const arr = map.get(key) || [];
|
||||
arr.push(r);
|
||||
map.set(key, arr);
|
||||
});
|
||||
if (map.size === 0 && type) {
|
||||
// 无匹配,忽略 type 做重新匹配
|
||||
all.forEach((r) => {
|
||||
const titleMatch = title ? r.title === title : r.title === query;
|
||||
const yearMatch = year ? r.year === year : true;
|
||||
if (!titleMatch || !yearMatch) {
|
||||
return;
|
||||
}
|
||||
const key = `${r.title}-${r.year}`;
|
||||
const arr = map.get(key) || [];
|
||||
arr.push(r);
|
||||
map.set(key, arr);
|
||||
});
|
||||
}
|
||||
if (map.size == 1) {
|
||||
setResults(Array.from(map.values()).flat());
|
||||
} else if (map.size > 1) {
|
||||
@@ -75,6 +98,24 @@ function AggregatePageClient() {
|
||||
return v.length > best.length ? v : best;
|
||||
}, undefined);
|
||||
};
|
||||
// 出现次数最多的非 0 数字
|
||||
const chooseNumber = (vals: (number | undefined)[]): number | undefined => {
|
||||
const countMap = new Map<number, number>();
|
||||
vals.forEach((v) => {
|
||||
if (v !== undefined && v !== 0) {
|
||||
countMap.set(v, (countMap.get(v) || 0) + 1);
|
||||
}
|
||||
});
|
||||
let selected: number | undefined = undefined;
|
||||
let maxCount = 0;
|
||||
countMap.forEach((cnt, num) => {
|
||||
if (cnt > maxCount) {
|
||||
maxCount = cnt;
|
||||
selected = num;
|
||||
}
|
||||
});
|
||||
return selected;
|
||||
};
|
||||
|
||||
const aggregatedInfo = {
|
||||
title: title || query,
|
||||
@@ -83,6 +124,7 @@ function AggregatePageClient() {
|
||||
type: chooseString(results.map((d) => d.type_name)),
|
||||
year: chooseString(results.map((d) => d.year)),
|
||||
remarks: chooseString(results.map((d) => d.class)),
|
||||
douban_id: chooseNumber(results.map((d) => d.douban_id)),
|
||||
};
|
||||
|
||||
const infoReady = Boolean(
|
||||
@@ -166,6 +208,17 @@ function AggregatePageClient() {
|
||||
>
|
||||
<h1 className='text-3xl font-bold mb-2 tracking-wide flex items-center flex-shrink-0 text-center md:text-left w-full'>
|
||||
{aggregatedInfo.title}
|
||||
{aggregatedInfo.douban_id && (
|
||||
<a
|
||||
href={`https://movie.douban.com/subject/${aggregatedInfo.douban_id}/`}
|
||||
target='_blank'
|
||||
rel='noopener noreferrer'
|
||||
onClick={(e) => e.stopPropagation()}
|
||||
className='ml-2'
|
||||
>
|
||||
<LinkIcon className='w-4 h-4' strokeWidth={2} />
|
||||
</a>
|
||||
)}
|
||||
</h1>
|
||||
<div className='flex flex-wrap items-center gap-3 text-base mb-4 opacity-80 flex-shrink-0'>
|
||||
{aggregatedInfo.remarks && (
|
||||
|
||||
@@ -19,6 +19,7 @@ interface ApiSearchItem {
|
||||
vod_class?: string;
|
||||
vod_year?: string;
|
||||
vod_content?: string;
|
||||
vod_douban_id?: number;
|
||||
type_name?: string;
|
||||
}
|
||||
|
||||
@@ -48,7 +49,6 @@ async function searchFromApi(
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
|
||||
if (
|
||||
!data ||
|
||||
!data.list ||
|
||||
@@ -92,6 +92,7 @@ async function searchFromApi(
|
||||
year: item.vod_year ? item.vod_year.match(/\d{4}/)?.[0] || '' : '',
|
||||
desc: cleanHtmlTags(item.vod_content || ''),
|
||||
type_name: item.type_name,
|
||||
douban_id: item.vod_douban_id,
|
||||
};
|
||||
});
|
||||
|
||||
@@ -161,6 +162,7 @@ async function searchFromApi(
|
||||
: '',
|
||||
desc: cleanHtmlTags(item.vod_content || ''),
|
||||
type_name: item.type_name,
|
||||
douban_id: item.vod_douban_id,
|
||||
};
|
||||
});
|
||||
} catch (error) {
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
'use client';
|
||||
|
||||
import { Heart } from 'lucide-react';
|
||||
import { Heart, LinkIcon } from 'lucide-react';
|
||||
import Image from 'next/image';
|
||||
import { useSearchParams } from 'next/navigation';
|
||||
import { Suspense, useEffect, useState } from 'react';
|
||||
@@ -173,6 +173,17 @@ function DetailPageClient() {
|
||||
>
|
||||
<h1 className='text-3xl font-bold mb-2 tracking-wide flex items-center flex-shrink-0 text-center md:text-left w-full'>
|
||||
{detail.title || fallbackTitle}
|
||||
{detail.douban_id && (
|
||||
<a
|
||||
href={`https://movie.douban.com/subject/${detail.douban_id}/`}
|
||||
target='_blank'
|
||||
rel='noopener noreferrer'
|
||||
onClick={(e) => e.stopPropagation()}
|
||||
className='ml-2'
|
||||
>
|
||||
<LinkIcon className='w-4 h-4' strokeWidth={2} />
|
||||
</a>
|
||||
)}
|
||||
</h1>
|
||||
<div className='flex flex-wrap items-center gap-3 text-base mb-4 opacity-80 flex-shrink-0'>
|
||||
{detail.class && (
|
||||
|
||||
@@ -203,6 +203,7 @@ function DoubanPageClient() {
|
||||
title={item.title}
|
||||
poster={item.poster}
|
||||
rate={item.rate}
|
||||
type={type || 'movie'}
|
||||
/>
|
||||
</div>
|
||||
))}
|
||||
|
||||
@@ -197,6 +197,7 @@ function HomeClient() {
|
||||
title={movie.title}
|
||||
poster={movie.poster}
|
||||
rate={movie.rate}
|
||||
type='movie'
|
||||
/>
|
||||
</div>
|
||||
))}
|
||||
@@ -242,6 +243,7 @@ function HomeClient() {
|
||||
title={show.title}
|
||||
poster={show.poster}
|
||||
rate={show.rate}
|
||||
type='tv'
|
||||
/>
|
||||
</div>
|
||||
))}
|
||||
|
||||
@@ -494,7 +494,12 @@ function PlayPageClient() {
|
||||
result.title.toLowerCase() === videoTitle.toLowerCase() &&
|
||||
(videoYear
|
||||
? result.year.toLowerCase() === videoYear.toLowerCase()
|
||||
: true)
|
||||
: true) &&
|
||||
detailRef.current?.episodes.length &&
|
||||
((detailRef.current?.episodes.length === 1 &&
|
||||
result.episodes.length === 1) ||
|
||||
(detailRef.current?.episodes.length > 1 &&
|
||||
result.episodes.length > 1))
|
||||
);
|
||||
|
||||
if (exactMatch) {
|
||||
|
||||
@@ -44,7 +44,9 @@ function SearchPageClient() {
|
||||
const map = new Map<string, SearchResult[]>();
|
||||
searchResults.forEach((item) => {
|
||||
// 使用 title + year 作为键,若 year 不存在则使用 'unknown'
|
||||
const key = `${item.title}-${item.year || 'unknown'}`;
|
||||
const key = `${item.title}-${item.year || 'unknown'}-${
|
||||
item.episodes.length === 1 ? 'movie' : 'tv'
|
||||
}`;
|
||||
const arr = map.get(key) || [];
|
||||
arr.push(item);
|
||||
map.set(key, arr);
|
||||
@@ -196,6 +198,7 @@ function SearchPageClient() {
|
||||
episodes={item.episodes.length}
|
||||
source={item.source}
|
||||
source_name={item.source_name}
|
||||
douban_id={item.douban_id}
|
||||
from='search'
|
||||
/>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user