-
Notifications
You must be signed in to change notification settings - Fork 1
/
wx.py
131 lines (115 loc) · 5.47 KB
/
wx.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
from PyQt5.QtCore import QThread, pyqtSignal
import itchat
import time, os
import shutil
import re
from itchat.content import *
class weChatWord(QThread):
getMsgSignal = pyqtSignal(str) # pyqtSignal()必须写在__init__前面,里面可接收的参数类型挺多的,str,list,dict都支持
def __init__(self, parent=None):
super(weChatWord, self).__init__(parent)
self.msg_list = []
self.type_list = ['Picture', 'Recording', 'Attachment', 'Video']
def clearList(self):
'''
清空缓存消息和文件
:return:
'''
tm_now = time.time()
len_list = len(self.msg_list)
if len_list > 0:
for i in range(len_list):
if tm_now - self.msg_list[i]['msg_time'] > 121:
if self.msg_list[i]['msg_type'] in self.type_list:
try:
os.remove(".\\BackUp\\" + self.msg_list[i]['msg_content'])
except Exception as e:
print(e)
finally:
pass
else:
break
self.msg_list = self.msg_list[i:]
def run(self):
'''
重写run()函数,
:return:
'''
@itchat.msg_register([TEXT, PICTURE, MAP, CARD, SHARING, RECORDING, ATTACHMENT, VIDEO, FRIENDS],
isFriendChat=True,
isGroupChat=True)
def getMsg(msg):
'''
注册消息类型,并对不同类型的消息执行不用的操作
:param msg:
:return:
'''
msg_dict = {}
# pprint.pprint(msg)
msg_id = msg['MsgId'] # 消息ID
msg_time = msg['CreateTime']
msg_url = None
msg_group = ""
if (itchat.search_friends(userName=msg['FromUserName'])):
if itchat.search_friends(userName=msg['FromUserName'])['RemarkName']:
msg_from = itchat.search_friends(userName=msg['FromUserName'])['RemarkName'] # 消息发送人备注
elif itchat.search_friends(userName=msg['FromUserName'])['NickName']: # 消息发送人昵称
msg_from = itchat.search_friends(userName=msg['FromUserName'])['NickName'] # 消息发送人昵称
else:
msg_from = r"读取发送消息好友失败"
else:
msg_group = msg['User']['NickName']
msg_from = msg['ActualNickName']
msg_type = msg['Type']
if msg_type in ['Text', 'Friends', 'Sharing']:
msg_content = msg['Text']
msg_url = msg['Url']
elif msg_type in self.type_list:
msg_content = msg['FileName']
msg['Text'](msg['FileName'])
shutil.move(msg_content, r'.\\BackUp\\')
elif msg['Type'] == 'Card':
msg_content = msg['RecommendInfo']['NickName'] + r" 的名片"
elif msg['Type'] == 'Map':
x, y, location = re.search("<location x=\"(.*?)\" y=\"(.*?)\".*label=\"(.*?)\".*",
msg['OriContent']).group(1,
2,
3)
if location is None:
msg_content = r"纬度->" + x.__str__() + " 经度->" + y.__str__()
else:
msg_content = r"" + location
msg_dict = {'msg_id': msg_id, 'msg_time': msg_time, 'msg_from': msg_from, 'msg_group': msg_group,
'msg_content': msg_content, 'msg_type': msg_type, 'msg_url': msg_url}
self.msg_list.append(msg_dict)
self.clearList()
@itchat.msg_register([NOTE], isFriendChat=True, isGroupChat=True)
def recall(msg):
'''
当消息类型为通知类的时候,查找消息内容是否为撤回消息,如果是,则执行撤回后的防撤回操作
:param msg:
:return:
'''
# pprint.pprint(msg)
msg_content = msg['Content']
if re.search(r'\<replacemsg\>\<!\[CDATA\[(.*)撤回了一条消息\]\]\>\<\/replacemsg\>', msg_content):
msg_note = re.search(r'\<replacemsg\>\<!\[CDATA\[(.*)\]\]\>\<\/replacemsg\>', msg_content).group(1)
old_msg_id = re.search(r'\<msgid\>([0-9]+)\</msgid\>', msg_content).group(1)
for each in self.msg_list:
if each['msg_id'] == old_msg_id:
timeArray = time.localtime()
otherStyleTime = time.strftime("%Y-%m-%d %H:%M:%S,", timeArray)
msg_note = msg_note + ',撤回内容为:' + each['msg_content']
if each['msg_group'] != '':
msg_note = "群组(" + each['msg_group'] + ")中" + msg_note
msg_note = otherStyleTime + msg_note
itchat.send(msg_note, toUserName='filehelper')
self.msg_list.pop(self.msg_list.index(each))
self.getMsgSignal.emit(msg_note)
break
# 创建BuckUp文件夹
if not os.path.exists(".\\BackUp\\"):
os.mkdir('.\\BackUp\\')
# 启动itchat()
itchat.auto_login(hotReload=True)
itchat.run()