update python_wechat.py

master
xianhu 2017-05-16 12:03:28 +08:00
parent e37980bd4a
commit ffb22e7a33
1 changed files with 30 additions and 27 deletions

View File

@ -4,11 +4,9 @@
python_wechat.py by xianhu
主要包括如下功能
(1) 自动提醒群红包
(2) 自动提醒群中带有特殊关键字的消息
(3) 自动保存被撤回消息包括内容文件图片语音视频等
(2) 自动保存被撤回消息包括内容文件图片语音视频等
"""
import re
import time
import itchat
import logging
@ -17,15 +15,15 @@ from itchat.content import *
# 初始化
my = itchat.new_instance()
my.auto_login(hotReload=False, enableCmdQR=-2)
my.global_keys = ["创业", "齐现虎"]
my.auto_login(hotReload=False, enableCmdQR=2)
my.global_keys = ["创业", "算法", "人工智能"]
my.to_user_name = "filehelper"
# my还包括的以下属性注意用点.查看:
# (1) alive 是否还活着isLogging 是否已登陆
# (2) loginInfo 登陆信息其中的User属性为自己的信息User字典类包括UserName, NickName, RemarkName, Sex(1 or 2)
# (3) memberList 通讯录列表每一项为一个User字典类包括UserName, NickName, RemarkName, Sex(1 or 2)
# (4) chatroomList 群聊列表每一项为一个Chatroom字典类包括UserName, NickName, RemarkName, MemberCount, MemberList
# (2) loginInfo 登陆信息其中的User属性为自己的信息User字典类包括UserName, NickName, RemarkName, Sex(1 or 2), Signature, Province, City
# (3) memberList 通讯录列表每一项为一个User字典类包括UserName, NickName, RemarkName, Sex(1 or 2), Signature, Province, City
# (4) chatroomList 群聊列表每一项为一个Chatroom字典类包括UserName, NickName, RemarkName, MemberCount, MemberList, Self
# (5) mpList 订阅号列表每一项为一个MassivePlatform字典类包括UserName, NickName等
# 获取并更新通讯录: {UserName: UserInstance}
@ -41,7 +39,7 @@ def get_msg_list(msg):
提取消息内容消息来源分类
1来自好友的消息
2来自自己的消息
3来自文件传输助手的消息
3来自文件传输助手的消息
4来自群聊的消息
提取消息内容消息类型分类
1文字
@ -54,7 +52,7 @@ def get_msg_list(msg):
8分享
9附件
"""
# logging.warning("%s", msg)
logging.warning("message: %s", msg)
msg_id = msg["MsgId"] # 消息ID
from_user_name = msg["FromUserName"] # 消息发送者ID
to_user_name = msg["ToUserName"] # 消息接受者ID
@ -64,7 +62,11 @@ def get_msg_list(msg):
msg_time = datetime.datetime.fromtimestamp(msg["CreateTime"]) # 消息发送时间
msg_file = msg["FileName"] # 消息中所带文件的名称
msg_url = msg["Url"] # 消息中带有的链接地址
msg_file_length = msg["FileSize"] # 消息中所带文件的大小
msg_file_length = int(msg_file_length) if msg_file_length.strip() else 0
msg_voice_length = msg["VoiceLength"] # 消息中所带语音的长度(毫秒)
msg_play_length = msg["PlayLength"] # 消息中所带视频的长度(秒)
msg_url = msg["Url"] # 消息中所带链接的地址
wind_name = msg["User"]["RemarkName"] if msg["User"].get("RemarkName") else (
msg["User"]["NickName"] if msg["User"].get("NickName") else to_user_name
@ -79,8 +81,9 @@ def get_msg_list(msg):
we_type = msg["Type"] # 消息类型
we_text = msg["Text"] # 消息内容
logging.warning("wind: %s, nick:%s, %s/%s, %s/%s, %s", wind_name, nick_name, msg_type, we_type, msg_content, we_text, msg_time)
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
logging.warning("show: nick_name=%s, wind_name=%s, we_type=%s, we_text=%s", nick_name, wind_name, we_type, we_text)
return msg_id, from_user_name, to_user_name, msg_type, msg_content, msg_time, \
msg_file, msg_file_length, msg_voice_length, msg_play_length, msg_url, wind_name, nick_name, we_type, we_text
# 消息注册,主要处理群消息
@ -90,7 +93,8 @@ def text_reply(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_id, from_user_name, to_user_name, msg_type, msg_content, msg_time, \
msg_file, msg_file_length, msg_voice_length, msg_play_length, msg_url, wind_name, nick_name, we_type, we_text = get_msg_list(msg)
# 消息过滤, 只监测文字、注解、分享、图片、语音、视频、附件等
if we_type not in ["Text", "Note", "Sharing", "Picture", "Recording", "Video", "Attachment"]:
@ -114,12 +118,6 @@ def text_reply(msg):
if we_type == "Note" and we_text.find("收到红包,请在手机上查看") >= 0:
my.send("%s】中有红包,快抢!" % wind_name, toUserName=my.to_user_name)
# 关键字消息处理
for key in my.global_keys:
if msg_content.find(key) >= 0:
my.send("%s】中有关键字【%s】:\nFrom: %s\nTime: %s\n%s" % (wind_name, key, nick_name, msg_time, msg_content), toUserName=my.to_user_name)
break
# 撤回消息处理
if we_type == "Note" and we_text.find("撤回了一条消息") >= 0:
old_msg = my.msg_store.get(msg_content[msg_content.find("<msgid>")+7: msg_content.find("</msgid>")])
@ -127,13 +125,18 @@ def text_reply(msg):
logging.warning("not message id in my.msg_store")
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"]:
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)):
msg_id, from_user_name, to_user_name, msg_type, msg_content, msg_time, \
msg_file, msg_file_length, msg_voice_length, msg_play_length, msg_url, wind_name, nick_name, we_type, we_text = get_msg_list(old_msg)
if we_type == "Text":
msg_content = we_text
elif we_type in ["Picture", "Recording", "Video", "Attachment"]:
if (msg_file_length <= 500000) and (msg_voice_length <= 60000) and (msg_play_length <= 10):
try:
we_text(".Cache/" + msg_file)
logging.warning("downloading %s to .Cache/", msg_file)
# 更改内容
except:
logging.error("downloading %s to .Cache/ error", msg_file)
msg_content = msg_file
elif we_type == "Sharing":
msg_content = we_text + ": " + msg_url