添加抓取失败网址记录功能
- 新增 failed_urls 数据库表存储失败URL - 抓取失败时自动记录URL、标题、错误信息 - 搜索页面显示失败URL列表 - 支持重试单个/全部失败URL - 支持删除和清空失败记录 - 显示重试次数和时间
This commit is contained in:
@@ -106,6 +106,21 @@ class Database:
|
||||
)
|
||||
''')
|
||||
|
||||
# 失败的URL记录表
|
||||
cursor.execute('''
|
||||
CREATE TABLE IF NOT EXISTS failed_urls (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
url TEXT NOT NULL,
|
||||
title TEXT,
|
||||
error_message TEXT,
|
||||
retry_count INTEGER DEFAULT 0,
|
||||
status TEXT DEFAULT 'failed',
|
||||
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
||||
last_retry_at DATETIME,
|
||||
source TEXT DEFAULT 'search'
|
||||
)
|
||||
''')
|
||||
|
||||
conn.commit()
|
||||
|
||||
# ========== 内容库操作 ==========
|
||||
@@ -309,5 +324,77 @@ class Database:
|
||||
''', (key, value))
|
||||
conn.commit()
|
||||
|
||||
# ========== 失败URL操作 ==========
|
||||
def add_failed_url(self, url, title=None, error_message=None, source='search'):
|
||||
"""添加失败的URL"""
|
||||
with self.get_connection() as conn:
|
||||
cursor = conn.cursor()
|
||||
# 先检查是否已存在
|
||||
cursor.execute('SELECT id, retry_count FROM failed_urls WHERE url = ?', (url,))
|
||||
existing = cursor.fetchone()
|
||||
|
||||
if existing:
|
||||
# 更新重试次数和错误信息
|
||||
cursor.execute('''
|
||||
UPDATE failed_urls
|
||||
SET error_message = ?, last_retry_at = CURRENT_TIMESTAMP, retry_count = retry_count + 1
|
||||
WHERE url = ?
|
||||
''', (error_message, url))
|
||||
else:
|
||||
# 新增失败记录
|
||||
cursor.execute('''
|
||||
INSERT INTO failed_urls (url, title, error_message, source)
|
||||
VALUES (?, ?, ?, ?)
|
||||
''', (url, title, error_message, source))
|
||||
conn.commit()
|
||||
return cursor.lastrowid
|
||||
|
||||
def get_failed_urls(self, limit=100, status='failed'):
|
||||
"""获取失败的URL列表"""
|
||||
with self.get_connection() as conn:
|
||||
cursor = conn.cursor()
|
||||
cursor.execute('''
|
||||
SELECT * FROM failed_urls
|
||||
WHERE status = ?
|
||||
ORDER BY created_at DESC
|
||||
LIMIT ?
|
||||
''', (status, limit))
|
||||
return [dict(row) for row in cursor.fetchall()]
|
||||
|
||||
def get_failed_url_count(self):
|
||||
"""获取失败URL数量"""
|
||||
with self.get_connection() as conn:
|
||||
cursor = conn.cursor()
|
||||
cursor.execute('SELECT COUNT(*) FROM failed_urls WHERE status = "failed"')
|
||||
return cursor.fetchone()[0]
|
||||
|
||||
def mark_url_success(self, url):
|
||||
"""标记URL为成功(已处理)"""
|
||||
with self.get_connection() as conn:
|
||||
cursor = conn.cursor()
|
||||
cursor.execute('''
|
||||
UPDATE failed_urls
|
||||
SET status = 'success', last_retry_at = CURRENT_TIMESTAMP
|
||||
WHERE url = ?
|
||||
''', (url,))
|
||||
conn.commit()
|
||||
return cursor.rowcount > 0
|
||||
|
||||
def delete_failed_url(self, url_id):
|
||||
"""删除失败URL记录"""
|
||||
with self.get_connection() as conn:
|
||||
cursor = conn.cursor()
|
||||
cursor.execute('DELETE FROM failed_urls WHERE id = ?', (url_id,))
|
||||
conn.commit()
|
||||
return cursor.rowcount > 0
|
||||
|
||||
def clear_failed_urls(self):
|
||||
"""清空所有失败URL记录"""
|
||||
with self.get_connection() as conn:
|
||||
cursor = conn.cursor()
|
||||
cursor.execute('DELETE FROM failed_urls WHERE status = "failed"')
|
||||
conn.commit()
|
||||
return cursor.rowcount
|
||||
|
||||
# 全局数据库实例
|
||||
db = Database()
|
||||
Reference in New Issue
Block a user