增加 logging 支持
Changes to be committed: modified: weixin.py
This commit is contained in:
@@ -9,6 +9,7 @@ import json
|
|||||||
import time, re, sys, os, random
|
import time, re, sys, os, random
|
||||||
import multiprocessing
|
import multiprocessing
|
||||||
import platform
|
import platform
|
||||||
|
import logging
|
||||||
from collections import defaultdict
|
from collections import defaultdict
|
||||||
|
|
||||||
def catchKeyboardInterrupt(fn):
|
def catchKeyboardInterrupt(fn):
|
||||||
@@ -17,6 +18,7 @@ def catchKeyboardInterrupt(fn):
|
|||||||
return fn(*args)
|
return fn(*args)
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
print '\n[*] 强制退出程序'
|
print '\n[*] 强制退出程序'
|
||||||
|
logging.debug('[*] 强制退出程序')
|
||||||
return wrapper
|
return wrapper
|
||||||
|
|
||||||
def _decode_list(data):
|
def _decode_list(data):
|
||||||
@@ -268,6 +270,7 @@ class WebWeixin(object):
|
|||||||
dic = self._post(url, params)
|
dic = self._post(url, params)
|
||||||
if self.DEBUG:
|
if self.DEBUG:
|
||||||
print json.dumps(dic, indent=4)
|
print json.dumps(dic, indent=4)
|
||||||
|
logging.debug(json.dumps(dic, indent=4))
|
||||||
|
|
||||||
if dic['BaseResponse']['Ret'] == 0:
|
if dic['BaseResponse']['Ret'] == 0:
|
||||||
self.SyncKey = dic['SyncKey']
|
self.SyncKey = dic['SyncKey']
|
||||||
@@ -318,7 +321,7 @@ class WebWeixin(object):
|
|||||||
# Not work now for weixin haven't support this API
|
# Not work now for weixin haven't support this API
|
||||||
def webwxgetvideo(self, msgid):
|
def webwxgetvideo(self, msgid):
|
||||||
url = self.base_uri + '/webwxgetvideo?msgid=%s&skey=%s' % (msgid, self.skey)
|
url = self.base_uri + '/webwxgetvideo?msgid=%s&skey=%s' % (msgid, self.skey)
|
||||||
data = self._get(url)
|
data = self._get(url, api='webwxgetvideo')
|
||||||
fn = 'video_'+msgid+'.mp4'
|
fn = 'video_'+msgid+'.mp4'
|
||||||
with open(fn, 'wb') as f: f.write(data)
|
with open(fn, 'wb') as f: f.write(data)
|
||||||
return fn
|
return fn
|
||||||
@@ -347,11 +350,13 @@ class WebWeixin(object):
|
|||||||
def handleMsg(self, r):
|
def handleMsg(self, r):
|
||||||
for msg in r['AddMsgList']:
|
for msg in r['AddMsgList']:
|
||||||
print '[*] 你有新的消息,请注意查收'
|
print '[*] 你有新的消息,请注意查收'
|
||||||
|
logging.debug('[*] 你有新的消息,请注意查收')
|
||||||
|
|
||||||
if self.DEBUG:
|
if self.DEBUG:
|
||||||
fn = 'msg' + str(int(random.random() * 1000)) + '.json'
|
fn = 'msg' + str(int(random.random() * 1000)) + '.json'
|
||||||
with open(fn, 'w') as f: f.write(json.dumps(msg))
|
with open(fn, 'w') as f: f.write(json.dumps(msg))
|
||||||
print '[*] 该消息已储存到文件: ' + fn
|
print '[*] 该消息已储存到文件: ' + fn
|
||||||
|
logging.debug('[*] 该消息已储存到文件: %s' % (fn))
|
||||||
|
|
||||||
msgType = msg['MsgType']
|
msgType = msg['MsgType']
|
||||||
name = self.getUserRemarkName(msg['FromUserName'])
|
name = self.getUserRemarkName(msg['FromUserName'])
|
||||||
@@ -359,36 +364,45 @@ class WebWeixin(object):
|
|||||||
msgid = msg['MsgId']
|
msgid = msg['MsgId']
|
||||||
if msgType == 51:
|
if msgType == 51:
|
||||||
print '[*] 成功截获微信初始化消息'
|
print '[*] 成功截获微信初始化消息'
|
||||||
|
logging.debug('[*] 成功截获微信初始化消息')
|
||||||
elif msgType == 1:
|
elif msgType == 1:
|
||||||
if content.find('http://weixin.qq.com/cgi-bin/redirectforward?args=') != -1:
|
if content.find('http://weixin.qq.com/cgi-bin/redirectforward?args=') != -1:
|
||||||
# 地理位置消息
|
# 地理位置消息
|
||||||
data = self._get(content).decode('gbk').encode('utf-8')
|
data = self._get(content).decode('gbk').encode('utf-8')
|
||||||
pos = self._searchContent('title', data, 'xml')
|
pos = self._searchContent('title', data, 'xml')
|
||||||
print '%s 给你发送了一个位置消息 [我在%s]' % (name, pos)
|
print '%s 给你发送了一个位置消息 [我在%s]' % (name, pos)
|
||||||
|
logging.info('%s 给你发送了一个位置消息 [我在%s]' % (name, pos))
|
||||||
elif msg['ToUserName'] == 'filehelper':
|
elif msg['ToUserName'] == 'filehelper':
|
||||||
print '%s -> 文件传输助手: %s' % (name, content.replace('<br/>','\n'))
|
print '%s -> 文件传输助手: %s' % (name, content.replace('<br/>','\n'))
|
||||||
|
logging.info('%s -> 文件传输助手: %s' % (name, content.replace('<br/>','\n')))
|
||||||
elif msg['FromUserName'] == self.User['UserName']:
|
elif msg['FromUserName'] == self.User['UserName']:
|
||||||
pass
|
pass
|
||||||
elif msg['FromUserName'][:2] == '@@':
|
elif msg['FromUserName'][:2] == '@@':
|
||||||
[people, content] = content.split(':<br/>')
|
[people, content] = content.split(':<br/>')
|
||||||
group = self.getUserRemarkName(msg['FromUserName'])
|
group = self.getUserRemarkName(msg['FromUserName'])
|
||||||
name = self.getUserRemarkName(people)
|
name = self.getUserRemarkName(people)
|
||||||
print '|%s| %s: %s' % (group, name, content.replace('<br/>','\n'))
|
print '|%s| %s: %s' % (group.strip(), name.strip(), content.replace('<br/>','\n'))
|
||||||
|
logging.info('|%s| %s: %s' % (group.strip(), name.strip(), content.replace('<br/>','\n')))
|
||||||
else:
|
else:
|
||||||
print name+': '+content
|
print name+': '+content
|
||||||
|
logging.info(name+': '+content)
|
||||||
if self.autoReplyMode:
|
if self.autoReplyMode:
|
||||||
ans = self._xiaodoubi(content)+'\n[微信机器人自动回复]'
|
ans = self._xiaodoubi(content)+'\n[微信机器人自动回复]'
|
||||||
if self.webwxsendmsg(ans, msg['FromUserName']):
|
if self.webwxsendmsg(ans, msg['FromUserName']):
|
||||||
print '自动回复: '+ans
|
print '自动回复: '+ans
|
||||||
|
logging.info('自动回复: '+ans)
|
||||||
else:
|
else:
|
||||||
print '自动回复失败'
|
print '自动回复失败'
|
||||||
|
logging.info('自动回复失败')
|
||||||
elif msgType == 3:
|
elif msgType == 3:
|
||||||
image = self.webwxgetmsgimg(msgid)
|
image = self.webwxgetmsgimg(msgid)
|
||||||
print '%s 给你发送了一张图片: %s' % (name, image)
|
print '%s 给你发送了一张图片: %s' % (name, image)
|
||||||
|
logging.info('%s 给你发送了一张图片: %s' % (name, image))
|
||||||
self._safe_open(image)
|
self._safe_open(image)
|
||||||
elif msgType == 34:
|
elif msgType == 34:
|
||||||
voice = self.webwxgetvoice(msgid)
|
voice = self.webwxgetvoice(msgid)
|
||||||
print '%s 给你发了一段语音: %s' % (name, voice)
|
print '%s 给你发了一段语音: %s' % (name, voice)
|
||||||
|
logging.info('%s 给你发送了一张图片: %s' % (name, image))
|
||||||
self._safe_open(voice)
|
self._safe_open(voice)
|
||||||
elif msgType == 42:
|
elif msgType == 42:
|
||||||
info = msg['RecommendInfo']
|
info = msg['RecommendInfo']
|
||||||
@@ -399,9 +413,11 @@ class WebWeixin(object):
|
|||||||
print '= 地区: %s %s' % (info['Province'], info['City'])
|
print '= 地区: %s %s' % (info['Province'], info['City'])
|
||||||
print '= 性别: %s' % ['未知', '男', '女'][info['Sex']]
|
print '= 性别: %s' % ['未知', '男', '女'][info['Sex']]
|
||||||
print '========================='
|
print '========================='
|
||||||
|
logging.info('%s 给你发送了一张名片: %s' % (name.strip(), json.dumps(info)))
|
||||||
elif msgType == 47:
|
elif msgType == 47:
|
||||||
url = self._searchContent('cdnurl', content)
|
url = self._searchContent('cdnurl', content)
|
||||||
print '%s 给你发了一个动画表情,点击下面链接查看:\n%s' % (name, url)
|
print '%s 给你发了一个动画表情,点击下面链接查看:\n%s' % (name, url)
|
||||||
|
logging.info('%s 给你发了一个动画表情,点击下面链接查看:\n%s' % (name, url))
|
||||||
self._safe_open(url)
|
self._safe_open(url)
|
||||||
elif msgType == 49:
|
elif msgType == 49:
|
||||||
appMsgType = defaultdict(lambda : "")
|
appMsgType = defaultdict(lambda : "")
|
||||||
@@ -413,25 +429,32 @@ class WebWeixin(object):
|
|||||||
print '= 链接: %s' % msg['Url']
|
print '= 链接: %s' % msg['Url']
|
||||||
print '= 来自: %s' % self._searchContent('appname', content, 'xml')
|
print '= 来自: %s' % self._searchContent('appname', content, 'xml')
|
||||||
print '========================='
|
print '========================='
|
||||||
|
string = "%s 给你分享了一个%s:" % (name, appMsgType[msg['AppMsgType']])
|
||||||
elif msgType == 62:
|
elif msgType == 62:
|
||||||
video = self.webwxgetvideo(msgid)
|
|
||||||
print name+' 给你发了一个小视频,请在手机上查看'
|
print name+' 给你发了一个小视频,请在手机上查看'
|
||||||
|
logging.info(name+' 给你发了一个小视频,请在手机上查看')
|
||||||
|
video = self.webwxgetvideo(msgid)
|
||||||
self._safe_open(video)
|
self._safe_open(video)
|
||||||
elif msgType == 10002:
|
elif msgType == 10002:
|
||||||
print name+' 撤回消息'
|
print name+' 撤回消息'
|
||||||
|
logging.info(name+' 撤回消息')
|
||||||
else:
|
else:
|
||||||
print '[*] 该消息类型为: %d,可能是表情,图片或链接' % msg['MsgType']
|
print '[*] 该消息类型为: %d,可能是表情,图片或链接' % msg['MsgType']
|
||||||
print msg
|
print msg
|
||||||
|
logging.debug('[*] 该消息类型为: %d,可能是表情,图片或链接: ' % (msg['MsgType'], json.dumps(msg)))
|
||||||
|
|
||||||
def listenMsgMode(self):
|
def listenMsgMode(self):
|
||||||
print '[*] 进入消息监听模式 ... 成功'
|
print '[*] 进入消息监听模式 ... 成功'
|
||||||
|
logging.debug('[*] 进入消息监听模式 ... 成功')
|
||||||
self._run('[*] 进行同步线路测试 ... ', self.testsynccheck)
|
self._run('[*] 进行同步线路测试 ... ', self.testsynccheck)
|
||||||
playWeChat = 0
|
playWeChat = 0
|
||||||
while True:
|
while True:
|
||||||
[retcode, selector] = self.synccheck()
|
[retcode, selector] = self.synccheck()
|
||||||
if self.DEBUG: print 'retcode: %s, selector: %s' % (retcode, selector)
|
if self.DEBUG: print 'retcode: %s, selector: %s' % (retcode, selector)
|
||||||
|
logging.debug('retcode: %s, selector: %s' % (retcode, selector))
|
||||||
if retcode == '1100':
|
if retcode == '1100':
|
||||||
print '[*] 你在手机上登出了微信,债见'
|
print '[*] 你在手机上登出了微信,债见'
|
||||||
|
logging.debug('[*] 你在手机上登出了微信,债见')
|
||||||
break
|
break
|
||||||
elif retcode == '0':
|
elif retcode == '0':
|
||||||
if selector == '2':
|
if selector == '2':
|
||||||
@@ -440,6 +463,7 @@ class WebWeixin(object):
|
|||||||
elif selector == '7':
|
elif selector == '7':
|
||||||
playWeChat += 1
|
playWeChat += 1
|
||||||
print '[*] 你在手机上玩微信被我发现了 %d 次' % playWeChat
|
print '[*] 你在手机上玩微信被我发现了 %d 次' % playWeChat
|
||||||
|
logging.debug('[*] 你在手机上玩微信被我发现了 %d 次' % playWeChat)
|
||||||
r = self.webwxsync()
|
r = self.webwxsync()
|
||||||
elif selector == '0':
|
elif selector == '0':
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
@@ -460,10 +484,13 @@ class WebWeixin(object):
|
|||||||
else:
|
else:
|
||||||
if self.webwxsendmsg(word, id):
|
if self.webwxsendmsg(word, id):
|
||||||
print '[*] 消息发送成功'
|
print '[*] 消息发送成功'
|
||||||
|
logging.debug('[*] 消息发送成功')
|
||||||
else:
|
else:
|
||||||
print '[*] 消息发送失败'
|
print '[*] 消息发送失败'
|
||||||
|
logging.debug('[*] 消息发送失败')
|
||||||
else:
|
else:
|
||||||
print '[*] 此用户不存在'
|
print '[*] 此用户不存在'
|
||||||
|
logging.debug('[*] 此用户不存在')
|
||||||
|
|
||||||
def sendMsgToAll(self, word):
|
def sendMsgToAll(self, word):
|
||||||
for contact in self.ContactList:
|
for contact in self.ContactList:
|
||||||
@@ -478,23 +505,26 @@ class WebWeixin(object):
|
|||||||
|
|
||||||
@catchKeyboardInterrupt
|
@catchKeyboardInterrupt
|
||||||
def start(self):
|
def start(self):
|
||||||
print '[*] 微信网页版 ... 开动'
|
self._echo('[*] 微信网页版 ... 开动'); print; logging.debug('[*] 微信网页版 ... 开动')
|
||||||
self._run('[*] 正在获取 uuid ... ', self.getUUID)
|
self._run('[*] 正在获取 uuid ... ', self.getUUID)
|
||||||
print '[*] 正在获取二维码 ... 成功'; self.genQRCode()
|
self._echo('[*] 正在获取二维码 ... 成功'); print; logging.debug('[*] 微信网页版 ... 开动'); self.genQRCode()
|
||||||
self._run('[*] 请使用微信扫描二维码以登录 ... ', self.waitForLogin)
|
self._run('[*] 请使用微信扫描二维码以登录 ... ', self.waitForLogin)
|
||||||
self._run('[*] 请在手机上点击确认以登录 ... ', self.waitForLogin, 0)
|
self._run('[*] 请在手机上点击确认以登录 ... ', self.waitForLogin, 0)
|
||||||
self._run('[*] 正在登录 ... ', self.login)
|
self._run('[*] 正在登录 ... ', self.login)
|
||||||
self._run('[*] 微信初始化 ... ', self.webwxinit)
|
self._run('[*] 微信初始化 ... ', self.webwxinit)
|
||||||
self._run('[*] 开启状态通知 ... ', self.webwxstatusnotify)
|
self._run('[*] 开启状态通知 ... ', self.webwxstatusnotify)
|
||||||
self._run('[*] 获取联系人 ... ', self.webwxgetcontact)
|
self._run('[*] 获取联系人 ... ', self.webwxgetcontact)
|
||||||
print '[*] 共有 %d 位联系人' % len(self.ContactList)
|
self._echo('[*] 共有 %d 位联系人' % len(self.ContactList)); print; logging.debug('[*] 微信网页版 ... 开动')
|
||||||
if self.DEBUG: print self
|
if self.DEBUG: print self
|
||||||
|
logging.debug(self)
|
||||||
|
|
||||||
if raw_input('[*] 是否开启自动回复模式(y/n): ') == 'y':
|
if raw_input('[*] 是否开启自动回复模式(y/n): ') == 'y':
|
||||||
self.autoReplyMode = True
|
self.autoReplyMode = True
|
||||||
print '[*] 自动回复模式 ... 开启'
|
print '[*] 自动回复模式 ... 开启'
|
||||||
|
logging.debug('[*] 自动回复模式 ... 开启')
|
||||||
else:
|
else:
|
||||||
print '[*] 自动回复模式 ... 关闭'
|
print '[*] 自动回复模式 ... 关闭'
|
||||||
|
logging.debug('[*] 自动回复模式 ... 关闭')
|
||||||
|
|
||||||
listenProcess = multiprocessing.Process(target=self.listenMsgMode)
|
listenProcess = multiprocessing.Process(target=self.listenMsgMode)
|
||||||
listenProcess.start()
|
listenProcess.start()
|
||||||
@@ -504,6 +534,7 @@ class WebWeixin(object):
|
|||||||
if text == 'quit':
|
if text == 'quit':
|
||||||
listenProcess.terminate()
|
listenProcess.terminate()
|
||||||
print('[*] 退出微信')
|
print('[*] 退出微信')
|
||||||
|
logging.debug('[*] 退出微信')
|
||||||
exit()
|
exit()
|
||||||
elif text[:2] == '->':
|
elif text[:2] == '->':
|
||||||
[name, word] = text[2:].split(':')
|
[name, word] = text[2:].split(':')
|
||||||
@@ -514,8 +545,10 @@ class WebWeixin(object):
|
|||||||
self.sendMsg(name, file, True)
|
self.sendMsg(name, file, True)
|
||||||
elif text[:3] == 'f->':
|
elif text[:3] == 'f->':
|
||||||
print '发送文件'
|
print '发送文件'
|
||||||
|
logging.debug('发送文件')
|
||||||
elif text[:3] == 'i->':
|
elif text[:3] == 'i->':
|
||||||
print '发送图片'
|
print '发送图片'
|
||||||
|
logging.debug('发送图片')
|
||||||
|
|
||||||
def _safe_open(self, path):
|
def _safe_open(self, path):
|
||||||
if platform.system() == "Linux":
|
if platform.system() == "Linux":
|
||||||
@@ -525,8 +558,8 @@ class WebWeixin(object):
|
|||||||
|
|
||||||
def _run(self, str, func, *args):
|
def _run(self, str, func, *args):
|
||||||
self._echo(str)
|
self._echo(str)
|
||||||
if func(*args): print '成功'
|
if func(*args): print '成功'; logging.debug('%s... 开启' % (str))
|
||||||
else: print('失败\n[*] 退出程序');exit()
|
else: print('失败\n[*] 退出程序'); logging.debug('%s... 失败' % (str)); logging.debug('[*] 退出程序'); exit()
|
||||||
|
|
||||||
def _echo(self, str):
|
def _echo(self, str):
|
||||||
sys.stdout.write(str)
|
sys.stdout.write(str)
|
||||||
@@ -560,6 +593,7 @@ class WebWeixin(object):
|
|||||||
if api == 'webwxgetvideo': request.add_header('Range', 'bytes=0-')
|
if api == 'webwxgetvideo': request.add_header('Range', 'bytes=0-')
|
||||||
response = urllib2.urlopen(request)
|
response = urllib2.urlopen(request)
|
||||||
data = response.read()
|
data = response.read()
|
||||||
|
logging.debug(url)
|
||||||
return data
|
return data
|
||||||
|
|
||||||
def _post(self, url, params, jsonfmt = True):
|
def _post(self, url, params, jsonfmt = True):
|
||||||
@@ -619,5 +653,10 @@ if sys.stdout.encoding == 'cp936':
|
|||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
import coloredlogs
|
||||||
|
coloredlogs.install(level='DEBUG')
|
||||||
|
|
||||||
webwx = WebWeixin()
|
webwx = WebWeixin()
|
||||||
webwx.start()
|
webwx.start()
|
||||||
|
|||||||
Reference in New Issue
Block a user