Merge pull request #183 from Jolly23/master

fix: dev: 增加命令行显示二维码功能及开关 & 使用timeout参数提升微信初始化时线路检测速度 & 解决常见ssl错误
This commit is contained in:
Urinx
2017-04-16 20:13:49 +08:00
committed by GitHub
2 changed files with 54 additions and 18 deletions
+2
View File
@@ -6,3 +6,5 @@ qrcode
requests requests
six six
requests_toolbelt requests_toolbelt
pyqrcode
certifi
+37 -3
View File
@@ -1,6 +1,7 @@
#!/usr/bin/env python #!/usr/bin/env python
# coding: utf-8 # coding: utf-8
import qrcode import qrcode
from pyqrcode import QRCode
import urllib.request, urllib.parse, urllib.error import urllib.request, urllib.parse, urllib.error
import urllib.request, urllib.error, urllib.parse import urllib.request, urllib.error, urllib.parse
import http.cookiejar import http.cookiejar
@@ -8,6 +9,7 @@ import requests
import xml.dom.minidom import xml.dom.minidom
import json import json
import time import time
import ssl
import re import re
import sys import sys
import os import os
@@ -20,6 +22,7 @@ import http.client
from collections import defaultdict from collections import defaultdict
from urllib.parse import urlparse from urllib.parse import urlparse
from lxml import html from lxml import html
from socket import timeout as timeout_error
#import pdb #import pdb
# for media upload # for media upload
@@ -83,6 +86,7 @@ class WebWeixin(object):
def __init__(self): def __init__(self):
self.DEBUG = False self.DEBUG = False
self.commandLineQRCode = False
self.uuid = '' self.uuid = ''
self.base_uri = '' self.base_uri = ''
self.redirect_uri = '' self.redirect_uri = ''
@@ -167,6 +171,10 @@ class WebWeixin(object):
self._str2qr('https://login.weixin.qq.com/l/' + self.uuid) self._str2qr('https://login.weixin.qq.com/l/' + self.uuid)
def _showQRCodeImg(self, str): def _showQRCodeImg(self, str):
if self.commandLineQRCode:
qrCode = QRCode('https://login.weixin.qq.com/l/' + self.uuid)
self._showCommandLineQRCode(qrCode.text(1))
else:
url = 'https://login.weixin.qq.com/qrcode/' + self.uuid url = 'https://login.weixin.qq.com/qrcode/' + self.uuid
params = { params = {
't': 'webwx', 't': 'webwx',
@@ -184,6 +192,28 @@ class WebWeixin(object):
else: else:
return return
def _showCommandLineQRCode(self, qr_data, enableCmdQR=2):
try:
b = u'\u2588'
sys.stdout.write(b + '\r')
sys.stdout.flush()
except UnicodeEncodeError:
white = 'MM'
else:
white = b
black = ' '
blockCount = int(enableCmdQR)
if abs(blockCount) == 0:
blockCount = 1
white *= abs(blockCount)
if blockCount < 0:
white, black = black, white
sys.stdout.write(' ' * 50 + '\r')
sys.stdout.flush()
qr = qr_data.replace('0', white).replace('1', black)
sys.stdout.write(qr)
sys.stdout.flush()
def waitForLogin(self, tip=1): def waitForLogin(self, tip=1):
time.sleep(tip) time.sleep(tip)
url = 'https://login.weixin.qq.com/cgi-bin/mmwebwx-bin/login?tip=%s&uuid=%s&_=%s' % ( url = 'https://login.weixin.qq.com/cgi-bin/mmwebwx-bin/login?tip=%s&uuid=%s&_=%s' % (
@@ -377,7 +407,7 @@ class WebWeixin(object):
'_': int(time.time()), '_': int(time.time()),
} }
url = 'https://' + self.syncHost + '/cgi-bin/mmwebwx-bin/synccheck?' + urllib.parse.urlencode(params) url = 'https://' + self.syncHost + '/cgi-bin/mmwebwx-bin/synccheck?' + urllib.parse.urlencode(params)
data = self._get(url) data = self._get(url, timeout=5)
if data == '': if data == '':
return [-1,-1] return [-1,-1]
@@ -1065,7 +1095,7 @@ class WebWeixin(object):
result = data.decode('utf-8') result = data.decode('utf-8')
return result return result
def _get(self, url: object, api: object = None) -> object: def _get(self, url: object, api: object = None, timeout: object = None) -> object:
request = urllib.request.Request(url=url) request = urllib.request.Request(url=url)
request.add_header('Referer', 'https://wx.qq.com/') request.add_header('Referer', 'https://wx.qq.com/')
if api == 'webwxgetvoice': if api == 'webwxgetvoice':
@@ -1073,7 +1103,7 @@ class WebWeixin(object):
if api == 'webwxgetvideo': if api == 'webwxgetvideo':
request.add_header('Range', 'bytes=0-') request.add_header('Range', 'bytes=0-')
try: try:
response = urllib.request.urlopen(request) response = urllib.request.urlopen(request, timeout=timeout) if timeout else urllib.request.urlopen(request)
data = response.read().decode('utf-8') data = response.read().decode('utf-8')
logging.debug(url) logging.debug(url)
return data return data
@@ -1083,6 +1113,10 @@ class WebWeixin(object):
logging.error('URLError = ' + str(e.reason)) logging.error('URLError = ' + str(e.reason))
except http.client.HTTPException as e: except http.client.HTTPException as e:
logging.error('HTTPException') logging.error('HTTPException')
except timeout_error as e:
pass
except ssl.CertificateError as e:
pass
except Exception: except Exception:
import traceback import traceback
logging.error('generic exception: ' + traceback.format_exc()) logging.error('generic exception: ' + traceback.format_exc())