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
params = {
't': 'webwx',
'_': int(time.time())
}
data = self._post(url, params, False)
if data == '':
return
QRCODE_PATH = self._saveFile('qrcode.jpg', data, '_showQRCodeImg')
if str == 'win':
os.startfile(QRCODE_PATH)
elif str == 'macos':
subprocess.call(["open", QRCODE_PATH])
else:
return
python类QRCode()的实例源码
def get_QR(self, uuid=None, enableCmdQR=False, picDir=None, qrCallback=None):
uuid = uuid or self.uuid
picDir = picDir or config.DEFAULT_QR
qrStorage = io.BytesIO()
qrCode = QRCode('https://login.weixin.qq.com/l/' + uuid)
qrCode.png(qrStorage, scale=10)
if hasattr(qrCallback, '__call__'):
qrCallback(uuid=uuid, status='0', qrcode=qrStorage.getvalue())
else:
if enableCmdQR:
utils.print_cmd_qr(qrCode.text(1), enableCmdQR=enableCmdQR)
else:
with open(picDir, 'wb') as f:
f.write(qrStorage.getvalue())
utils.print_qr(picDir)
return qrStorage
def get_QR(self, uuid=None, enableCmdQR=False, picDir=None, qrCallback=None):
uuid = uuid or self.uuid
picDir = picDir or config.DEFAULT_QR
qrStorage = io.BytesIO()
qrCode = QRCode('https://login.weixin.qq.com/l/' + uuid)
qrCode.png(qrStorage, scale=10)
if hasattr(qrCallback, '__call__'):
qrCallback(uuid=uuid, status='0', qrcode=qrStorage.getvalue())
else:
if enableCmdQR:
utils.print_cmd_qr(qrCode.text(1), enableCmdQR=enableCmdQR)
else:
with open(picDir, 'wb') as f:
f.write(qrStorage.getvalue())
utils.print_qr(picDir)
return qrStorage
def get_QR(self, uuid=None, enableCmdQR=False, picDir=None, qrCallback=None):
uuid = uuid or self.uuid
picDir = picDir or config.DEFAULT_QR
qrStorage = io.BytesIO()
qrCode = QRCode('https://login.weixin.qq.com/l/' + uuid)
qrCode.png(qrStorage, scale=10)
if hasattr(qrCallback, '__call__'):
qrCallback(uuid=uuid, status='0', qrcode=qrStorage.getvalue())
else:
if enableCmdQR:
utils.print_cmd_qr(qrCode.text(1), enableCmdQR=enableCmdQR)
else:
with open(picDir, 'wb') as f:
f.write(qrStorage.getvalue())
utils.print_qr(picDir)
return qrStorage
def _str2qr(self, str):
print(str)
qr = qrcode.QRCode()
qr.border = 1
qr.add_data(str)
qr.make()
# img = qr.make_image()
# img.save("qrcode.png")
#mat = qr.get_matrix()
#self._printQR(mat) # qr.print_tty() or qr.print_ascii()
qr.print_ascii(invert=True)
def make_qr(data, version=4):
qr = pyqrcode.QRCode(data, version=version)
text = qr.text(quiet_zone=1)
return [map(int, row) for row in text.split()]
def console_qr_code(self, uuid, status, qrcode):
status = int(status)
if status == 201:
QR = 'Tap "Confirm" to continue.'
return self.logger.critical(QR)
elif status == 200:
QR = "Successfully authorized."
return self.logger.critical(QR)
elif uuid != self.qr_uuid:
# 0: First QR code
# 408: Updated QR code
QR = "WeChat: Scan QR code with WeChat to continue. (%s, %s)\n" % (uuid, status)
if status == 408:
QR += "Previous code expired. Please scan the new one.\n"
QR += "\n"
qr_url = "https://login.weixin.qq.com/l/" + uuid
qr_obj = QRCode(qr_url)
if self._flag("imgcat_qr", False):
qr_file = io.BytesIO()
qr_obj.png(qr_file, scale=10)
QR += self.imgcat(qr_file, "%s_QR_%s.png" % (self.channel_id, uuid))
else:
QR += qr_obj.terminal()
QR += "\nIf you cannot read the QR code above, " \
"please visit the following URL:\n" \
"https://login.weixin.qq.com/qrcode/" + uuid
return self.logger.critical(QR)
self.qr_uuid = uuid
def master_qr_code(self, uuid, status, qrcode):
status = int(status)
msg = EFBMsg(self)
msg.type = MsgType.Text
msg.source = MsgSource.System
msg.origin = {
'name': '%s Auth' % self.channel_name,
'alias': '%s Auth' % self.channel_name,
'uid': -1
}
if status == 201:
msg.type = MsgType.Text
msg.text = 'Tap "Confirm" to continue.'
elif status == 200:
msg.type = MsgType.Text
msg.text = "Successfully authenticated."
elif uuid != self.qr_uuid:
msg.type = MsgType.Image
path = os.path.join("storage", self.channel_id)
if not os.path.exists(path):
os.makedirs(path)
path = os.path.join(path, 'QR-%s.jpg' % int(time.time()))
self.logger.debug("master_qr_code file path: %s", path)
qr_url = "https://login.weixin.qq.com/l/" + uuid
QRCode(qr_url).png(path, scale=10)
msg.text = 'Scan this QR Code with WeChat to continue.'
msg.path = path
msg.file = open(path, 'rb')
msg.mime = 'image/jpeg'
if status in (200, 201) or uuid != self.qr_uuid:
self.queue.put(msg)
self.qr_uuid = uuid