- Refactor `gui_core.py` to improve code readability and maintainability by adding line breaks for long statements. - Introduce `inspect_lines.py` script for inspecting specific line ranges in files. - Add `generate_param_locations.py` to scan the repository and generate a report of function definitions, argparse parameters, and tracking calls. - Implement `param_usage_tool.py` for analyzing `param_usage.json` records and scanning code for tracking calls. - Create `simulate_reservation_preempted_flow.py` and `simulate_reservation_preempted_flow_dynamic.py` to validate behavior of reservation logic under specific conditions. - Add global parameter tracking feature in `run_cli.py` to enable detailed usage tracing.
148 lines
5.6 KiB
Python
148 lines
5.6 KiB
Python
from __future__ import annotations
|
||
|
||
"""
|
||
福袋详情数据验证模块
|
||
====================
|
||
|
||
验证福袋详情数据的合理性,检测识别错误。
|
||
"""
|
||
|
||
from typing import Any, Tuple
|
||
import re
|
||
import os
|
||
import json
|
||
import path_config
|
||
|
||
|
||
def validate_fudai_detail(detail: Tuple, analyser: Any = None) -> Tuple[bool, str]:
|
||
"""验证福袋详情数据的合理性。
|
||
|
||
返回:
|
||
(is_valid, reason) - is_valid=True 表示数据合理,False 表示识别出错
|
||
reason 为失败原因描述
|
||
"""
|
||
if not isinstance(detail, tuple) or len(detail) < 6:
|
||
return False, "detail_tuple_invalid"
|
||
|
||
content = str(detail[0] or "").strip()
|
||
time_text = str(detail[1] or "").strip()
|
||
try:
|
||
participants = int(detail[2] or 0)
|
||
except Exception:
|
||
participants = 0
|
||
try:
|
||
copies = int(detail[3] or 0)
|
||
except Exception:
|
||
copies = 0
|
||
try:
|
||
prize_value_yuan = float(detail[4] or 0.0)
|
||
except Exception:
|
||
prize_value_yuan = 0.0
|
||
|
||
countdown_tuple = detail[5]
|
||
countdown_start, countdown_end = None, None
|
||
if isinstance(countdown_tuple, (tuple, list)) and len(countdown_tuple) >= 2:
|
||
try:
|
||
countdown_start = float(countdown_tuple[0] or 0.0)
|
||
countdown_end = float(countdown_tuple[1] or 0.0)
|
||
except Exception:
|
||
pass
|
||
|
||
# 检查1:内容不能为空(至少要有一些文字)
|
||
# 如果运行时显式禁用了内容识别,则跳过此检查(允许内容为空)
|
||
try:
|
||
disable_content = False
|
||
if analyser:
|
||
disable_content = bool(
|
||
getattr(analyser, "fudai_disable_content_recognition", False)
|
||
)
|
||
# 如果 analyser 未设置该属性,再尝试从 config.json 读取全局配置
|
||
if not disable_content:
|
||
try:
|
||
cfg_path = path_config.get_config_path()
|
||
if cfg_path and os.path.exists(cfg_path):
|
||
with open(cfg_path, "r", encoding="utf-8") as f:
|
||
cfg = json.load(f)
|
||
disable_content = bool(
|
||
cfg.get("fudai_disable_content_recognition", False)
|
||
)
|
||
except Exception:
|
||
pass
|
||
if not disable_content:
|
||
if not content or len(content.strip()) < 2:
|
||
return False, "content_empty_or_too_short"
|
||
except Exception:
|
||
if not content or len(content.strip()) < 2:
|
||
return False, "content_empty_or_too_short"
|
||
|
||
# 检查2:价值应该大于0(福袋至少应该有价值)
|
||
if prize_value_yuan <= 0:
|
||
return False, "prize_value_zero_or_negative"
|
||
|
||
# 检查3:价值应该合理(不能太大,比如超过100万)
|
||
if prize_value_yuan > 1000000:
|
||
return False, "prize_value_too_large"
|
||
|
||
# 检查4:倒计时应该合理
|
||
if countdown_end is not None and countdown_start is not None:
|
||
remain_seconds = countdown_end - countdown_start
|
||
# 倒计时不能是负数
|
||
if remain_seconds < 0:
|
||
return False, "countdown_negative"
|
||
# 倒计时不能太长(超过24小时)
|
||
if remain_seconds > 24 * 3600:
|
||
return False, "countdown_too_long"
|
||
# 倒计时不能为0(至少应该有几秒)
|
||
if remain_seconds == 0 and not time_text:
|
||
return False, "countdown_zero_and_no_time_text"
|
||
# 倒计时合理性检查:如果 time_text 存在,验证倒计时与 time_text 是否一致
|
||
# 直接参与标志已移除:始终执行倒计时与时间文本一致性检查
|
||
force_attend = False
|
||
|
||
if time_text and not force_attend:
|
||
try:
|
||
# 解析 time_text 为秒数
|
||
m = re.search(r"(\d{1,2})[::](\d{1,2})", time_text)
|
||
if m:
|
||
mm = int(m.group(1))
|
||
ss = int(m.group(2))
|
||
# 如果秒数>=60,尝试交换分秒
|
||
if ss >= 60 and mm < 60:
|
||
mm, ss = ss, mm
|
||
# 如果秒数仍然>=60,进行进位
|
||
if ss >= 60:
|
||
mm += ss // 60
|
||
ss = ss % 60
|
||
text_seconds = mm * 60 + ss
|
||
# 允许一定误差(±10%),但差异过大则判定为识别错误
|
||
if text_seconds > 0:
|
||
diff_ratio = abs(remain_seconds - text_seconds) / max(
|
||
text_seconds, 1.0
|
||
)
|
||
if diff_ratio > 0.15: # 差异超过15%视为不一致
|
||
return (
|
||
False,
|
||
f"countdown_mismatch_time_text (remain={int(remain_seconds)}, text={text_seconds}, diff={round(diff_ratio*100, 1)}%)",
|
||
)
|
||
except Exception:
|
||
pass
|
||
|
||
# 检查5:参与人数应该合理(不能是负数)
|
||
if participants < 0:
|
||
return False, "participants_negative"
|
||
|
||
# 检查6:份数应该合理(不能是负数,至少应该是1)
|
||
if copies < 0:
|
||
return False, "copies_negative"
|
||
|
||
# 检查7:如果份数为0,但参与人数大于0,可能识别错误
|
||
if copies == 0 and participants > 0:
|
||
return False, "copies_zero_but_participants_positive"
|
||
|
||
# 检查8:时间文本应该存在(至少应该有倒计时信息)
|
||
if not time_text and (countdown_end is None or countdown_end <= 0):
|
||
return False, "no_time_text_and_no_countdown"
|
||
|
||
# 所有检查通过
|
||
return True, "ok"
|