inti
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
"""工具模块"""
|
||||
|
||||
from .logger import setup_logger
|
||||
from .helpers import load_accounts, save_cookie
|
||||
|
||||
__all__ = ["setup_logger", "load_accounts", "save_cookie"]
|
||||
@@ -0,0 +1,95 @@
|
||||
"""辅助工具函数"""
|
||||
|
||||
import json
|
||||
from pathlib import Path
|
||||
from typing import List
|
||||
from loguru import logger
|
||||
|
||||
from douyu.config import Account
|
||||
|
||||
|
||||
def load_accounts(config_path: str = "config.yaml") -> List[Account]:
|
||||
"""加载账号列表"""
|
||||
from douyu.config import Config
|
||||
|
||||
config = Config(config_path)
|
||||
accounts = config.get_accounts()
|
||||
|
||||
logger.info(f"加载了 {len(accounts)} 个账号")
|
||||
return accounts
|
||||
|
||||
|
||||
def save_cookie(username: str, cookie: str, cookie_dir: str = "data/cookies") -> str:
|
||||
"""
|
||||
保存Cookie到文件
|
||||
|
||||
Args:
|
||||
username: 用户名
|
||||
cookie: Cookie字符串
|
||||
cookie_dir: Cookie存储目录
|
||||
|
||||
Returns:
|
||||
文件路径
|
||||
"""
|
||||
import time
|
||||
|
||||
cookie_path = Path(cookie_dir)
|
||||
cookie_path.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
filepath = cookie_path / f"{username}.json"
|
||||
|
||||
data = {
|
||||
'username': username,
|
||||
'cookie': cookie,
|
||||
'timestamp': int(time.time()),
|
||||
}
|
||||
|
||||
with open(filepath, 'w', encoding='utf-8') as f:
|
||||
json.dump(data, f, ensure_ascii=False, indent=2)
|
||||
|
||||
logger.info(f"Cookie已保存: {filepath}")
|
||||
return str(filepath)
|
||||
|
||||
|
||||
def load_cookie(username: str, cookie_dir: str = "data/cookies") -> str:
|
||||
"""
|
||||
从文件加载Cookie
|
||||
|
||||
Args:
|
||||
username: 用户名
|
||||
cookie_dir: Cookie存储目录
|
||||
|
||||
Returns:
|
||||
Cookie字符串,不存在返回空字符串
|
||||
"""
|
||||
filepath = Path(cookie_dir) / f"{username}.json"
|
||||
|
||||
if not filepath.exists():
|
||||
return ""
|
||||
|
||||
try:
|
||||
with open(filepath, 'r', encoding='utf-8') as f:
|
||||
data = json.load(f)
|
||||
return data.get('cookie', '')
|
||||
except Exception as e:
|
||||
logger.error(f"加载Cookie失败: {e}")
|
||||
return ""
|
||||
|
||||
|
||||
def format_cookie_for_browser(cookie_str: str) -> dict:
|
||||
"""
|
||||
将Cookie字符串转换为浏览器格式
|
||||
|
||||
Args:
|
||||
cookie_str: Cookie字符串
|
||||
|
||||
Returns:
|
||||
Cookie字典
|
||||
"""
|
||||
cookies = {}
|
||||
for item in cookie_str.split(';'):
|
||||
item = item.strip()
|
||||
if '=' in item:
|
||||
key, value = item.split('=', 1)
|
||||
cookies[key.strip()] = value.strip()
|
||||
return cookies
|
||||
@@ -0,0 +1,42 @@
|
||||
"""日志配置模块"""
|
||||
|
||||
import sys
|
||||
from pathlib import Path
|
||||
from loguru import logger
|
||||
|
||||
|
||||
def setup_logger(level: str = "INFO", log_file: str = None) -> None:
|
||||
"""
|
||||
配置日志
|
||||
|
||||
Args:
|
||||
level: 日志级别
|
||||
log_file: 日志文件路径
|
||||
"""
|
||||
# 移除默认handler
|
||||
logger.remove()
|
||||
|
||||
# 控制台输出
|
||||
logger.add(
|
||||
sys.stdout,
|
||||
level=level,
|
||||
format="<green>{time:YYYY-MM-DD HH:mm:ss}</green> | "
|
||||
"<level>{level: <8}</level> | "
|
||||
"<cyan>{name}</cyan>:<cyan>{function}</cyan>:<cyan>{line}</cyan> | "
|
||||
"<level>{message}</level>",
|
||||
colorize=True,
|
||||
)
|
||||
|
||||
# 文件输出
|
||||
if log_file:
|
||||
log_path = Path(log_file)
|
||||
log_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
logger.add(
|
||||
log_file,
|
||||
level=level,
|
||||
format="{time:YYYY-MM-DD HH:mm:ss} | {level: <8} | {name}:{function}:{line} | {message}",
|
||||
rotation="10 MB",
|
||||
retention="7 days",
|
||||
encoding="utf-8",
|
||||
)
|
||||
Reference in New Issue
Block a user