重构项目结构,优化模块划分,修复数据结构和登录验证问题
This commit is contained in:
@@ -0,0 +1,292 @@
|
||||
"""配置管理模块"""
|
||||
import json
|
||||
import os
|
||||
from logger import get_logger
|
||||
|
||||
logger = get_logger(__name__)
|
||||
|
||||
DEFAULT_CONFIG_FILE = "config.json"
|
||||
DEFAULT_COOKIE_DATA_FILE = "cookie_data.json"
|
||||
|
||||
|
||||
class ConfigManager:
|
||||
"""配置管理器"""
|
||||
|
||||
def __init__(self, config_file: str = DEFAULT_CONFIG_FILE):
|
||||
"""
|
||||
初始化配置管理器
|
||||
|
||||
Args:
|
||||
config_file: 配置文件路径
|
||||
"""
|
||||
self.config_file = config_file
|
||||
self.config = self._load_config()
|
||||
|
||||
def _load_config(self) -> dict:
|
||||
"""加载配置文件"""
|
||||
try:
|
||||
if os.path.exists(self.config_file):
|
||||
with open(self.config_file, 'r', encoding='utf-8') as f:
|
||||
config = json.load(f)
|
||||
logger.info(f"配置文件加载成功: {self.config_file}")
|
||||
return config
|
||||
else:
|
||||
logger.warning(f"配置文件不存在: {self.config_file}")
|
||||
return {}
|
||||
except Exception as e:
|
||||
logger.error(f"加载配置文件失败: {e}")
|
||||
return {}
|
||||
|
||||
def save_config(self, config: dict) -> bool:
|
||||
"""
|
||||
保存配置文件
|
||||
|
||||
Args:
|
||||
config: 配置字典
|
||||
|
||||
Returns:
|
||||
是否保存成功
|
||||
"""
|
||||
try:
|
||||
with open(self.config_file, 'w', encoding='utf-8') as f:
|
||||
json.dump(config, f, indent=2, ensure_ascii=False)
|
||||
logger.info(f"配置文件保存成功: {self.config_file}")
|
||||
return True
|
||||
except Exception as e:
|
||||
logger.error(f"保存配置文件失败: {e}")
|
||||
return False
|
||||
|
||||
def get(self, key: str, default=None):
|
||||
"""
|
||||
获取配置项
|
||||
|
||||
Args:
|
||||
key: 配置键
|
||||
default: 默认值
|
||||
|
||||
Returns:
|
||||
配置值
|
||||
"""
|
||||
return self.config.get(key, default)
|
||||
|
||||
def set(self, key: str, value):
|
||||
"""
|
||||
设置配置项
|
||||
|
||||
Args:
|
||||
key: 配置键
|
||||
value: 配置值
|
||||
"""
|
||||
self.config[key] = value
|
||||
self.save_config(self.config)
|
||||
|
||||
def get_users(self) -> list:
|
||||
"""
|
||||
获取所有用户配置
|
||||
|
||||
Returns:
|
||||
用户配置列表
|
||||
"""
|
||||
return self.config.get('users', [])
|
||||
|
||||
def get_user_config(self, user_name: str) -> dict:
|
||||
"""
|
||||
获取指定用户的配置
|
||||
|
||||
Args:
|
||||
user_name: 用户名
|
||||
|
||||
Returns:
|
||||
用户配置字典
|
||||
"""
|
||||
users = self.get_users()
|
||||
for user in users:
|
||||
if user.get('name') == user_name:
|
||||
return user
|
||||
return {}
|
||||
|
||||
def get_cookie_cloud_config(self, user_name: str) -> dict:
|
||||
"""
|
||||
获取用户的Cookie Cloud配置
|
||||
|
||||
Args:
|
||||
user_name: 用户名
|
||||
|
||||
Returns:
|
||||
Cookie Cloud配置字典
|
||||
"""
|
||||
user_config = self.get_user_config(user_name)
|
||||
return user_config.get('cookie_cloud', {})
|
||||
|
||||
def get_notification_config(self, user_name: str) -> dict:
|
||||
"""
|
||||
获取用户的通知配置
|
||||
|
||||
Args:
|
||||
user_name: 用户名
|
||||
|
||||
Returns:
|
||||
通知配置字典
|
||||
"""
|
||||
user_config = self.get_user_config(user_name)
|
||||
return user_config.get('notification', {})
|
||||
|
||||
def get_browser_config(self, user_name: str) -> dict:
|
||||
"""
|
||||
获取用户的浏览器配置
|
||||
|
||||
Args:
|
||||
user_name: 用户名
|
||||
|
||||
Returns:
|
||||
浏览器配置字典
|
||||
"""
|
||||
user_config = self.get_user_config(user_name)
|
||||
return user_config.get('browser', {})
|
||||
|
||||
def get_websites(self, user_name: str) -> list:
|
||||
"""
|
||||
获取用户的网站配置
|
||||
|
||||
Args:
|
||||
user_name: 用户名
|
||||
|
||||
Returns:
|
||||
网站配置列表
|
||||
"""
|
||||
user_config = self.get_user_config(user_name)
|
||||
return user_config.get('websites', [])
|
||||
|
||||
|
||||
class CookieDataManager:
|
||||
"""Cookie数据管理器"""
|
||||
|
||||
def __init__(self, data_file: str = DEFAULT_COOKIE_DATA_FILE):
|
||||
"""
|
||||
初始化Cookie数据管理器
|
||||
|
||||
Args:
|
||||
data_file: Cookie数据文件路径
|
||||
"""
|
||||
self.data_file = data_file
|
||||
self.data = self._load_data()
|
||||
|
||||
def _load_data(self) -> dict:
|
||||
"""加载Cookie数据文件"""
|
||||
try:
|
||||
if os.path.exists(self.data_file):
|
||||
with open(self.data_file, 'r', encoding='utf-8') as f:
|
||||
data = json.load(f)
|
||||
logger.info(f"Cookie数据文件加载成功: {self.data_file}")
|
||||
return data
|
||||
else:
|
||||
logger.warning(f"Cookie数据文件不存在: {self.data_file}")
|
||||
return {}
|
||||
except Exception as e:
|
||||
logger.error(f"加载Cookie数据文件失败: {e}")
|
||||
return {}
|
||||
|
||||
def save_data(self, data: dict) -> bool:
|
||||
"""
|
||||
保存Cookie数据文件
|
||||
|
||||
Args:
|
||||
data: Cookie数据字典
|
||||
|
||||
Returns:
|
||||
是否保存成功
|
||||
"""
|
||||
try:
|
||||
with open(self.data_file, 'w', encoding='utf-8') as f:
|
||||
json.dump(data, f, indent=2, ensure_ascii=False)
|
||||
logger.info(f"Cookie数据文件保存成功: {self.data_file}")
|
||||
return True
|
||||
except Exception as e:
|
||||
logger.error(f"保存Cookie数据文件失败: {e}")
|
||||
return False
|
||||
|
||||
def get_user_cookies(self, user_name: str) -> dict:
|
||||
"""
|
||||
获取用户的Cookie数据
|
||||
|
||||
Args:
|
||||
user_name: 用户名
|
||||
|
||||
Returns:
|
||||
用户Cookie数据字典
|
||||
"""
|
||||
return self.data.get(user_name, {})
|
||||
|
||||
def save_user_cookies(self, user_name: str, cookies: list, url: str, website_name: str = ""):
|
||||
"""
|
||||
保存用户的Cookie数据
|
||||
|
||||
Args:
|
||||
user_name: 用户名
|
||||
cookies: Cookie列表
|
||||
url: 网站URL
|
||||
website_name: 网站名称
|
||||
"""
|
||||
if user_name not in self.data:
|
||||
self.data[user_name] = {}
|
||||
|
||||
from urllib.parse import urlparse
|
||||
parsed = urlparse(url)
|
||||
domain = parsed.netloc
|
||||
|
||||
cookie_entry = {
|
||||
"cookies": cookies,
|
||||
"url": url,
|
||||
"website_name": website_name
|
||||
}
|
||||
|
||||
self.data[user_name][domain] = cookie_entry
|
||||
self.save_data(self.data)
|
||||
logger.info(f"已保存 {len(cookies)} 个 cookie ({user_name} -> {domain})")
|
||||
|
||||
def get_domain_cookies(self, user_name: str, domain: str) -> dict:
|
||||
"""
|
||||
获取指定域名的Cookie数据
|
||||
|
||||
Args:
|
||||
user_name: 用户名
|
||||
domain: 域名
|
||||
|
||||
Returns:
|
||||
域名Cookie数据字典
|
||||
"""
|
||||
user_cookies = self.get_user_cookies(user_name)
|
||||
|
||||
for cookie_domain, cookie_data in user_cookies.items():
|
||||
clean_domain = cookie_domain.lstrip('.')
|
||||
if domain in clean_domain or clean_domain in domain:
|
||||
return cookie_data
|
||||
|
||||
return {}
|
||||
|
||||
def save_simple_cookies(self, cookies: list, url: str):
|
||||
"""
|
||||
保存简单的Cookie数据(用于Cookie提取器)
|
||||
|
||||
Args:
|
||||
cookies: Cookie列表
|
||||
url: 网站URL
|
||||
"""
|
||||
data = {
|
||||
"cookies": cookies,
|
||||
"url": url
|
||||
}
|
||||
self.save_data(data)
|
||||
logger.info(f"已保存 {len(cookies)} 个 cookie")
|
||||
|
||||
def load_simple_cookies(self) -> dict:
|
||||
"""
|
||||
加载简单的Cookie数据(用于Cookie提取器)
|
||||
|
||||
Returns:
|
||||
Cookie数据字典
|
||||
"""
|
||||
return {
|
||||
"cookies": self.data.get("cookies", []),
|
||||
"url": self.data.get("url", "")
|
||||
}
|
||||
Reference in New Issue
Block a user