diff --git a/baha_blacklist/actions.py b/baha_blacklist/actions.py index 2765156..19dc310 100644 --- a/baha_blacklist/actions.py +++ b/baha_blacklist/actions.py @@ -7,8 +7,6 @@ from .main import GamerAPIExtended from .utils import base64_decode, base64_encode, write_users -cookie_path = "decoded_cookies.txt" - def cookies_to_base64( input_file: str = "cookies.txt", @@ -25,7 +23,7 @@ def cookies_to_base64( return cookies_base64 -def decode_cookies_from_base64() -> None: +def decode_cookies_from_base64(cookie_path: str) -> None: """從 GitHub 環境變數獲取 Base64 編碼的 cookie, 解碼並寫進臨時檔案""" cookies_base64 = os.getenv("COOKIES_BASE64") if not cookies_base64: @@ -54,13 +52,13 @@ def simplified_logger(loglevel: int = logging.DEBUG) -> logging.Logger: if __name__ == "__main__": - decode_cookies_from_base64() + cookie_path = "decoded_cookies.txt" + decode_cookies_from_base64(cookie_path) logger = simplified_logger() - config_loader = ConfigLoader(Config()) + defaults = Config(username=os.environ["BAHA_USERNAME"], cookie_path=cookie_path) + config_loader = ConfigLoader(defaults) config = config_loader.load_config() - config.cookie_path = cookie_path - config.username = os.environ["BAHA_USERNAME"] api = GamerAPIExtended(config) diff --git a/baha_blacklist/config.py b/baha_blacklist/config.py index 3f1fb8e..5bd028c 100644 --- a/baha_blacklist/config.py +++ b/baha_blacklist/config.py @@ -25,8 +25,13 @@ class Config: browser: str = "chrome131" def validate(self) -> None: + # 別忘了修改 actions.py + if self.username == "your user name here": + raise ValueError( + "未設定使用者帳號,請使用 -u 參數設定帳號名稱或到 config.json 修改預設值" + ) if self.min_sleep > self.max_sleep: - raise ValueError("min_sleep must not be greater than max_sleep.") + raise ValueError("min_sleep 必須大於 max_sleep.") class ConfigLoader: @@ -36,43 +41,39 @@ def __init__(self, defaults: Config): def load_config( self, json_path: str | None = None, - args: dict[str, Any] | Namespace | None = None, + args: dict[str, Any] | Namespace = {}, env_mapping: dict[str, str] | None = None, ) -> Config: - logger.info("Starting to load configuration.") + logger.info("開始載入設定") env_mapping = env_mapping or {} json_config = self.load_from_json(json_path) cli_config = self.load_from_cli(args) env_config = self.load_from_env(env_mapping) final_config = self.merge_configs(env_config, json_config, cli_config) final_config.validate() - logger.info("Configuration successfully loaded and validated.") + logger.info("設定已成功載入並驗證") return final_config def load_from_json(self, file_path: str | None) -> dict[str, Any]: if file_path and os.path.exists(file_path): - logger.debug(f"Loading configuration from JSON file: {file_path}") + logger.debug(f"開始從 JSON 文件載入設定:{file_path}") with open(file_path) as file: try: return json.load(file) except json.JSONDecodeError as e: - logger.error(f"Invalid JSON format in {file_path}: {e}") - raise ValueError(f"Invalid JSON format in {file_path}: {e}") - logger.warning("No JSON configuration file provided or file does not exist.") + logger.error(f"{file_path} 中的 JSON 格式無效:{e}") + raise ValueError(f"{file_path} 中的 JSON 格式無效:{e}") + logger.warning("未提供 JSON 設定檔或檔案不存在") return {} - def load_from_cli(self, args: dict[str, Any] | Namespace | None) -> dict[str, Any]: - if args is None: - logger.info("No CLI arguments provided.") - return {} - - logger.debug("Loading configuration from CLI arguments.") + def load_from_cli(self, args: dict[str, Any] | Namespace) -> dict[str, Any]: + logger.debug("開始從 CLI 參數載入設定") if isinstance(args, Namespace): args = vars(args) return args def load_from_env(self, env_mapping: dict[str, str]) -> dict[str, Any]: - logger.debug("Loading configuration from environment variables.") + logger.debug("開始從環境變數載入設定") return { key: os.getenv(env_var) for key, env_var in env_mapping.items() @@ -80,7 +81,7 @@ def load_from_env(self, env_mapping: dict[str, str]) -> dict[str, Any]: } def merge_configs(self, *configs: dict[str, Any]) -> Config: - logger.debug("Merging configurations.") + logger.debug("開始合併設定") merged = asdict(self.defaults) for config in configs: for key, value in config.items(): @@ -90,10 +91,10 @@ def merge_configs(self, *configs: dict[str, Any]) -> Config: merged[key] = value elif key in merged: logger.error( - f"Type mismatch for key '{key}': Expected {type(merged[key])}, got {type(value)}" + f"key '{key}' 型別錯誤: Expected {type(merged[key])}, got {type(value)}" ) raise TypeError( - f"Type mismatch for key '{key}': Expected {type(merged[key])}, got {type(value)}" + f"key '{key}' 型別錯誤: Expected {type(merged[key])}, got {type(value)}" ) - logger.debug("Configurations merged successfully.") + logger.debug("設定已成功合併") return Config(**merged) diff --git a/baha_blacklist/main.py b/baha_blacklist/main.py index 60d6168..fe74f99 100644 --- a/baha_blacklist/main.py +++ b/baha_blacklist/main.py @@ -359,9 +359,6 @@ def init_app(args: Namespace, config_name: str = "config.json") -> tuple[Config, config_loader = ConfigLoader(Config()) config = config_loader.load_config(json_path, args) - if config.username == "your user name here": - raise ValueError("帳號錯誤,請使用 -u 參數設定帳號名稱或到 config.json 修改預設值") - api = GamerAPIExtended(config) return config, api