把 douyin/reservation/douyin_reservation_logic.py 按职责拆成 douyin/reservation/reservation_logic/ 子包: - _common (57) 共享错误处理工具占位 + ReservationItem 类型 - helpers (130) 内部工具:标题归一化 / 相似度 / 排序 / 时间戳 - add (447) add_reservation_core_logic 主入口(添加新预约) - cleanup_validate (136) validate_back_to_reservation / cleanup_expired_reservations - queue_full (305) handle_queue_full_wait_at_earliest 队列满时等待策略 - attend_window (1437) process_reservation_attend_window 进入预约窗口(最大头) - record (328) record_high_value_reservation 高价值预约登记入口 原文件改为 53 行 re-export shim,17 个公共符号全部保留。 - process_reservation_attend_window 7 处外部引用 - handle_queue_full_wait_at_earliest 5 处外部引用 - _now_ts 5 处 / cleanup_expired_reservations 4 处 / _sort_reservations_inplace 4 处 - 其他 5 处以下 全部不需修改。 回归: - 8 个新文件 + shim compileall 通过 - pytest 353 passed / 1 skipped - 旧 import 路径与新子包路径返回同一函数对象 - _now_ts / _normalize_room_title / _calculate_string_similarity 等纯函数 调用结果与拆分前一致 Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
306 lines
9.7 KiB
Python
306 lines
9.7 KiB
Python
"""reservation_logic.queue_full - 队列满时的等待策略。
|
|
|
|
handle_queue_full_wait_at_earliest:当预约队列达到上限时,
|
|
回退到最早开奖的预约直播间等待,确保不丢失最高优先级的预约。
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import time
|
|
from typing import Any, Callable, Dict, List, Optional
|
|
|
|
from ._common import (
|
|
ReservationItem,
|
|
format_reservation_context,
|
|
log_reservation_error,
|
|
safe_write_state,
|
|
)
|
|
from .helpers import (
|
|
_calculate_string_similarity,
|
|
_normalize_room_title_for_matching,
|
|
_now_ts,
|
|
_sort_reservations_inplace,
|
|
)
|
|
|
|
|
|
def handle_queue_full_wait_at_earliest(
|
|
analyser: Any,
|
|
reservations: List[ReservationItem],
|
|
room_pos: int,
|
|
reservation_focus_until_ts_ref: List[float],
|
|
reservation_back_swipe_delay_s: float,
|
|
swipe_down_back_callback: Callable[[], None],
|
|
seek_reservation_room_callback: Callable[[ReservationItem], bool],
|
|
sort_reservations_callback: Callable[[], None],
|
|
write_state_callback: Callable[[], None],
|
|
) -> bool:
|
|
"""队列满时,回退到最早开奖的预约直播间等待开奖。
|
|
|
|
返回:
|
|
bool: 如果已回退到最早开奖的预约直播间,返回 True;否则返回 False。
|
|
"""
|
|
start_time = time.time()
|
|
|
|
try:
|
|
analyser._log_kv(
|
|
"预约",
|
|
"[预约] handle_queue_full_wait_at_earliest 函数入口",
|
|
queue_len=len(reservations) if reservations else 0,
|
|
room_pos=room_pos,
|
|
)
|
|
except Exception:
|
|
pass
|
|
|
|
if not reservations:
|
|
try:
|
|
analyser._log_kv(
|
|
"预约",
|
|
"[预约] handle_queue_full_wait_at_earliest:预约队列为空,跳过",
|
|
)
|
|
except Exception:
|
|
pass
|
|
return False
|
|
|
|
# 排序确保取到最早开奖的
|
|
sort_reservations_callback()
|
|
|
|
# 取最早开奖的预约
|
|
earliest = reservations[0]
|
|
try:
|
|
ft = float(earliest.get("future_ts") or 0.0)
|
|
except Exception:
|
|
ft = 0.0
|
|
|
|
if ft <= 0:
|
|
try:
|
|
analyser._log_kv(
|
|
"预约",
|
|
"[预约] handle_queue_full_wait_at_earliest:最早预约 future_ts 无效,跳过",
|
|
future_ts=ft,
|
|
)
|
|
except Exception:
|
|
pass
|
|
return False
|
|
|
|
now_ts = _now_ts()
|
|
focus_until = float(
|
|
reservation_focus_until_ts_ref[0] if reservation_focus_until_ts_ref else 0.0
|
|
)
|
|
|
|
# 用于锁定期判断/匹配的轮询提前量(允许为 0,禁止为负数)
|
|
poll_before: float = 0.0
|
|
|
|
# 如果已经在锁定期内,不需要重复回退
|
|
if focus_until > 0 and now_ts < focus_until:
|
|
try:
|
|
from datetime import datetime
|
|
|
|
focus_until_str = datetime.fromtimestamp(focus_until).strftime(
|
|
"%Y-%m-%d %H:%M:%S"
|
|
)
|
|
analyser._log_kv(
|
|
"预约",
|
|
"[预约] handle_queue_full_wait_at_earliest:已在锁定期内,跳过回退",
|
|
focus_until=int(focus_until),
|
|
focus_until_str=focus_until_str,
|
|
lock_period_remain_seconds=int(focus_until - now_ts),
|
|
)
|
|
except Exception:
|
|
pass
|
|
return False
|
|
|
|
try:
|
|
analyser._log_kv(
|
|
"预约",
|
|
"[预约] 队列已满,回退到最早开奖的预约直播间等待",
|
|
earliest_future_ts=int(ft),
|
|
queue_len=len(reservations),
|
|
room_pos=int(room_pos),
|
|
)
|
|
except Exception:
|
|
pass
|
|
|
|
# 先进行阶段诊断,处理弹窗等,避免卡死在某个页面
|
|
try:
|
|
# 确保有最新截图
|
|
try:
|
|
analyser.operation.get_screenshot(analyser.device_id, use_service=False)
|
|
except Exception:
|
|
pass
|
|
|
|
from douyin.stage import douyin_stage
|
|
|
|
result = douyin_stage.diagnose_stage_from_current_screen(analyser)
|
|
if result is None:
|
|
stage, reason = ("IN_LIVE_ROOM", "阶段诊断返回 None,默认判定为在直播间")
|
|
else:
|
|
stage, reason = result
|
|
|
|
# 如果检测到需要处理的弹窗或异常状态,先处理
|
|
if stage in (
|
|
"LUCKY_DRAW_RESULT",
|
|
"COUPON_POPUP",
|
|
"SHOPPING_REDPACKET_POPUP",
|
|
"FANS_CLUB_JOIN",
|
|
):
|
|
try:
|
|
analyser._log_kv(
|
|
"预约",
|
|
"[预约] 回退前检测到弹窗,先处理",
|
|
stage=stage,
|
|
reason=reason,
|
|
)
|
|
except Exception:
|
|
pass
|
|
|
|
# 处理弹窗:点击返回或关闭按钮
|
|
try:
|
|
analyser.operation.click_back(analyser.device_id)
|
|
time.sleep(0.5)
|
|
# 重新截图并再次诊断,确保已处理
|
|
try:
|
|
analyser.operation.get_screenshot(
|
|
analyser.device_id, use_service=False
|
|
)
|
|
except Exception:
|
|
pass
|
|
result = douyin_stage.diagnose_stage_from_current_screen(analyser)
|
|
if result is None:
|
|
stage, reason = (
|
|
"IN_LIVE_ROOM",
|
|
"阶段诊断返回 None,默认判定为在直播间",
|
|
)
|
|
else:
|
|
stage, reason = result
|
|
except Exception:
|
|
pass
|
|
elif stage == "FUDai_DETAIL":
|
|
# 如果在福袋详情页,先关闭详情页
|
|
try:
|
|
analyser._log_kv(
|
|
"预约",
|
|
"[预约] 回退前检测到在福袋详情页,先关闭",
|
|
stage=stage,
|
|
)
|
|
except Exception:
|
|
pass
|
|
try:
|
|
analyser.operation.click_back(analyser.device_id)
|
|
time.sleep(0.5)
|
|
except Exception:
|
|
pass
|
|
except Exception as e:
|
|
try:
|
|
analyser._log_kv(
|
|
"预约",
|
|
"[预约] 回退前阶段诊断失败,继续执行回退",
|
|
error=str(e),
|
|
error_type=type(e).__name__,
|
|
)
|
|
except Exception:
|
|
pass
|
|
|
|
# 获取当前实际的 room_pos(可能已经被更新)
|
|
try:
|
|
current_room_pos = int(getattr(analyser, "room_pos", room_pos) or room_pos)
|
|
except Exception:
|
|
current_room_pos = int(room_pos)
|
|
|
|
# 计算位置差
|
|
try:
|
|
target_pos = int(earliest.get("pos_seen") or 0)
|
|
except Exception:
|
|
target_pos = 0
|
|
_pos_diff = current_room_pos - target_pos # 位置差(用于调试,当前未使用)
|
|
|
|
# 直接调用回退回调,不需要预先下滑
|
|
# 原因:
|
|
# 1. 回退前已经进行了阶段诊断,处理了弹窗和福袋详情页等异常状态
|
|
# 2. 如果已经在直播间,不需要下滑
|
|
# 3. 预先下滑会导致 room_pos 减1,影响后续的回退计算,导致少回退一个直播间
|
|
|
|
# 回退到最早开奖的预约直播间
|
|
ok = False
|
|
try:
|
|
ok = bool(seek_reservation_room_callback(earliest))
|
|
except Exception:
|
|
ok = False
|
|
|
|
if ok:
|
|
# 回退后等待页面加载
|
|
try:
|
|
analyser.operation.delay(1.5) # 等待页面加载
|
|
except Exception:
|
|
time.sleep(1.5)
|
|
|
|
# 队列满后回退到最早直播间,继续执行后续流程
|
|
try:
|
|
analyser._log_kv(
|
|
"预约",
|
|
"[预约] 队列满回退后,继续执行后续流程",
|
|
earliest_future_ts=int(ft),
|
|
now_ts=int(now_ts),
|
|
remain_seconds_to_open=int(ft - now_ts) if ft > now_ts else 0,
|
|
)
|
|
except Exception:
|
|
pass
|
|
|
|
# 队列满后立即参与,不设置锁定期(设置为0),使用标志控制
|
|
# 这样不会影响切换直播间的逻辑,同时后续的 process_reservation_attend_window 不会因为锁定期而跳过
|
|
focus_until_new = 0.0 # 设置为0,表示不设置锁定期,使用标志控制
|
|
if reservation_focus_until_ts_ref:
|
|
reservation_focus_until_ts_ref[0] = focus_until_new
|
|
else:
|
|
reservation_focus_until_ts_ref.append(focus_until_new)
|
|
|
|
try:
|
|
from datetime import datetime
|
|
|
|
ft_str = (
|
|
datetime.fromtimestamp(ft).strftime("%Y-%m-%d %H:%M:%S")
|
|
if ft > 0
|
|
else "无效"
|
|
)
|
|
analyser._log_kv(
|
|
"预约",
|
|
"[预约] 已回退到最早开奖的预约直播间,立即参与(不等待开奖窗口)",
|
|
earliest_future_ts=int(ft),
|
|
earliest_future_str=ft_str,
|
|
now_ts=int(now_ts),
|
|
focus_until=int(focus_until_new),
|
|
remain_seconds_to_open=int(ft - now_ts) if ft > now_ts else 0,
|
|
immediate_attend=True,
|
|
)
|
|
except Exception:
|
|
pass
|
|
|
|
try:
|
|
write_state_callback()
|
|
except Exception:
|
|
pass
|
|
|
|
try:
|
|
elapsed = time.time() - start_time
|
|
analyser._log_kv(
|
|
"预约",
|
|
"[预约] handle_queue_full_wait_at_earliest 完成(已回退)",
|
|
elapsed_seconds=round(elapsed, 3),
|
|
)
|
|
except Exception:
|
|
pass
|
|
|
|
return True
|
|
|
|
try:
|
|
elapsed = time.time() - start_time
|
|
analyser._log_kv(
|
|
"预约",
|
|
"[预约] handle_queue_full_wait_at_earliest 完成(回退失败)",
|
|
elapsed_seconds=round(elapsed, 3),
|
|
)
|
|
except Exception:
|
|
pass
|
|
|
|
return False
|
|
|
|
|