fix: 修复代码格式问题,提升可读性

This commit is contained in:
Your Name committed 2026-01-16 02:31:34 +08:00
1 parent 71d2f218cf
commit 362b7c841c
6 files changed
+126 -35

No files matched your search

+2 -2
View File
@@ -293,8 +293,8 @@ class fudai_analyse:
)
# 运行时直播间标题和位置映射表(用于回退校验)
# 格式:{room_pos: (room_title, room_title_raw)}
self._room_pos_to_title_map: dict[int, tuple[str, str]] = {}
# 格式:{room_pos: {"title": str, "title_raw": str, "has_fudai": bool, "fudai_content": str, "fudai_detail": Any}}
self._room_pos_to_title_map: dict[int, dict[str, Any]] = {}
# 每次重新运行时丢弃磁盘上旧的映射表,确保只记录本次运行的结果。
# 同时尝试删除旧的导出文件(JSON/CSV/MD),以避免 GUI 在未写入新数据前显示过期内容。
try:
@@ -84,7 +84,14 @@ class ReservationHandler:
best_match_pos: Optional[int] = None
best_match_score = 0.0
for pos, (recorded_title, recorded_title_raw) in room_pos_map.items():
for pos, room_info in room_pos_map.items():
if isinstance(room_info, dict):
recorded_title = room_info.get("title", "")
recorded_title_raw = room_info.get("title_raw", "")
else:
# 兼容旧格式
recorded_title, recorded_title_raw = room_info
if not recorded_title and not recorded_title_raw:
continue
@@ -179,7 +186,7 @@ class ReservationHandler:
参数:
analyser: 分析器对象
"""
# 检查配置项,只有在启用时才记录映射表
# 检查配置项,只有在启用相关功能时才记录映射表
try:
mapping_enabled = bool(
getattr(analyser, "record_room_title_mapping_enabled", True)
@@ -187,7 +194,10 @@ class ReservationHandler:
revisit_enabled = bool(
getattr(analyser, "high_value_room_revisit_enabled", False)
)
if not mapping_enabled or not revisit_enabled:
back_verify_enabled = bool(
getattr(analyser, "reservation_back_verify_enabled", True)
)
if not mapping_enabled or (not revisit_enabled and not back_verify_enabled):
return
except Exception:
pass
@@ -211,23 +221,38 @@ class ReservationHandler:
except Exception:
pass
# 记录到映射表
# 记录到映射表(包含福袋信息)
try:
# 检查是否有福袋
has_fudai = bool(getattr(analyser, "_current_fudai_content", ""))
fudai_content = str(getattr(analyser, "_current_fudai_content", ""))
fudai_detail = getattr(analyser, "_last_fudai_detail_tuple", None)
room_info = {
"title": room_title,
"title_raw": room_title_raw,
"has_fudai": has_fudai,
"fudai_content": fudai_content,
"fudai_detail": fudai_detail,
"timestamp": time.time(),
}
if room_pos not in getattr(analyser, "_room_pos_to_title_map", {}):
analyser._room_pos_to_title_map[room_pos] = (
room_title,
room_title_raw,
)
analyser._room_pos_to_title_map[room_pos] = room_info
try:
analyser._log_kv( # type: ignore[attr-defined]
"预约",
"[预约] 记录直播间标题和位置到运行时映射表",
"[预约] 记录直播间信息到运行时映射表",
log_type="OP",
room_pos=room_pos,
room_title=room_title[:50] if room_title else "",
room_title_raw=(
room_title_raw[:50] if room_title_raw else ""
),
has_fudai=has_fudai,
fudai_content=(
fudai_content[:50] if fudai_content else ""
),
map_size=len(
getattr(analyser, "_room_pos_to_title_map", {})
),
@@ -240,25 +265,33 @@ class ReservationHandler:
except Exception:
pass
else:
# 如果已存在,更新标题(可能标题有变化)
old_title, old_title_raw = analyser._room_pos_to_title_map[
room_pos
]
if old_title != room_title or old_title_raw != room_title_raw:
analyser._room_pos_to_title_map[room_pos] = (
room_title,
room_title_raw,
)
# 如果已存在,检查是否需要更新
old_info = analyser._room_pos_to_title_map[room_pos]
needs_update = (
old_info.get("title") != room_title
or old_info.get("title_raw") != room_title_raw
or old_info.get("has_fudai") != has_fudai
or old_info.get("fudai_content") != fudai_content
)
if needs_update:
analyser._room_pos_to_title_map[room_pos] = room_info
try:
analyser._log_kv( # type: ignore[attr-defined]
"预约",
"[预约] 更新直播间标题和位置到运行时映射表",
"[预约] 更新直播间信息到运行时映射表",
log_type="OP",
room_pos=room_pos,
old_room_title=old_title[:50] if old_title else "",
new_room_title=(
room_title[:50] if room_title else ""
old_room_title=(
old_info.get("title", "")[:30]
if old_info.get("title")
else ""
),
new_room_title=(
room_title[:30] if room_title else ""
),
old_has_fudai=old_info.get("has_fudai", False),
new_has_fudai=has_fudai,
)
except Exception:
pass
@@ -277,17 +310,38 @@ class ReservationHandler:
getattr(analyser, "current_room_title_raw", "") or ""
)
if room_title or room_title_raw:
analyser._room_pos_to_title_map[room_pos] = (
room_title,
room_title_raw,
# 检查是否有福袋
has_fudai = bool(
getattr(analyser, "_current_fudai_content", "")
)
fudai_content = str(
getattr(analyser, "_current_fudai_content", "")
)
fudai_detail = getattr(
analyser, "_last_fudai_detail_tuple", None
)
room_info = {
"title": room_title,
"title_raw": room_title_raw,
"has_fudai": has_fudai,
"fudai_content": fudai_content,
"fudai_detail": fudai_detail,
"timestamp": time.time(),
}
analyser._room_pos_to_title_map[room_pos] = room_info
try:
analyser._log_kv( # type: ignore[attr-defined]
"预约",
"[预约] 使用缓存标题记录直播间位置到运行时映射表",
"[预约] 使用缓存标题记录直播间信息到运行时映射表",
log_type="OP",
room_pos=room_pos,
room_title=room_title[:50] if room_title else "",
has_fudai=has_fudai,
fudai_content=(
fudai_content[:50] if fudai_content else ""
),
error=str(e),
)
except Exception:
@@ -291,7 +291,18 @@ def validate_back_to_reservation(
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]
room_info = room_pos_map[target_pos_seen]
if isinstance(room_info, dict):
recorded_title = room_info.get("title", "")
recorded_title_raw = room_info.get("title_raw", "")
has_fudai = room_info.get("has_fudai", False)
fudai_content = room_info.get("fudai_content", "")
else:
# 兼容旧格式
recorded_title, recorded_title_raw = room_info
has_fudai = False
fudai_content = ""
try:
analyser._log_kv(
"预约",
@@ -305,6 +316,8 @@ def validate_back_to_reservation(
recorded_room_title_raw=(
recorded_title_raw[:50] if recorded_title_raw else ""
),
has_fudai=has_fudai,
fudai_content=fudai_content[:50] if fudai_content else "",
)
except Exception:
pass
+13 -3
View File
@@ -2759,9 +2759,19 @@ def main():
keys = list(parsed.keys())
for k in keys:
try:
v = parsed.get(k, ["", ""]) or ["", ""]
title = (v[0] or "").strip() or "-"
title_raw = (v[1] or "").strip() or "-"
v = parsed.get(k, {})
if isinstance(v, dict):
# 新格式:字典
title = (v.get("title", "") or "").strip() or "-"
title_raw = (v.get("title_raw", "") or "").strip() or "-"
has_fudai = v.get("has_fudai", False)
fudai_content = (v.get("fudai_content", "") or "").strip()
if has_fudai and fudai_content:
title += f" [福袋:{fudai_content[:20]}]"
else:
# 兼容旧格式:元组
title = (v[0] or "").strip() or "-"
title_raw = (v[1] or "").strip() or "-"
except Exception:
title, title_raw = "-", "-"
mapping_tree.insert("", "end", values=(k, title, title_raw))
+9 -2
View File
@@ -284,8 +284,15 @@ def test_reservation_backtrack():
# 根据当前位置返回对应的标题
pos = analyser.room_pos
if pos in analyser._room_pos_to_title_map:
title, title_raw = analyser._room_pos_to_title_map[pos]
return title, title_raw
room_info = analyser._room_pos_to_title_map[pos]
if isinstance(room_info, dict):
return room_info.get("title", "未知直播间"), room_info.get(
"title_raw", "未知直播间"
)
else:
# 兼容旧格式
title, title_raw = room_info
return title, title_raw
return "未知直播间", "未知直播间"
# 替换实际函数
+8 -1
View File
@@ -306,7 +306,14 @@ def test_error_scenarios():
analyser.room_pos = 2 # 在目标位置
# 修改映射表,使当前位置的标题与预约记录不一致
analyser._room_pos_to_title_map[2] = ("错误的直播间", "错误的直播间")
analyser._room_pos_to_title_map[2] = {
"title": "错误的直播间",
"title_raw": "错误的直播间",
"has_fudai": False,
"fudai_content": "",
"fudai_detail": None,
"timestamp": time.time(),
}
# 设置OCR识别结果为错误的标题
analyser.operation.mock_ocr_result = "错误的直播间"