fix: Clean up blank lines and enhance readability across multiple files

- Removed excessive blank lines from various markdown and Python files to improve readability and maintainability.
- Ensured consistent formatting across documentation and code files, contributing to overall project clarity.
- Enhanced logging mechanisms for better consistency and debugging support.
This commit is contained in:
Your Name committed 2025-12-27 15:19:04 +08:00
1 parent 4678adb8f3
commit efaa3d3adc
36 files changed
+15075 -517

No files matched your search

+222 -167
View File
@@ -155,21 +155,40 @@ def validate_back_to_reservation(
quick: bool = False,
max_checks: int = 3,
) -> Tuple[bool, Optional[Tuple]]:
"""回退后校验:必须看到福袋并且详情匹配预约。
"""回退后校验:只校验直播间标题,不校验福袋。
如果直播间标题不符合,则根据映射表找到正确位置并回退。
参数:
analyser: 分析器对象
item: 预约项字典
quick: 是否快速模式(减少检查次数)
max_checks: 最大检查次数
max_checks: 最大检查次数(未使用,保留兼容性)
返回:
(is_valid, detail_tuple) - 如果校验通过,返回详情 tuple,否则返回 None
(is_valid, None) - 如果校验通过返回 True,否则返回 False
"""
try:
analyser._log_kv(
"预约",
"[预约] validate_back_to_reservation:函数入口",
target_pos=int(item.get("pos_seen", 0)),
target_room_title=str(item.get("room_title", ""))[:50],
quick=quick,
max_checks=max_checks,
level="DEBUG",
)
except Exception:
pass
# 若开关关闭,则跳过复杂校验,直接认为回退成功以加速流程
try:
if not bool(getattr(analyser, "reservation_back_verify_enabled", True)):
analyser._log_kv("预约", "回退校验已被禁用:跳过校验")
verify_enabled = bool(getattr(analyser, "reservation_back_verify_enabled", True))
if not verify_enabled:
try:
analyser._log_kv("预约", "回退校验已被禁用:跳过校验", level="DEBUG")
except Exception:
pass
return True, None
except Exception:
pass
@@ -195,6 +214,65 @@ def validate_back_to_reservation(
expected_title = str(item.get("room_title") or "").strip()
expected_title_raw = str(item.get("room_title_raw") or "").strip()
# 使用运行时映射表验证位置信息
target_pos_seen = int(item.get("pos_seen") or 0)
try:
room_pos_map = getattr(analyser, "_room_pos_to_title_map", {})
if target_pos_seen in room_pos_map:
recorded_title, recorded_title_raw = room_pos_map[target_pos_seen]
try:
analyser._log_kv(
"预约",
"回退校验:运行时映射表验证位置信息",
target_pos=target_pos_seen,
expected_room_title=expected_title[:50] if expected_title else "",
recorded_room_title=recorded_title[:50] if recorded_title else "",
expected_room_title_raw=expected_title_raw[:50] if expected_title_raw else "",
recorded_room_title_raw=recorded_title_raw[:50] if recorded_title_raw else "",
)
except Exception:
pass
# 如果映射表中的标题与预期标题匹配,增强验证信心
if expected_title and recorded_title:
try:
import douyin_high_value_room
if hasattr(douyin_high_value_room, '_normalize_room_title'):
expected_norm = douyin_high_value_room._normalize_room_title(expected_title) # type: ignore[attr-defined]
recorded_norm = douyin_high_value_room._normalize_room_title(recorded_title) # type: ignore[attr-defined]
if expected_norm == recorded_norm or expected_norm in recorded_norm or recorded_norm in expected_norm:
try:
analyser._log_kv(
"预约",
"回退校验:运行时映射表验证通过,位置和标题匹配",
target_pos=target_pos_seen,
)
except Exception:
pass
except Exception:
pass
else:
try:
analyser._log_kv(
"预约",
"回退校验:运行时映射表中未找到目标位置",
target_pos=target_pos_seen,
map_size=len(room_pos_map),
available_positions=list(room_pos_map.keys())[:10] if room_pos_map else [],
)
except Exception:
pass
except Exception as e:
try:
analyser._log_kv(
"预约",
"回退校验:运行时映射表验证时出错",
error=str(e),
error_type=type(e).__name__,
)
except Exception:
pass
if expected_title:
try:
y_pianyi_val = int(getattr(analyser, "y_pianyi", 0) or 0)
@@ -228,178 +306,155 @@ def validate_back_to_reservation(
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
try:
analyser._log_kv(
"预约",
"回退校验:直播间标题不匹配,尝试根据映射表找到正确位置",
expected_title=expected_title[:50] if expected_title else "",
current_title=current_title[:50] if current_title else "",
ratio=round(ratio, 3),
)
except Exception:
pass
# 根据映射表找到正确位置
try:
if hasattr(analyser, "_find_room_pos_by_title"):
correct_pos = analyser._find_room_pos_by_title(expected_title, expected_title_raw)
if correct_pos is not None:
try:
analyser._log_kv(
"预约",
"回退校验:在映射表中找到正确位置,准备回退",
correct_pos=correct_pos,
current_pos=int(getattr(analyser, "room_pos", 0) or 0),
expected_title=expected_title[:50] if expected_title else "",
)
except Exception:
pass
# 回退到正确位置
try:
# 更新 item 中的 pos_seen
item["pos_seen"] = correct_pos
# 调用回退函数
if hasattr(analyser, "_seek_reservation_room"):
seek_ok = analyser._seek_reservation_room(item)
if seek_ok:
# 重新读取标题验证
try:
y_pianyi_val = int(getattr(analyser, "y_pianyi", 0) or 0)
new_title, new_title_raw = douyin_fudai_detail.read_live_room_title_core(
analyser, y_pianyi_val
)
if new_title:
new_ratio = 0.0
try:
new_ratio = float(difflib.SequenceMatcher(None, expected_title, new_title).ratio())
except Exception:
pass
if (expected_title in new_title) or (new_title in expected_title) or new_ratio >= 0.45:
try:
analyser._log_kv(
"预约",
"回退校验:根据映射表回退后,标题匹配成功",
expected_title=expected_title[:50] if expected_title else "",
new_title=new_title[:50] if new_title else "",
new_ratio=round(new_ratio, 3),
)
except Exception:
pass
return True, None
else:
try:
analyser._log_kv(
"预约",
"回退校验:根据映射表回退后,标题仍不匹配",
expected_title=expected_title[:50] if expected_title else "",
new_title=new_title[:50] if new_title else "",
new_ratio=round(new_ratio, 3),
)
except Exception:
pass
return False, None
except Exception as e:
try:
analyser._log_kv(
"预约",
"回退校验:根据映射表回退后,重新读取标题失败",
error=str(e),
)
except Exception:
pass
return False, None
else:
try:
analyser._log_kv(
"预约",
"回退校验:根据映射表回退失败",
)
except Exception:
pass
return False, None
except Exception as e:
try:
analyser._log_kv(
"预约",
"回退校验:根据映射表回退时出错",
error=str(e),
error_type=type(e).__name__,
)
except Exception:
pass
return False, None
else:
try:
analyser._log_kv(
"预约",
"回退校验:映射表中未找到匹配的标题",
expected_title=expected_title[:50] if expected_title else "",
)
except Exception:
pass
return False, None
else:
try:
analyser._log_kv(
"预约",
"回退校验:analyser 没有 _find_room_pos_by_title 方法",
)
except Exception:
pass
return False, None
except Exception as e:
try:
analyser._log_kv(
"预约",
"回退校验:根据映射表查找位置时出错",
error=str(e),
error_type=type(e).__name__,
)
except Exception:
pass
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
# 校验通过,提取福袋详情
fc, tt, participants, copies, pv, cd = detail2[:6]
try:
remain_seconds = 0
if isinstance(cd, (tuple, list)) and len(cd) >= 2:
cd_end = float(cd[1] or 0.0)
remain_seconds = int(cd_end - time.time()) if cd_end > time.time() else 0
from datetime import datetime
cd_end_str = ""
if isinstance(cd, (tuple, list)) and len(cd) >= 2:
cd_end_val = float(cd[1] or 0.0)
if cd_end_val > 0:
cd_end_str = datetime.fromtimestamp(cd_end_val).strftime("%Y-%m-%d %H:%M:%S")
# 运行日志:添加福袋详情
analyser._log_kv(
"预约",
"回退校验通过:福袋详情匹配",
reason=reason,
target_room_title=str(item.get("room_title") or "").strip(),
target_room_title_raw=str(item.get("room_title_raw") or "").strip(),
fudai_content=str(fc or ""),
fudai_time_text=str(tt or ""),
fudai_participants=int(participants or 0),
fudai_copies=int(copies or 0),
fudai_value_yuan=float(pv or 0.0),
fudai_countdown_end=cd_end_str if cd_end_str else "",
fudai_remain_seconds=remain_seconds,
**dbg
"回退校验通过:直播间标题匹配",
target_room_title=str(item.get("room_title") or "").strip()[:50],
)
# 行为日志:添加福袋详情
analyser._log_kv(
"行为",
"回退校验通过:福袋详情匹配",
action="回退校验",
result="成功",
detail={
"目标直播间": str(item.get("room_title") or "").strip(),
"目标直播间原始": str(item.get("room_title_raw") or "").strip(),
"福袋内容": str(fc or ""),
"倒计时文本": str(tt or ""),
"参与人数": int(participants or 0),
"福袋份数": int(copies or 0),
"奖品价值": float(pv or 0.0),
"倒计时结束时间": cd_end_str if cd_end_str else "",
"剩余秒数": remain_seconds,
"校验原因": reason,
},
)
except Exception as e:
# 如果提取详情失败,至少记录基本信息
try:
analyser._log_kv("预约", "回退校验通过:福袋详情匹配", reason=reason, error=str(e), **dbg)
except Exception:
pass
except Exception:
pass
return True, detail2
return True, None