-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwechatrevoke.py
More file actions
128 lines (116 loc) · 5.73 KB
/
wechatrevoke.py
File metadata and controls
128 lines (116 loc) · 5.73 KB
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
126
127
128
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2017/7/11 下午5:45
# @Author : hukezhu
# @Site :
# @File : wechatrevoke.py
# @Software: PyCharm
import os
import re
import shutil
import time
import itchat
from itchat.content import *
# {msg_id:(msg_from,msg_to,msg_time,msg_time_touser,msg_type,msg_content,msg_url)}
msg_dict = {}
#ClearTimeOutMsg用于清理消息字典,把超时消息清理掉
#为减少资源占用,此函数只在有新消息动态时调用
def ClearTimeOutMsg():
if msg_dict.__len__() > 0:
for msgid in list(msg_dict): #由于字典在遍历过程中不能删除元素,故使用此方法
if time.time() - msg_dict.get(msgid, None)["msg_time"] > 130.0: #超时两分钟
item = msg_dict.pop(msgid)
#print("超时的消息:", item['msg_content'])
#可下载类消息,并删除相关文件
if item['msg_type'] == "Picture" \
or item['msg_type'] == "Recording" \
or item['msg_type'] == "Video" \
or item['msg_type'] == "Attachment":
print("要删除的文件:", item['msg_content'])
os.remove(item['msg_content'])
#将接收到的消息存放在字典中,当接收到新消息时对字典中超时的消息进行清理
#没有注册note(通知类)消息,通知类消息一般为:红包 转账 消息撤回提醒等,不具有撤回功能
@itchat.msg_register([TEXT, PICTURE, MAP, CARD, SHARING, RECORDING, ATTACHMENT, VIDEO, FRIENDS])
def Revocation(msg):
mytime = time.localtime() # 这儿获取的是本地时间
#获取用于展示给用户看的时间 2017/03/03 13:23:53
msg_time_touser = mytime.tm_year.__str__() \
+ "/" + mytime.tm_mon.__str__() \
+ "/" + mytime.tm_mday.__str__() \
+ " " + mytime.tm_hour.__str__() \
+ ":" + mytime.tm_min.__str__() \
+ ":" + mytime.tm_sec.__str__()
msg_id = msg['MsgId'] #消息ID
msg_time = msg['CreateTime'] #消息时间
msg_from = itchat.search_friends(userName=msg['FromUserName'])['NickName'] #消息发送人昵称
msg_type = msg['Type'] #消息类型
msg_content = None #根据消息类型不同,消息内容不同
msg_url = None #分享类消息有url
#图片 语音 附件 视频,可下载消息将内容下载暂存到当前目录
if msg['Type'] == 'Text':
msg_content = msg['Text']
elif msg['Type'] == 'Picture':
msg_content = msg['FileName']
msg['Text'](msg['FileName'])
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
elif msg['Type'] == 'Sharing':
msg_content = msg['Text']
msg_url = msg['Url']
elif msg['Type'] == 'Recording':
msg_content = msg['FileName']
msg['Text'](msg['FileName'])
elif msg['Type'] == 'Attachment':
msg_content = r"" + msg['FileName']
msg['Text'](msg['FileName'])
elif msg['Type'] == 'Video':
msg_content = msg['FileName']
msg['Text'](msg['FileName'])
elif msg['Type'] == 'Friends':
msg_content = msg['Text']
#更新字典
# {msg_id:(msg_from,msg_time,msg_time_touser,msg_type,msg_content,msg_url)}
msg_dict.update(
{msg_id: {"msg_from": msg_from, "msg_time": msg_time, "msg_time_touser": msg_time_touser, "msg_type": msg_type,
"msg_content": msg_content, "msg_url": msg_url}})
#清理字典
ClearTimeOutMsg()
#收到note类消息,判断是不是撤回并进行相应操作
@itchat.msg_register([NOTE])
def SaveMsg(msg):
# print(msg)
#创建可下载消息内容的存放文件夹,并将暂存在当前目录的文件移动到该文件中
if not os.path.exists(".\\Revocation\\"):
os.mkdir(".\\Revocation\\")
if re.search(r"\<replacemsg\>\<\!\[CDATA\[.*撤回了一条消息\]\]\>\<\/replacemsg\>", msg['Content']) != None:
old_msg_id = re.search("\<msgid\>(.*?)\<\/msgid\>", msg['Content']).group(1)
old_msg = msg_dict.get(old_msg_id, {})
#print(old_msg_id, old_msg)
msg_send = r"您的好友:" \
+ old_msg.get('msg_from', None) \
+ r" 在 [" + old_msg.get('msg_time_touser', None) \
+ r"], 撤回了一条 ["+old_msg['msg_type']+"] 消息, 内容如下:" \
+ old_msg.get('msg_content', None)
if old_msg['msg_type'] == "Sharing":
msg_send += r", 链接: " \
+ old_msg.get('msg_url', None)
elif old_msg['msg_type'] == 'Picture' \
or old_msg['msg_type'] == 'Recording' \
or old_msg['msg_type'] == 'Video' \
or old_msg['msg_type'] == 'Attachment':
msg_send += r", 存储在当前目录下Revocation文件夹中"
shutil.move(old_msg['msg_content'], r".\\Revocation\\")
itchat.send(msg_send, toUserName='filehelper') #将撤回消息的通知以及细节发送到文件助手
msg_dict.pop(old_msg_id)
ClearTimeOutMsg()
if __name__ == '__main__':
itchat.auto_login(hotReload=True)
itchat.run()