169 lines
4.5 KiB
Python
169 lines
4.5 KiB
Python
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
|
||
api_proxy: Optional[str] = None
|
||
|
||
|
||
@dataclass
|
||
class RegisterConfig:
|
||
delay_range: Tuple[int, int]
|
||
batch_size: int
|
||
|
||
|
||
@dataclass
|
||
class EmailConfig:
|
||
file_path: str
|
||
|
||
|
||
@dataclass
|
||
class SelfHostedEmailConfig:
|
||
"""自建邮箱配置"""
|
||
api_base_url: str
|
||
api_key: Optional[str] = None
|
||
|
||
|
||
@dataclass
|
||
class ServerConfig:
|
||
hostname: str
|
||
description: Optional[str] = None
|
||
|
||
|
||
@dataclass
|
||
class AutoServiceConfig:
|
||
check_interval: int = 60
|
||
upload_interval: int = 300
|
||
email_check_threshold: int = 30
|
||
email_fetch_count: int = 2
|
||
|
||
|
||
@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
|
||
server_config: Optional[ServerConfig] = None
|
||
auto_service_config: Optional[AutoServiceConfig] = None
|
||
self_hosted_email_config: Optional[SelfHostedEmailConfig] = None
|
||
hostname: Optional[str] = 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
|
||
|
||
# 创建服务器配置对象
|
||
server_config = ServerConfig(**data.get('server_config', {})) if 'server_config' in data else None
|
||
|
||
# 创建自动服务配置对象
|
||
auto_service_config = AutoServiceConfig(**data.get('auto_service', {})) if 'auto_service' in data else None
|
||
|
||
# 创建自建邮箱配置对象
|
||
self_hosted_email_config = SelfHostedEmailConfig(**data.get('self_hosted_email', {})) if 'self_hosted_email' in data else None
|
||
|
||
# 设置hostname (优先使用server_config中的hostname)
|
||
hostname = None
|
||
if server_config and hasattr(server_config, 'hostname'):
|
||
hostname = server_config.hostname
|
||
|
||
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,
|
||
server_config=server_config,
|
||
auto_service_config=auto_service_config,
|
||
self_hosted_email_config=self_hosted_email_config,
|
||
hostname=hostname
|
||
)
|