diff --git a/douyin/core/douyin_fudai.py b/douyin/core/douyin_fudai.py index da88149..7df4c89 100644 --- a/douyin/core/douyin_fudai.py +++ b/douyin/core/douyin_fudai.py @@ -116,242 +116,9 @@ class fudai_analyse: douyin_logging.configure_logging() except Exception: pass - self._forced_reservation_detail = None - self._forced_reservation_fudai_x = None - - # 进入直播间后诊断与阶段流转所需的一些状态 - self._last_diag_stage: str = "INIT" - self._last_diag_seen: str = "-" - self._last_diag_ts: float = 0.0 - - # 30分钟超时相关:记录最后一次成功参与福袋的时间 - self._last_successful_attend_ts: float = 0.0 - self._no_attend_timeout_minutes: float = ( - 30.0 # 30分钟没有参与成功则返回推荐页面(可通过config.json配置) - ) - self._run_start_ts: float = ( - time.time() - ) # 记录运行开始时间,用于从未成功参与时的超时检查 - # 预约状态上次写盘时间(初始化以避免 attribute-defined-outside-init 警告) - self._reservations_state_last_write_ts: float = 0.0 - self.last_fudai_x: int = 0 # 上次检测到的福袋图标X坐标(用于优化搜索) - - # 与配置相关的核心字段(只初始化最关键的几项,其余保持默认) - self.contains_not_want: list[str] = [] - self.contains_want: list[str] = [] - self.min_prize_value_yuan: float = 0.0 - self.min_participants: int = 0 - self.max_participants: int = 0 - self.skip_if_countdown_gt_seconds: int = 900 - self.skip_if_countdown_lt_seconds: int = 0 # 倒计时小于该秒数则不参与 - self.no_fudai_watchdog_enabled: bool = False - # 福袋图标检测节流相关默认值 - # 实际识别到福袋一般需要 4–6 秒,这里取中间值 5 秒作为默认上限,具体值可在 GUI / config.json 中调整 - # 注意:已改为使用时间限制(fudai_icon_max_wait_seconds),不再使用尝试次数限制 - self.fudai_icon_retry_delay_seconds: float = 0.6 - self.fudai_icon_max_wait_seconds: float = 5.0 - # 连续未找到福袋超过此阈值后,自动按返回键退出直播间(默认999999=禁用) - self.fudai_exit_after_misses: int = 999999 - # 是否启用自动返回键退出(默认False=禁用) - self.enable_back_on_misses: bool = False - # 点击福袋图标后等待详情页加载的时间(秒) - self.fudai_click_wait_seconds: float = 1.0 - # 切换直播间后等待阶段诊断的时间(秒) - self.switch_room_wait_after_stage_diag_seconds: float = 7.0 - # 滑动速度(毫秒),影响滑动持续时间,值越大滑动越慢 - self.swipe_duration_ms: int = 600 - # fudai_find_max_attempts 已废弃,保留仅为兼容性,实际不再使用 - self.fudai_find_max_attempts: int = 10 - - # 优化查找器相关配置 - self.fudai_finder_optimized_enabled: bool = True # 默认启用优化查找器 - self.fudai_click_verify_enabled: bool = True # 点击后验证是否成功打开详情页 - self.fudai_find_max_retries: int = 3 # 最大重试次数 - self.screenshot_diagnose_min_interval_seconds: float = 8.0 - # 开奖页面检测红色占比阈值(0.0~1.0) - self.lucky_draw_red_ratio_threshold: float = 0.9 - # 优惠券模板匹配阈值(0.0~1.0,默认0.75) - self.coupon_template_match_threshold: float = 0.75 - # 参与按钮红色占比阈值(0.0~1.0,默认0.25) - self.attend_button_red_ratio_threshold: float = 0.25 - # 粉丝团加入红色占比阈值(0.0~1.0,默认0.25) - self.fansclub_join_red_ratio_threshold: float = 0.25 - - # 直播间检测阈值(模板匹配阈值,0.0~1.0,默认0.8) - self.live_room_match_threshold: float = 0.8 - # 福袋模板匹配开关(默认关闭) - self.fudai_template_match_enabled: bool = False - # 福袋模板匹配阈值(0.0~1.0,默认0.7) - self.fudai_template_match_threshold: float = 0.7 - - # —— 高价值福袋 / 预约相关配置(从原 fudai1 精简迁移) —— - # 是否启用“高价值福袋预约参与” - self.high_value_fudai_enabled: bool = False - # 高价值阈值(元) - self.high_value_fudai_threshold_yuan: float = 500.0 - # 提前参与秒数:开奖前 N 秒回到该直播间并参与 - self.high_value_fudai_join_ahead_seconds: int = 100 - # 从当前位置最多回滑多少次以回到预约直播间 - self.high_value_fudai_max_swipe_back: int = 35 - # 同时记录的最大预约数量 - self.high_value_fudai_max_reservations: int = 3 - # 完成某个预约福袋后的动作:stay=停留当前直播间;continue=继续执行抢福袋 - self.high_value_fudai_after_action: str = "continue" - self.high_value_fudai_after_stay_seconds: int = 120 - # 开奖前轮询时间(秒,默认10.0) - self.high_value_fudai_poll_before_seconds: float = 10.0 - - # 高价值直播间"回访"相关(当前仅保留配置,不在本地版强制启用复杂逻辑) - self.high_value_room_revisit_enabled: bool = False - self.high_value_room_revisit_only_current_run: bool = False - self.high_value_room_revisit_min_interval_seconds: int = 300 - self.high_value_room_revisit_max_per_run: int = 5 - self.high_value_room_revisit_min_prize_value: float = 0.0 - self.high_value_room_revisit_max_age_days: int = 30 - - # 粉丝团相关配置 - self.auto_join_fansclub: bool = False # 是否自动加入粉丝团 - self.fansclub_fail_action: str = ( - "stay" # 粉丝团失败策略:stay=停留,switch=切换 - ) - - # 预约回退相关 - self.reservation_back_verify_enabled: bool = True # 默认启用回退校验 - # 未做回退校验时的快速回滑延迟(秒) - self.reservation_back_swipe_delay: float = 0.6 - - # 滑动次数重置相关 - self.reset_after_swipes_enabled: bool = False # 由 Web/CLI 显式开关 - self.reset_after_swipes: int = 10 # 达到阈值次数后回退,需启用开关后生效 - - # 意外返回桌面自救相关 - self.desktop_icon_x: int = 153 # 桌面图标 X 坐标(默认值) - self.desktop_icon_y: int = 260 # 桌面图标 Y 坐标(默认值) - self.yanghao_fallback_live_x: int = 540 # 养号备用直播按钮 X 坐标 - self.yanghao_fallback_live_y: int = 2000 # 养号备用直播按钮 Y 坐标 - self.yanghao_fallback_enter_live_x: int = 540 # 养号备用进入直播间 X 坐标 - self.yanghao_fallback_enter_live_y: int = 1200 # 养号备用进入直播间 Y 坐标 - - # 推荐页养号默认配置(若 config.json 未设置则使用这些默认值) - self.yanghao_enabled: bool = True - self.yanghao_enable_actions: bool = True - self.yanghao_step_screenshot: bool = False - self.yanghao_recommend_duration_seconds: int = 60 - self.yanghao_recommend_interval_seconds: int = 0 - self.yanghao_like_prob: float = 0.15 - self.yanghao_fav_prob: float = 0.05 - self.yanghao_comment_prob: float = 0.0 - self.yanghao_dwell_on_action_min_seconds: float = 5.0 - self.yanghao_dwell_on_action_max_seconds: float = 30.0 - self.yanghao_icon_roi_size: int = 180 - # 调整默认阈值为 0.70(更宽松,减少漏检) - self.yanghao_icon_match_threshold: float = 0.70 - self.yanghao_like_roi_scale: float = 1.5 - self.yanghao_fav_roi_scale: float = 1.5 - self.yanghao_like_icon_match_threshold: float | None = None - self.yanghao_fav_icon_match_threshold: float | None = None - # icon 坐标(基于 1080x2400) - self.yanghao_like_icon_x: int = 0 - self.yanghao_like_icon_y: int = 0 - self.yanghao_fav_icon_x: int = 0 - self.yanghao_fav_icon_y: int = 0 - # roi 偏移 - self.yanghao_like_roi_offset_x: int = 0 - self.yanghao_like_roi_offset_y: int = 0 - self.yanghao_fav_roi_offset_x: int = 0 - self.yanghao_fav_roi_offset_y: int = 0 - # 评论相关默认坐标与文案列表 - # 最近一次触发推荐页养号的时间戳(用于定时触发) - self._last_yanghao_ts: float = 0.0 - - self.yanghao_comment_btn_x: int = 0 - self.yanghao_comment_btn_y: int = 0 - self.yanghao_comment_input_x: int = 0 - self.yanghao_comment_input_y: int = 0 - self.yanghao_comment_send_x: int = 0 - self.yanghao_comment_send_y: int = 0 - self.yanghao_comment_close_x: int = 0 - self.yanghao_comment_close_y: int = 0 - self.yanghao_comment_texts: list[str] = [] - - # —— 预约运行时状态(单次运行内有效) —— - # 高价值福袋预约队列 - self.reservations: list[dict[str, Any]] = [] - # 标记是否是第一次运行 run_once_basic(用于刷新预约队列) - self._first_run_once_basic: bool = True - # 当前"直播间位置"计数:上划 +1,下滑 -1(仅在本次连续切换过程中有效) - self.room_pos: int = 0 - # 回退到预约直播间后,在该时间戳前不允许上划离开 - self.reservation_focus_until_ts: float = 0.0 - # 当前是否还有"完成某个预约后的后续动作"待执行 - self.reservation_after_action_pending: bool = False - # 达到"无福袋切换阈值"后,是否在处理完预约后回到列表第一个直播间 - self.reset_to_list_after_reservations: bool = False - # 队列满时是否已回退到最早开奖的预约直播间等待 - self.queue_full_waiting_at_earliest: bool = False - - # 预约队列状态文件(供 WebGUI / 其他进程查看) - try: - from douyin.core import path_config as _pc - self._reservations_state_path: str = _pc.get_reservations_path() - except Exception: - self._reservations_state_path = os.path.join( - os.path.dirname(__file__), "state", "reservations.json" - ) - - # 运行时直播间标题和位置映射表(用于回退校验) - # 格式:{room_pos: {"title": str, "title_raw": str, "has_fudai": bool, "fudai_content": str, "fudai_detail": Any}} - self._room_pos_to_title_map: dict[int, dict[str, Any]] = {} - # 每次重新运行时丢弃磁盘上旧的映射表,确保只记录本次运行的结果。 - # 同时尝试删除旧的导出文件(JSON/CSV/MD),以避免 GUI 在未写入新数据前显示过期内容。 - try: - state_dir = os.path.dirname(self._reservations_state_path) - state_path = os.path.join(state_dir, "room_pos_map.json") - csv_path = os.path.join(state_dir, "room_pos_map.csv") - md_path = os.path.join(state_dir, "room_pos_map.md") - for p in (state_path, csv_path, md_path): - try: - if os.path.exists(p): - os.remove(p) - except Exception: - pass - except Exception: - pass - - # 已记录统计的福袋跟踪(避免同一个直播间同一个福袋重复记录统计) - self._recorded_fudai_stats: set[tuple[str, str]] = ( - set() - ) # (room_title, fudai_content) - - # 运行时状态标志(避免在外部定义属性的警告) - self.needswitch: bool | None = ( - None # 是否允许切换直播间,将从配置文件加载,默认 True - ) - self._waiting_for_lottery_result: bool = False # 是否正在等待开奖结果 - self._current_attending_reservation_fudai: bool = ( - False # 当前正在参与的是否为预约福袋 - ) - self._skip_stage_diagnosis_and_find_fudai: bool = False # 跳过阶段诊断标志 - self._just_switched_room: bool = False # 是否刚刚切换了直播间 - - # 高价值房间重访相关状态 - self._last_high_value_revisit_ts: float = ( - time.time() - ) # 最后一次重访时间戳,初始化为当前时间避免启动后立即重访 - self._current_run_revisit_count: int = 0 # 本次运行的重访次数 - self._current_run_high_value_rooms: list[dict[str, Any]] = ( - [] - ) # 本次运行发现的高价值房间 - self._last_fudai_detail_tuple: Optional[Tuple[Any, ...]] = ( - None # 上次读取的福袋详情 - ) - self._current_fudai_future_ts: float = 0.0 # 当前福袋的开奖时间戳 - self._current_fudai_content: str = "" # 当前福袋的内容标识 - self._fudai_detail_first_read: bool = False # 是否第一次读取福袋详情 - self._fudai_detail_consecutive_failures: int = 0 # 连续失败次数 - self._detail_closed_by_attend: bool = False # 详情页是否由参与操作关闭 - self._lottery_result_expected_ts: float = 0.0 # 开奖结果预期时间戳 - self._lottery_wait_start_ts: float = 0.0 # 开始等待开奖的时间戳 + # 应用所有"配置默认值 + 运行时状态字段初始化" + from douyin.core.fudai_modules.init_helpers import apply_fudai_defaults + apply_fudai_defaults(self) # 初次加载配置 self.load_config() diff --git a/douyin/core/fudai_modules/init_helpers.py b/douyin/core/fudai_modules/init_helpers.py new file mode 100644 index 0000000..17d650f --- /dev/null +++ b/douyin/core/fudai_modules/init_helpers.py @@ -0,0 +1,217 @@ +"""douyin/core/fudai_modules/init_helpers - fudai_analyse 配置默认值与运行时状态初始化 + +把 fudai_analyse.__init__ 中"配置字段默认值 + 运行时状态字段初始化 + 状态文件 +路径解析"的整段抽出来。原 __init__ 保留:设备/分辨率、operation 实例、日志系统 +初始化、_run_id、_log_kv 等需要早期就绪的核心字段。 + +入口:apply_fudai_defaults(self) 一次性把所有"普通字段默认值"应用到实例。 +""" +from __future__ import annotations + +import os +import time +from typing import Any, Optional, Tuple + + +def apply_fudai_defaults(self: Any) -> None: + """把 fudai_analyse 实例的所有配置默认值 + 运行时状态字段一次性初始化。 + + 依赖前提(必须由 caller 在调用前已设置): + - self.operation, self.device_id, self.y_pianyi + - self.resolution_ratio_x, self.resolution_ratio_y + - self._log_kv 等日志相关字段 + + 本函数不调用 load_config(让 caller 决定时机)。 + """ + self._forced_reservation_detail = None + self._forced_reservation_fudai_x = None + + # 进入直播间后诊断与阶段流转所需的一些状态 + self._last_diag_stage = "INIT" + self._last_diag_seen = "-" + self._last_diag_ts = 0.0 + + # 30 分钟超时相关:记录最后一次成功参与福袋的时间 + self._last_successful_attend_ts = 0.0 + self._no_attend_timeout_minutes = 30.0 + self._run_start_ts = time.time() + # 预约状态上次写盘时间(初始化以避免 attribute-defined-outside-init 警告) + self._reservations_state_last_write_ts = 0.0 + self.last_fudai_x = 0 # 上次检测到的福袋图标 X 坐标(用于优化搜索) + + # 与配置相关的核心字段 + self.contains_not_want = [] + self.contains_want = [] + self.min_prize_value_yuan = 0.0 + self.min_participants = 0 + self.max_participants = 0 + self.skip_if_countdown_gt_seconds = 900 + self.skip_if_countdown_lt_seconds = 0 + self.no_fudai_watchdog_enabled = False + # 福袋图标检测节流相关默认值 + self.fudai_icon_retry_delay_seconds = 0.6 + self.fudai_icon_max_wait_seconds = 5.0 + self.fudai_exit_after_misses = 999999 + self.enable_back_on_misses = False + self.fudai_click_wait_seconds = 1.0 + self.switch_room_wait_after_stage_diag_seconds = 7.0 + self.swipe_duration_ms = 600 + self.fudai_find_max_attempts = 10 # 已废弃,保留兼容性 + + # 优化查找器相关配置 + self.fudai_finder_optimized_enabled = True + self.fudai_click_verify_enabled = True + self.fudai_find_max_retries = 3 + self.screenshot_diagnose_min_interval_seconds = 8.0 + self.lucky_draw_red_ratio_threshold = 0.9 + self.coupon_template_match_threshold = 0.75 + self.attend_button_red_ratio_threshold = 0.25 + self.fansclub_join_red_ratio_threshold = 0.25 + self.live_room_match_threshold = 0.8 + self.fudai_template_match_enabled = False + self.fudai_template_match_threshold = 0.7 + + # 高价值福袋 / 预约相关配置 + self.high_value_fudai_enabled = False + self.high_value_fudai_threshold_yuan = 500.0 + self.high_value_fudai_join_ahead_seconds = 100 + self.high_value_fudai_max_swipe_back = 35 + self.high_value_fudai_max_reservations = 3 + self.high_value_fudai_after_action = "continue" + self.high_value_fudai_after_stay_seconds = 120 + self.high_value_fudai_poll_before_seconds = 10.0 + + # 高价值直播间"回访"相关 + self.high_value_room_revisit_enabled = False + self.high_value_room_revisit_only_current_run = False + self.high_value_room_revisit_min_interval_seconds = 300 + self.high_value_room_revisit_max_per_run = 5 + self.high_value_room_revisit_min_prize_value = 0.0 + self.high_value_room_revisit_max_age_days = 30 + + # 粉丝团相关配置 + self.auto_join_fansclub = False + self.fansclub_fail_action = "stay" + + # 预约回退相关 + self.reservation_back_verify_enabled = True + self.reservation_back_swipe_delay = 0.6 + + # 滑动次数重置相关 + self.reset_after_swipes_enabled = False + self.reset_after_swipes = 10 + + # 意外返回桌面自救相关(坐标兜底,主路径已改用 launch_package 包名启动) + self.desktop_icon_x = 153 + self.desktop_icon_y = 260 + self.yanghao_fallback_live_x = 540 + self.yanghao_fallback_live_y = 2000 + self.yanghao_fallback_enter_live_x = 540 + self.yanghao_fallback_enter_live_y = 1200 + + # 推荐页养号默认配置 + _apply_yanghao_defaults(self) + + # 预约运行时状态(单次运行内有效) + self.reservations = [] + self._first_run_once_basic = True + self.room_pos = 0 + self.reservation_focus_until_ts = 0.0 + self.reservation_after_action_pending = False + self.reset_to_list_after_reservations = False + self.queue_full_waiting_at_earliest = False + + # 预约队列状态文件 + 清理映射表导出文件 + _resolve_reservations_state_path(self) + _cleanup_old_room_pos_map_files(self) + + # 已记录统计的福袋跟踪 + self._recorded_fudai_stats = set() + + # 运行时状态标志 + self.needswitch = None + self._waiting_for_lottery_result = False + self._current_attending_reservation_fudai = False + self._skip_stage_diagnosis_and_find_fudai = False + self._just_switched_room = False + + # 高价值房间重访相关状态 + self._last_high_value_revisit_ts = time.time() + self._current_run_revisit_count = 0 + self._current_run_high_value_rooms = [] + self._last_fudai_detail_tuple = None + self._current_fudai_future_ts = 0.0 + self._current_fudai_content = "" + self._fudai_detail_first_read = False + self._fudai_detail_consecutive_failures = 0 + self._detail_closed_by_attend = False + self._lottery_result_expected_ts = 0.0 + self._lottery_wait_start_ts = 0.0 + + +def _apply_yanghao_defaults(self: Any) -> None: + """推荐页养号相关默认配置。""" + self.yanghao_enabled = True + self.yanghao_enable_actions = True + self.yanghao_step_screenshot = False + self.yanghao_recommend_duration_seconds = 60 + self.yanghao_recommend_interval_seconds = 0 + self.yanghao_like_prob = 0.15 + self.yanghao_fav_prob = 0.05 + self.yanghao_comment_prob = 0.0 + self.yanghao_dwell_on_action_min_seconds = 5.0 + self.yanghao_dwell_on_action_max_seconds = 30.0 + self.yanghao_icon_roi_size = 180 + self.yanghao_icon_match_threshold = 0.70 + self.yanghao_like_roi_scale = 1.5 + self.yanghao_fav_roi_scale = 1.5 + self.yanghao_like_icon_match_threshold = None + self.yanghao_fav_icon_match_threshold = None + self.yanghao_like_icon_x = 0 + self.yanghao_like_icon_y = 0 + self.yanghao_fav_icon_x = 0 + self.yanghao_fav_icon_y = 0 + self.yanghao_like_roi_offset_x = 0 + self.yanghao_like_roi_offset_y = 0 + self.yanghao_fav_roi_offset_x = 0 + self.yanghao_fav_roi_offset_y = 0 + self._last_yanghao_ts = 0.0 + self.yanghao_comment_btn_x = 0 + self.yanghao_comment_btn_y = 0 + self.yanghao_comment_input_x = 0 + self.yanghao_comment_input_y = 0 + self.yanghao_comment_send_x = 0 + self.yanghao_comment_send_y = 0 + self.yanghao_comment_close_x = 0 + self.yanghao_comment_close_y = 0 + self.yanghao_comment_texts = [] + + +def _resolve_reservations_state_path(self: Any) -> None: + """通过 path_config 解析预约状态文件路径,失败时退化到相对兜底。""" + try: + from douyin.core import path_config as _pc + self._reservations_state_path = _pc.get_reservations_path() + except Exception: + # 兜底:path_config 不可用时退到相对路径,与 douyin/core/state/ 一致 + self._reservations_state_path = os.path.normpath( + os.path.join(os.path.dirname(__file__), "..", "state", "reservations.json") + ) + + # 运行时直播间标题/位置映射表(清空后存) + self._room_pos_to_title_map = {} + + +def _cleanup_old_room_pos_map_files(self: Any) -> None: + """每次启动时删除上次运行的导出文件,避免 GUI 显示过期内容。""" + try: + state_dir = os.path.dirname(self._reservations_state_path) + for filename in ("room_pos_map.json", "room_pos_map.csv", "room_pos_map.md"): + try: + p = os.path.join(state_dir, filename) + if os.path.exists(p): + os.remove(p) + except Exception: + pass + except Exception: + pass diff --git a/tests/test_runtime_path_anchors.py b/tests/test_runtime_path_anchors.py index ff0a45e..831b289 100644 --- a/tests/test_runtime_path_anchors.py +++ b/tests/test_runtime_path_anchors.py @@ -80,9 +80,17 @@ class TestDouyinFudaiPaths: assert "path_config" in src and "get_actions_log_path()" in src def test_reservations_state_path_uses_path_config(self): - """reservations.json 必须用 path_config(兜底也保留,但首选 path_config)。""" - src = _read("douyin", "core", "douyin_fudai.py") - assert "get_reservations_path()" in src + """reservations.json 必须用 path_config(兜底也保留,但首选 path_config)。 + + 路径解析逻辑已抽到 fudai_modules/init_helpers.py 的 + _resolve_reservations_state_path,所以两个文件任一处都算通过。 + """ + sources = [ + _read("douyin", "core", "douyin_fudai.py"), + _read("douyin", "core", "fudai_modules", "init_helpers.py"), + ] + combined = "\n".join(sources) + assert "get_reservations_path()" in combined def test_actions_log_no_naive_dirname_pattern(self): """actions.log 兜底分支必须用 ".." 回到项目根,不能再裸拼 'logs/actions.log'。"""