修复无法自动解密(解密报错的问题)

This commit is contained in:
xaoyaoo 2024-05-06 21:54:16 +08:00
parent d2f37eba5b
commit b9abf401f9
4 changed files with 83 additions and 52 deletions

View File

@ -28,4 +28,4 @@ except:
# PYWXDUMP_ROOT_PATH = os.path.dirname(__file__) # PYWXDUMP_ROOT_PATH = os.path.dirname(__file__)
# db_init = DBPool("DBPOOL_INIT") # db_init = DBPool("DBPOOL_INIT")
__version__ = "3.0.16" __version__ = "3.0.17"

View File

@ -75,18 +75,39 @@ def init_key():
if not my_wxid: if not my_wxid:
return ReJson(1002, body=f"my_wxid is required: {my_wxid}") return ReJson(1002, body=f"my_wxid is required: {my_wxid}")
old_merge_save_path = read_session(g.sf, my_wxid, "merge_path")
if os.path.exists(old_merge_save_path):
pmsg = ParsingMSG(old_merge_save_path)
pmsg.close_all_connection()
out_path = os.path.join(g.tmp_path, "decrypted", my_wxid) if my_wxid else os.path.join(g.tmp_path, "decrypted") out_path = os.path.join(g.tmp_path, "decrypted", my_wxid) if my_wxid else os.path.join(g.tmp_path, "decrypted")
# 检查文件夹中文件是否被占用 # 检查文件夹中文件是否被占用
if os.path.exists(out_path): if os.path.exists(out_path):
try: try:
shutil.rmtree(out_path) shutil.rmtree(out_path)
except PermissionError as e: except PermissionError as e:
# 显示堆栈信息
logging.error(f"{e}", exc_info=True)
return ReJson(2001, body=str(e)) return ReJson(2001, body=str(e))
code, merge_save_path = decrypt_merge(wx_path=wx_path, key=key, outpath=out_path) code, merge_save_path = decrypt_merge(wx_path=wx_path, key=key, outpath=out_path)
time.sleep(1) time.sleep(1)
if code: if code:
save_session(g.sf, my_wxid, "merge_path", merge_save_path) # 移动merge_save_path到g.tmp_path/my_wxid
if not os.path.exists(os.path.join(g.tmp_path, my_wxid)):
os.makedirs(os.path.join(g.tmp_path, my_wxid))
merge_save_path_new = os.path.join(g.tmp_path, my_wxid, "merge_all.db")
shutil.move(merge_save_path, str(merge_save_path_new))
# 删除out_path
if os.path.exists(out_path):
try:
shutil.rmtree(out_path)
except PermissionError as e:
# 显示堆栈信息
logging.error(f"{e}", exc_info=True)
save_session(g.sf, my_wxid, "merge_path", merge_save_path_new)
save_session(g.sf, my_wxid, "wx_path", wx_path) save_session(g.sf, my_wxid, "wx_path", wx_path)
save_session(g.sf, my_wxid, "key", key) save_session(g.sf, my_wxid, "key", key)
save_session(g.sf, my_wxid, "my_wxid", my_wxid) save_session(g.sf, my_wxid, "my_wxid", my_wxid)

View File

@ -69,6 +69,13 @@ class DatabaseBase:
logging.info(f"关闭数据库 - {self._db_path}") logging.info(f"关闭数据库 - {self._db_path}")
self._db_connection = None self._db_connection = None
def close_all_connection(self):
for db_path in self._connection_pool:
if self._connection_pool[db_path]:
self._connection_pool[db_path].close()
logging.info(f"关闭数据库 - {db_path}")
self._connection_pool[db_path] = None
def show__singleton_instances(self): def show__singleton_instances(self):
print(self._singleton_instances) print(self._singleton_instances)

View File

