refactor(reservation): 抽 _write_state 闭包到顶层 _write_reservations_state
_run_reservation_attend_window 内嵌的 _write_state 闭包是 75 行 JSON 状态文件 写盘逻辑(含 1s 节流),独立性强,纯写文件无副作用。 抽到顶层 _write_reservations_state(self) helper,原闭包改为单行委托。 内部用到的 import json/os/datetime 移到 helper 内部,主函数顶部去掉。 行为完全保留: - 1s 节流(避免高频写盘) - 缺路径时 makedirs - 异常时通过 self._log_kv 记录 + 不抛 文件总行数 1492 → 1495(+3):闭包变成委托后总行数略增 3,但消除了 1 层嵌套,给后续测试 _write_reservations_state 留出空间(可独立测试)。 1361 passed. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
e737002b1e
commit
8ecd2783db
1 file changed
+81
-78
@@ -1171,10 +1171,6 @@ def _run_reservation_attend_window(
|
||||
|
||||
返回 True 表示触发了 return-out 出口(队列满等待 / 重置到第一个直播间)。
|
||||
"""
|
||||
import json
|
||||
import os
|
||||
from datetime import datetime
|
||||
|
||||
from douyin.reservation import douyin_reservation_logic
|
||||
|
||||
def _swipe_down_once() -> None:
|
||||
@@ -1184,80 +1180,7 @@ def _run_reservation_attend_window(
|
||||
douyin_reservation_logic._sort_reservations_inplace(self.reservations) # type: ignore[attr-defined]
|
||||
|
||||
def _write_state() -> None:
|
||||
# 将预约队列写入状态文件,供外部查看
|
||||
try:
|
||||
# 节流:避免高频写盘
|
||||
now_ts = time.time()
|
||||
last_ts = float(
|
||||
getattr(self, "_reservations_state_last_write_ts", 0.0) or 0.0
|
||||
)
|
||||
if (now_ts - last_ts) < 1.0:
|
||||
return
|
||||
self._reservations_state_last_write_ts = now_ts
|
||||
|
||||
os.makedirs(
|
||||
os.path.dirname(self._reservations_state_path),
|
||||
exist_ok=True,
|
||||
)
|
||||
|
||||
items = []
|
||||
for r in self.reservations or []:
|
||||
try:
|
||||
ft = float(r.get("future_ts") or 0)
|
||||
except Exception:
|
||||
ft = 0.0
|
||||
try:
|
||||
ft_str = (
|
||||
datetime.fromtimestamp(ft).strftime("%Y-%m-%d %H:%M:%S")
|
||||
if ft
|
||||
else ""
|
||||
)
|
||||
except Exception:
|
||||
ft_str = str(r.get("future_str", ""))
|
||||
|
||||
items.append(
|
||||
{
|
||||
"prize_value_yuan": float(
|
||||
r.get("prize_value_yuan") or 0
|
||||
),
|
||||
"content": str(r.get("content") or ""),
|
||||
"future_ts": ft,
|
||||
"future_str": ft_str,
|
||||
"pos_seen": int(r.get("pos_seen") or 0),
|
||||
"room_title": str(r.get("room_title", "") or ""),
|
||||
"room_title_raw": str(r.get("room_title_raw", "") or ""),
|
||||
"created_ts": float(r.get("created_ts") or 0),
|
||||
}
|
||||
)
|
||||
|
||||
current_title = str(getattr(self, "current_room_title", "") or "")
|
||||
current_title_raw = str(
|
||||
getattr(self, "current_room_title_raw", "") or ""
|
||||
)
|
||||
|
||||
data = {
|
||||
"updated_ts": time.time(),
|
||||
"reason": "process_reservation",
|
||||
"room_pos": int(getattr(self, "room_pos", 0) or 0),
|
||||
"current_room_title": current_title,
|
||||
"current_room_title_raw": current_title_raw,
|
||||
"reservations": items,
|
||||
}
|
||||
|
||||
with open(
|
||||
self._reservations_state_path, "w", encoding="utf-8"
|
||||
) as f:
|
||||
json.dump(data, f, ensure_ascii=False, indent=2)
|
||||
except Exception as exc:
|
||||
try:
|
||||
self._log_kv(
|
||||
"预约",
|
||||
"写入预约队列状态文件失败",
|
||||
error=str(exc),
|
||||
error_type=type(exc).__name__,
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
_write_reservations_state(self)
|
||||
|
||||
focus_ref = [float(self.reservation_focus_until_ts or 0.0)]
|
||||
after_action_ref = [bool(self.reservation_after_action_pending or False)]
|
||||
@@ -1447,6 +1370,86 @@ def _run_reservation_attend_window(
|
||||
return False
|
||||
|
||||
|
||||
def _write_reservations_state(self: "fudai_analyse") -> None:
|
||||
"""把预约队列写入状态文件(节流 1s/次),供外部 GUI 跨进程查看。
|
||||
|
||||
从 _run_reservation_attend_window 内嵌闭包抽出来,行为不变。
|
||||
依赖 self 上的 _reservations_state_path / _reservations_state_last_write_ts /
|
||||
reservations / current_room_title* / room_pos / _log_kv。
|
||||
"""
|
||||
import json
|
||||
import os
|
||||
from datetime import datetime
|
||||
|
||||
try:
|
||||
# 节流:避免高频写盘
|
||||
now_ts = time.time()
|
||||
last_ts = float(
|
||||
getattr(self, "_reservations_state_last_write_ts", 0.0) or 0.0
|
||||
)
|
||||
if (now_ts - last_ts) < 1.0:
|
||||
return
|
||||
self._reservations_state_last_write_ts = now_ts
|
||||
|
||||
os.makedirs(
|
||||
os.path.dirname(self._reservations_state_path),
|
||||
exist_ok=True,
|
||||
)
|
||||
|
||||
items = []
|
||||
for r in self.reservations or []:
|
||||
try:
|
||||
ft = float(r.get("future_ts") or 0)
|
||||
except Exception:
|
||||
ft = 0.0
|
||||
try:
|
||||
ft_str = (
|
||||
datetime.fromtimestamp(ft).strftime("%Y-%m-%d %H:%M:%S")
|
||||
if ft
|
||||
else ""
|
||||
)
|
||||
except Exception:
|
||||
ft_str = str(r.get("future_str", ""))
|
||||
|
||||
items.append(
|
||||
{
|
||||
"prize_value_yuan": float(r.get("prize_value_yuan") or 0),
|
||||
"content": str(r.get("content") or ""),
|
||||
"future_ts": ft,
|
||||
"future_str": ft_str,
|
||||
"pos_seen": int(r.get("pos_seen") or 0),
|
||||
"room_title": str(r.get("room_title", "") or ""),
|
||||
"room_title_raw": str(r.get("room_title_raw", "") or ""),
|
||||
"created_ts": float(r.get("created_ts") or 0),
|
||||
}
|
||||
)
|
||||
|
||||
current_title = str(getattr(self, "current_room_title", "") or "")
|
||||
current_title_raw = str(getattr(self, "current_room_title_raw", "") or "")
|
||||
|
||||
data = {
|
||||
"updated_ts": time.time(),
|
||||
"reason": "process_reservation",
|
||||
"room_pos": int(getattr(self, "room_pos", 0) or 0),
|
||||
"current_room_title": current_title,
|
||||
"current_room_title_raw": current_title_raw,
|
||||
"reservations": items,
|
||||
}
|
||||
|
||||
with open(self._reservations_state_path, "w", encoding="utf-8") as f:
|
||||
json.dump(data, f, ensure_ascii=False, indent=2)
|
||||
except Exception as exc:
|
||||
try:
|
||||
self._log_kv(
|
||||
"预约",
|
||||
"写入预约队列状态文件失败",
|
||||
error=str(exc),
|
||||
error_type=type(exc).__name__,
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
|
||||
def _log_reservation_attend_check(
|
||||
self: "fudai_analyse", needswitch_cfg: bool
|
||||
) -> None:
|
||||
|
||||
Reference in new issue
Block a user