From dadb0b6852dea90479f2238bab4e5a6626bb8266 Mon Sep 17 00:00:00 2001 From: Codex Date: Fri, 22 May 2026 12:55:17 +0800 Subject: [PATCH] =?UTF-8?q?refactor(reservation):=20=E6=8A=BD=20process=5F?= =?UTF-8?q?reservation=5Fattend=5Fwindow=20=E4=B8=AD"=E5=9B=9E=E9=80=80?= =?UTF-8?q?=E5=90=8E=E7=AD=89=E5=BE=85=E7=9B=B4=E6=92=AD=E9=97=B4=E5=8A=A0?= =?UTF-8?q?=E8=BD=BD"=E5=9D=97=E5=88=B0=20wait=5Fafter=5Fseek=5Fand=5Fveri?= =?UTF-8?q?fy?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 将 43 行的等待逻辑抽到 helper: - 读 reservation_back_verify_wait_seconds(默认 5.0,限制 0.1-10.0) - 写"开始等待"日志 - 调用 operation.delay 或 time.sleep - 写"等待完成"日志(含实际等待秒数) attend_window.py: 1030 → 991 行 (-39) attend_window_helpers.py: 645 → 699 行 attend_window.py 跌破 1000 行(1437 → 991,本会话累计削减 446 行 = -31%) 验证: - pytest 1013 passed - compile OK Co-Authored-By: Claude Opus 4.7 (1M context) --- .../reservation_logic/attend_window.py | 45 ++-------------- .../attend_window_helpers.py | 54 +++++++++++++++++++ 2 files changed, 57 insertions(+), 42 deletions(-) diff --git a/douyin/reservation/reservation_logic/attend_window.py b/douyin/reservation/reservation_logic/attend_window.py index 5ee2f3a..8d5291e 100644 --- a/douyin/reservation/reservation_logic/attend_window.py +++ b/douyin/reservation/reservation_logic/attend_window.py @@ -418,49 +418,10 @@ def process_reservation_attend_window( pass if verify_enabled: - # 回退操作后等待直播间加载完成,然后再进行校验 - # 使用配置项控制等待时间,默认3秒(确保直播间标题等信息加载完成) - try: - verify_wait_seconds = float( - getattr(analyser, "reservation_back_verify_wait_seconds", 5.0) - or 5.0 - ) - if verify_wait_seconds < 0.1: - verify_wait_seconds = 0.1 # 最小0.1秒 - if verify_wait_seconds > 10.0: - verify_wait_seconds = 10.0 # 最大10秒,避免等待过久 - except Exception: - verify_wait_seconds = 3.0 + # 回退操作后等待直播间加载完成 + from .attend_window_helpers import wait_after_seek_and_verify - try: - analyser._log_kv( - "预约", - "[预约] process_reservation_attend_window:回退后等待直播间加载完成(开始等待)", - verify_wait_seconds=verify_wait_seconds, - level="INFO", - ) - except Exception: - pass - - # 记录等待开始时间,并在等待结束后写入实际等待时长,便于验证配置是否生效 - wait_start_ts = time.time() - try: - try: - analyser.operation.delay(verify_wait_seconds) - except Exception: - time.sleep(verify_wait_seconds) - finally: - waited = time.time() - wait_start_ts - try: - analyser._log_kv( - "预约", - "[预约] process_reservation_attend_window:回退等待完成", - expected_wait_seconds=verify_wait_seconds, - waited_seconds=round(waited, 3), - level="INFO", - ) - except Exception: - pass + wait_after_seek_and_verify(analyser) try: try: diff --git a/douyin/reservation/reservation_logic/attend_window_helpers.py b/douyin/reservation/reservation_logic/attend_window_helpers.py index 37946a5..d36fd63 100644 --- a/douyin/reservation/reservation_logic/attend_window_helpers.py +++ b/douyin/reservation/reservation_logic/attend_window_helpers.py @@ -105,6 +105,60 @@ def log_attend_window_detail_check( pass +def wait_after_seek_and_verify(analyser: Any) -> float: + """回退操作成功后:等待直播间加载完成(写日志 + delay),返回实际等待秒数。 + + 流程: + - 读 reservation_back_verify_wait_seconds(默认 5.0,限制 0.1-10.0) + - 写"开始等待"日志 + - 调用 operation.delay 或 time.sleep + - 写"等待完成"日志 + """ + import time as _time + + try: + verify_wait_seconds = float( + getattr(analyser, "reservation_back_verify_wait_seconds", 5.0) or 5.0 + ) + if verify_wait_seconds < 0.1: + verify_wait_seconds = 0.1 + if verify_wait_seconds > 10.0: + verify_wait_seconds = 10.0 + except Exception: + verify_wait_seconds = 3.0 + + try: + analyser._log_kv( + "预约", + "[预约] process_reservation_attend_window:回退后等待直播间加载完成(开始等待)", + verify_wait_seconds=verify_wait_seconds, + level="INFO", + ) + except Exception: + pass + + wait_start_ts = _time.time() + try: + try: + analyser.operation.delay(verify_wait_seconds) + except Exception: + _time.sleep(verify_wait_seconds) + finally: + waited = _time.time() - wait_start_ts + try: + analyser._log_kv( + "预约", + "[预约] process_reservation_attend_window:回退等待完成", + expected_wait_seconds=verify_wait_seconds, + waited_seconds=round(waited, 3), + level="INFO", + ) + except Exception: + pass + + return verify_wait_seconds + + def log_seek_failure_and_finish( analyser: Any, target: ReservationItem,