feat(reservation): Update reservation details and improve logging
- Updated reservation JSON data with new room titles, prize values, and timestamps. - Enhanced logging in `douyin_fudai_core.py` to include OCR duration summaries for better analysis. - Modified `SwipeHandler` to allow forced switching during seek processes to prevent deadlocks. - Improved logging wrapper to handle field extraction and elapsed time calculations more robustly. - Refactored log formatting functions for better readability and consistency. - Added total timeout handling in `process_reservation_attend_window` to prevent blocking. - Removed automatic backtracking in reservation verification to avoid infinite loops. - Updated configuration management to include total timeout settings for reservations. - Enhanced error handling and logging in `fudai_detail_reader.py` for validation failures.
This commit is contained in:
1 parent
1223a1ea5a
commit
2fa2073f2e
11 files changed
+282
-784
No files matched your search
+40
-350
@@ -248,6 +248,7 @@ class fudai_analyse:
|
||||
self._waiting_for_lottery_result: bool = False # 是否正在等待开奖结果
|
||||
self._reservation_back_direct_attend: bool = False # 预约回退后直接参与标志
|
||||
self._force_attend_after_reservation_back: bool = False # 强制参与标志
|
||||
self._reservation_back_attempts: int = 0 # 直接参与尝试计数,防止无限重试
|
||||
self._skip_stage_diagnosis_and_find_fudai: bool = False # 跳过阶段诊断标志
|
||||
self._just_switched_room: bool = False # 是否刚刚切换了直播间
|
||||
self._last_fudai_detail_tuple: Optional[Tuple[Any, ...]] = (
|
||||
@@ -1049,6 +1050,16 @@ class fudai_analyse:
|
||||
def _write_state() -> None:
|
||||
# 将预约队列写入状态文件,供外部查看(格式与 douyin_fudai_core.py 一致)
|
||||
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
|
||||
|
||||
import json
|
||||
|
||||
os.makedirs(
|
||||
@@ -1194,140 +1205,18 @@ class fudai_analyse:
|
||||
try:
|
||||
self._log_kv(
|
||||
"预约",
|
||||
"[预约] 队列满等待状态,已设置直接参与标志,立即执行参与(不再循环检查)",
|
||||
"[预约] 队列满等待状态检测到直接参与标志:为避免卡死已禁用该分支,改走正常流程",
|
||||
reservation_direct_attend=reservation_direct_attend,
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
# 立即执行参与,不再循环检查预约回退
|
||||
# 先进行阶段诊断,然后根据阶段执行参与
|
||||
# 清理标志,避免每轮提前 return 导致卡在状态检查
|
||||
try:
|
||||
self.operation.get_screenshot(self.device_id)
|
||||
self._reservation_back_direct_attend = False
|
||||
self._force_attend_after_reservation_back = False
|
||||
self._reservation_back_attempts = 0
|
||||
except Exception:
|
||||
pass
|
||||
from douyin.stage import (
|
||||
douyin_stage,
|
||||
) # pylint: disable=redefined-outer-name
|
||||
|
||||
result = douyin_stage.diagnose_stage_from_current_screen(self)
|
||||
if result is None:
|
||||
stage, _ = (
|
||||
"IN_LIVE_ROOM",
|
||||
"阶段诊断返回 None,默认判定为在直播间",
|
||||
)
|
||||
else:
|
||||
stage, _ = result
|
||||
|
||||
if stage == "FUDai_DETAIL":
|
||||
# 在福袋详情页,直接参与
|
||||
try:
|
||||
self._log_kv(
|
||||
"预约",
|
||||
"[预约] 检测到福袋详情页,立即执行直接参与",
|
||||
stage=stage,
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
self._reservation_back_direct_attend = False # 清除标志
|
||||
self._force_attend_after_reservation_back = (
|
||||
True # 设置强制参与标志
|
||||
)
|
||||
self._handle_reservation_direct_attend()
|
||||
elif stage == "IN_LIVE_ROOM":
|
||||
# 在直播间,查找福袋并打开详情页
|
||||
try:
|
||||
self._log_kv(
|
||||
"预约",
|
||||
"[预约] 检测到直播间,查找福袋并打开详情页后执行直接参与",
|
||||
stage=stage,
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
# 查找福袋并打开详情页
|
||||
from douyin.core import (
|
||||
douyin_flow_control,
|
||||
) # pylint: disable=redefined-outer-name
|
||||
|
||||
room_pos_ref = [int(getattr(self, "room_pos", 0) or 0)]
|
||||
original_max_wait = getattr(
|
||||
self, "fudai_icon_max_wait_seconds", None
|
||||
)
|
||||
try:
|
||||
wait_seconds_for_find = float(
|
||||
getattr(
|
||||
self,
|
||||
"switch_room_wait_after_stage_diag_seconds",
|
||||
7.0,
|
||||
)
|
||||
or 7.0
|
||||
)
|
||||
self.fudai_icon_max_wait_seconds = wait_seconds_for_find
|
||||
self._log_kv(
|
||||
"预约",
|
||||
"[预约] 开始查找福袋并打开详情页(使用 switch_room_wait_after_stage_diag_seconds)",
|
||||
room_pos=room_pos_ref[0],
|
||||
max_wait_seconds=wait_seconds_for_find,
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
found = douyin_flow_control.find_and_open_fudai_detail(
|
||||
self, room_pos_ref
|
||||
)
|
||||
# 恢复原始配置
|
||||
try:
|
||||
if original_max_wait is not None:
|
||||
self.fudai_icon_max_wait_seconds = original_max_wait
|
||||
elif hasattr(self, "fudai_icon_max_wait_seconds"):
|
||||
delattr(self, "fudai_icon_max_wait_seconds")
|
||||
except Exception:
|
||||
pass
|
||||
if found:
|
||||
# 成功打开详情页,直接参与
|
||||
try:
|
||||
self._log_kv(
|
||||
"预约",
|
||||
"[预约] 成功打开详情页,立即执行直接参与",
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
self._reservation_back_direct_attend = False # 清除标志
|
||||
self._force_attend_after_reservation_back = (
|
||||
True # 设置强制参与标志
|
||||
)
|
||||
self._handle_reservation_direct_attend()
|
||||
else:
|
||||
# 查找福袋失败,保留标志,下一轮继续尝试
|
||||
try:
|
||||
self._reservation_back_direct_attend = (
|
||||
True # 恢复标志
|
||||
)
|
||||
self._log_kv(
|
||||
"预约",
|
||||
"[预约] 预约回退后查找福袋失败,保留直接参与标志,下一轮继续尝试",
|
||||
flag_restored=True,
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
else:
|
||||
# 其他阶段,保留标志,下一轮继续尝试
|
||||
try:
|
||||
self._reservation_back_direct_attend = True # 恢复标志
|
||||
self._log_kv(
|
||||
"预约",
|
||||
"[预约] 检测到其他阶段,保留直接参与标志,下一轮继续尝试",
|
||||
stage=stage,
|
||||
flag_restored=True,
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
elapsed = time.time() - start_time
|
||||
self._log_kv(
|
||||
"流程",
|
||||
"run_once_basic完成(预约回退直接参与)",
|
||||
elapsed_seconds=round(elapsed, 2),
|
||||
)
|
||||
return
|
||||
|
||||
# 检查是否所有预约已完成,需要重置到第一个直播间
|
||||
if self.queue_full_waiting_at_earliest and not self.reservations:
|
||||
@@ -1472,6 +1361,11 @@ class fudai_analyse:
|
||||
# 已触发预约回退,设置标志:下一轮直接参与,跳过详情识别
|
||||
try:
|
||||
self._reservation_back_direct_attend = True
|
||||
# reset attempts counter when first scheduling direct attend
|
||||
try:
|
||||
self._reservation_back_attempts = 0
|
||||
except Exception:
|
||||
pass
|
||||
self._log_kv(
|
||||
"预约",
|
||||
"[预约] 预约回退成功,设置直接参与标志(跳过详情识别)",
|
||||
@@ -1899,82 +1793,24 @@ class fudai_analyse:
|
||||
except Exception:
|
||||
pass
|
||||
if reservation_direct_attend:
|
||||
# 先检查预约是否过期
|
||||
expired = False
|
||||
try:
|
||||
if self.reservations and len(self.reservations) > 0:
|
||||
first_reservation = self.reservations[0]
|
||||
future_ts = float(
|
||||
first_reservation.get("future_ts", 0.0) or 0.0
|
||||
)
|
||||
if future_ts > 0:
|
||||
now_ts = time.time()
|
||||
poll_before = float(
|
||||
getattr(
|
||||
self, "high_value_fudai_poll_before_seconds", 10.0
|
||||
)
|
||||
or 10.0
|
||||
)
|
||||
expired_threshold = future_ts + max(0.0, poll_before)
|
||||
if now_ts > expired_threshold:
|
||||
expired = True
|
||||
from datetime import datetime
|
||||
|
||||
future_str = (
|
||||
datetime.fromtimestamp(future_ts).strftime(
|
||||
"%Y-%m-%d %H:%M:%S"
|
||||
)
|
||||
if future_ts > 0
|
||||
else "无效"
|
||||
)
|
||||
expired_seconds = int(now_ts - expired_threshold)
|
||||
self._log_kv(
|
||||
"预约",
|
||||
"[预约] 预约回退后直接参与:福袋已过期,清除标志",
|
||||
future_ts=int(future_ts),
|
||||
future_str=future_str,
|
||||
expired_seconds=expired_seconds,
|
||||
)
|
||||
# 清除相关标志
|
||||
self._reservation_back_direct_attend = False
|
||||
self._force_attend_after_reservation_back = False
|
||||
self.queue_full_waiting_at_earliest = False
|
||||
# 清理过期预约
|
||||
from douyin.reservation import (
|
||||
douyin_reservation_logic,
|
||||
) # pylint: disable=redefined-outer-name
|
||||
|
||||
douyin_reservation_logic.cleanup_expired_reservations(
|
||||
analyser=self,
|
||||
reservations=self.reservations,
|
||||
write_state_callback=lambda: None,
|
||||
)
|
||||
self._log_kv(
|
||||
"预约",
|
||||
"[预约] 检测到直接参与标志:为避免卡死已禁用该分支,改走正常详情流程",
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
if not expired:
|
||||
# 预约回退后直接参与:使用正常流程,但设置强制参与标志
|
||||
try:
|
||||
self._reservation_back_direct_attend = False # 清除标志
|
||||
# 设置强制参与标志,让 basic_join_decision 跳过倒计时检查
|
||||
self._force_attend_after_reservation_back = True
|
||||
self._log_kv(
|
||||
"预约",
|
||||
"[预约] 预约回退后直接参与:使用正常流程,设置强制参与标志",
|
||||
flag_cleared=True,
|
||||
force_attend=True,
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
# 使用正常流程处理
|
||||
self._handle_reservation_direct_attend()
|
||||
else:
|
||||
# 已过期,执行正常流程
|
||||
self._handle_in_fudai_detail()
|
||||
try:
|
||||
self._reservation_back_direct_attend = False
|
||||
self._force_attend_after_reservation_back = False
|
||||
self._reservation_back_attempts = 0
|
||||
except Exception:
|
||||
pass
|
||||
self._handle_in_fudai_detail()
|
||||
elapsed = time.time() - start_time
|
||||
self._log_kv(
|
||||
"流程",
|
||||
"run_once_basic完成(预约回退直接参与)",
|
||||
"run_once_basic完成(处理福袋详情)",
|
||||
elapsed_seconds=round(elapsed, 2),
|
||||
)
|
||||
else:
|
||||
@@ -2009,165 +1845,19 @@ class fudai_analyse:
|
||||
except Exception:
|
||||
pass
|
||||
if reservation_direct_attend:
|
||||
# 先检查预约是否过期
|
||||
expired = False
|
||||
try:
|
||||
if self.reservations and len(self.reservations) > 0:
|
||||
first_reservation = self.reservations[0]
|
||||
future_ts = float(
|
||||
first_reservation.get("future_ts", 0.0) or 0.0
|
||||
)
|
||||
if future_ts > 0:
|
||||
now_ts = time.time()
|
||||
poll_before = float(
|
||||
getattr(
|
||||
self, "high_value_fudai_poll_before_seconds", 10.0
|
||||
)
|
||||
or 10.0
|
||||
)
|
||||
expired_threshold = future_ts + max(0.0, poll_before)
|
||||
if now_ts > expired_threshold:
|
||||
expired = True
|
||||
from datetime import datetime
|
||||
|
||||
future_str = (
|
||||
datetime.fromtimestamp(future_ts).strftime(
|
||||
"%Y-%m-%d %H:%M:%S"
|
||||
)
|
||||
if future_ts > 0
|
||||
else "无效"
|
||||
)
|
||||
expired_seconds = int(now_ts - expired_threshold)
|
||||
self._log_kv(
|
||||
"预约",
|
||||
"[预约] 预约回退后直接参与:福袋已过期,清除标志",
|
||||
future_ts=int(future_ts),
|
||||
future_str=future_str,
|
||||
expired_seconds=expired_seconds,
|
||||
)
|
||||
# 清除相关标志
|
||||
self._reservation_back_direct_attend = False
|
||||
self._force_attend_after_reservation_back = False
|
||||
self.queue_full_waiting_at_earliest = False
|
||||
# 清理过期预约
|
||||
from douyin.reservation import (
|
||||
douyin_reservation_logic,
|
||||
) # pylint: disable=redefined-outer-name
|
||||
|
||||
douyin_reservation_logic.cleanup_expired_reservations(
|
||||
analyser=self,
|
||||
reservations=self.reservations,
|
||||
write_state_callback=lambda: None,
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
if not expired:
|
||||
# 预约回退后直接参与:查找福袋并打开详情页,然后直接参与
|
||||
try:
|
||||
self._reservation_back_direct_attend = False # 清除标志
|
||||
# 设置强制参与标志,让 basic_join_decision 跳过倒计时检查
|
||||
self._force_attend_after_reservation_back = True
|
||||
self._log_kv(
|
||||
"预约",
|
||||
"[预约] 预约回退后直接参与:查找福袋并打开详情页,设置强制参与标志",
|
||||
flag_cleared=True,
|
||||
force_attend=True,
|
||||
stage=stage,
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
# 回退后等待页面加载和查找福袋都使用 switch_room_wait_after_stage_diag_seconds 参数
|
||||
try:
|
||||
wait_seconds = float(
|
||||
getattr(self, "switch_room_wait_after_stage_diag_seconds", 7.0)
|
||||
or 7.0
|
||||
)
|
||||
self._log_kv(
|
||||
"预约",
|
||||
"[预约] 预约回退后等待页面加载(使用 switch_room_wait_after_stage_diag_seconds)",
|
||||
wait_seconds=wait_seconds,
|
||||
)
|
||||
self.operation.delay(wait_seconds)
|
||||
except Exception:
|
||||
try:
|
||||
time.sleep(7.0)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# 查找福袋并打开详情页(使用 switch_room_wait_after_stage_diag_seconds 作为最大等待时间)
|
||||
from douyin.core import (
|
||||
douyin_flow_control,
|
||||
) # pylint: disable=redefined-outer-name
|
||||
|
||||
room_pos_ref = [int(getattr(self, "room_pos", 0) or 0)]
|
||||
# 临时设置 fudai_icon_max_wait_seconds 为 switch_room_wait_after_stage_diag_seconds
|
||||
original_max_wait = None
|
||||
try:
|
||||
original_max_wait = getattr(
|
||||
self, "fudai_icon_max_wait_seconds", None
|
||||
)
|
||||
wait_seconds_for_find = float(
|
||||
getattr(self, "switch_room_wait_after_stage_diag_seconds", 7.0)
|
||||
or 7.0
|
||||
)
|
||||
self.fudai_icon_max_wait_seconds = wait_seconds_for_find
|
||||
self._log_kv(
|
||||
"预约",
|
||||
"[预约] 开始查找福袋并打开详情页(使用 switch_room_wait_after_stage_diag_seconds)",
|
||||
room_pos=room_pos_ref[0],
|
||||
max_wait_seconds=wait_seconds_for_find,
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
found = douyin_flow_control.find_and_open_fudai_detail(
|
||||
self, room_pos_ref
|
||||
)
|
||||
# 恢复原始配置
|
||||
try:
|
||||
if original_max_wait is not None:
|
||||
self.fudai_icon_max_wait_seconds = original_max_wait
|
||||
elif hasattr(self, "fudai_icon_max_wait_seconds"):
|
||||
delattr(self, "fudai_icon_max_wait_seconds")
|
||||
except Exception:
|
||||
pass
|
||||
try:
|
||||
self._log_kv(
|
||||
"预约",
|
||||
"[预约] 查找福袋结果",
|
||||
found=found,
|
||||
room_pos=room_pos_ref[0],
|
||||
"[预约] 检测到直接参与标志:为避免卡死已禁用该分支,改走正常直播间流程",
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
if found:
|
||||
# 成功打开详情页,直接参与
|
||||
try:
|
||||
self._log_kv(
|
||||
"预约",
|
||||
"[预约] 成功打开详情页,开始直接参与",
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
self._handle_reservation_direct_attend()
|
||||
else:
|
||||
# 查找福袋失败,保留标志,下一轮继续尝试
|
||||
try:
|
||||
self._reservation_back_direct_attend = True # 恢复标志
|
||||
self._log_kv(
|
||||
"预约",
|
||||
"[预约] 预约回退后查找福袋失败,保留直接参与标志,下一轮继续尝试",
|
||||
flag_restored=True,
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
elapsed = time.time() - start_time
|
||||
self._log_kv(
|
||||
"流程",
|
||||
"run_once_basic完成(预约回退直接参与)",
|
||||
elapsed_seconds=round(elapsed, 2),
|
||||
)
|
||||
return
|
||||
try:
|
||||
self._reservation_back_direct_attend = False
|
||||
self._force_attend_after_reservation_back = False
|
||||
self._reservation_back_attempts = 0
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# 在直播间:如果刚刚切换了直播间,等待福袋加载
|
||||
just_switched = getattr(self, "_just_switched_room", False)
|
||||
|
||||
@@ -1,35 +1,49 @@
|
||||
{
|
||||
"updated_ts": 1767415357.50075,
|
||||
"updated_ts": 1767418177.7360778,
|
||||
"reason": "record_reservation",
|
||||
"room_pos": 1,
|
||||
"current_room_title": "扬叔来了",
|
||||
"current_room_title_raw": "扬叔来了",
|
||||
"room_pos": 5,
|
||||
"current_room_title": "希娜周",
|
||||
"current_room_title_raw": "希娜周",
|
||||
"reservations": [
|
||||
{
|
||||
"prize_value_yuan": 5999.0,
|
||||
"prize_value_yuan": 899.0,
|
||||
"content": "",
|
||||
"future_ts": 1767415961.174306,
|
||||
"future_str": "2026-01-03 12:52:41",
|
||||
"pos_seen": 0,
|
||||
"room_title": "一枝梅3号",
|
||||
"room_title_raw": "一枝梅3号",
|
||||
"created_ts": 1767415328.6684387,
|
||||
"future_ts": 1767418453.861887,
|
||||
"future_str": "2026-01-03 13:34:13",
|
||||
"pos_seen": 2,
|
||||
"room_title": "作业帮数",
|
||||
"room_title_raw": "作业帮数",
|
||||
"created_ts": 1767418099.2724433,
|
||||
"task_count": 2,
|
||||
"time_text": "10:38",
|
||||
"time_text": "06:00",
|
||||
"participants": 0,
|
||||
"copies": 0
|
||||
},
|
||||
{
|
||||
"prize_value_yuan": 3399.0,
|
||||
"prize_value_yuan": 2999.0,
|
||||
"content": "",
|
||||
"future_ts": 1767416222.371734,
|
||||
"future_str": "2026-01-03 12:57:02",
|
||||
"pos_seen": 1,
|
||||
"room_title": "扬叔来了",
|
||||
"room_title_raw": "扬叔来了",
|
||||
"created_ts": 1767415357.4982371,
|
||||
"future_ts": 1767418858.452201,
|
||||
"future_str": "2026-01-03 13:40:58",
|
||||
"pos_seen": 3,
|
||||
"room_title": "壬lSsheila",
|
||||
"room_title_raw": "壬lSsheila",
|
||||
"created_ts": 1767418126.485631,
|
||||
"task_count": 2,
|
||||
"time_text": "14:30",
|
||||
"time_text": "12:17",
|
||||
"participants": 0,
|
||||
"copies": 0
|
||||
},
|
||||
{
|
||||
"prize_value_yuan": 599.0,
|
||||
"content": "",
|
||||
"future_ts": 1767418893.057496,
|
||||
"future_str": "2026-01-03 13:41:33",
|
||||
"pos_seen": 5,
|
||||
"room_title": "希娜周",
|
||||
"room_title_raw": "希娜周",
|
||||
"created_ts": 1767418177.7300756,
|
||||
"task_count": 2,
|
||||
"time_text": "12:00",
|
||||
"participants": 0,
|
||||
"copies": 0
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user