Files
auto_cursor/core/config.py
2025-04-01 15:43:27 +08:00

125 lines
3.0 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from dataclasses import dataclass
from typing import Tuple, Optional
import yaml
@dataclass
class GlobalConfig:
max_concurrency: int
timeout: int
retry_times: int
@dataclass
class DatabaseConfig:
# SQLite的配置字段保留用于兼容
path: Optional[str] = None
pool_size: int = 10
# MySQL配置
host: str = "localhost"
port: int = 3306
username: str = "auto_cursor_reg"
password: str = "this_password_jiaqiao"
database: str = "auto_cursor_reg"
# Redis配置
use_redis: bool = False
@dataclass
class RedisConfig:
host: str
port: int
password: str = ""
db: int = 0
@dataclass
class ProxyConfig:
api_url: str
batch_size: int
check_interval: int
@dataclass
class RegisterConfig:
delay_range: Tuple[int, int]
batch_size: int
@dataclass
class EmailConfig:
file_path: str
@dataclass
class CapsolverConfig:
api_key: str
website_url: str
website_key: str
@dataclass
class YesCaptchaConfig:
client_key: str
website_url: str
website_key: str
use_cn_server: bool
@dataclass
class CaptchaConfig:
provider: str
capsolver: CapsolverConfig
yescaptcha: YesCaptchaConfig
@dataclass
class Config:
global_config: GlobalConfig
database_config: DatabaseConfig
redis_config: Optional[RedisConfig] = None
proxy_config: ProxyConfig = None
register_config: RegisterConfig = None
email_config: EmailConfig = None
captcha_config: CaptchaConfig = None
@classmethod
def from_yaml(cls, path: str = "config.yaml"):
with open(path, 'r', encoding='utf-8') as f:
data = yaml.safe_load(f)
# 创建 database 配置对象
db_config = DatabaseConfig(**data.get('database', {}))
# 创建 redis 配置对象(如果有)
redis_config = None
if 'redis' in data and db_config.use_redis:
redis_config = RedisConfig(**data['redis'])
# 创建 captcha 配置对象
captcha_data = data.get('captcha', {})
captcha_config = None
if captcha_data:
captcha_config = CaptchaConfig(
provider=captcha_data.get('provider', 'capsolver'),
capsolver=CapsolverConfig(**captcha_data.get('capsolver', {})),
yescaptcha=YesCaptchaConfig(**captcha_data.get('yescaptcha', {}))
)
# 创建其他配置对象
global_config = GlobalConfig(**data.get('global', {}))
proxy_config = ProxyConfig(**data.get('proxy', {})) if 'proxy' in data else None
register_config = RegisterConfig(**data.get('register', {})) if 'register' in data else None
email_config = EmailConfig(**data.get('email', {})) if 'email' in data else None
return cls(
global_config=global_config,
database_config=db_config,
redis_config=redis_config,
proxy_config=proxy_config,
register_config=register_config,
email_config=email_config,
captcha_config=captcha_config
)