openlist增加定时扫描

This commit is contained in:
mtvpls
2025-12-22 22:28:54 +08:00
parent 76ee99cda2
commit 6f05f91a80
4 changed files with 89 additions and 1 deletions

View File

@@ -2544,6 +2544,7 @@ const OpenListConfigComponent = ({
const [username, setUsername] = useState('');
const [password, setPassword] = useState('');
const [rootPath, setRootPath] = useState('/');
const [scanInterval, setScanInterval] = useState(0);
const [videos, setVideos] = useState<any[]>([]);
const [refreshing, setRefreshing] = useState(false);
const [scanProgress, setScanProgress] = useState<{
@@ -2560,6 +2561,7 @@ const OpenListConfigComponent = ({
setUsername(config.OpenListConfig.Username || '');
setPassword(config.OpenListConfig.Password || '');
setRootPath(config.OpenListConfig.RootPath || '/');
setScanInterval(config.OpenListConfig.ScanInterval || 0);
}
}, [config]);
@@ -2597,6 +2599,7 @@ const OpenListConfigComponent = ({
Username: username,
Password: password,
RootPath: rootPath,
ScanInterval: scanInterval,
}),
});
@@ -2774,6 +2777,23 @@ const OpenListConfigComponent = ({
</p>
</div>
<div>
<label className='block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2'>
</label>
<input
type='number'
value={scanInterval}
onChange={(e) => setScanInterval(parseInt(e.target.value) || 0)}
placeholder='0'
min='0'
className='w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-lg bg-white dark:bg-gray-800 text-gray-900 dark:text-gray-100 focus:ring-2 focus:ring-blue-500 focus:border-transparent'
/>
<p className='mt-1 text-xs text-gray-500 dark:text-gray-400'>
0 60
</p>
</div>
<div className='flex gap-3'>
<button
onClick={handleSave}

View File

@@ -26,7 +26,7 @@ export async function POST(request: NextRequest) {
try {
const body = await request.json();
const { action, URL, Username, Password, RootPath } = body;
const { action, URL, Username, Password, RootPath, ScanInterval } = body;
const authInfo = getAuthInfoFromCookie(request);
if (!authInfo || !authInfo.username) {
@@ -56,6 +56,15 @@ export async function POST(request: NextRequest) {
);
}
// 验证扫描间隔
let scanInterval = parseInt(ScanInterval) || 0;
if (scanInterval > 0 && scanInterval < 60) {
return NextResponse.json(
{ error: '定时扫描间隔最低为 60 分钟' },
{ status: 400 }
);
}
// 验证账号密码是否正确
try {
console.log('[OpenList Config] 验证账号密码');
@@ -76,6 +85,7 @@ export async function POST(request: NextRequest) {
RootPath: RootPath || '/',
LastRefreshTime: adminConfig.OpenListConfig?.LastRefreshTime,
ResourceCount: adminConfig.OpenListConfig?.ResourceCount,
ScanInterval: scanInterval,
};
await db.saveAdminConfig(adminConfig);

View File

@@ -41,6 +41,7 @@ export async function GET(request: NextRequest) {
async function cronJob() {
await refreshConfig();
await refreshAllLiveChannels();
await refreshOpenList();
await refreshRecordAndFavorites();
}
@@ -264,3 +265,59 @@ async function refreshRecordAndFavorites() {
console.error('刷新播放记录/收藏任务启动失败', err);
}
}
async function refreshOpenList() {
try {
const config = await getConfig();
const openListConfig = config.OpenListConfig;
// 检查是否配置了 OpenList 和定时扫描
if (!openListConfig || !openListConfig.URL || !openListConfig.Username || !openListConfig.Password) {
console.log('跳过 OpenList 扫描:未配置');
return;
}
const scanInterval = openListConfig.ScanInterval || 0;
if (scanInterval === 0) {
console.log('跳过 OpenList 扫描:定时扫描已关闭');
return;
}
// 检查间隔时间是否满足最低要求60分钟
if (scanInterval < 60) {
console.log(`跳过 OpenList 扫描:间隔时间 ${scanInterval} 分钟小于最低要求 60 分钟`);
return;
}
// 检查上次扫描时间
const lastRefreshTime = openListConfig.LastRefreshTime || 0;
const now = Date.now();
const timeSinceLastRefresh = now - lastRefreshTime;
const intervalMs = scanInterval * 60 * 1000;
if (timeSinceLastRefresh < intervalMs) {
const remainingMinutes = Math.ceil((intervalMs - timeSinceLastRefresh) / 60000);
console.log(`跳过 OpenList 扫描:距离上次扫描仅 ${Math.floor(timeSinceLastRefresh / 60000)} 分钟,还需等待 ${remainingMinutes} 分钟`);
return;
}
console.log(`开始 OpenList 定时扫描(间隔: ${scanInterval} 分钟)`);
// 调用扫描接口
const response = await fetch(`${process.env.SITE_BASE || 'http://localhost:3000'}/api/openlist/refresh`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
});
if (!response.ok) {
throw new Error(`扫描请求失败: ${response.status}`);
}
const result = await response.json();
console.log('OpenList 定时扫描已启动任务ID:', result.taskId);
} catch (err) {
console.error('OpenList 定时扫描失败:', err);
}
}

View File

@@ -98,6 +98,7 @@ export interface AdminConfig {
RootPath: string; // 根目录路径,默认 "/"
LastRefreshTime?: number; // 上次刷新时间戳
ResourceCount?: number; // 资源数量
ScanInterval?: number; // 定时扫描间隔分钟0表示关闭最低60分钟
};
}