WeChatMsg/app/person.py

136 lines
4.0 KiB
Python
Raw Normal View History

2023-12-30 19:26:24 +08:00
"""
定义各种联系人
"""
2023-12-05 00:13:20 +08:00
import os.path
2023-12-11 22:49:17 +08:00
import re
2023-11-23 22:58:27 +08:00
from typing import Dict
from PyQt5.QtGui import QPixmap
from app.ui.Icon import Icon
2023-11-23 22:58:27 +08:00
def singleton(cls):
_instance = {}
def inner():
if cls not in _instance:
_instance[cls] = cls()
return _instance[cls]
return inner
2023-12-30 18:05:53 +08:00
class Person:
2023-12-30 19:26:24 +08:00
def __init__(self):
self.avatar_path = None
self.avatar = None
self.avatar_path_qt = Icon.Default_avatar_path
2023-11-23 22:58:27 +08:00
def set_avatar(self, img_bytes):
if not img_bytes:
self.avatar.load(Icon.Default_avatar_path)
return
if img_bytes[:4] == b'\x89PNG':
self.avatar.loadFromData(img_bytes, format='PNG')
else:
self.avatar.loadFromData(img_bytes, format='jfif')
2023-12-20 22:23:13 +08:00
def save_avatar(self, path=None):
if not self.avatar:
return
if path:
save_path = path
2023-12-30 18:05:53 +08:00
if os.path.exists(save_path):
self.avatar_path = save_path
return save_path
2023-12-20 22:23:13 +08:00
else:
os.makedirs('./data/avatar', exist_ok=True)
save_path = os.path.join(f'data/avatar/', self.wxid + '.png')
self.avatar_path = save_path
self.avatar.save(save_path)
print('保存头像', save_path)
2023-11-23 22:58:27 +08:00
2023-12-30 18:05:53 +08:00
@singleton
2023-12-30 19:26:24 +08:00
class Me(Person):
2023-12-30 18:05:53 +08:00
def __init__(self):
self.avatar = QPixmap(Icon.Default_avatar_path)
self.avatar_path = ':/icons/icons/default_avatar.svg'
self.wxid = ''
self.wx_dir = ''
self.name = ''
self.mobile = ''
self.smallHeadImgUrl = ''
2024-01-04 20:28:08 +08:00
self.nickName = self.name
self.remark = self.nickName
2023-12-30 18:05:53 +08:00
2023-12-30 19:26:24 +08:00
class Contact(Person):
2023-11-23 22:58:27 +08:00
def __init__(self, contact_info: Dict):
self.wxid = contact_info.get('UserName')
self.remark = contact_info.get('Remark')
# Alias,Type,Remark,NickName,PYInitial,RemarkPYInitial,ContactHeadImgUrl.smallHeadImgUrl,ContactHeadImgUrl,bigHeadImgUrl
self.alias = contact_info.get('Alias')
self.nickName = contact_info.get('NickName')
if not self.remark:
self.remark = self.nickName
2024-01-06 13:04:55 +08:00
self.remark = re.sub(r'[\/:*?"<>|\s\.]', '_', self.remark)
2023-11-23 22:58:27 +08:00
self.smallHeadImgUrl = contact_info.get('smallHeadImgUrl')
self.smallHeadImgBLOG = b''
self.avatar = QPixmap()
2023-12-02 22:34:05 +08:00
self.avatar_path = Icon.Default_avatar_path
2023-12-17 13:15:49 +08:00
self.is_chatroom = self.wxid.__contains__('@chatroom')
2024-01-03 20:40:04 +08:00
self.detail = contact_info.get('detail')
2024-01-04 20:28:08 +08:00
self.label_name = contact_info.get('label_name') # 联系人的标签分类
2024-01-03 23:45:41 +08:00
"""
detail存储了联系人的详细信息是个字典
{
'region': tuple[国家,省份,], # 地区三元组
'signature': str, # 个性签名
'telephone': str, # 电话号码,自己写的备注才会显示
'gender': int, # 性别 0未知12
}
"""
2023-11-23 22:58:27 +08:00
2023-12-30 18:05:53 +08:00
class ContactDefault(Person):
def __init__(self, wxid=""):
self.avatar = QPixmap(Icon.Default_avatar_path)
self.avatar_path = ':/icons/icons/default_avatar.svg'
self.wxid = wxid
self.remark = wxid
self.alias = wxid
self.nickName = wxid
self.smallHeadImgUrl = ""
self.smallHeadImgBLOG = b''
self.is_chatroom = False
2023-11-23 22:58:27 +08:00
2023-12-30 19:26:24 +08:00
class Contacts:
def __init__(self):
self.contacts: Dict[str:Contact] = {}
def add(self, wxid, contact: Contact):
if wxid not in contact:
self.contacts[wxid] = contact
def get(self, wxid: str) -> Contact:
return self.contacts.get(wxid)
def remove(self, wxid: str):
return self.contacts.pop(wxid)
def save_avatar(self, avatar_dir: str = './data/avatar/'):
for wxid, contact in self.contacts.items():
avatar_path = os.path.join(avatar_dir, wxid + '.png')
if os.path.exists(avatar_path):
continue
contact.save_avatar(avatar_path)
2023-11-23 22:58:27 +08:00
if __name__ == '__main__':
2023-12-30 19:26:24 +08:00
p1 = Me()
p2 = Me()
2023-11-23 22:58:27 +08:00
print(p1 == p2)