{ 沿途 }

python实战之微信防撤回..

先上code
#!/usr/bin/env python
#! http://wq91.cn/
# -*-encoding:utf-8-*-

import sys
reload(sys)
sys.setdefaultencoding('UTF8')

import os, re, shutil, time, collections, json

from HTMLParser import HTMLParser
from xml.etree import ElementTree as ETree

import itchat
from itchat.content import *

msg_store = collections.OrderedDict()
timeout = 600
sending_type = {'Picture': 'img', 'Video': 'vid'}
data_path = 'data'
nickname = ''
bot = None

if __name__ == '__main__':
if not os.path.exists(data_path):
os.mkdir(data_path)
# if the QR code doesn't show correctly, you can try to change the value
# of enableCdmQR to 1 or -1 or -2. It nothing works, you can change it to
# enableCmdQR=True and a picture will show up.
bot = itchat.new_instance()
bot.auto_login(hotReload=True, enableCmdQR=2)
nickname = bot.loginInfo['User']['NickName']

def clear_timeouted_message():
now = time.time()
count = 0
for k, v in msg_store.items():
if now - v['ReceivedTime'] > timeout:
count += 1
else:
break
for i in range(count):
item = msg_store.popitem(last=False)

def get_sender_receiver(msg):
sender = nickname
receiver = nickname
if msg['FromUserName'][0:2] == '@@': # group chat
sender = msg['ActualNickName']
m = bot.search_chatrooms(userName=msg['FromUserName'])
if m is not None:
receiver = m['NickName']
elif msg['ToUserName'][0:2] == '@@': # group chat by myself
if 'ActualNickName' in msg:
sender = msg['ActualNickName']
else:
m = bot.search_friends(userName=msg['FromUserName'])
if m is not None:
sender = m['NickName']
m = bot.search_chatrooms(userName=msg['ToUserName'])
if m is not None:
receiver = m['NickName']
else: # personal chat
m = bot.search_friends(userName=msg['FromUserName'])
if m is not None:
sender = m['NickName']
m = bot.search_friends(userName=msg['ToUserName'])
if m is not None:
receiver = m['NickName']
return HTMLParser().unescape(sender), HTMLParser().unescape(receiver)

def print_msg(msg):
msg_str = ' '.join(msg)
print msg_str
return msg_str

def get_whole_msg(msg, download=False):
sender, receiver = get_sender_receiver(msg)
msg_time_rec = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
if len(msg['FileName']) > 0 and len(msg['Url']) == 0:
if download: # download the file into data_path directory
fn = os.path.join(data_path, msg['FileName'])
msg['Text'](fn)
if os.path.getsize(fn) == 0:
return []
c = '@%s@%s' % (sending_type.get(msg['Type'], 'fil'), fn)
else:
c = '@%s@%s' % (sending_type.get(msg['Type'], 'fil'), msg['FileName'])
return ["[%s]->[%s]:\n%s\n" % (sender, receiver, msg_time_rec), c]
c = msg['Text']
if len(msg['Url']) > 0:
try: # handle map label
content_tree = ETree.fromstring(msg['OriContent'])
if content_tree is not None:
map_label = content_tree.find('location')
if map_label is not None:
c += ' ' + map_label.attrib['poiname']
c += ' ' + map_label.attrib['label']
except:
pass
url = HTMLParser().unescape(msg['Url'])
c += ' ' + url
return ["[%s]->[%s]:\n%s\n %s" % (sender, receiver, msg_time_rec, c)]

@bot.msg_register([TEXT, PICTURE, MAP, CARD, SHARING, RECORDING,
ATTACHMENT, VIDEO, FRIENDS], isFriendChat=True, isGroupChat=True)
def normal_msg(msg):
print_msg(get_whole_msg(msg))
now = time.time()
msg['ReceivedTime'] = now
msg_id = msg['MsgId']
msg_store[msg_id] = msg
clear_timeouted_message()

@bot.msg_register([NOTE], isFriendChat=True, isGroupChat=True)
def note_msg(msg):
print_msg(get_whole_msg(msg))
content = HTMLParser().unescape(msg['Content'])
try:
content_tree = ETree.fromstring(content)
except Exception:
# invent/remove to chatroom
return
if content_tree is None:
return
revoked = content_tree.find('revokemsg')
if revoked is None:
return
old_msg_id = revoked.find('msgid').text
old_msg = msg_store.get(old_msg_id)
if old_msg is None:
return
msg_send = get_whole_msg(old_msg, download=True)
for m in msg_send:
bot.send(m, toUserName='filehelper')
clear_timeouted_message()

if __name__ == '__main__':
bot.run()


环境

CentOS 7 x64
python pip

putty 会话指令

nohup python wechat-anti-revoke-v2.py &
执行完后
exit

VNC 执行指令

python wechat-anti-revoke-v2.py &或 python wechat-anti-revoke-v2.py

tips

使用 jobs 查看任务。
使用 fg %n 关闭。
fg调到前台,然后ctrl+c结束掉。
第一次登陆需要QR扫描一下 以后不需要 相关的登陆信息存储再itchat.pkl文件里 以后换另外一个号只要删itchat.pkl就行
只能再当前目录运行wechat-anti-revoke-v2.py 不然在别的目录会重写itchat.pkl

喜欢 0