Skip to content

Commit

Permalink
Merge pull request #63 from liuyang007/master
Browse files Browse the repository at this point in the history
optimize some code
  • Loading branch information
liuwons committed May 13, 2016
2 parents cd4f757 + 656e5dc commit ddc795f
Showing 1 changed file with 8 additions and 30 deletions.
38 changes: 8 additions & 30 deletions wxbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import os
import sys
import traceback
import webbrowser
import pyqrcode
import requests
Expand All @@ -12,6 +13,7 @@
import time
import re
import random

from requests.exceptions import ConnectionError, ReadTimeout
import HTMLParser

Expand All @@ -21,21 +23,21 @@
TIMEOUT = '408'


def show_image(file):
def show_image(file_path):
"""
跨平台显示图片文件
:param file: 图片文件路径
:param file_path: 图片文件路径
"""
if sys.version_info >= (3, 3):
from shlex import quote
else:
from pipes import quote

if sys.platform == "darwin":
command = "open -a /Applications/Preview.app %s&" % quote(file)
command = "open -a /Applications/Preview.app %s&" % quote(file_path)
os.system(command)
else:
webbrowser.open(file)
webbrowser.open(file_path)

class SafeSession(requests.Session):
def request(self, method, url, params=None, data=None, headers=None, cookies=None, files=None, auth=None,
Expand All @@ -46,7 +48,8 @@ def request(self, method, url, params=None, data=None, headers=None, cookies=Non
return super(SafeSession, self).request(method, url, params, data, headers, cookies, files, auth,
timeout,
allow_redirects, proxies, hooks, stream, verify, cert, json)
except:
except Exception as e:
print e.message,traceback.format_exc()
continue


Expand Down Expand Up @@ -227,13 +230,6 @@ def get_group_member_info(self, uid):
else:
return None

def get_group_member_info(self, uid, gid):
if gid not in self.group_members:
return None
for member in self.group_members[gid]:
if member['UserName'] == uid:
return {'type': 'group_member', 'info': member}
return None

def get_contact_name(self, uid):
info = self.get_contact_info(uid)
Expand Down Expand Up @@ -269,22 +265,6 @@ def get_group_member_name(self, uid):
else:
return name

def get_group_member_name(self, uid, gid):
info = self.get_group_member_info(uid, gid)
if info is None:
return None
info = info['info']
name = {}
if 'RemarkName' in info and info['RemarkName']:
name['remark_name'] = info['RemarkName']
if 'NickName' in info and info['NickName']:
name['nickname'] = info['NickName']
if 'DisplayName' in info and info['DisplayName']:
name['display_name'] = info['DisplayName']
if len(name) == 0:
return None
else:
return name

@staticmethod
def get_contact_prefer_name(name):
Expand Down Expand Up @@ -505,7 +485,6 @@ def extract_msg_content(self, msg_type_id, msg):
print ' %s[Animation] %s' % (msg_prefix, msg_content['data'])
elif mtype == 49:
msg_content['type'] = 7
app_msg_type = ''
if msg['AppMsgType'] == 3:
app_msg_type = 'music'
elif msg['AppMsgType'] == 5:
Expand Down Expand Up @@ -570,7 +549,6 @@ def handle_msg(self, r):
:param r: 原始微信消息
"""
for msg in r['AddMsgList']:
msg_type_id = 99
user = {'id': msg['FromUserName'], 'name': 'unknown'}
if msg['MsgType'] == 51: # init message
msg_type_id = 0
Expand Down

0 comments on commit ddc795f

Please sign in to comment.