一分钟搭建影视站,支持Docker等部署方式
Add aggregated search feature and update API source handling
developer16d241bf3e96
diff --git a/index.html b/index.htmlindex d385bfd..af06868 100644--- a/index.html+++ b/index.html@@ -53,7 +53,8 @@<div><label class="block text-sm font-medium text-gray-400 mb-2">选择采集站点</label><select id="apiSource" class="w-full bg-[#222] border border-[#333] text-white px-3 py-2 rounded-lg focus:outline-none focus:border-white transition-colors">- <option value="heimuer">黑木耳影视 (heimuer)</option>+ <option value="aggregated">聚合搜索 (所有源)</option>+ <option value="heimuer" selected>黑木耳影视 (heimuer)</option><option value="ffzy">非凡影视 (ffzy)</option><option value="tyyszy">天涯资源 (tyyszy)</option><option value="ckzy">CK资源 (ckzy)</option>@@ -63,7 +64,7 @@<option value="dbzy">豆瓣资源 (dbzy)</option><option value="custom">自定义接口</option></select>- <p class="text-xs text-gray-500 mt-1">目前有效源: 黑木耳、非凡、天涯、CK、360、卧龙、新华为、豆瓣</p>+</div><!-- 添加自定义接口输入框 -->diff --git a/js/api.js b/js/api.jsindex b692795..8797bc9 100644--- a/js/api.js+++ b/js/api.js@@ -15,10 +15,15 @@ async function handleApiRequest(url) {throw new Error('使用自定义API时必须提供API地址');}- if (!API_SITES[source] && source !== 'custom') {+ if (!API_SITES[source] && source !== 'custom' && source !== 'aggregated') {throw new Error('无效的API来源');}+ // 处理聚合搜索+ if (source === 'aggregated') {+ return await handleAggregatedSearch(searchQuery);+ }+const apiUrl = customApi? `${customApi}${API_CONFIG.search.path}${encodeURIComponent(searchQuery)}`: `${API_SITES[source].api}${API_CONFIG.search.path}${encodeURIComponent(searchQuery)}`;@@ -46,6 +51,12 @@ async function handleApiRequest(url) {throw new Error('API返回的数据格式无效');}+ // 添加源信息到每个结果+ data.list.forEach(item => {+ item.source_name = source === 'custom' ? '自定义源' : API_SITES[source].name;+ item.source_code = source;+ });+return JSON.stringify({code: 200,list: data.list || [],@@ -56,8 +67,11 @@ async function handleApiRequest(url) {}}+ // 聚合搜索的详情处理 - 需要根据存储在数据中的源信息获取if (url.pathname === '/api/detail') {const id = url.searchParams.get('id');+ const sourceCode = url.searchParams.get('source') || 'heimuer'; // 获取源代码+if (!id) {throw new Error('缺少视频ID参数');}@@ -68,17 +82,22 @@ async function handleApiRequest(url) {}// 验证API和source的有效性- if (source === 'custom' && !customApi) {+ if (sourceCode === 'custom' && !customApi) {throw new Error('使用自定义API时必须提供API地址');}- if (!API_SITES[source] && source !== 'custom') {+ if (!API_SITES[sourceCode] && sourceCode !== 'custom') {throw new Error('无效的API来源');}+ // 对于ffzy源,使用特殊处理方式+ if (sourceCode === 'ffzy' && API_SITES[sourceCode].detail) {+ return await handleFFZYDetail(id, sourceCode);+ }+const detailUrl = customApi? `${customApi}${API_CONFIG.detail.path}${id}`- : `${API_SITES[source].api}${API_CONFIG.detail.path}${id}`;+ : `${API_SITES[sourceCode].api}${API_CONFIG.detail.path}${id}`;// 添加超时处理const controller = new AbortController();@@ -148,7 +167,10 @@ async function handleApiRequest(url) {area: videoDetail.vod_area,director: videoDetail.vod_director,actor: videoDetail.vod_actor,- remarks: videoDetail.vod_remarks+ remarks: videoDetail.vod_remarks,+ // 添加源信息+ source_name: sourceCode === 'custom' ? '自定义源' : API_SITES[sourceCode].name,+ source_code: sourceCode}});} catch (fetchError) {@@ -169,6 +191,186 @@ async function handleApiRequest(url) {}}+// 添加: 处理非凡影视详情的特殊函数+async function handleFFZYDetail(id, sourceCode) {+ try {+ // 构建详情页URL(使用配置中的detail URL而不是api URL)+ const detailUrl = `${API_SITES[sourceCode].detail}/index.php/vod/detail/id/${id}.html`;++ // 添加超时处理+ const controller = new AbortController();+ const timeoutId = setTimeout(() => controller.abort(), 10000);++ // 获取详情页HTML+ const response = await fetch(PROXY_URL + encodeURIComponent(detailUrl), {+ headers: {+ 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36',+ },+ signal: controller.signal+ });++ clearTimeout(timeoutId);++ if (!response.ok) {+ throw new Error(`详情页请求失败: ${response.status}`);+ }++ // 获取HTML内容+ const html = await response.text();++ // 非凡影视使用不同的正则表达式+ const ffzyPattern = /(?<=\$)(https?:\/\/[^"'\s]+?\/\d{8}\/\d+_[a-f0-9]+\/index\.m3u8)/g;+ let matches = html.match(ffzyPattern) || [];++ // 处理可能包含括号的链接+ matches = matches.map(link => {+ const parenIndex = link.indexOf('(');+ return parenIndex > 0 ? link.substring(0, parenIndex) : link;+ });++ // 如果没有找到链接,尝试一个更通用的模式+ if (matches.length === 0) {+ const generalPattern = /(?<=\$)(https?:\/\/[^"'\s]+?\.m3u8)/g;+ matches = html.match(generalPattern) || [];+ matches = matches.map(link => {+ const parenIndex = link.indexOf('(');+ return parenIndex > 0 ? link.substring(0, parenIndex) : link;+ });+ }++ // 提取可能存在的标题、简介等基本信息+ // 这些正则可能需要根据网站实际HTML结构调整+ const titleMatch = html.match(/<h1[^>]*>([^<]+)<\/h1>/);+ const titleText = titleMatch ? titleMatch[1].trim() : '';++ const descMatch = html.match(/<div[^>]*class=["']sketch["'][^>]*>([\s\S]*?)<\/div>/);+ const descText = descMatch ? descMatch[1].replace(/<[^>]+>/g, ' ').trim() : '';++ return JSON.stringify({+ code: 200,+ episodes: matches,+ detailUrl: detailUrl,+ videoInfo: {+ title: titleText,+ desc: descText,+ source_name: API_SITES[sourceCode].name,+ source_code: sourceCode+ }+ });+ } catch (error) {+ console.error('非凡影视详情获取失败:', error);+ throw error;+ }+}++// 新增: 处理聚合搜索+async function handleAggregatedSearch(searchQuery) {+ // 获取可用的API源列表(排除aggregated和custom)+ const availableSources = Object.keys(API_SITES).filter(key =>+ key !== 'aggregated' && key !== 'custom'+ );++ if (availableSources.length === 0) {+ throw new Error('没有可用的API源');+ }++ // 创建所有API源的搜索请求+ const searchPromises = availableSources.map(async (source) => {+ try {+ const apiUrl = `${API_SITES[source].api}${API_CONFIG.search.path}${encodeURIComponent(searchQuery)}`;++ // 使用Promise.race添加超时处理+ const timeoutPromise = new Promise((_, reject) =>+ setTimeout(() => reject(new Error(`${source}源搜索超时`)), 8000)+ );++ const fetchPromise = fetch(PROXY_URL + encodeURIComponent(apiUrl), {+ headers: API_CONFIG.search.headers+ });++ const response = await Promise.race([fetchPromise, timeoutPromise]);++ if (!response.ok) {+ throw new Error(`${source}源请求失败: ${response.status}`);+ }++ const data = await response.json();++ if (!data || !Array.isArray(data.list)) {+ throw new Error(`${source}源返回的数据格式无效`);+ }++ // 为搜索结果添加源信息+ const results = data.list.map(item => ({+ ...item,+ source_name: API_SITES[source].name,+ source_code: source+ }));++ return results;+ } catch (error) {+ console.warn(`${source}源搜索失败:`, error);+ return []; // 返回空数组表示该源搜索失败+ }+ });++ try {+ // 并行执行所有搜索请求+ const resultsArray = await Promise.all(searchPromises);++ // 合并所有结果+ let allResults = [];+ resultsArray.forEach(results => {+ if (Array.isArray(results) && results.length > 0) {+ allResults = allResults.concat(results);+ }+ });++ // 如果没有搜索结果,返回空结果+ if (allResults.length === 0) {+ return JSON.stringify({+ code: 200,+ list: [],+ msg: '所有源均无搜索结果'+ });+ }++ // 去重(根据vod_id和source_code组合)+ const uniqueResults = [];+ const seen = new Set();++ allResults.forEach(item => {+ const key = `${item.source_code}_${item.vod_id}`;+ if (!seen.has(key)) {+ seen.add(key);+ uniqueResults.push(item);+ }+ });++ // 按照视频名称和来源排序+ uniqueResults.sort((a, b) => {+ // 首先按照视频名称排序+ const nameCompare = (a.vod_name || '').localeCompare(b.vod_name || '');+ if (nameCompare !== 0) return nameCompare;++ // 如果名称相同,则按照来源排序+ return (a.source_name || '').localeCompare(b.source_name || '');+ });++ return JSON.stringify({+ code: 200,+ list: uniqueResults,+ });+ } catch (error) {+ console.error('聚合搜索处理错误:', error);+ return JSON.stringify({+ code: 400,+ msg: '聚合搜索处理失败: ' + error.message,+ list: []+ });+ }+}+// 拦截API请求(function() {const originalFetch = window.fetch;diff --git a/js/app.js b/js/app.jsindex c30f2d8..fed3f3e 100644--- a/js/app.js+++ b/js/app.js@@ -215,13 +215,19 @@ async function search() {.replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"');+ const sourceInfo = item.source_name ?+ `<span class="bg-[#222] text-xs px-2 py-1 rounded-full">${item.source_name}</span>` : '';+ const sourceCode = item.source_code || currentApiSource;return `<div class="card-hover bg-[#111] rounded-lg overflow-hidden cursor-pointer p-6 h-fit"- onclick="showDetails('${safeId}','${safeName}')">+ onclick="showDetails('${safeId}','${safeName}','${sourceCode}')"><h3 class="text-xl font-semibold mb-3">${safeName}</h3><p class="text-gray-400 text-sm mb-2">${(item.type_name || '').toString().replace(/</g, '<')}</p>- <p class="text-gray-400 text-sm">${(item.vod_remarks || '').toString().replace(/</g, '<')}</p>+ <div class="flex justify-between items-center mt-3">+ <p class="text-gray-400 text-sm">${(item.vod_remarks || '').toString().replace(/</g, '<')}</p>+ ${sourceInfo}+ </div></div>`;}).join('');@@ -237,8 +243,8 @@ async function search() {}}-// 显示详情-async function showDetails(id, vod_name) {+// 显示详情 - 修改函数接受sourceCode参数+async function showDetails(id, vod_name, sourceCode = currentApiSource) {if (!id) {showToast('视频ID无效', 'error');return;@@ -246,9 +252,9 @@ async function showDetails(id, vod_name) {showLoading();try {- const apiParams = currentApiSource === 'custom'+ const apiParams = sourceCode === 'custom'? '&customApi=' + encodeURIComponent(customApiUrl)- : '&source=' + currentApiSource;+ : '&source=' + sourceCode;// 添加超时处理const controller = new AbortController();@@ -266,9 +272,16 @@ async function showDetails(id, vod_name) {const modalTitle = document.getElementById('modalTitle');const modalContent = document.getElementById('modalContent');- modalTitle.textContent = vod_name || '未知视频';+ // 显示来源信息+ const sourceName = data.videoInfo && data.videoInfo.source_name ?+ ` <span class="text-sm font-normal text-gray-400">(${data.videoInfo.source_name})</span>` : '';++ modalTitle.innerHTML = (vod_name || '未知视频') + sourceName;currentVideoTitle = vod_name || '未知视频';+ // 保存当前源码以便后续操作+ currentApiSource = sourceCode;+if (data.episodes && data.episodes.length > 0) {// 安全处理集数URLconst safeEpisodes = data.episodes.map(url => {diff --git a/js/config.js b/js/config.jsindex 296b81f..6725e08 100644--- a/js/config.js+++ b/js/config.js@@ -18,10 +18,12 @@ const API_SITES = {heimuer: {api: 'https://json.heimuer.xyz',name: '黑木耳',+ detail: 'https://heimuer.tv'},ffzy: {api: 'http://ffzy5.tv',name: '非凡影视',+ detail: 'http://ffzy5.tv'},tyyszy: {api: 'https://tyyszy.com',@@ -49,6 +51,15 @@ const API_SITES = {}};+// 添加聚合搜索的配置选项+const AGGREGATED_SEARCH_CONFIG = {+ enabled: true, // 是否启用聚合搜索+ timeout: 8000, // 单个源超时时间(毫秒)+ maxResults: 10000, // 最大结果数量+ parallelRequests: true, // 是否并行请求所有源+ showSourceBadges: true // 是否显示来源徽章+};+// 抽象API请求配置const API_CONFIG = {search: {