mirror of
https://github.com/zhinianboke/xianyu-auto-reply.git
synced 2025-08-02 20:47:35 +08:00
修复bug
This commit is contained in:
parent
cbd3ee64c0
commit
aa9214cc3a
@ -53,7 +53,7 @@ class XianyuLive:
|
|||||||
except:
|
except:
|
||||||
return "未知错误"
|
return "未知错误"
|
||||||
|
|
||||||
def __init__(self, cookies_str=None, cookie_id: str = "default"):
|
def __init__(self, cookies_str=None, cookie_id: str = "default", user_id: int = None):
|
||||||
"""初始化闲鱼直播类"""
|
"""初始化闲鱼直播类"""
|
||||||
if not cookies_str:
|
if not cookies_str:
|
||||||
cookies_str = COOKIES_STR
|
cookies_str = COOKIES_STR
|
||||||
@ -63,6 +63,7 @@ class XianyuLive:
|
|||||||
self.cookies = trans_cookies(cookies_str)
|
self.cookies = trans_cookies(cookies_str)
|
||||||
self.cookie_id = cookie_id # 唯一账号标识
|
self.cookie_id = cookie_id # 唯一账号标识
|
||||||
self.cookies_str = cookies_str # 保存原始cookie字符串
|
self.cookies_str = cookies_str # 保存原始cookie字符串
|
||||||
|
self.user_id = user_id # 保存用户ID,用于token刷新时保持正确的所有者关系
|
||||||
self.base_url = WEBSOCKET_URL
|
self.base_url = WEBSOCKET_URL
|
||||||
self.myid = self.cookies['unb']
|
self.myid = self.cookies['unb']
|
||||||
self.device_id = generate_device_id(self.myid)
|
self.device_id = generate_device_id(self.myid)
|
||||||
@ -239,7 +240,12 @@ class XianyuLive:
|
|||||||
# 更新数据库中的Cookie
|
# 更新数据库中的Cookie
|
||||||
if hasattr(self, 'cookie_id') and self.cookie_id:
|
if hasattr(self, 'cookie_id') and self.cookie_id:
|
||||||
try:
|
try:
|
||||||
db_manager.save_cookie(self.cookie_id, self.cookies_str)
|
# 获取当前Cookie的用户ID,避免在刷新时改变所有者
|
||||||
|
current_user_id = None
|
||||||
|
if hasattr(self, 'user_id') and self.user_id:
|
||||||
|
current_user_id = self.user_id
|
||||||
|
|
||||||
|
db_manager.save_cookie(self.cookie_id, self.cookies_str, current_user_id)
|
||||||
logger.debug(f"已更新Cookie到数据库: {self.cookie_id}")
|
logger.debug(f"已更新Cookie到数据库: {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)}")
|
||||||
|
@ -51,11 +51,11 @@ class CookieManager:
|
|||||||
return True
|
return True
|
||||||
|
|
||||||
# ------------------------ 内部协程 ------------------------
|
# ------------------------ 内部协程 ------------------------
|
||||||
async def _run_xianyu(self, cookie_id: str, cookie_value: str):
|
async def _run_xianyu(self, cookie_id: str, cookie_value: str, user_id: int = None):
|
||||||
"""在事件循环中启动 XianyuLive.main"""
|
"""在事件循环中启动 XianyuLive.main"""
|
||||||
from XianyuAutoAsync import XianyuLive # 延迟导入,避免循环
|
from XianyuAutoAsync import XianyuLive # 延迟导入,避免循环
|
||||||
try:
|
try:
|
||||||
live = XianyuLive(cookie_value, cookie_id=cookie_id)
|
live = XianyuLive(cookie_value, cookie_id=cookie_id, user_id=user_id)
|
||||||
await live.main()
|
await live.main()
|
||||||
except asyncio.CancelledError:
|
except asyncio.CancelledError:
|
||||||
logger.info(f"XianyuLive 任务已取消: {cookie_id}")
|
logger.info(f"XianyuLive 任务已取消: {cookie_id}")
|
||||||
@ -68,9 +68,18 @@ class CookieManager:
|
|||||||
self.cookies[cookie_id] = cookie_value
|
self.cookies[cookie_id] = cookie_value
|
||||||
# 保存到数据库,如果没有指定user_id,则保持原有绑定关系
|
# 保存到数据库,如果没有指定user_id,则保持原有绑定关系
|
||||||
db_manager.save_cookie(cookie_id, cookie_value, user_id)
|
db_manager.save_cookie(cookie_id, cookie_value, user_id)
|
||||||
task = self.loop.create_task(self._run_xianyu(cookie_id, cookie_value))
|
|
||||||
|
# 获取实际保存的user_id(如果没有指定,数据库会返回实际的user_id)
|
||||||
|
actual_user_id = user_id
|
||||||
|
if actual_user_id is None:
|
||||||
|
# 从数据库获取Cookie对应的user_id
|
||||||
|
cookie_info = db_manager.get_cookie_details(cookie_id)
|
||||||
|
if cookie_info:
|
||||||
|
actual_user_id = cookie_info.get('user_id')
|
||||||
|
|
||||||
|
task = self.loop.create_task(self._run_xianyu(cookie_id, cookie_value, actual_user_id))
|
||||||
self.tasks[cookie_id] = task
|
self.tasks[cookie_id] = task
|
||||||
logger.info(f"已启动账号任务: {cookie_id}")
|
logger.info(f"已启动账号任务: {cookie_id} (用户ID: {actual_user_id})")
|
||||||
|
|
||||||
async def _remove_cookie_async(self, cookie_id: str):
|
async def _remove_cookie_async(self, cookie_id: str):
|
||||||
task = self.tasks.pop(cookie_id, None)
|
task = self.tasks.pop(cookie_id, None)
|
||||||
@ -117,11 +126,17 @@ class CookieManager:
|
|||||||
def update_cookie(self, cookie_id: str, new_value: str):
|
def update_cookie(self, cookie_id: str, new_value: str):
|
||||||
"""替换指定账号的 Cookie 并重启任务"""
|
"""替换指定账号的 Cookie 并重启任务"""
|
||||||
async def _update():
|
async def _update():
|
||||||
|
# 获取原有的user_id
|
||||||
|
original_user_id = None
|
||||||
|
cookie_info = db_manager.get_cookie_details(cookie_id)
|
||||||
|
if cookie_info:
|
||||||
|
original_user_id = cookie_info.get('user_id')
|
||||||
|
|
||||||
# 先移除
|
# 先移除
|
||||||
if cookie_id in self.tasks:
|
if cookie_id in self.tasks:
|
||||||
await self._remove_cookie_async(cookie_id)
|
await self._remove_cookie_async(cookie_id)
|
||||||
# 再添加
|
# 再添加,保持原有的user_id
|
||||||
await self._add_cookie_async(cookie_id, new_value)
|
await self._add_cookie_async(cookie_id, new_value, original_user_id)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
current_loop = asyncio.get_running_loop()
|
current_loop = asyncio.get_running_loop()
|
||||||
|
@ -495,6 +495,25 @@ class DBManager:
|
|||||||
logger.error(f"根据ID获取Cookie失败: {e}")
|
logger.error(f"根据ID获取Cookie失败: {e}")
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
def get_cookie_details(self, cookie_id: str) -> Optional[Dict[str, any]]:
|
||||||
|
"""获取Cookie的详细信息,包括user_id"""
|
||||||
|
with self.lock:
|
||||||
|
try:
|
||||||
|
cursor = self.conn.cursor()
|
||||||
|
cursor.execute("SELECT id, value, user_id, created_at FROM cookies WHERE id = ?", (cookie_id,))
|
||||||
|
result = cursor.fetchone()
|
||||||
|
if result:
|
||||||
|
return {
|
||||||
|
'id': result[0],
|
||||||
|
'value': result[1],
|
||||||
|
'user_id': result[2],
|
||||||
|
'created_at': result[3]
|
||||||
|
}
|
||||||
|
return None
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"获取Cookie详细信息失败: {e}")
|
||||||
|
return None
|
||||||
|
|
||||||
# -------------------- 关键字操作 --------------------
|
# -------------------- 关键字操作 --------------------
|
||||||
def save_keywords(self, cookie_id: str, keywords: List[Tuple[str, str]]) -> bool:
|
def save_keywords(self, cookie_id: str, keywords: List[Tuple[str, str]]) -> bool:
|
||||||
"""保存关键字列表,先删除旧数据再插入新数据"""
|
"""保存关键字列表,先删除旧数据再插入新数据"""
|
||||||
|
Loading…
x
Reference in New Issue
Block a user