inti
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
"""配置管理模块"""
|
||||
|
||||
import yaml
|
||||
from pathlib import Path
|
||||
from dataclasses import dataclass
|
||||
from typing import List, Optional
|
||||
|
||||
|
||||
@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
|
||||
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', []):
|
||||
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', ''),
|
||||
email_imap_port=acc.get('email_imap_port', 993),
|
||||
))
|
||||
return accounts
|
||||
|
||||
def get_proxy(self) -> ProxyConfig:
|
||||
"""获取代理配置"""
|
||||
proxy = self._config.get('proxy', {})
|
||||
return ProxyConfig(
|
||||
enabled=proxy.get('enabled', False),
|
||||
http=proxy.get('http', ''),
|
||||
https=proxy.get('https', ''),
|
||||
)
|
||||
|
||||
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'})
|
||||
Reference in New Issue
Block a user