pyqt支持群聊显示,合并群聊和非群聊的获取

This commit is contained in:
shuaikangzhou
2024-01-07 20:24:12 +08:00
parent 1209caa378
commit cc281fb352
15 changed files with 187 additions and 82 deletions

View File

@@ -16,6 +16,70 @@ def is_database_exist():
return os.path.exists(db_path)
def parser_chatroom_message(messages):
from app.DataBase import micro_msg_db, misc_db
from app.util.protocbuf.msg_pb2 import MessageBytesExtra
from app.person import Contact, Me, ContactDefault
'''
获取一个群聊的聊天记录
return list
a[0]: localId,
a[1]: talkerId, 和strtalker对应的不是群聊信息发送人
a[2]: type,
a[3]: subType,
a[4]: is_sender,
a[5]: timestamp,
a[6]: status, (没啥用)
a[7]: str_content,
a[8]: str_time, (格式化的时间)
a[9]: msgSvrId,
a[10]: BytesExtra,
a[11]: CompressContent,
a[12]: msg_sender, ContactPC 或 ContactDefault 类型,这个才是群聊里的信息发送人,不是群聊或者自己是发送者没有这个字段)
'''
updated_messages = [] # 用于存储修改后的消息列表
for row in messages:
message = list(row)
if message[4] == 1: # 自己发送的就没必要解析了
message.append(Me())
updated_messages.append(tuple(message))
continue
if message[10] is None: # BytesExtra是空的跳过
message.append(ContactDefault(wxid))
updated_messages.append(tuple(message))
continue
msgbytes = MessageBytesExtra()
msgbytes.ParseFromString(message[10])
wxid = ''
for tmp in msgbytes.message2:
if tmp.field1 != 1:
continue
wxid = tmp.field2
if wxid == "": # 系统消息里面 wxid 不存在
message.append(ContactDefault(wxid))
updated_messages.append(tuple(message))
continue
contact_info_list = micro_msg_db.get_contact_by_username(wxid)
if contact_info_list is None: # 群聊中已退群的联系人不会保存在数据库里
message.append(ContactDefault(wxid))
updated_messages.append(tuple(message))
continue
contact_info = {
'UserName': contact_info_list[0],
'Alias': contact_info_list[1],
'Type': contact_info_list[2],
'Remark': contact_info_list[3],
'NickName': contact_info_list[4],
'smallHeadImgUrl': contact_info_list[7]
}
contact = Contact(contact_info)
contact.smallHeadImgBLOG = misc_db.get_avatar_buffer(contact.wxid)
contact.set_avatar(contact.smallHeadImgBLOG)
message.append(contact)
updated_messages.append(tuple(message))
return updated_messages
def singleton(cls):
_instance = {}
@@ -105,7 +169,7 @@ class Msg:
result = self.cursor.fetchall()
finally:
lock.release()
return result
return parser_chatroom_message(result) if username_.__contains__('@chatroom') else result
# result.sort(key=lambda x: x[5])
# return self.add_sender(result)
@@ -164,7 +228,7 @@ class Msg:
finally:
lock.release()
# result.sort(key=lambda x: x[5])
return result
return parser_chatroom_message(result) if username_.__contains__('@chatroom') else result
def get_messages_by_type(self, username_, type_, year_='all'):
if not self.open_flag:
@@ -629,14 +693,15 @@ if __name__ == '__main__':
msg.init_database()
wxid = 'wxid_0o18ef858vnu22'
wxid = '24521163022@chatroom'
wxid = 'wxid_vtz9jk9ulzjt22' # si
wxid = 'wxid_vtz9jk9ulzjt22' # si
print()
from app.util import compress_content
import xml.etree.ElementTree as ET
msgs = msg.get_messages(wxid)
for msg in msgs:
if msg[2]==49 and msg[3]==5:
if msg[2] == 49 and msg[3] == 5:
xml = compress_content.decompress_CompressContent(msg[11])
root = ET.XML(xml)
appmsg = root.find('appmsg')
@@ -658,4 +723,4 @@ if __name__ == '__main__':
print(thumb)
if tmp.field2 == 4:
app_logo = tmp.field2
print('logo',app_logo)
print('logo', app_logo)