Last active
March 22, 2017 05:02
-
-
Save lengshuiyulangcn/46d0dd81d7ebe07390fdc980a51f9ff1 to your computer and use it in GitHub Desktop.
need redis support
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: UTF-8 -*- | |
import redis | |
import itchat | |
import re | |
from itchat.content import * | |
import os | |
import shutil | |
redis_client = redis.Redis(host='localhost', port=6379, db=0) | |
@itchat.msg_register([TEXT, PICTURE, MAP, CARD, SHARING, RECORDING, ATTACHMENT, VIDEO, FRIENDS]) | |
def Revocation(msg): | |
msg_id = msg['MsgId'] | |
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'] + " 的名片" | |
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 = "纬度->" + x.__str__() + " 经度->" + y.__str__() | |
else: | |
msg_content = "" + 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 = "" + 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'] | |
redis_client.hmset(msg_id, {"msg_from": msg_from, "msg_time": msg_time, "msg_type": msg_type, | |
"msg_content": msg_content, "msg_url": msg_url}) | |
@itchat.msg_register([TEXT], isGroupChat = True) | |
def groupChat(msg): | |
msg_id = msg['MsgId'] | |
msg_time = msg['CreateTime'] | |
msg_from = msg['ActualNickName'] | |
group_from = msg['FromUserName'] | |
msg_content = msg['Content'] | |
redis_client.hmset(msg_id, {"msg_from": msg_from, "msg_time": msg_time, "group_from": group_from, | |
"msg_content": msg_content}) | |
@itchat.msg_register([NOTE], isGroupChat = True) | |
def notifyGroupMsg(msg): | |
if re.search(u"撤回了一条消息", msg['Content']) != None: | |
old_msg_id = re.search(u"\<msgid\>(.*?)\<\/msgid\>", msg['Content']).group(1) | |
old_msg = redis_client.hgetall(old_msg_id) | |
msg_send = "[" \ | |
+ old_msg.get('msg_from', '未知') \ | |
+ "], 撤回了一条消息, 内容如下:" \ | |
+ old_msg.get('msg_content', "未知") | |
itchat.send(msg_send.decode('utf-8'), toUserName='filehelper') | |
itchat.send(msg_send.decode('utf-8'), toUserName=old_msg["group_from"]) | |
@itchat.msg_register([NOTE]) | |
def SaveMsg(msg): | |
#创建可下载消息内容的存放文件夹,并将暂存在当前目录的文件移动到该文件中 | |
if not os.path.exists("./Revocation"): | |
os.mkdir("./Revocation") | |
if re.search(u"撤回了一条消息", msg['Content']) != None: | |
old_msg_id = re.search(u"\<msgid\>(.*?)\<\/msgid\>", msg['Content']).group(1) | |
old_msg = redis_client.hgetall(old_msg_id) | |
msg_send = "您的好友:[" \ | |
+ old_msg.get('msg_from', '未知') \ | |
+ "], 撤回了一条 ["+old_msg['msg_type']+"] 消息, 内容如下:" \ | |
+ old_msg.get('msg_content', "未知") | |
if old_msg['msg_type'] == "Sharing": | |
msg_send += ", 链接: " \ | |
+ 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 += ", 存储在当前目录下Revocation文件夹中" | |
shutil.move(old_msg['msg_content'], "./Revocation") | |
itchat.send(msg_send.decode('utf-8'), toUserName='filehelper') | |
if __name__ == '__main__': | |
itchat.auto_login(hotReload=True) | |
itchat.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment