调整通讯录管理

This commit is contained in:
liuwei
2025-04-03 11:18:46 +08:00
parent dbda48f01f
commit 29644271ce
3 changed files with 311 additions and 22 deletions

View File

@@ -12,8 +12,19 @@ class ContactManager:
_contacts: Dict[str, str] = {}
_group_contacts: Dict[str, str] = {} # 群组联系人
_personal_contacts: Dict[str, str] = {} # 个人联系人
_public_contacts: Dict[str, str] = {} # 公共好友
_official_accounts: Dict[str, str] = {} # 公众号
_initialized = False
_logger = logging.getLogger("ContactManager")
# 定义公共好友列表
_PUBLIC_FRIENDS = {
'fmessage': '朋友推荐消息',
'medianote': '语音记事本',
'floatbottle': '漂流瓶',
'mphelper': '公众平台安全助手',
'filehelper': '文件传输助手'
}
def __new__(cls):
if cls._instance is None:
@@ -40,24 +51,35 @@ class ContactManager:
contacts: 联系人字典,格式为 {"wxid": "NickName"}
"""
self._contacts = contacts
self._logger.info(f"联系人信息contacts={contacts}")
self._logger.info(f"联系人信息已更新,共 {len(contacts)} 个联系人")
# 分类联系人
self._classify_contacts()
def _classify_contacts(self) -> None:
"""将联系人分类为群组个人联系人"""
"""将联系人分类为群组个人联系人、公共好友和公众号"""
self._group_contacts = {}
self._personal_contacts = {}
self._public_contacts = {}
self._official_accounts = {}
for wxid, nickname in self._contacts.items():
# 微信群ID通常以@@开头
if wxid.startswith('@@'):
# 判断是否为公共好友
if wxid in self._PUBLIC_FRIENDS:
self._public_contacts[wxid] = self._PUBLIC_FRIENDS.get(wxid, nickname)
# 判断是否为公众号wxid以gh_开头
elif wxid.startswith('gh_'):
self._official_accounts[wxid] = nickname
# 判断是否为群组wxid以@chatroom结尾
elif wxid.endswith('@chatroom'):
self._group_contacts[wxid] = nickname
# 其他为普通好友和群成员
else:
self._personal_contacts[wxid] = nickname
self._logger.info(f"联系人分类完成: {len(self._group_contacts)} 个群组, {len(self._personal_contacts)} 个个人联系人")
self._logger.info(f"联系人分类完成: {len(self._group_contacts)} 个群组, "
f"{len(self._personal_contacts)} 个个人联系人, "
f"{len(self._public_contacts)} 个公共好友, "
f"{len(self._official_accounts)} 个公众号")
def get_contacts(self) -> Dict[str, str]:
"""获取所有联系人
@@ -82,6 +104,22 @@ class ContactManager:
个人联系人字典,格式为 {"wxid": "NickName"}
"""
return self._personal_contacts
def get_public_contacts(self) -> Dict[str, str]:
"""获取所有公共好友
Returns:
公共好友字典,格式为 {"wxid": "NickName"}
"""
return self._public_contacts
def get_official_accounts(self) -> Dict[str, str]:
"""获取所有公众号
Returns:
公众号字典,格式为 {"wxid": "NickName"}
"""
return self._official_accounts
def get_nickname(self, wxid: str) -> str:
"""根据微信ID获取昵称
@@ -103,7 +141,11 @@ class ContactManager:
"""
self._contacts[wxid] = nickname
# 更新分类
if wxid.endswith('@chatroom'):
if wxid in self._PUBLIC_FRIENDS:
self._public_contacts[wxid] = self._PUBLIC_FRIENDS.get(wxid, nickname)
elif wxid.startswith('gh_'):
self._official_accounts[wxid] = nickname
elif wxid.endswith('@chatroom'):
self._group_contacts[wxid] = nickname
else:
self._personal_contacts[wxid] = nickname
@@ -120,10 +162,12 @@ class ContactManager:
# 重新分类联系人
self._classify_contacts()
def get_contact_statistics(self) -> Tuple[int, int, int]:
def get_contact_statistics(self) -> Tuple[int, int, int, int, int]:
"""获取联系人统计信息
Returns:
包含总联系人数、群组数个人联系人数的元组
包含总联系人数、群组数个人联系人数、公共好友数和公众号数的元组
"""
return len(self._contacts), len(self._group_contacts), len(self._personal_contacts)
return (len(self._contacts), len(self._group_contacts),
len(self._personal_contacts), len(self._public_contacts),
len(self._official_accounts))