""" 配置管理模块 ============ 负责从 config.json 加载配置并设置到 analyser 对象 """ from __future__ import annotations import json import os from typing import Any, Optional class ConfigManager: """配置管理器类""" @staticmethod # type: ignore[misc] def load_config_from_file(analyser: Any, config_path: Optional[str] = None) -> None: """从配置文件加载配置并设置到 analyser 对象 参数: analyser: fudai_analyse 实例 config_path: 配置文件路径,如果为 None 则使用 analyser.config_path """ passed_in_config_path = config_path is not None if config_path is None: try: from douyin.core import path_config default_config_path = path_config.get_config_path() except Exception: from douyin.core import path_config default_config_path = path_config.get_config_path() config_path = getattr(analyser, "config_path", default_config_path) # 兼容旧版把 config_path 指到项目根目录的 `config.json` 的情况: # 本仓库真实配置文件为 `config/config.json`(见 path_config.get_config_path)。 # 若 analyser.config_path 指向根目录 config.json,则自动切换到 config/config.json。 try: preferred = default_config_path preferred_abs = os.path.abspath(preferred) cp_abs = os.path.abspath(str(config_path)) root_abs = os.path.abspath(path_config.get_project_root()) legacy_abs = os.path.join(root_abs, "config.json") if os.path.normcase(cp_abs) == os.path.normcase( legacy_abs ) and os.path.exists(preferred_abs): config_path = preferred_abs except Exception: pass # 如果调用方显式传入 config_path,则尊重其选择(不做自动迁移) if passed_in_config_path: pass # 如果 config_path 仍然为 None,直接返回(使用默认值) if config_path is None: return # 如果配置文件不存在,直接返回(使用默认值) if not os.path.exists(config_path): return try: with open(config_path, "r", encoding="utf-8") as f: config = json.load(f) except (json.JSONDecodeError, IOError, OSError): # 如果 JSON 解析失败或文件读取失败,直接返回(使用默认值) return # 保存 needswitch 的原始值(如果已通过命令行设置,则保留) original_needswitch = getattr(analyser, "needswitch", None) # 加载基本配置 if "min_prize_value_yuan" in config: analyser.min_prize_value_yuan = float(config["min_prize_value_yuan"] or 0.0) if "min_participants" in config: analyser.min_participants = int(config["min_participants"] or 0) if "max_participants" in config: analyser.max_participants = int(config["max_participants"] or 0) if "skip_if_countdown_gt_seconds" in config: analyser.skip_if_countdown_gt_seconds = int( config["skip_if_countdown_gt_seconds"] or 900 ) if "skip_if_countdown_lt_seconds" in config: analyser.skip_if_countdown_lt_seconds = int( config["skip_if_countdown_lt_seconds"] or 0 ) if "fudai_icon_max_wait_seconds" in config: analyser.fudai_icon_max_wait_seconds = float( config["fudai_icon_max_wait_seconds"] or 5.0 ) if "fudai_icon_retry_delay_seconds" in config: analyser.fudai_icon_retry_delay_seconds = float( config["fudai_icon_retry_delay_seconds"] or 0.6 ) if "fudai_exit_after_misses" in config: analyser.fudai_exit_after_misses = int( config["fudai_exit_after_misses"] or 999999 ) if "enable_back_on_misses" in config: analyser.enable_back_on_misses = bool(config["enable_back_on_misses"]) # 高价值福袋配置 if "high_value_fudai_enabled" in config: analyser.high_value_fudai_enabled = bool(config["high_value_fudai_enabled"]) if "high_value_fudai_threshold_yuan" in config: analyser.high_value_fudai_threshold_yuan = float( config["high_value_fudai_threshold_yuan"] or 500.0 ) if "high_value_fudai_max_reservations" in config: max_reservations = int(config["high_value_fudai_max_reservations"] or 3) # 限制最大预约数量为50 analyser.high_value_fudai_max_reservations = min(max_reservations, 50) # needswitch 配置(如果已通过命令行设置,则保留原值) if original_needswitch is not None: # 保留命令行参数值(优先级最高) analyser.needswitch = original_needswitch elif "needswitch" in config: # 从配置文件读取 analyser.needswitch = bool(config["needswitch"]) else: # 如果配置文件中没有,使用默认值 True analyser.needswitch = True # 列表配置 if "contains_not_want" in config: analyser.contains_not_want = list(config["contains_not_want"] or []) if "contains_want" in config: analyser.contains_want = list(config["contains_want"] or []) # 阈值配置(限制在 0.0~1.0 范围内) if "lucky_draw_red_ratio_threshold" in config: threshold = float(config["lucky_draw_red_ratio_threshold"] or 0.9) analyser.lucky_draw_red_ratio_threshold = max(0.0, min(1.0, threshold)) if "coupon_template_match_threshold" in config: threshold = float(config["coupon_template_match_threshold"] or 0.75) analyser.coupon_template_match_threshold = max(0.0, min(1.0, threshold)) if "attend_button_red_ratio_threshold" in config: threshold = float(config["attend_button_red_ratio_threshold"] or 0.25) analyser.attend_button_red_ratio_threshold = max(0.0, min(1.0, threshold)) if "fansclub_join_red_ratio_threshold" in config: threshold = float(config["fansclub_join_red_ratio_threshold"] or 0.25) analyser.fansclub_join_red_ratio_threshold = max(0.0, min(1.0, threshold)) if "shopping_redpacket_red_ratio_threshold" in config: threshold = float(config["shopping_redpacket_red_ratio_threshold"] or 0.15) analyser.shopping_redpacket_red_ratio_threshold = max( 0.0, min(1.0, threshold) ) else: analyser.shopping_redpacket_red_ratio_threshold = getattr( analyser, "shopping_redpacket_red_ratio_threshold", 0.15 ) if "shopping_redpacket_ocr_confidence_threshold" in config: threshold = float( config["shopping_redpacket_ocr_confidence_threshold"] or 0.35 ) analyser.shopping_redpacket_ocr_confidence_threshold = max( 0.0, min(1.0, threshold) ) else: analyser.shopping_redpacket_ocr_confidence_threshold = getattr( analyser, "shopping_redpacket_ocr_confidence_threshold", 0.35 ) # 粉丝团等待/点击延迟配置 if "fansclub_click_delay" in config: analyser.fansclub_click_delay = float(config["fansclub_click_delay"] or 0.6) else: analyser.fansclub_click_delay = 0.6 if "fansclub_auto_join_wait_seconds" in config: analyser.fansclub_auto_join_wait_seconds = float( config["fansclub_auto_join_wait_seconds"] or 5.0 ) else: analyser.fansclub_auto_join_wait_seconds = 5.0 if "fansclub_manual_wait_seconds" in config: analyser.fansclub_manual_wait_seconds = float( config["fansclub_manual_wait_seconds"] or 3.0 ) else: analyser.fansclub_manual_wait_seconds = 3.0 # 其他配置项(根据实际需要添加) if "no_attend_timeout_minutes" in config: analyser._no_attend_timeout_minutes = float( config["no_attend_timeout_minutes"] or 30.0 ) if "auto_join_fansclub" in config: analyser.auto_join_fansclub = bool(config["auto_join_fansclub"]) if "fansclub_fail_action" in config: analyser.fansclub_fail_action = str( config["fansclub_fail_action"] or "stay" ) if "high_value_fudai_join_ahead_seconds" in config: analyser.high_value_fudai_join_ahead_seconds = int( config["high_value_fudai_join_ahead_seconds"] or 120 ) if "high_value_fudai_max_swipe_back" in config: analyser.high_value_fudai_max_swipe_back = int( config["high_value_fudai_max_swipe_back"] or 35 ) if "high_value_fudai_after_action" in config: analyser.high_value_fudai_after_action = str( config["high_value_fudai_after_action"] or "continue" ) if "high_value_fudai_poll_before_seconds" in config: analyser.high_value_fudai_poll_before_seconds = float( config["high_value_fudai_poll_before_seconds"] or 10.0 ) if "high_value_fudai_after_stay_seconds" in config: analyser.high_value_fudai_after_stay_seconds = int( config["high_value_fudai_after_stay_seconds"] or 120 ) if "reservation_back_verify_enabled" in config: analyser.reservation_back_verify_enabled = bool( config["reservation_back_verify_enabled"] ) if "record_room_title_mapping_enabled" in config: analyser.record_room_title_mapping_enabled = bool( config["record_room_title_mapping_enabled"] ) if "reservation_back_swipe_delay" in config: analyser.reservation_back_swipe_delay = float( config["reservation_back_swipe_delay"] or 0.6 ) if "reservation_back_total_timeout_seconds" in config: analyser.reservation_back_total_timeout_seconds = float( config["reservation_back_total_timeout_seconds"] or 20.0 ) if "reset_after_swipes_enabled" in config: analyser.reset_after_swipes_enabled = bool( config["reset_after_swipes_enabled"] ) if "reset_after_swipes" in config: analyser.reset_after_swipes = int(config["reset_after_swipes"] or 10) # 回退方式配置:'swipe'(默认)或 'back' if "reset_after_swipes_action" in config: try: analyser.reset_after_swipes_action = str( config["reset_after_swipes_action"] or "swipe" ).lower() except Exception: analyser.reset_after_swipes_action = "swipe" if analyser.reset_after_swipes_action not in ("swipe", "back"): analyser.reset_after_swipes_action = "swipe" # 启用/禁用:达到连续未找到福袋阈值时按返回键回退 if "enable_back_on_misses" in config: try: analyser.enable_back_on_misses = bool(config["enable_back_on_misses"]) except Exception: analyser.enable_back_on_misses = True # 新增:下滑达到阈值后按返回并重新进入直播间的配置 if "swipe_down_back_enabled" in config: analyser.swipe_down_back_enabled = bool(config["swipe_down_back_enabled"]) if "swipe_down_back_count" in config: try: analyser.swipe_down_back_count = max( 1, int(config["swipe_down_back_count"] or 3) ) except Exception: analyser.swipe_down_back_count = 3 if "fudai_click_wait_seconds" in config: analyser.fudai_click_wait_seconds = float( config["fudai_click_wait_seconds"] or 1.0 ) if "switch_room_wait_after_stage_diag_seconds" in config: analyser.switch_room_wait_after_stage_diag_seconds = float( config["switch_room_wait_after_stage_diag_seconds"] or 7.0 ) if "swipe_duration_ms" in config: analyser.swipe_duration_ms = int(config["swipe_duration_ms"] or 600) if "swipe_up_start_y" in config: analyser.swipe_up_start_y = int(config["swipe_up_start_y"] or 1200) if "swipe_up_end_y" in config: analyser.swipe_up_end_y = int(config["swipe_up_end_y"] or 600) if "fudai_finder_optimized_enabled" in config: analyser.fudai_finder_optimized_enabled = bool( config["fudai_finder_optimized_enabled"] ) if "fudai_click_verify_enabled" in config: analyser.fudai_click_verify_enabled = bool( config["fudai_click_verify_enabled"] ) if "fudai_find_max_retries" in config: analyser.fudai_find_max_retries = int(config["fudai_find_max_retries"] or 3) if "screenshot_diagnose_min_interval_seconds" in config: analyser.screenshot_diagnose_min_interval_seconds = float( config["screenshot_diagnose_min_interval_seconds"] or 8.0 ) if "live_room_match_threshold" in config: threshold = float(config["live_room_match_threshold"] or 0.8) analyser.live_room_match_threshold = max(0.0, min(1.0, threshold)) if "fudai_template_match_enabled" in config: analyser.fudai_template_match_enabled = bool( config["fudai_template_match_enabled"] ) if "fudai_template_match_threshold" in config: threshold = float(config["fudai_template_match_threshold"] or 0.7) analyser.fudai_template_match_threshold = max(0.0, min(1.0, threshold)) if "y_pianyi" in config: analyser.y_pianyi = int(config["y_pianyi"] or 0) if "enable_whole_detail_ocr" in config: analyser.enable_whole_detail_ocr = bool(config["enable_whole_detail_ocr"]) # ===== 推荐页养号相关配置 ===== if "yanghao_enabled" in config: analyser.yanghao_enabled = bool(config["yanghao_enabled"]) if "yanghao_enable_actions" in config: analyser.yanghao_enable_actions = bool(config["yanghao_enable_actions"]) if "yanghao_step_screenshot" in config: analyser.yanghao_step_screenshot = bool(config["yanghao_step_screenshot"]) if "yanghao_recommend_duration_seconds" in config: analyser.yanghao_recommend_duration_seconds = int( config["yanghao_recommend_duration_seconds"] or 60 ) if "yanghao_recommend_interval_seconds" in config: analyser.yanghao_recommend_interval_seconds = int( config["yanghao_recommend_interval_seconds"] or 0 ) if "yanghao_like_prob" in config: analyser.yanghao_like_prob = float(config["yanghao_like_prob"] or 0.0) if "yanghao_fav_prob" in config: analyser.yanghao_fav_prob = float(config["yanghao_fav_prob"] or 0.0) if "yanghao_comment_prob" in config: analyser.yanghao_comment_prob = float(config["yanghao_comment_prob"] or 0.0) if "yanghao_dwell_on_action_min_seconds" in config: analyser.yanghao_dwell_on_action_min_seconds = float( config["yanghao_dwell_on_action_min_seconds"] or 5 ) if "yanghao_dwell_on_action_max_seconds" in config: analyser.yanghao_dwell_on_action_max_seconds = float( config["yanghao_dwell_on_action_max_seconds"] or 30 ) # 模板路径(点赞/收藏) if "yanghao_like_template_path" in config: try: analyser.yanghao_like_template_path = str( config["yanghao_like_template_path"] or "" ) except Exception: pass if "yanghao_fav_template_path" in config: try: analyser.yanghao_fav_template_path = str( config["yanghao_fav_template_path"] or "" ) except Exception: pass # icon 坐标/偏移/模板路径 if "yanghao_like_icon_x" in config: analyser.yanghao_like_icon_x = int(config["yanghao_like_icon_x"] or 0) if "yanghao_like_icon_y" in config: analyser.yanghao_like_icon_y = int(config["yanghao_like_icon_y"] or 0) if "yanghao_fav_icon_x" in config: analyser.yanghao_fav_icon_x = int(config["yanghao_fav_icon_x"] or 0) if "yanghao_fav_icon_y" in config: analyser.yanghao_fav_icon_y = int(config["yanghao_fav_icon_y"] or 0) if "yanghao_like_roi_offset_x" in config: analyser.yanghao_like_roi_offset_x = int( config["yanghao_like_roi_offset_x"] or 0 ) if "yanghao_like_roi_offset_y" in config: analyser.yanghao_like_roi_offset_y = int( config["yanghao_like_roi_offset_y"] or 0 ) if "yanghao_fav_roi_offset_x" in config: analyser.yanghao_fav_roi_offset_x = int( config["yanghao_fav_roi_offset_x"] or 0 ) if "yanghao_fav_roi_offset_y" in config: analyser.yanghao_fav_roi_offset_y = int( config["yanghao_fav_roi_offset_y"] or 0 ) if "yanghao_icon_roi_size" in config: analyser.yanghao_icon_roi_size = int(config["yanghao_icon_roi_size"] or 180) if "yanghao_icon_match_threshold" in config: analyser.yanghao_icon_match_threshold = float( config["yanghao_icon_match_threshold"] or 0.72 ) # per-action ROI scale multipliers (用于扩大检测范围) if "yanghao_like_roi_scale" in config: try: analyser.yanghao_like_roi_scale = float( config["yanghao_like_roi_scale"] or 1.5 ) except Exception: analyser.yanghao_like_roi_scale = 1.5 else: analyser.yanghao_like_roi_scale = getattr( analyser, "yanghao_like_roi_scale", 1.5 ) if "yanghao_fav_roi_scale" in config: try: analyser.yanghao_fav_roi_scale = float( config["yanghao_fav_roi_scale"] or 1.5 ) except Exception: analyser.yanghao_fav_roi_scale = 1.5 else: analyser.yanghao_fav_roi_scale = getattr( analyser, "yanghao_fav_roi_scale", 1.5 ) # per-action match thresholds (可在 config 中单独设置) if "yanghao_like_icon_match_threshold" in config: try: analyser.yanghao_like_icon_match_threshold = float( config["yanghao_like_icon_match_threshold"] ) except Exception: analyser.yanghao_like_icon_match_threshold = None else: analyser.yanghao_like_icon_match_threshold = getattr( analyser, "yanghao_like_icon_match_threshold", None ) if "yanghao_fav_icon_match_threshold" in config: try: analyser.yanghao_fav_icon_match_threshold = float( config["yanghao_fav_icon_match_threshold"] ) except Exception: analyser.yanghao_fav_icon_match_threshold = None else: analyser.yanghao_fav_icon_match_threshold = getattr( analyser, "yanghao_fav_icon_match_threshold", None ) # per-action ROI sizes if "yanghao_like_icon_roi_size" in config: try: analyser.yanghao_like_icon_roi_size = int( config["yanghao_like_icon_roi_size"] or analyser.yanghao_icon_roi_size ) except Exception: analyser.yanghao_like_icon_roi_size = analyser.yanghao_icon_roi_size if "yanghao_fav_icon_roi_size" in config: try: analyser.yanghao_fav_icon_roi_size = int( config["yanghao_fav_icon_roi_size"] or analyser.yanghao_icon_roi_size ) except Exception: analyser.yanghao_fav_icon_roi_size = analyser.yanghao_icon_roi_size # 支持矩形(x1,y1,x2,y2)或多边形坐标(yanghao_*_poly)配置 if "yanghao_like_rect_x1" in config: try: analyser.yanghao_like_rect_x1 = int(config["yanghao_like_rect_x1"] or 0) except Exception: analyser.yanghao_like_rect_x1 = None if "yanghao_like_rect_y1" in config: try: analyser.yanghao_like_rect_y1 = int(config["yanghao_like_rect_y1"] or 0) except Exception: analyser.yanghao_like_rect_y1 = None if "yanghao_like_rect_x2" in config: try: analyser.yanghao_like_rect_x2 = int(config["yanghao_like_rect_x2"] or 0) except Exception: analyser.yanghao_like_rect_x2 = None if "yanghao_like_rect_y2" in config: try: analyser.yanghao_like_rect_y2 = int(config["yanghao_like_rect_y2"] or 0) except Exception: analyser.yanghao_like_rect_y2 = None if "yanghao_fav_rect_x1" in config: try: analyser.yanghao_fav_rect_x1 = int(config["yanghao_fav_rect_x1"] or 0) except Exception: analyser.yanghao_fav_rect_x1 = None if "yanghao_fav_rect_y1" in config: try: analyser.yanghao_fav_rect_y1 = int(config["yanghao_fav_rect_y1"] or 0) except Exception: analyser.yanghao_fav_rect_y1 = None if "yanghao_fav_rect_x2" in config: try: analyser.yanghao_fav_rect_x2 = int(config["yanghao_fav_rect_x2"] or 0) except Exception: analyser.yanghao_fav_rect_x2 = None if "yanghao_fav_rect_y2" in config: try: analyser.yanghao_fav_rect_y2 = int(config["yanghao_fav_rect_y2"] or 0) except Exception: analyser.yanghao_fav_rect_y2 = None if "yanghao_like_poly" in config: try: analyser.yanghao_like_poly = list(config["yanghao_like_poly"] or []) except Exception: analyser.yanghao_like_poly = None if "yanghao_fav_poly" in config: try: analyser.yanghao_fav_poly = list(config["yanghao_fav_poly"] or []) except Exception: analyser.yanghao_fav_poly = None # 评论相关坐标与文案 if "yanghao_comment_btn_x" in config: analyser.yanghao_comment_btn_x = int(config["yanghao_comment_btn_x"] or 0) if "yanghao_comment_btn_y" in config: analyser.yanghao_comment_btn_y = int(config["yanghao_comment_btn_y"] or 0) if "yanghao_comment_input_x" in config: analyser.yanghao_comment_input_x = int( config["yanghao_comment_input_x"] or 0 ) if "yanghao_comment_input_y" in config: analyser.yanghao_comment_input_y = int( config["yanghao_comment_input_y"] or 0 ) if "yanghao_comment_send_x" in config: analyser.yanghao_comment_send_x = int(config["yanghao_comment_send_x"] or 0) if "yanghao_comment_send_y" in config: analyser.yanghao_comment_send_y = int(config["yanghao_comment_send_y"] or 0) if "yanghao_comment_close_x" in config: analyser.yanghao_comment_close_x = int( config["yanghao_comment_close_x"] or 0 ) if "yanghao_comment_close_y" in config: analyser.yanghao_comment_close_y = int( config["yanghao_comment_close_y"] or 0 ) if "yanghao_comment_texts" in config: # 支持配置为字符串数组或以换行分隔的长文本 raw = config.get("yanghao_comment_texts") or [] if isinstance(raw, list): analyser.yanghao_comment_texts = [str(x) for x in raw if str(x).strip()] else: # 按行拆分并过滤空行 analyser.yanghao_comment_texts = [ line.strip() for line in str(raw or "").splitlines() if line.strip() ] # ===== 日志配置 ===== if "logging" in config: logging_config = config["logging"] if "stage_diag_verbose" in logging_config: analyser.stage_diag_verbose = bool(logging_config["stage_diag_verbose"])