82 lines
2.4 KiB
Python
82 lines
2.4 KiB
Python
"""配置管理模块"""
|
|
|
|
import yaml
|
|
from pathlib import Path
|
|
from dataclasses import dataclass
|
|
from typing import List, Optional
|
|
|
|
from .email_verifier import get_email_config_for_account
|
|
|
|
|
|
@dataclass
|
|
class Account:
|
|
"""账号配置"""
|
|
username: str
|
|
password: str
|
|
email: str
|
|
email_password: str
|
|
email_imap_server: str
|
|
email_imap_port: int = 993
|
|
|
|
|
|
@dataclass
|
|
class ProxyConfig:
|
|
"""代理配置"""
|
|
enabled: bool = False
|
|
api_url: str = ""
|
|
http: str = ""
|
|
https: str = ""
|
|
|
|
|
|
class Config:
|
|
"""配置管理器"""
|
|
|
|
def __init__(self, config_path: str = "config.yaml"):
|
|
self.config_path = Path(config_path)
|
|
self._config = self._load_config()
|
|
|
|
def _load_config(self) -> dict:
|
|
"""加载配置文件"""
|
|
if not self.config_path.exists():
|
|
raise FileNotFoundError(f"配置文件不存在: {self.config_path}")
|
|
|
|
with open(self.config_path, 'r', encoding='utf-8') as f:
|
|
return yaml.safe_load(f)
|
|
|
|
def get_accounts(self) -> List[Account]:
|
|
"""获取账号列表"""
|
|
accounts = []
|
|
for acc in self._config.get('accounts', []):
|
|
email_config = get_email_config_for_account(acc['email'])
|
|
accounts.append(Account(
|
|
username=acc['username'],
|
|
password=acc['password'],
|
|
email=acc['email'],
|
|
email_password=acc['email_password'],
|
|
email_imap_server=acc.get('email_imap_server') or email_config['server'],
|
|
email_imap_port=acc.get('email_imap_port') or email_config['port'],
|
|
))
|
|
return accounts
|
|
|
|
def get_proxy(self) -> ProxyConfig:
|
|
"""获取代理配置"""
|
|
proxy = self._config.get('proxy', {})
|
|
return ProxyConfig(
|
|
enabled=proxy.get('enabled', False),
|
|
api_url=proxy.get('api_url', ''),
|
|
http=proxy.get('http', ''),
|
|
https=proxy.get('https', ''),
|
|
)
|
|
|
|
def get_geetest_config(self) -> dict:
|
|
"""获取极验配置"""
|
|
return self._config.get('geetest', {'max_retries': 5})
|
|
|
|
def get_cookie_dir(self) -> str:
|
|
"""获取Cookie存储目录"""
|
|
return self._config.get('cookie_dir', 'data/cookies')
|
|
|
|
def get_log_config(self) -> dict:
|
|
"""获取日志配置"""
|
|
return self._config.get('log', {'level': 'INFO', 'file': 'logs/douyu_login.log'})
|