""" 配置管理模块 ============ 负责从 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 """ if config_path is None: try: import path_config default_config_path = path_config.get_config_path() except Exception: import path_config default_config_path = path_config.get_config_path() config_path = getattr(analyser, "config_path", default_config_path) # 如果 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 "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 "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_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_preempt_min_value_yuan" in config: analyser.reservation_preempt_min_value_yuan = float( config["reservation_preempt_min_value_yuan"] or 0.0 ) if "poll_before_open_seconds" in config: analyser.poll_before_open_seconds = int( config["poll_before_open_seconds"] or 10 ) 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) 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 "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"])