From a783fdce88bbf29f7ee3519476da5f0ca0f5bea7 Mon Sep 17 00:00:00 2001 From: BillBillBillBill Date: Sat, 6 Feb 2016 19:12:09 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dlinux=E4=B8=8B=E4=B8=8D?= =?UTF-8?q?=E8=83=BD=E6=89=93=E5=BC=80=E6=96=87=E4=BB=B6=E7=9A=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98=20=E4=BF=AE=E5=A4=8D=E4=B8=80=E4=B8=AA=E8=8E=B7?= =?UTF-8?q?=E5=8F=96=E5=88=86=E4=BA=AB=E4=BF=A1=E6=81=AF=E6=97=B6=E5=87=BA?= =?UTF-8?q?=E7=8E=B0keyerror=E5=B4=A9=E6=BA=83=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- weixin.py | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/weixin.py b/weixin.py index 3d95d07..881e9b9 100755 --- a/weixin.py +++ b/weixin.py @@ -8,6 +8,8 @@ import xml.dom.minidom import json import time, re, sys, os, random import multiprocessing +import platform +from collections import defaultdict def catchKeyboardInterrupt(fn): def wrapper(*args): @@ -177,7 +179,7 @@ class WebWeixin(object): url = self.base_uri + '/webwxgetcontact?pass_ticket=%s&skey=%s&r=%s' % (self.pass_ticket, self.skey, int(time.time())) dic = self._post(url, {}) self.MemberList = dic['MemberList'] - + ContactList = self.MemberList[:] SpecialUsers = ['newsapp', 'fmessage', 'filehelper', 'weibo', 'qqmail', 'fmessage', 'tmessage', 'qmessage', 'qqsync', 'floatbottle', 'lbsapp', 'shakeapp', 'medianote', 'qqfriend', 'readerapp', 'blogapp', 'facebookapp', 'masssendapp', 'meishiapp', 'feedsapp', 'voip', 'blogappweixin', 'weixin', 'brandsessionholder', 'weixinreminder', 'wxid_novlwrv3lqwv11', 'gh_22b87fa7cb3c', 'officialaccounts', 'notification_messages', 'wxid_novlwrv3lqwv11', 'gh_22b87fa7cb3c', 'wxitil', 'userexperience_alarm', 'notification_messages'] for i in xrange(len(ContactList) - 1, -1, -1): @@ -238,7 +240,8 @@ class WebWeixin(object): 'rr': ~int(time.time()) } dic = self._post(url, params) - if self.DEBUG: print dic + if self.DEBUG: + print json.dumps(dic, indent=4) if dic['BaseResponse']['Ret'] == 0: self.SyncKey = dic['SyncKey'] @@ -353,13 +356,13 @@ class WebWeixin(object): else: print '自动回复失败' elif msgType == 3: - img = self.webwxgetmsgimg(msgid) - print '%s 给你发送了一张图片: %s' % (name, img) - os.system('open %s' % img) + image = self.webwxgetmsgimg(msgid) + print '%s 给你发送了一张图片: %s' % (name, image) + self._safe_open(image) elif msgType == 34: voice = self.webwxgetvoice(msgid) print '%s 给你发了一段语音: %s' % (name, voice) - os.system('open %s' % voice) + self._safe_open(voice) elif msgType == 42: info = msg['RecommendInfo'] print '%s 给你发送了一张名片:' % name @@ -372,9 +375,10 @@ class WebWeixin(object): elif msgType == 47: url = self._searchContent('cdnurl', content) print '%s 给你发了一个动画表情,点击下面链接查看:\n%s' % (name, url) - os.system('open %s' % url) + self._safe_open(url) elif msgType == 49: - appMsgType = {5:'链接', 3:'音乐'} + appMsgType = defaultdict(lambda : "") + appMsgType.update({5:'链接', 3:'音乐', 7:'微博'}) print '%s 给你分享了一个%s:' % (name, appMsgType[msg['AppMsgType']]) print '=========================' print '= 标题: %s' % msg['FileName'] @@ -451,7 +455,7 @@ class WebWeixin(object): print '[*] 自动回复模式 ... 开启' else: print '[*] 自动回复模式 ... 关闭' - + listenProcess = multiprocessing.Process(target=self.listenMsgMode) listenProcess.start() @@ -471,6 +475,12 @@ class WebWeixin(object): elif text[:3] == 'i->': print '发送图片' + def _safe_open(self, path): + if platform.system() == "Linux": + os.system("xdg-open %s &" % path) + else: + os.system('open %s &' % path) + def _run(self, str, func, *args): self._echo(str) if func(*args): print '成功'