This commit is contained in:
zhinianboke 2025-08-13 18:27:02 +08:00
parent 348aeeeaad
commit 307485df2f
2 changed files with 6 additions and 146 deletions

View File

@ -170,9 +170,6 @@ class XianyuLive:
self.token_refresh_task = None self.token_refresh_task = None
self.connection_restart_flag = False # 连接重启标志 self.connection_restart_flag = False # 连接重启标志
# 从数据库获取token信息
self._load_token_info_from_db()
# 通知防重复机制 # 通知防重复机制
self.last_notification_time = {} # 记录每种通知类型的最后发送时间 self.last_notification_time = {} # 记录每种通知类型的最后发送时间
self.notification_cooldown = 300 # 5分钟内不重复发送相同类型的通知 self.notification_cooldown = 300 # 5分钟内不重复发送相同类型的通知
@ -194,44 +191,7 @@ class XianyuLive:
# 启动定期清理过期暂停记录的任务 # 启动定期清理过期暂停记录的任务
self.cleanup_task = None self.cleanup_task = None
def _load_token_info_from_db(self):
"""从数据库加载token信息"""
try:
from db_manager import db_manager
token_info = db_manager.get_token_info(self.cookie_id)
if token_info:
self.last_token_refresh_time = token_info.get('last_token_refresh_time', 0)
self.current_token = token_info.get('current_token', None)
logger.info(f"{self.cookie_id}】从数据库加载token信息成功 - 上次刷新时间: {self.last_token_refresh_time}, token: {'已设置' if self.current_token else '未设置'}")
else:
logger.info(f"{self.cookie_id}】数据库中未找到token信息使用默认值")
self.last_token_refresh_time = 0
self.current_token = None
except Exception as e:
logger.error(f"{self.cookie_id}】从数据库加载token信息失败: {self._safe_str(e)}")
# 使用默认值
self.last_token_refresh_time = 0
self.current_token = None
def _save_token_info_to_db(self):
"""保存token信息到数据库"""
try:
from db_manager import db_manager
success = db_manager.update_token_info(
self.cookie_id,
self.last_token_refresh_time,
self.current_token
)
if success:
logger.debug(f"{self.cookie_id}】token信息已保存到数据库")
else:
logger.warning(f"{self.cookie_id}】token信息保存到数据库失败")
except Exception as e:
logger.error(f"{self.cookie_id}】保存token信息到数据库异常: {self._safe_str(e)}")
def is_auto_confirm_enabled(self) -> bool: def is_auto_confirm_enabled(self) -> bool:
"""检查当前账号是否启用自动确认发货""" """检查当前账号是否启用自动确认发货"""
@ -765,9 +725,6 @@ class XianyuLive:
self.current_token = new_token self.current_token = new_token
self.last_token_refresh_time = time.time() self.last_token_refresh_time = time.time()
# 保存token信息到数据库
self._save_token_info_to_db()
logger.info(f"{self.cookie_id}】Token刷新成功") logger.info(f"{self.cookie_id}】Token刷新成功")
return new_token return new_token
@ -797,13 +754,7 @@ class XianyuLive:
current_user_id = self.user_id current_user_id = self.user_id
# 保存cookies和token信息 # 保存cookies和token信息
db_manager.save_cookie( db_manager.save_cookie(self.cookie_id, self.cookies_str, current_user_id)
self.cookie_id,
self.cookies_str,
current_user_id,
self.last_token_refresh_time,
self.current_token
)
logger.debug(f"已更新Cookie和token信息到数据库: {self.cookie_id}") logger.debug(f"已更新Cookie和token信息到数据库: {self.cookie_id}")
except Exception as e: except Exception as e:
logger.error(f"更新数据库Cookie失败: {self._safe_str(e)}") logger.error(f"更新数据库Cookie失败: {self._safe_str(e)}")

View File

