feat(collect): persist product status snapshots

This commit is contained in:
chengma
2026-07-18 16:34:37 +08:00
parent b4250f36f8
commit 0cf23db403
11 changed files with 464 additions and 12 deletions
+31 -1
View File
@@ -5,7 +5,7 @@ import os
import time
from urllib.parse import urlparse
from . import appconfig, image_paths
from . import appconfig, image_paths, product_status
from .cdp import (
CDP,
close_tab,
@@ -112,6 +112,17 @@ JS_RECTS = (
"return JSON.stringify(a);})()"
)
JS_PRODUCT_STATUS_ALERTS = (
"(function(){"
"var nodes=[].slice.call(document.querySelectorAll('.eds-alert.eds-alert--warning'));"
"return JSON.stringify(nodes.map(function(node){"
"var title=node.querySelector('.eds-alert-title');"
"var desc=node.querySelector('.eds-alert-desc');"
"return {title:(title&&(title.innerText||title.textContent)||'').trim(),"
"description:(desc&&(desc.innerText||desc.textContent)||'').trim()};"
"}));})()"
)
JS_UPLOAD_STATE = (
"(function(){"
@@ -1153,6 +1164,8 @@ def collect(account, task, on_step=None) -> dict:
cdp = open_product(account, item_id, on_step=on_step, bring_to_front=False)
result = None
try:
_notify_collect_step(on_step, "read_product_status")
status_snapshot = read_product_status(cdp)
_notify_collect_step(on_step, "read_title")
old_title = read_title(cdp)
_notify_collect_step(on_step, "read_cover")
@@ -1171,6 +1184,7 @@ def collect(account, task, on_step=None) -> dict:
"old_title": old_title,
"old_cover_src": old_cover_src,
"old_cover_path": old_cover_path,
**status_snapshot,
}
finally:
close_target_confirmed = _close_collected_product(cdp)
@@ -1178,6 +1192,22 @@ def collect(account, task, on_step=None) -> dict:
return result
def read_product_status(cdp) -> dict:
"""Read the current product's warning state without retaining page HTML."""
try:
alerts = _json_value(cdp, JS_PRODUCT_STATUS_ALERTS, default=None)
if not isinstance(alerts, list):
raise ValueError("商品状态提示读取结果无效")
snapshot = product_status.classify_alerts(alerts)
snapshot["product_status_error"] = None
return snapshot
except Exception as exc:
snapshot = product_status.classify_alerts(None)
snapshot["product_status_error"] = str(exc) or exc.__class__.__name__
return snapshot
def _notify_collect_step(callback, step):
if callback is None: