update python_wechat.py

master
xianhu 2017-04-08 17:15:24 +08:00
parent 95236f7cc4
commit 1d48d41caf
2 changed files with 73 additions and 33 deletions

3
.gitignore vendored
View File

@ -7,6 +7,9 @@
*.dat
*.temp
*.png
*.pkl
*.mp3
*.mp4
.*
test/

View File

@ -8,6 +8,7 @@ python_wechat.py by xianhu
(3) 自动保存被撤回消息
"""
import re
import time
import itchat
import logging
@ -16,17 +17,18 @@ from itchat.content import *
# 初始化
newInstance = itchat.new_instance()
newInstance.auto_login(enableCmdQR=2)
newInstance.auto_login(hotReload=False, enableCmdQR=2)
newInstance.global_keys = ["人工智能", "机器学习", "算法", "数据挖掘"]
newInstance.to_user_name = "filehelper"
# 获取自己的属性字典
owner = newInstance.search_friends(name=None)
# 获取自己的属性字典UserName, NickName, RemarkName, Sex(1 or 2)
newInstance.owner = newInstance.loginInfo["User"]
# 获取通讯录: {UserName: UserInstance}
friends = {item["UserName"]: item for item in newInstance.get_friends(update=True)}
newInstance.friends = {user["UserName"]: user for user in newInstance.get_friends(update=True)}
# 消息存储队列
msg_store = {}
newInstance.msg_store = {}
# 消息提取函数
@ -34,22 +36,33 @@ def get_msg_list(msg):
"""
提取消息内容
"""
logging.warning("%s: %s", msg["MsgType"], msg)
logging.warning("%s", msg)
msg_id = msg["MsgId"] # 消息ID
from_user_name = msg["FromUserName"] # 消息发送者ID
to_user_name = msg["ToUserName"] # 消息接受者ID
msg_type = msg["MsgType"] # 消息类型
msg_content = msg["Content"] # 消息内容
msg_file = msg["FileName"] # 消息中所带文件的名称
msg_id = msg["MsgId"] # 消息ID
from_user_name = msg["FromUserName"] # 消息发送者ID
to_user_name = msg["ToUserName"] # 消息接受者ID
msg_type = msg["MsgType"] # 消息类型
msg_content = msg["Content"] # 消息内容
msg_time = datetime.datetime.fromtimestamp(msg["CreateTime"]) # 消息发送时间
wind_name = msg["User"]["NickName"] # 聊天窗口名称
user_name = msg["ActualUserName"] # 消息发送者的名称
nick_name = msg["ActualNickName"] if (user_name not in friends) or (friends[user_name]["RemarkName"] == "") else friends[user_name]["RemarkName"]
msg_file = msg["FileName"] # 消息中所带文件的名称
msg_url = msg["Url"] # 消息中带有的链接地址
print(msg_id, from_user_name, to_user_name, msg_type, msg_content, msg_file, msg_time, wind_name, nick_name)
return msg_id, from_user_name, to_user_name, msg_type, msg_content, msg_file, msg_time, wind_name, nick_name
if from_user_name.startswith("@@"):
# 群消息
wind_name = msg["User"]["RemarkName"] if msg["User"]["RemarkName"] else msg["User"]["NickName"]
nick_name = msg["ActualNickName"] if (msg["ActualUserName"] not in newInstance.friends) or \
(not newInstance.friends[msg["ActualUserName"]]["RemarkName"]) else newInstance.friends[msg["ActualUserName"]]["RemarkName"]
else:
# 个人消息
wind_name = msg["User"]["RemarkName"] if msg["User"]["RemarkName"] else msg["User"]["NickName"]
nick_name = wind_name
we_type = msg["Type"] # 消息类型
we_text = msg["Text"] # 消息内容
return msg_id, from_user_name, to_user_name, msg_type, msg_content, msg_time, msg_file, msg_url, wind_name, nick_name, we_type, we_text
# 消息注册,主要处理群消息
@ -59,37 +72,61 @@ def text_reply(msg):
消息自动接收, 接受全部的消息
"""
# 消息提取
msg_id, from_user_name, to_user_name, msg_type, msg_content, msg_file, msg_time, wind_name, nick_name = get_msg_list(msg)
msg_id, from_user_name, to_user_name, msg_type, msg_content, msg_time, msg_file, msg_url, wind_name, nick_name, we_type, we_text = get_msg_list(msg)
# 消息存储,并删除过期消息
msg_store[msg_id] = msg
for _id in msg_store:
if time.time() - msg_store[_id]["CreateTime"] > 120:
msg_store.pop(_id)
# 消息过滤,过滤自己发送的消息
if from_user_name == newInstance.owner["UserName"]:
logging.warning("message from myself, skip")
return
# 消息过滤, 只监测文字、注解、分享、图片、语音、视频、附件等
if we_type not in ["Text", "Note", "Sharing", "Picture", "Recording", "Video", "Attachment"]:
logging.warning("message ignored")
return
if we_type in ["Picture", "Recording", "Video", "Attachment"]:
re_length = re.search("[\"\s]length=\"(?P<length>[\d]+?)\"", msg_content, flags=re.IGNORECASE)
if (not msg_content) or (re_length and (int(re_length.group("length")) < 5000000)):
we_text(".Cache/" + msg_file)
logging.warning("downloading %s to .Cache/", msg_file)
# 消息存储
newInstance.msg_store[msg_id] = msg
# 删除过期消息
ids_list = [_id for _id in newInstance.msg_store if time.time() - newInstance.msg_store[_id]["CreateTime"] > 120]
for _id in ids_list:
logging.warning("delete message, message_id = %s", _id)
newInstance.msg_store.pop(_id)
# 处理群消息
if from_user_name.startswith("@@"):
# 红包消息处理
if msg_type == 10000 and msg_content.find("红包"):
newInstance.send("%s】中有红包,快抢!\nFrom: %s\nContent: %s\nTime: %s" % (wind_name, nick_name, msg_content, msg_time), toUserName="filehelper")
if we_type == "Note" and we_text.find("收到红包,请在手机上查看") >= 0:
newInstance.send("%s】中有红包,快抢!\nFrom: %s\nContent: %s\nTime: %s" % (wind_name, nick_name, msg_content, msg_time), toUserName=newInstance.to_user_name)
# 提到自己消息处理
if msg["IsAt"]:
newInstance.send("%s】中有@你的消息:\nFrom: %s\nContent: %s\nTime: %s" % (wind_name, nick_name, msg_content, msg_time), toUserName="filehelper")
newInstance.send("%s】中有@你的消息:\nFrom: %s\nContent: %s\nTime: %s" % (wind_name, nick_name, msg_content, msg_time), toUserName=newInstance.to_user_name)
for key in newInstance.global_keys:
if msg_content.find(key) >= 0:
newInstance.send("%s】中有关键字【%s】:\nFrom: %s\nContent: %s\nTime: %s" % (wind_name, key, nick_name, msg_content, msg_time), toUserName="filehelper")
newInstance.send("%s】中有关键字【%s】:\nFrom: %s\nContent: %s\nTime: %s" % (wind_name, key, nick_name, msg_content, msg_time), toUserName=newInstance.to_user_name)
break
# 撤回消息处理
if msg_type == 10002 and msg_content.find("撤回"):
old_msg_id = msg_content[msg_content.find("<msgid>")+7: msg_content.find("</msgid>")]
msg_id, from_user_name, to_user_name, msg_type, msg_content, msg_file, msg_time, wind_name, nick_name = get_msg_list(msg_store[old_msg_id])
newInstance.send("%s】中有消息被撤回:\nFrom: %s\nContent: %s\nFile: %s\nTime: %s" % (wind_name, nick_name, msg_content, msg_file, msg_time), toUserName="filehelper")
if msg_file:
msg_store[old_msg_id].download(msg_file)
if we_type == "Note" and we_text.find("撤回了一条消息") >= 0:
old_msg = newInstance.msg_store.get(msg_content[msg_content.find("<msgid>")+7: msg_content.find("</msgid>")])
if not old_msg:
return
msg_id, from_user_name, to_user_name, msg_type, msg_content, msg_time, msg_file, msg_url, wind_name, nick_name, we_type, we_text = get_msg_list(old_msg)
if we_type in ["Picture", "Recording", "Video", "Attachment"]:
msg_content = msg_file
elif we_type == "Sharing":
msg_content = we_text + ": " + msg_url
newInstance.send("%s】中有消息被撤回:\nFrom: %s\nType: %s\nContent: %s\nTime: %s" % (wind_name, nick_name, we_type, msg_content, msg_time), toUserName=newInstance.to_user_name)
return