fix: speed up reservation direct attend
This commit is contained in:
1 parent
84b9143fa6
commit
8389f85124
10 files changed
+291
-11
No files matched your search
+9
-3
@@ -134,6 +134,11 @@ def parse_args(argv: list[str]) -> argparse.Namespace:
|
||||
p.add_argument("--device-id", default="", help="ADB 设备 ID(为空则自动选择)")
|
||||
p.add_argument("--y-pianyi", type=int, default=0, help="y 轴偏移(通常 -50~50)")
|
||||
p.add_argument("--needswitch", action="store_true", help="是否允许上划切换直播间")
|
||||
p.add_argument(
|
||||
"--cold-start",
|
||||
action="store_true",
|
||||
help="启动本轮前 force-stop 并重新打开抖音(真机回归测试使用)",
|
||||
)
|
||||
p.add_argument(
|
||||
"--wait-minutes",
|
||||
type=int,
|
||||
@@ -755,9 +760,10 @@ def main(argv: list[str]) -> int:
|
||||
print("未检测到设备,退出。")
|
||||
return 2
|
||||
|
||||
if not _cold_start_douyin_for_test(analyser, device_id):
|
||||
print("抖音冷启动失败或前台不是抖音,退出。")
|
||||
return 4
|
||||
if bool(getattr(args, "cold_start", False)):
|
||||
if not _cold_start_douyin_for_test(analyser, device_id):
|
||||
print("抖音冷启动失败或前台不是抖音,退出。")
|
||||
return 4
|
||||
|
||||
exit_code = 0
|
||||
try:
|
||||
|
||||
@@ -137,16 +137,23 @@ def find_and_open_fudai_detail(
|
||||
stage: Any = "",
|
||||
seen: Any = "",
|
||||
func_start_time: float | None = None,
|
||||
max_wait_seconds: float | None = None,
|
||||
):
|
||||
"""主实现:检测福袋图标并点击打开详情页。"""
|
||||
effective_max_wait_seconds = float(
|
||||
(
|
||||
max_wait_seconds
|
||||
if max_wait_seconds is not None
|
||||
else getattr(analyser, "fudai_icon_max_wait_seconds", 5.0)
|
||||
)
|
||||
or 5.0
|
||||
)
|
||||
_emit_flow_flag(
|
||||
analyser,
|
||||
"fudai_find_open",
|
||||
"start",
|
||||
room_pos=int(getattr(analyser, "room_pos", 0) or 0),
|
||||
max_wait_seconds=float(
|
||||
getattr(analyser, "fudai_icon_max_wait_seconds", 5.0) or 5.0
|
||||
),
|
||||
max_wait_seconds=effective_max_wait_seconds,
|
||||
)
|
||||
if func_start_time is None:
|
||||
func_start_time = time.time()
|
||||
@@ -175,9 +182,7 @@ def find_and_open_fudai_detail(
|
||||
|
||||
# 1. 利用像素检测福袋图标的 X 坐标(1080 基准)
|
||||
# 在配置的最大等待时间内重复检测福袋图标
|
||||
max_wait_seconds = float(
|
||||
getattr(analyser, "fudai_icon_max_wait_seconds", 5.0) or 5.0
|
||||
)
|
||||
max_wait_seconds = effective_max_wait_seconds
|
||||
retry_delay_seconds = float(
|
||||
getattr(analyser, "fudai_icon_retry_delay_seconds", 0.6) or 0.6
|
||||
)
|
||||
|
||||
@@ -90,6 +90,21 @@ def _direct_attend_failure_limit(analyser: Any) -> int:
|
||||
return 2
|
||||
|
||||
|
||||
def _direct_attend_open_max_wait_seconds(analyser: Any) -> float:
|
||||
try:
|
||||
value = float(
|
||||
getattr(analyser, "reservation_direct_attend_open_max_wait_seconds", 2.5)
|
||||
or 2.5
|
||||
)
|
||||
except Exception:
|
||||
value = 2.5
|
||||
if value < 1.0:
|
||||
value = 1.0
|
||||
if value > 3.0:
|
||||
value = 3.0
|
||||
return value
|
||||
|
||||
|
||||
def _reservation_key(item: Dict[str, Any]) -> str:
|
||||
try:
|
||||
ft = int(float(item.get("future_ts") or 0.0))
|
||||
@@ -138,6 +153,32 @@ def _record_direct_attend_failure(analyser: Any, item: Dict[str, Any], reason: s
|
||||
return count
|
||||
|
||||
|
||||
def _record_direct_attend_failure_ts(analyser: Any, item: Dict[str, Any]) -> None:
|
||||
key = _reservation_key(item)
|
||||
state = getattr(analyser, "_reservation_direct_attend_failure_ts", None)
|
||||
if not isinstance(state, dict):
|
||||
state = {}
|
||||
try:
|
||||
setattr(analyser, "_reservation_direct_attend_failure_ts", state)
|
||||
except Exception:
|
||||
pass
|
||||
try:
|
||||
state[key] = time.time()
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
|
||||
def _recent_direct_attend_failure_seconds(analyser: Any, item: Dict[str, Any]) -> float:
|
||||
key = _reservation_key(item)
|
||||
state = getattr(analyser, "_reservation_direct_attend_failure_ts", None)
|
||||
if not isinstance(state, dict):
|
||||
return 999999.0
|
||||
try:
|
||||
return max(0.0, time.time() - float(state.get(key) or 0.0))
|
||||
except Exception:
|
||||
return 999999.0
|
||||
|
||||
|
||||
def _reset_direct_attend_failure_count(analyser: Any, item: Dict[str, Any]) -> None:
|
||||
state = getattr(analyser, "_reservation_direct_attend_failures", None)
|
||||
if not isinstance(state, dict):
|
||||
@@ -448,6 +489,38 @@ def handle_reservation_direct_attend(analyser: Any) -> None:
|
||||
|
||||
failure_count = _get_direct_attend_failure_count(analyser, first_reservation)
|
||||
failure_limit = _direct_attend_failure_limit(analyser)
|
||||
recent_failure_seconds = _recent_direct_attend_failure_seconds(
|
||||
analyser, first_reservation
|
||||
)
|
||||
if (
|
||||
failure_count > 0
|
||||
and recent_failure_seconds < 1.0
|
||||
and future_ts > 0
|
||||
and remain_seconds < (min_action_seconds + 2.0)
|
||||
):
|
||||
_log_direct_attend_marker(
|
||||
analyser,
|
||||
"fallback",
|
||||
reason="direct_attend_retry_throttled",
|
||||
future_ts=int(future_ts),
|
||||
remain_seconds=int(max(0, remain_seconds)),
|
||||
recent_failure_seconds=round(recent_failure_seconds, 3),
|
||||
failure_count=failure_count,
|
||||
)
|
||||
try:
|
||||
analyser._log_kv(
|
||||
"预约",
|
||||
"[预约] 预约回退后直接参与:刚失败过且剩余时间偏紧,跳过重复慢扫描",
|
||||
reason="direct_attend_retry_throttled",
|
||||
future_ts=int(future_ts),
|
||||
remain_seconds=round(remain_seconds, 3),
|
||||
recent_failure_seconds=round(recent_failure_seconds, 3),
|
||||
failure_count=failure_count,
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
_clear_direct_attend_flags(analyser)
|
||||
return
|
||||
if failure_count >= failure_limit:
|
||||
_log_direct_attend_marker(
|
||||
analyser,
|
||||
@@ -487,9 +560,24 @@ def handle_reservation_direct_attend(analyser: Any) -> None:
|
||||
import douyin.core.douyin_flow_control as douyin_flow_control
|
||||
|
||||
room_pos_ref = [int(getattr(analyser, "room_pos", 0) or 0)]
|
||||
open_max_wait_seconds = _direct_attend_open_max_wait_seconds(analyser)
|
||||
open_start_ts = time.time()
|
||||
opened = douyin_flow_control.find_and_open_fudai_detail(
|
||||
analyser, room_pos_ref
|
||||
analyser,
|
||||
room_pos_ref,
|
||||
max_wait_seconds=open_max_wait_seconds,
|
||||
)
|
||||
try:
|
||||
analyser._log_kv(
|
||||
"预约",
|
||||
"[预约] 预约直参打开详情耗时",
|
||||
direct_open_elapsed=round(time.time() - open_start_ts, 3),
|
||||
open_max_wait_seconds=round(open_max_wait_seconds, 3),
|
||||
remain_seconds_before_direct_attend=round(remain_seconds, 3),
|
||||
opened=bool(opened),
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
if not opened:
|
||||
_log_direct_attend_marker(analyser, "fallback", reason="fudai_icon_not_found")
|
||||
@@ -500,6 +588,7 @@ def handle_reservation_direct_attend(analyser: Any) -> None:
|
||||
future_ts=int(future_ts),
|
||||
remain_seconds=int(max(0, future_ts - time.time())) if future_ts > 0 else 0,
|
||||
)
|
||||
_record_direct_attend_failure_ts(analyser, first_reservation)
|
||||
_save_direct_attend_debug_screenshot(
|
||||
analyser,
|
||||
"verified_return_but_fudai_not_found",
|
||||
@@ -570,6 +659,7 @@ def handle_reservation_direct_attend(analyser: Any) -> None:
|
||||
|
||||
from douyin.detection import douyin_pixel
|
||||
|
||||
button_detect_start_ts = time.time()
|
||||
try:
|
||||
button_result = douyin_pixel.detect_attend_button_red_area(
|
||||
pic_rgb, return_info=True, analyser=analyser, save_debug=False
|
||||
@@ -581,6 +671,18 @@ def handle_reservation_direct_attend(analyser: Any) -> None:
|
||||
else:
|
||||
has_button = button_result
|
||||
button_info = {}
|
||||
try:
|
||||
analyser._log_kv(
|
||||
"预约",
|
||||
"[预约] 预约直参按钮检测耗时",
|
||||
direct_button_detect_elapsed=round(
|
||||
time.time() - button_detect_start_ts, 3
|
||||
),
|
||||
has_button=bool(has_button),
|
||||
button_info=button_info,
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
if not has_button:
|
||||
fallback_ok, fallback_button_text, fallback_button_status = (
|
||||
@@ -612,6 +714,7 @@ def handle_reservation_direct_attend(analyser: Any) -> None:
|
||||
"attend_button_not_detected",
|
||||
future_ts=int(future_ts),
|
||||
)
|
||||
_record_direct_attend_failure_ts(analyser, first_reservation)
|
||||
_save_direct_attend_debug_screenshot(
|
||||
analyser,
|
||||
"detail_open_but_attend_button_not_detected",
|
||||
@@ -685,9 +788,22 @@ def handle_reservation_direct_attend(analyser: Any) -> None:
|
||||
|
||||
# 再次查找并点击福袋图标
|
||||
room_pos_ref2 = [int(getattr(analyser, "room_pos", 0) or 0)]
|
||||
verify_open_start_ts = time.time()
|
||||
opened2 = douyin_flow_control.find_and_open_fudai_detail(
|
||||
analyser, room_pos_ref2
|
||||
analyser,
|
||||
room_pos_ref2,
|
||||
max_wait_seconds=open_max_wait_seconds,
|
||||
)
|
||||
try:
|
||||
analyser._log_kv(
|
||||
"预约",
|
||||
"[预约] 预约直参验证打开详情耗时",
|
||||
direct_open_elapsed=round(time.time() - verify_open_start_ts, 3),
|
||||
open_max_wait_seconds=round(open_max_wait_seconds, 3),
|
||||
opened=bool(opened2),
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
if not opened2:
|
||||
try:
|
||||
@@ -845,6 +961,7 @@ def handle_reservation_direct_attend(analyser: Any) -> None:
|
||||
"流程",
|
||||
"_handle_reservation_direct_attend完成",
|
||||
elapsed_seconds=round(elapsed, 2),
|
||||
direct_total_elapsed=round(elapsed, 3),
|
||||
join_result=join_result,
|
||||
)
|
||||
except Exception:
|
||||
|
||||
@@ -169,6 +169,11 @@ def seek_reservation_room(
|
||||
or cur_norm in target_norm
|
||||
)
|
||||
):
|
||||
try:
|
||||
analyser._reservation_seek_same_room_title_verified = True
|
||||
analyser._reservation_seek_same_room_verified_ts = time.time()
|
||||
except Exception:
|
||||
pass
|
||||
try:
|
||||
analyser._log_kv(
|
||||
operation_type,
|
||||
|
||||
@@ -101,11 +101,16 @@ def handle_back_verify(
|
||||
except Exception:
|
||||
verify_wait_seconds = 3.0
|
||||
|
||||
same_room_title_verified = bool(
|
||||
getattr(analyser, "_reservation_seek_same_room_title_verified", False)
|
||||
)
|
||||
|
||||
try:
|
||||
analyser._log_kv(
|
||||
"预约",
|
||||
"[预约] process_reservation_attend_window:回退后等待直播间加载完成(开始等待)",
|
||||
verify_wait_seconds=verify_wait_seconds,
|
||||
fast_path=same_room_title_verified,
|
||||
level="INFO",
|
||||
)
|
||||
except Exception:
|
||||
@@ -125,12 +130,33 @@ def handle_back_verify(
|
||||
"[预约] process_reservation_attend_window:回退等待完成",
|
||||
expected_wait_seconds=verify_wait_seconds,
|
||||
waited_seconds=round(waited, 3),
|
||||
back_verify_wait_elapsed=round(waited, 3),
|
||||
fast_path=same_room_title_verified,
|
||||
level="INFO",
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
if same_room_title_verified:
|
||||
try:
|
||||
analyser._reservation_seek_same_room_title_verified = False
|
||||
except Exception:
|
||||
pass
|
||||
try:
|
||||
analyser._log_kv(
|
||||
"预约",
|
||||
"[预约] 回退校验快路径通过:已在目标位置且标题匹配,跳过OCR标题校验",
|
||||
target_pos=int(target.get("pos_seen", 0)),
|
||||
target_room_title=target_room_title[:50] if target_room_title else "",
|
||||
reason="same_room_title_verified_fast_path",
|
||||
level="INFO",
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
return None
|
||||
|
||||
try:
|
||||
verify_start_ts = time.time()
|
||||
try:
|
||||
analyser._log_kv(
|
||||
"预约",
|
||||
@@ -161,6 +187,7 @@ def handle_back_verify(
|
||||
"[预约] process_reservation_attend_window:回退校验函数返回",
|
||||
verify_ok=verify_ok,
|
||||
verify_detail_exists=verify_detail is not None,
|
||||
back_verify_ocr_elapsed=round(time.time() - verify_start_ts, 3),
|
||||
verify_detail_preview=(
|
||||
str(verify_detail)[:300] if verify_detail is not None else ""
|
||||
),
|
||||
|
||||
@@ -290,6 +290,26 @@ class ConfigManager:
|
||||
config["reservation_back_verify_enabled"]
|
||||
)
|
||||
|
||||
if "reservation_back_verify_wait_seconds" in config:
|
||||
analyser.reservation_back_verify_wait_seconds = float(
|
||||
config["reservation_back_verify_wait_seconds"] or 1.5
|
||||
)
|
||||
|
||||
if "reservation_direct_attend_min_action_seconds" in config:
|
||||
analyser.reservation_direct_attend_min_action_seconds = float(
|
||||
config["reservation_direct_attend_min_action_seconds"] or 8.0
|
||||
)
|
||||
|
||||
if "reservation_direct_attend_max_failures" in config:
|
||||
analyser.reservation_direct_attend_max_failures = int(
|
||||
config["reservation_direct_attend_max_failures"] or 2
|
||||
)
|
||||
|
||||
if "reservation_direct_attend_open_max_wait_seconds" in config:
|
||||
analyser.reservation_direct_attend_open_max_wait_seconds = float(
|
||||
config["reservation_direct_attend_open_max_wait_seconds"] or 2.5
|
||||
)
|
||||
|
||||
if "record_room_title_mapping_enabled" in config:
|
||||
analyser.record_room_title_mapping_enabled = bool(
|
||||
config["record_room_title_mapping_enabled"]
|
||||
|
||||
@@ -129,3 +129,40 @@ def test_second_seek_must_verify_before_setting_direct_attend():
|
||||
assert pending_ref[0] is False
|
||||
assert focus_ref[0] == 0.0
|
||||
write_state.assert_called_once()
|
||||
|
||||
|
||||
def test_same_room_title_verified_uses_fast_back_verify_path():
|
||||
from douyin.reservation.reservation_logic import attend_window_verify as mod
|
||||
|
||||
analyser = _mk_analyser()
|
||||
analyser.operation = MagicMock()
|
||||
analyser.operation.delay = MagicMock(return_value=None)
|
||||
analyser.reservation_back_verify_enabled = True
|
||||
analyser.reservation_back_verify_wait_seconds = 1.5
|
||||
analyser._reservation_seek_same_room_title_verified = True
|
||||
target = {
|
||||
"pos_seen": 2,
|
||||
"room_title": "料音城ATK官抖音旗舰",
|
||||
"room_title_raw": "料音城ATK官抖音旗舰",
|
||||
}
|
||||
|
||||
with patch(
|
||||
"douyin.reservation.douyin_reservation_verify.validate_back_to_reservation"
|
||||
) as validate:
|
||||
result = mod.handle_back_verify(
|
||||
analyser=analyser,
|
||||
target=target,
|
||||
reservations=[target],
|
||||
room_pos=2,
|
||||
ft=9999999999.0,
|
||||
poll_before=10.0,
|
||||
reservation_focus_until_ts_ref=[0.0],
|
||||
reservation_after_action_pending_ref=[False],
|
||||
seek_reservation_room_callback=lambda item: False,
|
||||
write_state_callback=lambda: None,
|
||||
)
|
||||
|
||||
assert result is None
|
||||
analyser.operation.delay.assert_called_once_with(1.5)
|
||||
validate.assert_not_called()
|
||||
assert analyser._reservation_seek_same_room_title_verified is False
|
||||
@@ -124,6 +124,15 @@ class TestRunCliParseArgs:
|
||||
assert result.regression_collection_max_images == 120
|
||||
assert result.regression_full_run_package is True
|
||||
|
||||
def test_cold_start_is_opt_in(self):
|
||||
from bin.run_cli import parse_args
|
||||
|
||||
default_result = parse_args([])
|
||||
enabled_result = parse_args(["--cold-start"])
|
||||
|
||||
assert default_result.cold_start is False
|
||||
assert enabled_result.cold_start is True
|
||||
|
||||
|
||||
class TestRunCliModule:
|
||||
"""run_cli 模块测试。"""
|
||||
|
||||
@@ -266,6 +266,29 @@ class TestFudaiDetectionDefaults:
|
||||
assert analyser.pixel_detect_shopping_redpacket_enabled is True
|
||||
assert analyser.ocr_shopping_redpacket_enabled is False
|
||||
|
||||
def test_config_loader_loads_reservation_timing_fields(self):
|
||||
from douyin.utils.douyin_config import ConfigManager
|
||||
|
||||
analyser = SimpleNamespace()
|
||||
config = {
|
||||
"reservation_back_verify_wait_seconds": 1.5,
|
||||
"reservation_direct_attend_min_action_seconds": 6.0,
|
||||
"reservation_direct_attend_max_failures": 3,
|
||||
"reservation_direct_attend_open_max_wait_seconds": 2.25,
|
||||
}
|
||||
with tempfile.NamedTemporaryFile("w", suffix=".json", delete=False) as f:
|
||||
json.dump(config, f)
|
||||
tmp_path = f.name
|
||||
try:
|
||||
ConfigManager.load_config_from_file(analyser, tmp_path)
|
||||
finally:
|
||||
os.unlink(tmp_path)
|
||||
|
||||
assert analyser.reservation_back_verify_wait_seconds == 1.5
|
||||
assert analyser.reservation_direct_attend_min_action_seconds == 6.0
|
||||
assert analyser.reservation_direct_attend_max_failures == 3
|
||||
assert analyser.reservation_direct_attend_open_max_wait_seconds == 2.25
|
||||
|
||||
|
||||
class TestCoreModules:
|
||||
"""core 子模块存在性测试。"""
|
||||
|
||||
@@ -90,6 +90,37 @@ def test_direct_attend_rescans_fudai_after_join_click_for_verification():
|
||||
analyser.operation.click.assert_called()
|
||||
|
||||
|
||||
def test_direct_attend_uses_short_open_budget_for_both_scans():
|
||||
from douyin.reservation.douyin_reservation_handler_pkg.direct_attend import (
|
||||
handle_reservation_direct_attend,
|
||||
)
|
||||
|
||||
analyser = _mk_analyser()
|
||||
analyser.fudai_icon_max_wait_seconds = 6.5
|
||||
analyser.reservation_direct_attend_open_max_wait_seconds = 2.25
|
||||
|
||||
with patch(
|
||||
"douyin.core.douyin_flow_control.find_and_open_fudai_detail",
|
||||
side_effect=[True, False],
|
||||
) as finder, patch(
|
||||
"douyin.core.path_config.get_screenshot_path",
|
||||
return_value="C:/mock/screenshot.png",
|
||||
), patch(
|
||||
"PIL.Image.open",
|
||||
) as mock_open, patch(
|
||||
"douyin.detection.douyin_pixel.detect_attend_button_red_area",
|
||||
return_value=(True, {"mock": True}),
|
||||
):
|
||||
mock_image = MagicMock()
|
||||
mock_open.return_value = mock_image
|
||||
handle_reservation_direct_attend(analyser)
|
||||
|
||||
assert finder.call_count == 2
|
||||
for call in finder.call_args_list:
|
||||
assert analyser.fudai_icon_max_wait_seconds == 6.5
|
||||
assert call.kwargs["max_wait_seconds"] == 2.25
|
||||
|
||||
|
||||
def test_direct_attend_logs_fixed_failure_marker_when_button_still_exists():
|
||||
from douyin.reservation.douyin_reservation_handler_pkg.direct_attend import (
|
||||
handle_reservation_direct_attend,
|
||||
|
||||
Reference in new issue
Block a user