添加抓取失败网址记录功能
- 新增 failed_urls 数据库表存储失败URL - 抓取失败时自动记录URL、标题、错误信息 - 搜索页面显示失败URL列表 - 支持重试单个/全部失败URL - 支持删除和清空失败记录 - 显示重试次数和时间
This commit is contained in:
@@ -351,4 +351,87 @@
|
||||
width: 100%;
|
||||
justify-content: space-between;
|
||||
}
|
||||
}
|
||||
|
||||
/* 失败URL区域 */
|
||||
.failed-urls-section {
|
||||
background: white;
|
||||
border-radius: 12px;
|
||||
box-shadow: 0 2px 4px rgba(0,0,0,0.05);
|
||||
margin-top: 20px;
|
||||
}
|
||||
|
||||
.failed-urls-section .panel-header {
|
||||
background: #fef3c7;
|
||||
}
|
||||
|
||||
.failed-urls-section .panel-header h2 {
|
||||
color: #92400e;
|
||||
}
|
||||
|
||||
.failed-count {
|
||||
color: #92400e;
|
||||
font-weight: bold;
|
||||
}
|
||||
|
||||
.failed-urls-list {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 10px;
|
||||
}
|
||||
|
||||
.failed-url-item {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: flex-start;
|
||||
padding: 15px;
|
||||
border: 1px solid #fcd34d;
|
||||
border-radius: 8px;
|
||||
background: #fffbeb;
|
||||
}
|
||||
|
||||
.failed-url-item.loading {
|
||||
opacity: 0.5;
|
||||
}
|
||||
|
||||
.failed-url-info {
|
||||
flex: 1;
|
||||
}
|
||||
|
||||
.failed-url-title {
|
||||
font-weight: 500;
|
||||
color: #333;
|
||||
margin-bottom: 5px;
|
||||
}
|
||||
|
||||
.failed-url-detail {
|
||||
font-size: 13px;
|
||||
margin-bottom: 5px;
|
||||
}
|
||||
|
||||
.failed-url-detail a {
|
||||
color: #3b82f6;
|
||||
text-decoration: none;
|
||||
}
|
||||
|
||||
.failed-url-detail a:hover {
|
||||
text-decoration: underline;
|
||||
}
|
||||
|
||||
.failed-error {
|
||||
color: #dc2626;
|
||||
margin-left: 10px;
|
||||
font-size: 12px;
|
||||
}
|
||||
|
||||
.failed-url-meta {
|
||||
font-size: 12px;
|
||||
color: #666;
|
||||
display: flex;
|
||||
gap: 15px;
|
||||
}
|
||||
|
||||
.failed-url-actions {
|
||||
display: flex;
|
||||
gap: 8px;
|
||||
}
|
||||
@@ -13,6 +13,9 @@ document.addEventListener('DOMContentLoaded', () => {
|
||||
doSearch();
|
||||
}
|
||||
});
|
||||
|
||||
// 加载失败URL
|
||||
loadFailedUrls();
|
||||
});
|
||||
|
||||
// 执行搜索
|
||||
@@ -199,17 +202,36 @@ async function fetchResult(index) {
|
||||
showToast('抓取成功', 'success');
|
||||
displayResults();
|
||||
} else {
|
||||
// 记录失败URL
|
||||
await recordFailedUrl(result.url, result.title, data.error);
|
||||
btn.disabled = false;
|
||||
btn.innerHTML = '<i class="ri-download-line"></i> 抓取';
|
||||
btn.className = 'btn btn-sm btn-danger';
|
||||
showToast('抓取失败: ' + data.error, 'error');
|
||||
}
|
||||
} catch (error) {
|
||||
// 记录失败URL
|
||||
await recordFailedUrl(result.url, result.title, '抓取出错');
|
||||
btn.disabled = false;
|
||||
btn.innerHTML = '<i class="ri-download-line"></i> 抓取';
|
||||
btn.className = 'btn btn-sm btn-danger';
|
||||
showToast('抓取出错', 'error');
|
||||
}
|
||||
}
|
||||
|
||||
// 记录失败的URL
|
||||
async function recordFailedUrl(url, title, errorMessage) {
|
||||
try {
|
||||
await fetch(`${API_BASE}/api/articles/failed-urls`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ url, title, error_message: errorMessage })
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('记录失败URL出错:', error);
|
||||
}
|
||||
}
|
||||
|
||||
// 保存单个结果
|
||||
async function saveResult(index) {
|
||||
const result = searchResults[index];
|
||||
@@ -431,4 +453,121 @@ function escapeHtml(text) {
|
||||
const div = document.createElement('div');
|
||||
div.textContent = text;
|
||||
return div.innerHTML;
|
||||
}
|
||||
|
||||
// ========== 失败URL管理 ==========
|
||||
|
||||
// 加载失败URL列表
|
||||
async function loadFailedUrls() {
|
||||
try {
|
||||
const response = await fetch(`${API_BASE}/api/articles/failed-urls`);
|
||||
const data = await response.json();
|
||||
|
||||
if (data.success) {
|
||||
document.getElementById('failed-count').textContent = `${data.count} 条`;
|
||||
|
||||
const container = document.getElementById('failed-urls-list');
|
||||
|
||||
if (data.urls.length === 0) {
|
||||
container.innerHTML = '<div class="empty-text">暂无失败记录</div>';
|
||||
} else {
|
||||
container.innerHTML = data.urls.map(url => `
|
||||
<div class="failed-url-item" id="failed-${url.id}">
|
||||
<div class="failed-url-info">
|
||||
<div class="failed-url-title">${escapeHtml(url.title || url.url.substring(0, 50))}</div>
|
||||
<div class="failed-url-detail">
|
||||
<a href="${escapeHtml(url.url)}" target="_blank">
|
||||
<i class="ri-external-link-line"></i> ${escapeHtml(url.url.substring(0, 60))}${url.url.length > 60 ? '...' : ''}
|
||||
</a>
|
||||
<span class="failed-error">${escapeHtml(url.error_message || '未知错误')}</span>
|
||||
</div>
|
||||
<div class="failed-url-meta">
|
||||
<span>重试: ${url.retry_count || 0} 次</span>
|
||||
<span>${url.created_at || ''}</span>
|
||||
</div>
|
||||
</div>
|
||||
<div class="failed-url-actions">
|
||||
<button onclick="retryFailedUrl(${url.id}, '${escapeHtml(url.url)}')" class="btn btn-sm btn-warning">
|
||||
<i class="ri-restart-line"></i> 重试
|
||||
</button>
|
||||
<button onclick="deleteFailedUrl(${url.id})" class="btn btn-sm btn-danger">
|
||||
<i class="ri-delete-bin-line"></i>
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
`).join('');
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('加载失败URL出错:', error);
|
||||
}
|
||||
}
|
||||
|
||||
// 重试单个失败URL
|
||||
async function retryFailedUrl(urlId, url) {
|
||||
const item = document.getElementById(`failed-${urlId}`);
|
||||
item.classList.add('loading');
|
||||
|
||||
try {
|
||||
const response = await fetch(`${API_BASE}/api/articles/failed-urls/retry`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ url })
|
||||
});
|
||||
|
||||
const data = await response.json();
|
||||
|
||||
if (data.success) {
|
||||
showToast('重试成功', 'success');
|
||||
loadFailedUrls();
|
||||
} else {
|
||||
showToast('重试失败: ' + data.error, 'error');
|
||||
item.classList.remove('loading');
|
||||
}
|
||||
} catch (error) {
|
||||
showToast('重试出错', 'error');
|
||||
item.classList.remove('loading');
|
||||
}
|
||||
}
|
||||
|
||||
// 全部重试
|
||||
async function retryAllFailed() {
|
||||
showToast('正在重试所有失败URL...', '');
|
||||
|
||||
const response = await fetch(`${API_BASE}/api/articles/failed-urls`);
|
||||
const data = await response.json();
|
||||
|
||||
if (data.success && data.urls.length > 0) {
|
||||
for (const url of data.urls) {
|
||||
await retryFailedUrl(url.id, url.url);
|
||||
await new Promise(r => setTimeout(r, 500)); // 避免太快
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 删除失败URL记录
|
||||
async function deleteFailedUrl(urlId) {
|
||||
try {
|
||||
await fetch(`${API_BASE}/api/articles/failed-urls/${urlId}`, {
|
||||
method: 'DELETE'
|
||||
});
|
||||
loadFailedUrls();
|
||||
} catch (error) {
|
||||
showToast('删除失败', 'error');
|
||||
}
|
||||
}
|
||||
|
||||
// 清空所有失败URL
|
||||
async function clearFailedUrls() {
|
||||
if (!confirm('确定要清空所有失败记录吗?')) return;
|
||||
|
||||
try {
|
||||
await fetch(`${API_BASE}/api/articles/failed-urls/clear`, {
|
||||
method: 'POST'
|
||||
});
|
||||
showToast('已清空', 'success');
|
||||
loadFailedUrls();
|
||||
} catch (error) {
|
||||
showToast('清空失败', 'error');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user