- Added comprehensive documentation for the unified screenshot and score recording process in the new markdown file. - Updated existing markdown files to include additional spacing for better readability. - Improved code structure by adding type hints and comments for clarity in various Python scripts. - Enhanced logging functionality to support better debugging and monitoring across modules. - Streamlined configuration settings to ensure consistency and clarity in the project structure.
354 lines
13 KiB
Python
354 lines
13 KiB
Python
from __future__ import annotations
|
|
|
|
"""
|
|
预约回退校验模块
|
|
================
|
|
|
|
提供回退到预约直播间后的详细校验功能:
|
|
- 直播间名称匹配
|
|
- 福袋详情匹配(内容、价值、开奖时间)
|
|
- 任务类型检查(排除"观看XX分钟"类任务)
|
|
"""
|
|
|
|
from typing import Any, Dict, Tuple, Optional
|
|
import re
|
|
import difflib
|
|
import time
|
|
|
|
import douyin_fudai_detail
|
|
|
|
|
|
def _compact_text(text: str) -> str:
|
|
"""压缩文本:去除空格,用于比较。"""
|
|
try:
|
|
return re.sub(r"\s+", "", str(text or "")).strip()
|
|
except Exception:
|
|
return ""
|
|
|
|
|
|
def match_reservation(item: Dict[str, Any], detail: Tuple) -> Tuple[bool, str, Dict[str, Any]]:
|
|
"""校验回退后的福袋详情是否匹配预约。返回 (ok, reason, debug_dict)
|
|
|
|
参数:
|
|
item: 预约项字典
|
|
detail: 福袋详情 tuple (content, time_text, participants, copies, prize_value_yuan, countdown_tuple)
|
|
|
|
返回:
|
|
(is_match, reason, debug_dict)
|
|
"""
|
|
try:
|
|
expected_value = float(item.get("prize_value_yuan") or 0.0)
|
|
except Exception:
|
|
expected_value = 0.0
|
|
try:
|
|
expected_future = float(item.get("future_ts") or 0.0)
|
|
except Exception:
|
|
expected_future = 0.0
|
|
expected_content = _compact_text(item.get("content") or "")
|
|
|
|
if not isinstance(detail, tuple) or len(detail) < 6:
|
|
return False, "detail_invalid", {
|
|
"expected_future": expected_future,
|
|
"expected_value": expected_value,
|
|
"expected_content": expected_content,
|
|
}
|
|
|
|
fc, tt, participants, copies, pv, cd = detail[:6]
|
|
actual_content = _compact_text(fc or "")
|
|
try:
|
|
actual_value = float(pv or 0.0)
|
|
except Exception:
|
|
actual_value = 0.0
|
|
try:
|
|
actual_future = float(cd[1] if (isinstance(cd, (tuple, list)) and len(cd) >= 2) else 0.0)
|
|
except Exception:
|
|
actual_future = 0.0
|
|
|
|
# 检查是否为"观看XX分钟"类任务:若是,则视为任务型福袋,回退校验应判为不匹配(跳过预约)
|
|
try:
|
|
fc_full = str(fc or "")
|
|
chinese_num_chars = "零一二三四五六七八九十百千两"
|
|
patt = rf"(?:观看|看)(?:直播)?\s*(?:\d+|[{chinese_num_chars}]+)\s*分钟"
|
|
if re.search(patt, fc_full):
|
|
return False, "watch_task_detected", {
|
|
"expected_future": expected_future,
|
|
"actual_future": actual_future,
|
|
"expected_value": expected_value,
|
|
"actual_value": actual_value,
|
|
"expected_content": expected_content,
|
|
"actual_content": actual_content,
|
|
}
|
|
except Exception:
|
|
pass
|
|
|
|
# future 必须可验证(否则无法确认回退成功)
|
|
if not (expected_future > 0 and actual_future > 0):
|
|
return False, "future_missing", {
|
|
"expected_future": expected_future,
|
|
"actual_future": actual_future,
|
|
"expected_value": expected_value,
|
|
"actual_value": actual_value,
|
|
"expected_content": expected_content,
|
|
"actual_content": actual_content,
|
|
}
|
|
|
|
future_diff = abs(expected_future - actual_future)
|
|
|
|
# value/content 二选一命中即可(OCR 有误差,且部分福袋可能不展示价值)
|
|
value_ok = False
|
|
if expected_value > 0 and actual_value > 0:
|
|
tol = max(50.0, expected_value * 0.25)
|
|
value_ok = abs(expected_value - actual_value) <= tol
|
|
|
|
content_ok = False
|
|
if expected_content and actual_content:
|
|
content_ok = (expected_content in actual_content) or (actual_content in expected_content)
|
|
|
|
if not (value_ok or content_ok):
|
|
return False, "content_or_value_mismatch", {
|
|
"expected_future": expected_future,
|
|
"actual_future": actual_future,
|
|
"future_diff_s": int(future_diff),
|
|
"expected_value": expected_value,
|
|
"actual_value": actual_value,
|
|
"expected_content": expected_content,
|
|
"actual_content": actual_content,
|
|
}
|
|
|
|
# 优先用 future 精确匹配;但在"内容+价值都一致"时,允许一定范围的 future 偏差,避免 OCR 倒计时误读导致误判。
|
|
# 说明:正常情况下 future_diff 应该很小;若 future_diff 过大仍会判不匹配。
|
|
if future_diff > 180:
|
|
if value_ok and content_ok and future_diff <= 1200:
|
|
return True, "future_mismatch_relaxed", {
|
|
"expected_future": expected_future,
|
|
"actual_future": actual_future,
|
|
"future_diff_s": int(future_diff),
|
|
"expected_value": expected_value,
|
|
"actual_value": actual_value,
|
|
"expected_content": expected_content,
|
|
"actual_content": actual_content,
|
|
}
|
|
return False, "future_mismatch", {
|
|
"expected_future": expected_future,
|
|
"actual_future": actual_future,
|
|
"future_diff_s": int(future_diff),
|
|
"expected_value": expected_value,
|
|
"actual_value": actual_value,
|
|
"expected_content": expected_content,
|
|
"actual_content": actual_content,
|
|
}
|
|
|
|
return True, "ok", {
|
|
"expected_future": expected_future,
|
|
"actual_future": actual_future,
|
|
"future_diff_s": int(future_diff),
|
|
"expected_value": expected_value,
|
|
"actual_value": actual_value,
|
|
"expected_content": expected_content,
|
|
"actual_content": actual_content,
|
|
}
|
|
|
|
|
|
def validate_back_to_reservation(
|
|
analyser: Any,
|
|
item: Dict[str, Any],
|
|
quick: bool = False,
|
|
max_checks: int = 3,
|
|
) -> Tuple[bool, Optional[Tuple]]:
|
|
"""回退后校验:必须看到福袋并且详情匹配预约。
|
|
|
|
参数:
|
|
analyser: 分析器对象
|
|
item: 预约项字典
|
|
quick: 是否快速模式(减少检查次数)
|
|
max_checks: 最大检查次数
|
|
|
|
返回:
|
|
(is_valid, detail_tuple) - 如果校验通过,返回详情 tuple,否则返回 None
|
|
"""
|
|
# 若开关关闭,则跳过复杂校验,直接认为回退成功以加速流程
|
|
try:
|
|
if not bool(getattr(analyser, "reservation_back_verify_enabled", True)):
|
|
analyser._log_kv("预约", "回退校验已被禁用:跳过校验")
|
|
return True, None
|
|
except Exception:
|
|
pass
|
|
|
|
# 先用"直播间名"做一次快速判断:不匹配则大概率回错房间,优先驱动微调继续找
|
|
try:
|
|
# 使用 douyin_high_value_room 中的规范化函数
|
|
import douyin_high_value_room
|
|
if hasattr(douyin_high_value_room, '_normalize_room_title'):
|
|
expected_title = douyin_high_value_room._normalize_room_title(item.get("room_title") or "") # type: ignore[attr-defined]
|
|
else:
|
|
expected_title = str(item.get("room_title") or "").strip()
|
|
except Exception:
|
|
try:
|
|
expected_title_raw_text = str(item.get("room_title") or "")
|
|
# 简单的规范化:去除空格和特殊字符
|
|
expected_title = re.sub(r"\s+", "", expected_title_raw_text).strip()
|
|
# 去掉常见无关词
|
|
for w in ("点赞", "赞", "关注", "粉丝", "直播中", "正在直播", "进入直播间", "推荐", "在线", "人在线"):
|
|
expected_title = expected_title.replace(w, "")
|
|
expected_title = re.sub(r"[^0-9A-Za-z\u4e00-\u9fff]", "", expected_title)
|
|
except Exception:
|
|
expected_title = str(item.get("room_title") or "").strip()
|
|
expected_title_raw = str(item.get("room_title_raw") or "").strip()
|
|
|
|
if expected_title:
|
|
try:
|
|
y_pianyi_val = int(getattr(analyser, "y_pianyi", 0) or 0)
|
|
current_title, current_title_raw = douyin_fudai_detail.read_live_room_title_core(
|
|
analyser, y_pianyi_val
|
|
)
|
|
# 更新 analyser 的属性
|
|
try:
|
|
analyser.current_room_title = current_title
|
|
analyser.current_room_title_raw = current_title_raw
|
|
except Exception:
|
|
pass
|
|
|
|
if current_title:
|
|
ratio = 0.0
|
|
try:
|
|
ratio = float(difflib.SequenceMatcher(None, expected_title, current_title).ratio())
|
|
except Exception:
|
|
ratio = 0.0
|
|
|
|
try:
|
|
analyser._log_kv(
|
|
"预约",
|
|
"回退校验:直播间名对比",
|
|
room_title_expected=expected_title,
|
|
room_title_current=current_title,
|
|
room_title_raw_expected=expected_title_raw,
|
|
room_title_raw_current=current_title_raw,
|
|
room_title_ratio=round(ratio, 3),
|
|
)
|
|
except Exception:
|
|
pass
|
|
|
|
# 强不匹配:直接失败(避免打开错误福袋详情浪费时间)
|
|
if current_title and (expected_title not in current_title) and (current_title not in expected_title) and ratio < 0.45:
|
|
return False, None
|
|
except Exception as e:
|
|
try:
|
|
analyser._log_kv("预约", "回退校验:读取直播间标题失败", error=str(e))
|
|
except Exception:
|
|
pass
|
|
|
|
# 检测福袋图标
|
|
x2 = 0
|
|
check_count = 1 if quick else max_checks
|
|
for i in range(check_count):
|
|
try:
|
|
import douyin_detection
|
|
analyser.operation.get_screenshot(analyser.device_id)
|
|
x2 = douyin_detection.check_have_fudai_with_ready_screenshot(analyser, screenshot_ready=True)
|
|
if x2 and x2 > 0:
|
|
break
|
|
except Exception:
|
|
x2 = 0
|
|
if i < check_count - 1:
|
|
try:
|
|
analyser._log_kv("预约", "回退校验:未发现福袋", attempt=i + 1)
|
|
except Exception:
|
|
pass
|
|
try:
|
|
analyser.operation.delay(0.5 if quick else 1.0)
|
|
except Exception:
|
|
time.sleep(0.5 if quick else 1.0)
|
|
|
|
if not x2 or x2 <= 0:
|
|
try:
|
|
analyser._log_kv("预约", "回退校验失败:未发现福袋", result="未发现福袋")
|
|
except Exception:
|
|
pass
|
|
return False, None
|
|
|
|
# 打开详情并确认进入详情页(避免点击后截图过早导致读错屏)
|
|
try:
|
|
analyser.click(x2, 400) # 点击福袋图标
|
|
analyser.operation.delay(0.8 if quick else 1.5)
|
|
except Exception:
|
|
try:
|
|
analyser.operation.click(analyser.device_id, x2, 400)
|
|
analyser.operation.delay(0.8 if quick else 1.5)
|
|
except Exception:
|
|
pass
|
|
|
|
# 确认是否在详情页
|
|
try:
|
|
import douyin_stage
|
|
analyser.operation.get_screenshot(analyser.device_id)
|
|
result = douyin_stage.diagnose_stage_from_current_screen(analyser)
|
|
if result is None:
|
|
stage = "IN_LIVE_ROOM"
|
|
else:
|
|
stage, _ = result
|
|
if stage != "FUDai_DETAIL":
|
|
try:
|
|
analyser._log_kv("预约", "回退校验失败:未能打开福袋详情", result="详情未打开", stage=stage)
|
|
except Exception:
|
|
pass
|
|
return False, None
|
|
except Exception:
|
|
pass
|
|
|
|
# 详情页刚出现时可能仍在动画/加载,稍等再识别更稳定
|
|
analyser.operation.delay(0.6 if quick else 1.1)
|
|
|
|
# 读取详情
|
|
try:
|
|
detail2 = douyin_fudai_detail.read_fudai_detail_with_retries(
|
|
analyser,
|
|
fudai_x=x2,
|
|
read_once_callback=douyin_fudai_detail.read_fudai_detail_once,
|
|
close_detail_callback=douyin_fudai_detail.close_fudai_detail,
|
|
ensure_open_callback=douyin_fudai_detail.ensure_fudai_detail_open,
|
|
)
|
|
|
|
if not detail2 or not isinstance(detail2, tuple) or len(detail2) < 6:
|
|
try:
|
|
analyser._log_kv("预约", "回退校验失败:详情读取异常", result="详情异常")
|
|
except Exception:
|
|
pass
|
|
# 确保关闭详情
|
|
try:
|
|
douyin_fudai_detail.close_fudai_detail(analyser)
|
|
except Exception:
|
|
pass
|
|
return False, None
|
|
except Exception as e:
|
|
try:
|
|
analyser._log_kv("预约", "回退校验失败:详情读取异常", error=str(e))
|
|
except Exception:
|
|
pass
|
|
try:
|
|
douyin_fudai_detail.close_fudai_detail(analyser)
|
|
except Exception:
|
|
pass
|
|
return False, None
|
|
|
|
# 匹配校验
|
|
ok, reason, dbg = match_reservation(item, detail2)
|
|
if not ok:
|
|
try:
|
|
analyser._log_kv("预约", "回退校验失败:福袋详情不匹配", result="不匹配", reason=reason, **dbg)
|
|
except Exception:
|
|
pass
|
|
try:
|
|
douyin_fudai_detail.close_fudai_detail(analyser)
|
|
except Exception:
|
|
pass
|
|
return False, None
|
|
|
|
# 校验通过
|
|
try:
|
|
analyser._log_kv("预约", "回退校验通过:福袋详情匹配", reason=reason, **dbg)
|
|
except Exception:
|
|
pass
|
|
|
|
return True, detail2
|
|
|