Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
32eebf3dd1 | ||
|
|
d9c9f0c633 | ||
|
|
909e8e01b5 | ||
|
|
e749d70a43 |
@@ -4,3 +4,5 @@ data/*.json
|
|||||||
data/cookies_*.json
|
data/cookies_*.json
|
||||||
logs/
|
logs/
|
||||||
out/
|
out/
|
||||||
|
data/exports/
|
||||||
|
data/auto_state/
|
||||||
@@ -104,13 +104,16 @@ def persist_cb(task_id, run):
|
|||||||
print(f"[db] 同步失败: {e}", flush=True)
|
print(f"[db] 同步失败: {e}", flush=True)
|
||||||
|
|
||||||
|
|
||||||
def start_run(task):
|
def start_run(task, skip_seed=False):
|
||||||
"""为任务启动一次爬取, 返回 (run, error)"""
|
"""为任务启动一次爬取, 返回 (run, error)
|
||||||
|
skip_seed: auto 任务继续爬取模式, 跳过起始网址直接从缓存队列消费
|
||||||
|
"""
|
||||||
with JOBS_LOCK:
|
with JOBS_LOCK:
|
||||||
job = JOBS.get(task["id"])
|
job = JOBS.get(task["id"])
|
||||||
if job and job.is_running():
|
if job and job.is_running():
|
||||||
return None, "该任务已有正在运行的爬取"
|
return None, "该任务已有正在运行的爬取"
|
||||||
run = make_run(task)
|
run = make_run(task)
|
||||||
|
run["skip_seed"] = bool(skip_seed)
|
||||||
store.add_run(task["id"], run)
|
store.add_run(task["id"], run)
|
||||||
job = CrawlJob(task, run, persist_cb)
|
job = CrawlJob(task, run, persist_cb)
|
||||||
JOBS[task["id"]] = job
|
JOBS[task["id"]] = job
|
||||||
@@ -305,11 +308,19 @@ def api_task_detail(tid):
|
|||||||
result["run_page"] = page
|
result["run_page"] = page
|
||||||
result["run_pages"] = pages
|
result["run_pages"] = pages
|
||||||
result["run_page_size"] = page_size
|
result["run_page_size"] = page_size
|
||||||
# auto 状态数量 (独立文件)
|
# auto 状态数量: 运行中优先读内存实时值, 否则读状态文件
|
||||||
if task.get("mode") == "auto":
|
if task.get("mode") == "auto":
|
||||||
|
live_v = live_p = None
|
||||||
|
with JOBS_LOCK:
|
||||||
|
job = JOBS.get(tid)
|
||||||
|
if job:
|
||||||
|
live_v = getattr(job, "_auto_visited", None)
|
||||||
|
live_p = getattr(job, "_auto_pending", None)
|
||||||
st = store.load_auto_state(tid, task)
|
st = store.load_auto_state(tid, task)
|
||||||
result["auto_pending_count"] = len(st.get("pending", []))
|
result["auto_pending_count"] = (
|
||||||
result["auto_visited_count"] = len(st.get("visited", []))
|
live_p if live_p is not None else len(st.get("pending", [])))
|
||||||
|
result["auto_visited_count"] = (
|
||||||
|
live_v if live_v is not None else len(st.get("visited", [])))
|
||||||
return jsonify(result)
|
return jsonify(result)
|
||||||
|
|
||||||
|
|
||||||
@@ -424,6 +435,23 @@ def api_trash_clear():
|
|||||||
|
|
||||||
# ---------------- API: 运行控制 ----------------
|
# ---------------- API: 运行控制 ----------------
|
||||||
|
|
||||||
|
@app.route("/api/tasks/<tid>/continue", methods=["POST"])
|
||||||
|
def api_continue(tid):
|
||||||
|
"""继续爬取: auto 任务从待爬缓存队列接着爬 (跳过起始网址, 保留已爬集合)"""
|
||||||
|
task = store.get_task(tid)
|
||||||
|
if not task:
|
||||||
|
return jsonify({"error": "任务不存在"}), 404
|
||||||
|
if task.get("mode") != "auto":
|
||||||
|
return jsonify({"error": "仅自动爬取任务支持继续爬取"}), 400
|
||||||
|
st = store.load_auto_state(tid, task)
|
||||||
|
if not st.get("pending"):
|
||||||
|
return jsonify({"error": "没有待爬缓存链接,无需继续"}), 400
|
||||||
|
run, err = start_run(task, skip_seed=True)
|
||||||
|
if err:
|
||||||
|
return jsonify({"error": err}), 409
|
||||||
|
return jsonify(run)
|
||||||
|
|
||||||
|
|
||||||
@app.route("/api/tasks/<tid>/start", methods=["POST"])
|
@app.route("/api/tasks/<tid>/start", methods=["POST"])
|
||||||
def api_start(tid):
|
def api_start(tid):
|
||||||
task = store.get_task(tid)
|
task = store.get_task(tid)
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -226,6 +226,9 @@ class CrawlJob:
|
|||||||
self.persist = persist # callable(task_id, run)
|
self.persist = persist # callable(task_id, run)
|
||||||
self._stop = threading.Event()
|
self._stop = threading.Event()
|
||||||
self._pause = threading.Event()
|
self._pause = threading.Event()
|
||||||
|
# 运行中的 auto 实时状态 (供详情接口读取; 任务结束时由状态文件兜底)
|
||||||
|
self._auto_visited = None
|
||||||
|
self._auto_pending = None
|
||||||
self._cfg_lock = threading.RLock()
|
self._cfg_lock = threading.RLock()
|
||||||
self.thread = None
|
self.thread = None
|
||||||
|
|
||||||
@@ -598,9 +601,10 @@ class CrawlJob:
|
|||||||
def _crawl_auto(self):
|
def _crawl_auto(self):
|
||||||
"""自动爬取: 持续递归 爬取->发现链接->爬取... 直到无新链接可爬或达到上限
|
"""自动爬取: 持续递归 爬取->发现链接->爬取... 直到无新链接可爬或达到上限
|
||||||
- max_depth: 0=无限制, N=只爬 N 层
|
- max_depth: 0=无限制, N=只爬 N 层
|
||||||
- max_pages: 0=无限制, N=安全上限
|
- max_pages: 0=无限制, N=安全上限 (继续爬取模式按本次新增页数重新计算)
|
||||||
- 提取但未爬取的链接持久化到 auto.pending, 已爬集合持久化到 auto.visited
|
- 提取但未爬取的链接持久化到 auto.pending, 已爬集合持久化到 auto.visited
|
||||||
- 停止后再次运行从缓存队列继续爬; 起始网址每次运行都重新爬(不去重)
|
- 停止后再次运行从缓存队列继续爬; 起始网址每次运行都重新爬(不去重)
|
||||||
|
- skip_seed(继续爬取): 跳过起始网址直接消费缓存队列, 页数上限按本次新增重新计算
|
||||||
"""
|
"""
|
||||||
run = self.run
|
run = self.run
|
||||||
auto = self.task.get("auto", {})
|
auto = self.task.get("auto", {})
|
||||||
@@ -615,8 +619,13 @@ class CrawlJob:
|
|||||||
state = store.load_auto_state(self.task["id"], self.task)
|
state = store.load_auto_state(self.task["id"], self.task)
|
||||||
visited = set(state.get("visited", []) or [])
|
visited = set(state.get("visited", []) or [])
|
||||||
pending = state.get("pending", []) or []
|
pending = state.get("pending", []) or []
|
||||||
|
visited_base = len(visited)
|
||||||
# 起始网址每次运行都爬(不做去重), 缓存队列继续消费
|
# 起始网址每次运行都爬(不做去重), 缓存队列继续消费
|
||||||
queue = [(seed, 0, "")]
|
# 继续爬取模式(skip_seed): 有缓存队列时跳过起始网址, 直接从待爬队列接着爬
|
||||||
|
skip_seed = bool(self.run.get("skip_seed"))
|
||||||
|
queue = []
|
||||||
|
if not (skip_seed and pending):
|
||||||
|
queue.append((seed, 0, ""))
|
||||||
if pending:
|
if pending:
|
||||||
queue.extend((p["url"], p.get("depth", 0), p.get("source", "")) for p in pending)
|
queue.extend((p["url"], p.get("depth", 0), p.get("source", "")) for p in pending)
|
||||||
queued = set(visited)
|
queued = set(visited)
|
||||||
@@ -624,20 +633,30 @@ class CrawlJob:
|
|||||||
queued.add(normalize_url(u))
|
queued.add(normalize_url(u))
|
||||||
|
|
||||||
run["progress"]["total"] = len(queue)
|
run["progress"]["total"] = len(queue)
|
||||||
|
self._auto_visited = len(visited)
|
||||||
|
self._auto_pending = len(queue)
|
||||||
self._persist()
|
self._persist()
|
||||||
|
|
||||||
p, browser, ctx, page, cookie_file = self._open_browser()
|
p, browser, ctx, page, cookie_file = self._open_browser()
|
||||||
try:
|
try:
|
||||||
while queue and not self._stop.is_set():
|
while queue and not self._stop.is_set():
|
||||||
self._wait_if_paused()
|
self._wait_if_paused()
|
||||||
if max_pages > 0 and len(visited) >= max_pages:
|
if max_pages > 0:
|
||||||
self._log("info", f"达到最大页数上限 {max_pages}, 停止")
|
if skip_seed:
|
||||||
break
|
# 继续爬取模式: 页数上限按本次新增页数重新计算 (累计 visited 不阻塞继续)
|
||||||
|
if len(visited) - visited_base >= max_pages:
|
||||||
|
self._log("info", f"本次继续爬取达到页数上限 {max_pages}, 停止")
|
||||||
|
break
|
||||||
|
elif len(visited) >= max_pages:
|
||||||
|
self._log("info", f"达到最大页数上限 {max_pages}, 停止")
|
||||||
|
break
|
||||||
url, depth, src = queue.pop(0)
|
url, depth, src = queue.pop(0)
|
||||||
key = normalize_url(url)
|
key = normalize_url(url)
|
||||||
if key in visited and url != seed: # 起始网址不去重, 其余已爬跳过
|
if key in visited and url != seed: # 起始网址不去重, 其余已爬跳过
|
||||||
continue
|
continue
|
||||||
visited.add(key)
|
visited.add(key)
|
||||||
|
self._auto_visited = len(visited)
|
||||||
|
self._auto_pending = len(queue)
|
||||||
idx = len(visited)
|
idx = len(visited)
|
||||||
run["progress"]["current_url"] = url
|
run["progress"]["current_url"] = url
|
||||||
run["progress"]["done"] = len(visited)
|
run["progress"]["done"] = len(visited)
|
||||||
@@ -654,6 +673,7 @@ class CrawlJob:
|
|||||||
if lk not in visited and lk not in queued:
|
if lk not in visited and lk not in queued:
|
||||||
queued.add(lk)
|
queued.add(lk)
|
||||||
queue.append((link, depth + 1, url))
|
queue.append((link, depth + 1, url))
|
||||||
|
self._auto_pending = len(queue) # 新链接入队后实时刷新
|
||||||
if entry["status"] == "OK":
|
if entry["status"] == "OK":
|
||||||
self._delay()
|
self._delay()
|
||||||
run["progress"]["total"] = len(visited)
|
run["progress"]["total"] = len(visited)
|
||||||
|
|||||||
+13
-1
@@ -602,7 +602,10 @@ function renderDetail() {
|
|||||||
${t.running ? `
|
${t.running ? `
|
||||||
<button class="btn" onclick="actTask('${t.id}','pause')">⏸ 暂停</button>
|
<button class="btn" onclick="actTask('${t.id}','pause')">⏸ 暂停</button>
|
||||||
<button class="btn danger" onclick="actTask('${t.id}','stop')">⏹ 终止</button>`
|
<button class="btn danger" onclick="actTask('${t.id}','stop')">⏹ 终止</button>`
|
||||||
: `<button class="btn primary" onclick="actTask('${t.id}','start')">▶ 立即执行</button>`}
|
: `
|
||||||
|
<button class="btn primary" onclick="actTask('${t.id}','start')">▶ 立即执行</button>
|
||||||
|
${t.mode === "auto" && (t.auto_pending_count || 0) > 0 ? `
|
||||||
|
<button class="btn" onclick="contTask('${t.id}')">⏩ 继续爬取(缓存 ${t.auto_pending_count} 条)</button>` : ""}`}
|
||||||
${t.mode === "auto" ? `<button class="btn" onclick="probeTask('${t.id}')">🧪 试爬取</button>
|
${t.mode === "auto" ? `<button class="btn" onclick="probeTask('${t.id}')">🧪 试爬取</button>
|
||||||
<button class="btn" ${t.running ? "disabled" : ""} onclick="clearCache('${t.id}')">🧹 清空缓存</button>` : ""}
|
<button class="btn" ${t.running ? "disabled" : ""} onclick="clearCache('${t.id}')">🧹 清空缓存</button>` : ""}
|
||||||
<button class="btn" onclick="openEdit('${t.id}')">✏️ 编辑配置</button>
|
<button class="btn" onclick="openEdit('${t.id}')">✏️ 编辑配置</button>
|
||||||
@@ -734,6 +737,15 @@ function stopLogPoll() {
|
|||||||
if (state.logTimer) { clearInterval(state.logTimer); state.logTimer = null; }
|
if (state.logTimer) { clearInterval(state.logTimer); state.logTimer = null; }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function contTask(tid) {
|
||||||
|
try {
|
||||||
|
const r = await api(`/api/tasks/${tid}/continue`, { method: "POST" });
|
||||||
|
toast("已开始继续爬取缓存队列");
|
||||||
|
loadTasks();
|
||||||
|
if (state.detail.task && state.detail.task.id === tid) openDetail(tid);
|
||||||
|
} catch (e) { toast(e.message, true); }
|
||||||
|
}
|
||||||
|
|
||||||
/* ---------------- 打包导出 ---------------- */
|
/* ---------------- 打包导出 ---------------- */
|
||||||
async function exportZip(tid) {
|
async function exportZip(tid) {
|
||||||
const btn = window.event && window.event.target;
|
const btn = window.event && window.event.target;
|
||||||
|
|||||||
Reference in New Issue
Block a user