Browse Source

Fix image openning in Mac.

liuwons 9 years ago
parent
commit
686dc3f997
2 changed files with 13 additions and 11 deletions
  1. 2 1
      README.md
  2. 11 10
      wxbot.py

+ 2 - 1
README.md

@@ -41,7 +41,7 @@ Web微信协议参考资料:
 
 此版本只能运行于Python 2环境 。
 
-**wxBot** 用到了Python **requests** , **pypng** , 以及 **pyqrcode** 库。
+**wxBot** 用到了Python **requests** , **pypng** , **Pillow* 以及 **pyqrcode** 库。
 
 使用之前需要所依赖的库:
 
@@ -49,6 +49,7 @@ Web微信协议参考资料:
 pip install requests
 pip install pyqrcode
 pip install pypng
+pip install Pillow
 ```
 
 ## 2 快速开发

+ 11 - 10
wxbot.py

@@ -9,7 +9,7 @@ import urllib
 import time
 import re
 import random
-import webbrowser
+from PIL import Image
 from requests.exceptions import ConnectionError, ReadTimeout
 import HTMLParser
 
@@ -676,12 +676,12 @@ class WXBot:
         self.get_uuid()
         self.gen_qr_code('qr.png')
         print '[INFO] Please use WeChat to scan the QR code .'
-        
+
         result = self.wait4login()
         if result != SUCCESS:
             print '[ERROR] Web WeChat login failed. failed code=%s'%(result, )
             return
-        
+
         if self.login():
             print '[INFO] Web WeChat login succeed .'
         else:
@@ -723,7 +723,8 @@ class WXBot:
         qr = pyqrcode.create(string)
         if self.conf['qr'] == 'png':
             qr.png(qr_file_path, scale=8)
-            webbrowser.open(qr_file_path)
+            img = Image.open(qr_file_path)
+            img.show()
         elif self.conf['qr'] == 'tty':
             print(qr.terminal(quiet_zone=1))
 
@@ -738,10 +739,10 @@ class WXBot:
     def wait4login(self):
         '''
         http comet:
-        tip=1, the request wait for user to scan the qr, 
+        tip=1, the request wait for user to scan the qr,
                201: scaned
                408: timeout
-        tip=0, the request wait for user confirm, 
+        tip=0, the request wait for user confirm,
                200: confirmed
         '''
         LOGIN_TEMPLATE = 'https://login.weixin.qq.com/cgi-bin/mmwebwx-bin/login?tip=%s&uuid=%s&_=%s'
@@ -749,9 +750,9 @@ class WXBot:
 
         try_later_secs = 1
         MAX_RETRY_TIMES = 10
-        
+
         code = UNKONWN
-        
+
         retry_time = MAX_RETRY_TIMES
         while retry_time > 0:
             url = LOGIN_TEMPLATE % (tip, self.uuid, int(time.time()))
@@ -772,12 +773,12 @@ class WXBot:
                 retry_time -= 1
                 time.sleep(try_later_secs)
             else:
-                print ('[ERROR] WeChat login exception return_code=%s. retry in %s secs later...' % 
+                print ('[ERROR] WeChat login exception return_code=%s. retry in %s secs later...' %
                         (code, try_later_secs))
                 tip = 1
                 retry_time -= 1
                 time.sleep(try_later_secs)
-            
+
         return code
 
     def login(self):