@ -236,60 +236,63 @@ def merge_db(db_paths, save_path="merge.db", CreateTime: int = 0, endCreateTime:
# 获取表名 # 获取表名
sql = f"SELECT name FROM sqlite_master WHERE type='table' ORDER BY name;" sql = f"SELECT name FROM sqlite_master WHERE type='table' ORDER BY name;"
tables = execute_sql(db, sql) tables = execute_sql(db, sql)
for table in tables: try:
table = table[0] for table in tables:
if table == "sqlite_sequence": table = table[0]
continue if table == "sqlite_sequence":
# 获取表中的字段名 continue
sql = f"PRAGMA table_info({table})" # 获取表中的字段名
columns = execute_sql(db, sql) sql = f"PRAGMA table_info({table})"
if not columns or len(columns) < 1: columns = execute_sql(db, sql)
continue if not columns or len(columns) < 1:
col_type = { continue
(i[1] if isinstance(i[1], str) else i[1].decode(), i[2] if isinstance(i[2], str) else i[2].decode()) for col_type = {
i in columns} (i[1] if isinstance(i[1], str) else i[1].decode(), i[2] if isinstance(i[2], str) else i[2].decode()) for
columns = [i[1] if isinstance(i[1], str) else i[1].decode() for i in columns] i in columns}
if not columns or len(columns) < 1: columns = [i[1] if isinstance(i[1], str) else i[1].decode() for i in columns]
continue if not columns or len(columns) < 1:
continue
# 检测表是否存在 # 检测表是否存在
sql = f"SELECT name FROM sqlite_master WHERE type='table' AND name='{table}'" sql = f"SELECT name FROM sqlite_master WHERE type='table' AND name='{table}'"
out_cursor.execute(sql)
if len(out_cursor.fetchall()) < 1:
# 创建表
# 拼接创建表的SQL语句
column_definitions = []
for column in col_type:
column_name = column[0] if isinstance(column[0], str) else column[0].decode()
column_type = column[1] if isinstance(column[1], str) else column[1].decode()
column_definition = f"{column_name} {column_type}"
column_definitions.append(column_definition)
sql = f"CREATE TABLE IF NOT EXISTS {table} ({','.join(column_definitions)})"
# sql = f"CREATE TABLE IF NOT EXISTS {table} ({','.join(columns)})"
out_cursor.execute(sql) out_cursor.execute(sql)
if len(out_cursor.fetchall()) < 1:
# 创建表
# 拼接创建表的SQL语句
column_definitions = []
for column in col_type:
column_name = column[0] if isinstance(column[0], str) else column[0].decode()
column_type = column[1] if isinstance(column[1], str) else column[1].decode()
column_definition = f"{column_name} {column_type}"
column_definitions.append(column_definition)
sql = f"CREATE TABLE IF NOT EXISTS {table} ({','.join(column_definitions)})"
# sql = f"CREATE TABLE IF NOT EXISTS {table} ({','.join(columns)})"
out_cursor.execute(sql)
# 创建包含 NULL 值比较的 UNIQUE 索引 # 创建包含 NULL 值比较的 UNIQUE 索引
index_name = f"{table}_unique_index" index_name = f"{table}_unique_index"
coalesce_columns = ','.join(f"COALESCE({column}, '')" for column in columns) # 将 NULL 值转换为 '' coalesce_columns = ','.join(f"COALESCE({column}, '')" for column in columns) # 将 NULL 值转换为 ''
sql = f"CREATE UNIQUE INDEX IF NOT EXISTS {index_name} ON {table} ({coalesce_columns})" sql = f"CREATE UNIQUE INDEX IF NOT EXISTS {index_name} ON {table} ({coalesce_columns})"
out_cursor.execute(sql) out_cursor.execute(sql)
# 获取表中的数据 # 获取表中的数据
if "CreateTime" in columns and CreateTime > 0: if "CreateTime" in columns and CreateTime > 0:
sql = f"SELECT {','.join([i[0] for i in col_type])} FROM {table} WHERE CreateTime>? ORDER BY CreateTime" sql = f"SELECT {','.join([i[0] for i in col_type])} FROM {table} WHERE CreateTime>? ORDER BY CreateTime"
src_data = execute_sql(db, sql, (CreateTime,)) src_data = execute_sql(db, sql, (CreateTime,))
else: else:
sql = f"SELECT {','.join([i[0] for i in col_type])} FROM {table}" sql = f"SELECT {','.join([i[0] for i in col_type])} FROM {table}"
src_data = execute_sql(db, sql) src_data = execute_sql(db, sql)
if not src_data or len(src_data) < 1: if not src_data or len(src_data) < 1:
continue continue
# 插入数据 # 插入数据
sql = f"INSERT OR IGNORE INTO {table} ({','.join([i[0] for i in col_type])}) VALUES ({','.join(['?'] * len(columns))})" sql = f"INSERT OR IGNORE INTO {table} ({','.join([i[0] for i in col_type])}) VALUES ({','.join(['?'] * len(columns))})"
try: try:
out_cursor.executemany(sql, src_data) out_cursor.executemany(sql, src_data)
except Exception as e: except Exception as e:
logging.error(f"error: {alias}\n{table}\n{sql}\n{src_data}\n{len(src_data)}\n{e}\n**********") logging.error(f"error: {alias}\n{table}\n{sql}\n{src_data}\n{len(src_data)}\n{e}\n**********")
outdb.commit() outdb.commit()
except Exception as e:
logging.error(f"fun(merge_db) error: {alias}\n{e}\n**********")
db.close() db.close()
outdb.close() outdb.close()
return save_path return save_path