Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
57d45d901b |
@@ -15,7 +15,7 @@ from flask import Flask, jsonify, request, send_file, send_from_directory
|
|||||||
import store
|
import store
|
||||||
import db
|
import db
|
||||||
import notify
|
import notify
|
||||||
from engine import CrawlJob, probe_links, normalize_url
|
from engine import CrawlJob, probe_links, normalize_url, url_excluded
|
||||||
from scheduler import Scheduler, cron_next, interval_delta
|
from scheduler import Scheduler, cron_next, interval_delta
|
||||||
|
|
||||||
HERE = os.path.dirname(os.path.abspath(__file__))
|
HERE = os.path.dirname(os.path.abspath(__file__))
|
||||||
@@ -345,7 +345,22 @@ def api_update_task(tid):
|
|||||||
if running:
|
if running:
|
||||||
job.update_config(body["config"]) # 运行中热更新
|
job.update_config(body["config"]) # 运行中热更新
|
||||||
if "auto" in body and task.get("mode") == "auto":
|
if "auto" in body and task.get("mode") == "auto":
|
||||||
|
old_exclude = task.get("auto", {}).get("exclude") or []
|
||||||
task["auto"] = {**task.get("auto", {}), **body["auto"]}
|
task["auto"] = {**task.get("auto", {}), **body["auto"]}
|
||||||
|
new_exclude = task["auto"].get("exclude") or []
|
||||||
|
# 排除规则发生变化时, 同步清理待爬队列中已命中的 URL (已爬 visited 保留)
|
||||||
|
if new_exclude and new_exclude != old_exclude and not running:
|
||||||
|
st = store.load_auto_state(tid, task)
|
||||||
|
kept = [p for p in st.get("pending", [])
|
||||||
|
if not url_excluded(p.get("url", ""), task["auto"])]
|
||||||
|
removed = len(st.get("pending", [])) - len(kept)
|
||||||
|
if removed:
|
||||||
|
store.save_auto_state(tid, {
|
||||||
|
"pending": kept,
|
||||||
|
"visited": st.get("visited", []),
|
||||||
|
})
|
||||||
|
if "exclude" in body.get("auto", {}):
|
||||||
|
task["auto"]["_cleaned"] = removed
|
||||||
if "schedule" in body and task.get("mode") == "scheduled":
|
if "schedule" in body and task.get("mode") == "scheduled":
|
||||||
sch = {**task.get("schedule", {}), **body["schedule"]}
|
sch = {**task.get("schedule", {}), **body["schedule"]}
|
||||||
try:
|
try:
|
||||||
|
|||||||
@@ -151,6 +151,21 @@ def normalize_url(url):
|
|||||||
return str(url)
|
return str(url)
|
||||||
|
|
||||||
|
|
||||||
|
def url_excluded(url, auto=None):
|
||||||
|
"""判断 URL 是否命中排除规则 (与 filter_links 的 exclude 判定逻辑一致, 供队列清理使用)"""
|
||||||
|
if not auto:
|
||||||
|
return False
|
||||||
|
exclude = auto.get("exclude") or []
|
||||||
|
if not exclude:
|
||||||
|
return False
|
||||||
|
use_regex = bool(auto.get("use_regex"))
|
||||||
|
s = str(url or "")
|
||||||
|
if use_regex:
|
||||||
|
return any(re.search(p, s) for p in exclude)
|
||||||
|
low = s.lower()
|
||||||
|
return any(p.lower() in low for p in exclude)
|
||||||
|
|
||||||
|
|
||||||
def filter_links(hrefs, seed_url, include=None, exclude=None,
|
def filter_links(hrefs, seed_url, include=None, exclude=None,
|
||||||
same_domain=True, use_regex=False):
|
same_domain=True, use_regex=False):
|
||||||
"""按规则过滤链接, 返回 (included, excluded); excluded 含排除原因"""
|
"""按规则过滤链接, 返回 (included, excluded); excluded 含排除原因"""
|
||||||
|
|||||||
+1
-1
@@ -126,7 +126,7 @@
|
|||||||
<div class="field"><label>包含规则(每行一个,子串或正则)</label>
|
<div class="field"><label>包含规则(每行一个,子串或正则)</label>
|
||||||
<textarea name="include" rows="3" placeholder="techpowerup.com/review /news/"></textarea></div>
|
<textarea name="include" rows="3" placeholder="techpowerup.com/review /news/"></textarea></div>
|
||||||
<div class="field"><label>排除规则</label>
|
<div class="field"><label>排除规则</label>
|
||||||
<textarea name="exclude" rows="3" placeholder="login, signup, /tag/, /forum/"></textarea></div>
|
<textarea name="exclude" rows="3" placeholder="每行一个关键词,URL 含任一关键词即不爬取 如: MyComments.html、OtherPosts.html、/comments 勾选正则后按正则匹配"></textarea></div>
|
||||||
</div>
|
</div>
|
||||||
<div class="row2">
|
<div class="row2">
|
||||||
<div class="field check"><label><input name="same_domain" type="checkbox" checked> 仅爬同域名</label></div>
|
<div class="field check"><label><input name="same_domain" type="checkbox" checked> 仅爬同域名</label></div>
|
||||||
|
|||||||
Reference in New Issue
Block a user