2023-11-15 23:53:39 +08:00
|
|
|
import os.path
|
2023-11-16 23:16:38 +08:00
|
|
|
import re
|
2023-11-15 23:53:39 +08:00
|
|
|
import sqlite3
|
2023-11-17 21:34:22 +08:00
|
|
|
import threading
|
2023-11-15 23:53:39 +08:00
|
|
|
|
2023-11-16 23:16:38 +08:00
|
|
|
DB = []
|
|
|
|
cursor = []
|
|
|
|
msg_root_path = "./app/Database/Msg/"
|
2023-11-17 21:34:22 +08:00
|
|
|
lock = threading.Lock()
|
2023-11-15 23:53:39 +08:00
|
|
|
# misc_path = './Msg/Misc.db'
|
2023-11-16 23:16:38 +08:00
|
|
|
if os.path.exists(msg_root_path):
|
|
|
|
for root, dirs, files in os.walk(msg_root_path):
|
|
|
|
for file in files:
|
|
|
|
if re.match('^MSG[0-9]+\.db$', file):
|
|
|
|
# print('ok', file)
|
|
|
|
msg_path = os.path.join(msg_root_path, file)
|
|
|
|
DB0 = sqlite3.connect(msg_path, check_same_thread=False)
|
|
|
|
# '''创建游标'''
|
|
|
|
cursor0 = DB0.cursor()
|
|
|
|
DB.append(DB0)
|
|
|
|
cursor.append(cursor0)
|
2023-11-16 22:39:59 +08:00
|
|
|
|
|
|
|
|
|
|
|
def init_database():
|
|
|
|
global DB
|
|
|
|
global cursor
|
|
|
|
if not DB:
|
2023-11-16 23:16:38 +08:00
|
|
|
if os.path.exists(msg_root_path):
|
|
|
|
for root, dirs, files in os.walk(msg_root_path):
|
|
|
|
for file in files:
|
|
|
|
# print(file)
|
|
|
|
if re.match('^MSG[0-9]+\.db$', file):
|
|
|
|
print('ok', file)
|
|
|
|
msg_path = os.path.join(msg_root_path, file)
|
|
|
|
DB0 = sqlite3.connect(msg_path, check_same_thread=False)
|
|
|
|
# '''创建游标'''
|
|
|
|
cursor0 = DB0.cursor()
|
|
|
|
DB.append(DB0)
|
|
|
|
cursor.append(cursor0)
|
2023-11-16 22:39:59 +08:00
|
|
|
|
|
|
|
|
|
|
|
def get_messages(username_):
|
|
|
|
sql = '''
|
|
|
|
select localId,TalkerId,Type,SubType,IsSender,CreateTime,Status,StrContent,strftime('%Y-%m-%d %H:%M:%S',CreateTime,'unixepoch','localtime') as StrTime
|
|
|
|
from MSG
|
|
|
|
where StrTalker=?
|
|
|
|
order by CreateTime
|
|
|
|
'''
|
2023-11-16 23:16:38 +08:00
|
|
|
result = []
|
|
|
|
for cur in cursor:
|
|
|
|
cur.execute(sql, [username_])
|
|
|
|
result_ = cur.fetchall()
|
|
|
|
# print(len(result))
|
|
|
|
result += result_
|
|
|
|
result.sort(key=lambda x: x[5])
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
2023-11-17 21:34:22 +08:00
|
|
|
def get_message_by_num(username_, n):
|
|
|
|
sql = '''
|
|
|
|
select localId,TalkerId,Type,SubType,IsSender,CreateTime,Status,StrContent,strftime('%Y-%m-%d %H:%M:%S',CreateTime,'unixepoch','localtime') as StrTime
|
|
|
|
from MSG
|
|
|
|
where StrTalker=?
|
2023-11-17 23:59:52 +08:00
|
|
|
order by CreateTime desc
|
2023-11-17 23:54:34 +08:00
|
|
|
limit 100
|
2023-11-17 21:34:22 +08:00
|
|
|
'''
|
|
|
|
result = []
|
|
|
|
try:
|
|
|
|
lock.acquire(True)
|
|
|
|
for cur in cursor:
|
|
|
|
cur = cursor[-1]
|
|
|
|
cur.execute(sql, [username_])
|
|
|
|
result_ = cur.fetchall()
|
2023-11-17 23:59:52 +08:00
|
|
|
result_.reverse()
|
2023-11-17 21:34:22 +08:00
|
|
|
result += result_
|
|
|
|
return result_
|
|
|
|
finally:
|
|
|
|
lock.release()
|
|
|
|
result.sort(key=lambda x: x[5])
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
2023-11-16 23:16:38 +08:00
|
|
|
def close():
|
|
|
|
for db in DB:
|
|
|
|
db.close()
|
2023-11-16 22:39:59 +08:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2023-11-16 23:16:38 +08:00
|
|
|
msg_root_path = './Msg/'
|
2023-11-16 22:39:59 +08:00
|
|
|
init_database()
|
|
|
|
|
2023-11-17 21:34:22 +08:00
|
|
|
# username = 'wxid_0o18ef858vnu22'
|
|
|
|
# result = get_messages(username)
|
|
|
|
# pprint(result)
|
|
|
|
# pprint(len(result))
|
|
|
|
result = get_message_by_num('wxid_0o18ef858vnu22', 0)
|
|
|
|
print(result)
|