From 307485df2f590af141287d40fdc97710c27f1131 Mon Sep 17 00:00:00 2001 From: zhinianboke <115088296+zhinianboke@users.noreply.github.com> Date: Wed, 13 Aug 2025 18:27:02 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9B=9E=E9=80=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- XianyuAutoAsync.py | 51 +---------------------- db_manager.py | 101 +++------------------------------------------ 2 files changed, 6 insertions(+), 146 deletions(-) diff --git a/XianyuAutoAsync.py b/XianyuAutoAsync.py index f4b908f..173bc5c 100644 --- a/XianyuAutoAsync.py +++ b/XianyuAutoAsync.py @@ -170,9 +170,6 @@ class XianyuLive: self.token_refresh_task = None self.connection_restart_flag = False # 连接重启标志 - # 从数据库获取token信息 - self._load_token_info_from_db() - # 通知防重复机制 self.last_notification_time = {} # 记录每种通知类型的最后发送时间 self.notification_cooldown = 300 # 5分钟内不重复发送相同类型的通知 @@ -194,44 +191,7 @@ class XianyuLive: # 启动定期清理过期暂停记录的任务 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: """检查当前账号是否启用自动确认发货""" @@ -765,9 +725,6 @@ class XianyuLive: self.current_token = new_token self.last_token_refresh_time = time.time() - # 保存token信息到数据库 - self._save_token_info_to_db() - logger.info(f"【{self.cookie_id}】Token刷新成功") return new_token @@ -797,13 +754,7 @@ class XianyuLive: current_user_id = self.user_id # 保存cookies和token信息 - db_manager.save_cookie( - self.cookie_id, - self.cookies_str, - current_user_id, - self.last_token_refresh_time, - self.current_token - ) + db_manager.save_cookie(self.cookie_id, self.cookies_str, current_user_id) logger.debug(f"已更新Cookie和token信息到数据库: {self.cookie_id}") except Exception as e: logger.error(f"更新数据库Cookie失败: {self._safe_str(e)}") diff --git a/db_manager.py b/db_manager.py index d8420a0..f8c25d5 100644 --- a/db_manager.py +++ b/db_manager.py @@ -115,8 +115,6 @@ class DBManager: auto_confirm INTEGER DEFAULT 1, remark TEXT DEFAULT '', pause_duration INTEGER DEFAULT 10, - last_token_refresh_time REAL DEFAULT 0, - current_token TEXT DEFAULT '', created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE ) @@ -1080,8 +1078,7 @@ class DBManager: return cursor.executemany(sql, params_list) # -------------------- Cookie操作 -------------------- - def save_cookie(self, cookie_id: str, cookie_value: str, user_id: int = None, - last_token_refresh_time: float = None, current_token: str = None) -> bool: + def save_cookie(self, cookie_id: str, cookie_value: str, user_id: int = None) -> bool: """保存Cookie到数据库,如存在则更新""" with self.lock: try: @@ -1100,36 +1097,10 @@ class DBManager: user_id = admin_user[0] if admin_user else 1 # 如果提供了token相关信息,则更新这些字段 - if last_token_refresh_time is not None or current_token is not None: - # 先获取现有记录的token信息 - self._execute_sql(cursor, - "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._execute_sql(cursor, + "INSERT OR REPLACE INTO cookies (id, value, user_id) VALUES (?, ?, ?)", + (cookie_id, cookie_value, user_id) + ) self.conn.commit() logger.info(f"Cookie保存成功: {cookie_id} (用户ID: {user_id})") @@ -1147,68 +1118,6 @@ class DBManager: self.conn.rollback() 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: """从数据库删除Cookie及其关键字"""