@ -115,8 +115,6 @@ class DBManager:
auto_confirm INTEGER DEFAULT 1, auto_confirm INTEGER DEFAULT 1,
remark TEXT DEFAULT '', remark TEXT DEFAULT '',
pause_duration INTEGER DEFAULT 10, pause_duration INTEGER DEFAULT 10,
last_token_refresh_time REAL DEFAULT 0,
current_token TEXT DEFAULT '',
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
) )
@ -1080,8 +1078,7 @@ class DBManager:
return cursor.executemany(sql, params_list) return cursor.executemany(sql, params_list)
# -------------------- Cookie操作 -------------------- # -------------------- Cookie操作 --------------------
def save_cookie(self, cookie_id: str, cookie_value: str, user_id: int = None, def save_cookie(self, cookie_id: str, cookie_value: str, user_id: int = None) -> bool:
last_token_refresh_time: float = None, current_token: str = None) -> bool:
"""保存Cookie到数据库如存在则更新""" """保存Cookie到数据库如存在则更新"""
with self.lock: with self.lock:
try: try:
@ -1100,36 +1097,10 @@ class DBManager:
user_id = admin_user[0] if admin_user else 1 user_id = admin_user[0] if admin_user else 1
# 如果提供了token相关信息则更新这些字段 # 如果提供了token相关信息则更新这些字段
if last_token_refresh_time is not None or current_token is not None: self._execute_sql(cursor,
# 先获取现有记录的token信息 "INSERT OR REPLACE INTO cookies (id, value, user_id) VALUES (?, ?, ?)",
self._execute_sql(cursor, (cookie_id, cookie_value, user_id)
"SELECT last_token_refresh_time, current_token FROM cookies WHERE id = ?", )
(cookie_id,))
existing_token_info = cursor.fetchone()
if existing_token_info:
# 如果没有提供新值,使用现有值
if last_token_refresh_time is None:
last_token_refresh_time = existing_token_info[0]
if current_token is None:
current_token = existing_token_info[1]
else:
# 如果没有现有记录,使用默认值
if last_token_refresh_time is None:
last_token_refresh_time = 0
if current_token is None:
current_token = ''
self._execute_sql(cursor,
"INSERT OR REPLACE INTO cookies (id, value, user_id, last_token_refresh_time, current_token) VALUES (?, ?, ?, ?, ?)",
(cookie_id, cookie_value, user_id, last_token_refresh_time, current_token)
)
else:
# 如果没有提供token信息保持现有的token信息不变
self._execute_sql(cursor,
"INSERT OR REPLACE INTO cookies (id, value, user_id) VALUES (?, ?, ?)",
(cookie_id, cookie_value, user_id)
)
self.conn.commit() self.conn.commit()
logger.info(f"Cookie保存成功: {cookie_id} (用户ID: {user_id})") logger.info(f"Cookie保存成功: {cookie_id} (用户ID: {user_id})")
@ -1147,68 +1118,6 @@ class DBManager:
self.conn.rollback() self.conn.rollback()
return False return False
def update_token_info(self, cookie_id: str, last_token_refresh_time: float = None, current_token: str = None) -> bool:
"""更新Cookie的token信息"""
with self.lock:
try:
cursor = self.conn.cursor()
# 构建动态SQL语句
update_fields = []
params = []
if last_token_refresh_time is not None:
update_fields.append("last_token_refresh_time = ?")
params.append(last_token_refresh_time)
if current_token is not None:
update_fields.append("current_token = ?")
params.append(current_token)
if not update_fields:
logger.warning(f"没有提供要更新的token信息: {cookie_id}")
return True
params.append(cookie_id)
sql = f"UPDATE cookies SET {', '.join(update_fields)} WHERE id = ?"
self._execute_sql(cursor, sql, params)
self.conn.commit()
if cursor.rowcount > 0:
logger.debug(f"Token信息更新成功: {cookie_id}")
return True
else:
logger.warning(f"未找到要更新的Cookie记录: {cookie_id}")
return False
except Exception as e:
logger.error(f"Token信息更新失败: {e}")
self.conn.rollback()
return False
def get_token_info(self, cookie_id: str) -> Optional[Dict[str, any]]:
"""获取Cookie的token信息"""
with self.lock:
try:
cursor = self.conn.cursor()
self._execute_sql(cursor,
"SELECT last_token_refresh_time, current_token FROM cookies WHERE id = ?",
(cookie_id,))
result = cursor.fetchone()
if result:
return {
'last_token_refresh_time': result[0] if result[0] is not None else 0,
'current_token': result[1] if result[1] is not None else ''
}
else:
logger.warning(f"未找到Cookie记录: {cookie_id}")
return None
except Exception as e:
logger.error(f"获取Token信息失败: {e}")
return None
def delete_cookie(self, cookie_id: str) -> bool: def delete_cookie(self, cookie_id: str) -> bool:
"""从数据库删除Cookie及其关键字""" """从数据库删除Cookie及其关键字"""