WeChatMsg/app/DataBase/msg.py

101 lines
2.5 KiB
Python
Raw Normal View History

2023-11-15 23:53:39 +08:00
import os.path
import sqlite3
import threading
2023-11-18 13:25:56 +08:00
from pprint import pprint
2023-11-15 23:53:39 +08:00
2023-11-29 21:23:44 +08:00
DB = None
cursor = None
db_path = "./app/Database/Msg/MSG.db"
lock = threading.Lock()
2023-11-25 00:20:35 +08:00
2023-11-15 23:53:39 +08:00
# misc_path = './Msg/Misc.db'
2023-11-29 21:23:44 +08:00
if os.path.exists(db_path):
DB = sqlite3.connect(db_path, check_same_thread=False)
# '''创建游标'''
cursor = DB.cursor()
2023-11-16 22:39:59 +08:00
2023-11-25 00:20:35 +08:00
def is_database_exist():
2023-11-29 21:23:44 +08:00
return os.path.exists(db_path)
2023-11-25 00:20:35 +08:00
2023-11-16 22:39:59 +08:00
def init_database():
global DB
global cursor
if not DB:
2023-11-29 21:23:44 +08:00
if os.path.exists(db_path):
DB = sqlite3.connect(db_path, check_same_thread=False)
# '''创建游标'''
cursor = DB.cursor()
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-29 21:23:44 +08:00
try:
lock.acquire(True)
cursor.execute(sql, [username_])
result = cursor.fetchall()
finally:
lock.release()
2023-11-16 23:16:38 +08:00
result.sort(key=lambda x: x[5])
return result
2023-11-27 21:23:26 +08:00
def get_messages_all():
sql = '''
select localId,TalkerId,Type,SubType,IsSender,CreateTime,Status,StrContent,strftime('%Y-%m-%d %H:%M:%S',CreateTime,'unixepoch','localtime') as StrTime
from MSG
order by CreateTime
'''
2023-11-29 21:23:44 +08:00
try:
lock.acquire(True)
cursor.execute(sql)
result = cursor.fetchall()
finally:
lock.release()
2023-11-27 21:23:26 +08:00
result.sort(key=lambda x: x[5])
return result
2023-11-18 13:25:56 +08:00
def get_message_by_num(username_, local_id):
sql = '''
select localId,TalkerId,Type,SubType,IsSender,CreateTime,Status,StrContent,strftime('%Y-%m-%d %H:%M:%S',CreateTime,'unixepoch','localtime') as StrTime
from MSG
2023-11-18 13:25:56 +08:00
where StrTalker = ? and localId < ?
order by CreateTime desc
2023-11-22 00:22:50 +08:00
limit 10
'''
try:
lock.acquire(True)
2023-11-29 21:23:44 +08:00
cursor.execute(sql, [username_, local_id])
result = cursor.fetchall()
finally:
lock.release()
2023-11-29 21:23:44 +08:00
# 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()
# username = 'wxid_0o18ef858vnu22'
# result = get_messages(username)
# pprint(result)
# pprint(len(result))
2023-11-18 13:25:56 +08:00
result = get_message_by_num('wxid_0o18ef858vnu22', 9999999)
print(result)
2023-11-18 13:25:56 +08:00
print(result[-1][0])
local_id = result[-1][0]
pprint(get_message_by_num('wxid_0o18ef858vnu22', local_id))