覆盖 14 个文件中的 Image.open() 未正确关闭问题: - fansclub_handler: 5 处(pic/pic_rgb/img 未 close) - image_ops: 2 处(crop 后 pic 未 close) - douyin_popups: 2 处(pic.convert 重赋值泄漏原始句柄) - douyin_fudai: 1 处(open().convert() 链式调用泄漏) - douyin_robot: 2 处(同上) - save_files/direct_attend/finder/stage/find_open/detail/_common/ocr_preprocess: 各 1-2 处 统一修复模式:分离 open 与 convert,try/finally 确保原始句柄关闭。 Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
448 lines
17 KiB
Python
448 lines
17 KiB
Python
"""douyin_reservation_handler_pkg.direct_attend - 预约直接参与流程。
|
||
|
||
handle_reservation_direct_attend:进入预约窗口后,从状态文件加载预约记录,
|
||
回退到目标直播间,识别详情并参与抽奖。
|
||
"""
|
||
from __future__ import annotations
|
||
|
||
import csv
|
||
import difflib
|
||
import json
|
||
import os
|
||
import time
|
||
from typing import Any, Dict, Optional
|
||
|
||
from douyin.core import path_config
|
||
from douyin.fudai import douyin_fudai_detail
|
||
from douyin.handlers import douyin_swipe_handler
|
||
from douyin.reservation import douyin_reservation_logic
|
||
|
||
# 错误处理工具占位(与原文件保持兼容降级)
|
||
try:
|
||
from douyin.reservation.core.reservation_error_utils import (
|
||
log_reservation_error,
|
||
format_reservation_context,
|
||
)
|
||
|
||
_RESERVATION_ERROR_UTILS_AVAILABLE = True
|
||
except ImportError:
|
||
def log_reservation_error(*args, **kwargs):
|
||
return None
|
||
|
||
def format_reservation_context(*args, **kwargs):
|
||
return {}
|
||
|
||
_RESERVATION_ERROR_UTILS_AVAILABLE = False
|
||
|
||
|
||
def handle_reservation_direct_attend(analyser: Any) -> None:
|
||
"""预约回退后直接参与:直接查找福袋并点击,使用按钮检测进行决策,跳过OCR识别。
|
||
|
||
新流程:
|
||
1. 直接查找福袋图标并点击
|
||
2. 点击后调用阶段检测中的按钮检测进行决策
|
||
3. 完成决策后,再点击福袋图标验证参与状态
|
||
4. 参与成功后进入开奖倒计时
|
||
|
||
参数:
|
||
analyser: 分析器对象
|
||
"""
|
||
start_time = time.time()
|
||
try:
|
||
analyser._log_kv( # type: ignore[attr-defined]
|
||
"预约",
|
||
"[预约] 预约回退后直接参与:直接查找福袋,使用按钮检测进行决策,跳过OCR识别",
|
||
)
|
||
except Exception:
|
||
pass
|
||
try:
|
||
analyser._log_kv( # type: ignore[attr-defined]
|
||
"诊断",
|
||
"handle_reservation_direct_attend:当前关键 flags",
|
||
reservation_after_action_pending=bool(
|
||
getattr(analyser, "reservation_after_action_pending", False)
|
||
),
|
||
reservation_focus_until_ts=float(
|
||
getattr(analyser, "reservation_focus_until_ts", 0.0) or 0.0
|
||
),
|
||
waiting_for_lottery=bool(
|
||
getattr(analyser, "_waiting_for_lottery_result", False)
|
||
),
|
||
room_pos=int(getattr(analyser, "room_pos", 0) or 0),
|
||
)
|
||
except Exception:
|
||
pass
|
||
|
||
# 从预约记录中获取详情信息
|
||
if not analyser.reservations or len(analyser.reservations) == 0:
|
||
try:
|
||
analyser._log_kv( # type: ignore[attr-defined]
|
||
"预约",
|
||
"[预约] 预约回退后直接参与:预约队列为空,无法获取详情",
|
||
)
|
||
except Exception:
|
||
pass
|
||
# 如果预约队列为空,回退到正常流程
|
||
analyser._handle_in_fudai_detail() # type: ignore[attr-defined]
|
||
return
|
||
|
||
first_reservation = analyser.reservations[0]
|
||
try:
|
||
# 从预约记录中获取信息
|
||
future_ts = float(first_reservation.get("future_ts", 0.0) or 0.0)
|
||
prize_value_yuan = float(
|
||
first_reservation.get("prize_value_yuan", 0.0) or 0.0
|
||
)
|
||
|
||
# 检查福袋是否已过期
|
||
now_ts = time.time()
|
||
poll_before = float(
|
||
getattr(analyser, "high_value_fudai_poll_before_seconds", 10.0) or 10.0
|
||
)
|
||
expired_threshold = future_ts + max(0.0, poll_before) # 开奖时间 + 缓冲时间
|
||
|
||
if future_ts > 0 and now_ts > expired_threshold:
|
||
# 福袋已过期,清除标志并退出
|
||
try:
|
||
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)
|
||
analyser._log_kv( # type: ignore[attr-defined]
|
||
"预约",
|
||
"[预约] 预约回退后直接参与:福袋已过期,清除标志并退出",
|
||
future_ts=int(future_ts),
|
||
future_str=future_str,
|
||
expired_threshold=int(expired_threshold),
|
||
expired_seconds=expired_seconds,
|
||
now_ts=int(now_ts),
|
||
)
|
||
except Exception:
|
||
pass
|
||
|
||
# 此属性已废弃,不再使用(保留原意为兼容,故不做任何操作)
|
||
analyser.queue_full_waiting_at_earliest = False
|
||
|
||
# 从预约队列中移除过期预约
|
||
try:
|
||
douyin_reservation_logic.cleanup_expired_reservations(
|
||
analyser=analyser,
|
||
reservations=analyser.reservations,
|
||
write_state_callback=lambda: None, # 暂时不写入,避免重复写入
|
||
)
|
||
except Exception:
|
||
pass
|
||
|
||
return
|
||
|
||
# 设置当前福袋的 future_ts(用于参与成功后从预约队列中移除)
|
||
analyser._current_fudai_future_ts = future_ts
|
||
|
||
# 步骤1:直接查找福袋图标并点击
|
||
try:
|
||
analyser._log_kv( # type: ignore[attr-defined]
|
||
"预约",
|
||
"[预约] 步骤1:直接查找福袋图标并点击",
|
||
)
|
||
except Exception:
|
||
pass
|
||
|
||
import douyin.core.douyin_flow_control as douyin_flow_control
|
||
|
||
room_pos_ref = [int(getattr(analyser, "room_pos", 0) or 0)]
|
||
opened = douyin_flow_control.find_and_open_fudai_detail(
|
||
analyser, room_pos_ref
|
||
)
|
||
|
||
if not opened:
|
||
try:
|
||
analyser._log_kv( # type: ignore[attr-defined]
|
||
"预约",
|
||
"[预约] 未找到福袋图标,回退到正常流程",
|
||
)
|
||
except Exception:
|
||
pass
|
||
analyser._handle_in_fudai_detail() # type: ignore[attr-defined]
|
||
return
|
||
|
||
# 等待页面加载
|
||
try:
|
||
analyser.operation.delay(0.5) # 等待详情页加载(减少延迟)
|
||
except Exception:
|
||
time.sleep(0.5)
|
||
|
||
# 步骤2:调用阶段检测中的按钮检测进行决策
|
||
try:
|
||
analyser._log_kv( # type: ignore[attr-defined]
|
||
"预约",
|
||
"[预约] 步骤2:调用按钮检测进行决策",
|
||
)
|
||
except Exception:
|
||
pass
|
||
|
||
# 获取截图
|
||
try:
|
||
analyser.operation.get_screenshot(analyser.device_id, use_service=False)
|
||
except Exception:
|
||
pass
|
||
|
||
# 检测参与按钮
|
||
from PIL import Image
|
||
|
||
try:
|
||
screenshot_path = path_config.get_screenshot_path()
|
||
pic_rgb = Image.open(screenshot_path) # type: ignore[attr-defined]
|
||
except Exception:
|
||
try:
|
||
analyser._log_kv( # type: ignore[attr-defined]
|
||
"预约",
|
||
"[预约] 无法读取截图,回退到正常流程",
|
||
)
|
||
except Exception:
|
||
pass
|
||
analyser._handle_in_fudai_detail() # type: ignore[attr-defined]
|
||
return
|
||
|
||
from douyin.detection import douyin_pixel
|
||
|
||
try:
|
||
button_result = douyin_pixel.detect_attend_button_red_area(
|
||
pic_rgb, return_info=True, analyser=analyser
|
||
)
|
||
finally:
|
||
pic_rgb.close()
|
||
if isinstance(button_result, tuple):
|
||
has_button, button_info = button_result
|
||
else:
|
||
has_button = button_result
|
||
button_info = {}
|
||
|
||
if not has_button:
|
||
try:
|
||
analyser._log_kv( # type: ignore[attr-defined]
|
||
"预约",
|
||
"[预约] 未检测到参与按钮,回退到正常流程",
|
||
button_info=button_info,
|
||
)
|
||
except Exception:
|
||
pass
|
||
analyser._handle_in_fudai_detail() # type: ignore[attr-defined]
|
||
return
|
||
|
||
# 步骤3:点击参与按钮
|
||
try:
|
||
analyser._log_kv( # type: ignore[attr-defined]
|
||
"预约",
|
||
"[预约] 步骤3:点击参与按钮",
|
||
button_info=button_info,
|
||
)
|
||
except Exception:
|
||
pass
|
||
|
||
# 点击参与按钮(基于截图优化坐标:OCR区域(306,2030)-(780,2110),区域中心(543, 2070))
|
||
try:
|
||
rx = int(getattr(analyser, "resolution_ratio_x", 1080) or 1080)
|
||
ry = int(getattr(analyser, "resolution_ratio_y", 2400) or 2400)
|
||
except Exception:
|
||
rx, ry = 1080, 2400
|
||
|
||
attend_x = int(543 * rx / 1080)
|
||
attend_y = int(2070 * ry / 2400)
|
||
analyser.operation.click(analyser.device_id, attend_x, attend_y)
|
||
|
||
try:
|
||
analyser._log_kv( # type: ignore[attr-defined]
|
||
"预约",
|
||
"[预约] 已点击参与按钮",
|
||
click_x=attend_x,
|
||
click_y=attend_y,
|
||
)
|
||
except Exception:
|
||
pass
|
||
|
||
# 等待点击后的响应
|
||
try:
|
||
analyser.operation.delay(0.8) # 等待参与响应(减少延迟)
|
||
except Exception:
|
||
time.sleep(0.8)
|
||
|
||
# 步骤4:点击福袋图标验证参与状态
|
||
try:
|
||
analyser._log_kv( # type: ignore[attr-defined]
|
||
"预约",
|
||
"[预约] 步骤4:点击福袋图标验证参与状态",
|
||
)
|
||
except Exception:
|
||
pass
|
||
|
||
# 再次查找并点击福袋图标
|
||
room_pos_ref2 = [int(getattr(analyser, "room_pos", 0) or 0)]
|
||
opened2 = douyin_flow_control.find_and_open_fudai_detail(
|
||
analyser, room_pos_ref2
|
||
)
|
||
|
||
if not opened2:
|
||
try:
|
||
analyser._log_kv( # type: ignore[attr-defined]
|
||
"预约",
|
||
"[预约] 验证时未找到福袋图标,可能已参与成功",
|
||
)
|
||
except Exception:
|
||
pass
|
||
else:
|
||
# 等待详情页加载
|
||
try:
|
||
analyser.operation.delay(0.5) # 减少延迟
|
||
except Exception:
|
||
time.sleep(0.5)
|
||
|
||
# 再次检测按钮状态,验证是否参与成功
|
||
try:
|
||
analyser.operation.get_screenshot(
|
||
analyser.device_id, use_service=False
|
||
)
|
||
screenshot_path2 = path_config.get_screenshot_path()
|
||
pic_rgb2 = Image.open(screenshot_path2) # type: ignore[attr-defined]
|
||
try:
|
||
button_result2 = douyin_pixel.detect_attend_button_red_area(
|
||
pic_rgb2, return_info=True, analyser=analyser
|
||
)
|
||
finally:
|
||
pic_rgb2.close()
|
||
if isinstance(button_result2, tuple):
|
||
has_button2, button_info2 = button_result2
|
||
else:
|
||
has_button2 = button_result2
|
||
button_info2 = {}
|
||
|
||
if has_button2:
|
||
try:
|
||
analyser._log_kv( # type: ignore[attr-defined]
|
||
"预约",
|
||
"[预约] 验证时仍检测到参与按钮,可能参与失败",
|
||
button_info=button_info2,
|
||
)
|
||
except Exception:
|
||
pass
|
||
# 如果还有按钮,回退到正常流程
|
||
analyser._handle_in_fudai_detail() # type: ignore[attr-defined]
|
||
return
|
||
except Exception:
|
||
pass
|
||
|
||
# 步骤5:参与成功,设置开奖等待状态并从预约队列中移除
|
||
join_result = True # 假设参与成功
|
||
if join_result and future_ts > 0:
|
||
# 记录统计:预约回退成功算作成功参与一次
|
||
try:
|
||
analyser._log_kv( # type: ignore[attr-defined]
|
||
"统计",
|
||
"metric",
|
||
metric="fudai_attend",
|
||
metric_type="success",
|
||
run_id=getattr(analyser, "_run_id", ""),
|
||
source="reservation_backtrack",
|
||
future_ts=int(future_ts),
|
||
)
|
||
except Exception:
|
||
pass
|
||
|
||
try:
|
||
# 设置开奖等待状态
|
||
current_time = time.time()
|
||
remain_seconds = max(0, future_ts - current_time)
|
||
if remain_seconds > 0:
|
||
analyser._waiting_for_lottery_result = True
|
||
analyser._lottery_result_expected_ts = future_ts
|
||
analyser._lottery_wait_start_ts = current_time
|
||
analyser._current_attending_reservation_fudai = (
|
||
True # 标记当前正在参与预约福袋
|
||
)
|
||
analyser._log_kv( # type: ignore[attr-defined]
|
||
"预约",
|
||
"[预约] 预约回退后参与成功,设置开奖等待状态",
|
||
remain_seconds=int(remain_seconds),
|
||
future_ts=int(future_ts),
|
||
lottery_result_expected_ts=int(
|
||
analyser._lottery_result_expected_ts
|
||
),
|
||
)
|
||
|
||
# 设置锁定期,防止过早切换直播间
|
||
poll_before = float(
|
||
getattr(
|
||
analyser, "high_value_fudai_poll_before_seconds", 10.0
|
||
)
|
||
or 10.0
|
||
)
|
||
analyser.reservation_focus_until_ts = future_ts + max(
|
||
0.0, poll_before
|
||
)
|
||
|
||
# 从预约队列中移除已参与的预约
|
||
if analyser.reservations and len(analyser.reservations) > 0:
|
||
remaining_reservations = []
|
||
for res in analyser.reservations:
|
||
try:
|
||
res_future_ts = float(res.get("future_ts") or 0.0)
|
||
if (
|
||
res_future_ts > 0
|
||
and abs(res_future_ts - future_ts) <= 5.0
|
||
):
|
||
# 匹配的预约,移除
|
||
analyser._log_kv( # type: ignore[attr-defined]
|
||
"预约",
|
||
"[预约] 预约回退后参与成功,从预约队列中移除该预约",
|
||
future_ts=int(res_future_ts),
|
||
value_yuan=res.get("prize_value_yuan", 0),
|
||
content=(
|
||
res.get("content", "")[:50]
|
||
if res.get("content")
|
||
else ""
|
||
),
|
||
)
|
||
else:
|
||
# 保留不匹配的预约
|
||
remaining_reservations.append(res)
|
||
except Exception:
|
||
# 如果解析预约项时出错,保留该项(容错处理)
|
||
remaining_reservations.append(res)
|
||
|
||
if len(remaining_reservations) < len(analyser.reservations):
|
||
analyser.reservations = remaining_reservations
|
||
|
||
analyser.reservation_after_action_pending = False
|
||
except Exception as e:
|
||
analyser._log_kv( # type: ignore[attr-defined]
|
||
"预约",
|
||
"[预约] 预约回退后参与成功,设置状态时出错",
|
||
error=str(e),
|
||
error_type=type(e).__name__,
|
||
)
|
||
|
||
elapsed = time.time() - start_time
|
||
try:
|
||
analyser._log_kv( # type: ignore[attr-defined]
|
||
"流程",
|
||
"_handle_reservation_direct_attend完成",
|
||
elapsed_seconds=round(elapsed, 2),
|
||
join_result=join_result,
|
||
)
|
||
except Exception:
|
||
pass
|
||
except Exception as e:
|
||
try:
|
||
analyser._log_kv( # type: ignore[attr-defined]
|
||
"预约",
|
||
"[预约] 预约回退后直接参与:从预约记录获取详情时出错,回退到正常流程",
|
||
error=str(e),
|
||
)
|
||
except Exception:
|
||
pass
|
||
# 如果出错,回退到正常流程
|
||
analyser._handle_in_fudai_detail() # type: ignore[attr-defined]
|