Skip to content

Commit

Permalink
Add option for QRCode print in command line [PR#40: tempdban]
Browse files Browse the repository at this point in the history
  • Loading branch information
littlecodersh committed Jul 21, 2016
1 parent 3df9d7c commit dd72fcb
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 18 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ Q: 为什么中文的文件没有办法上传?

A: 这是由于`requests`的编码问题导致的。若需要支持中文文件传输,将[fields.py](https://github.com/littlecodersh/ItChat/blob/robot/plugin/config/fields.py)文件放入requests包的packages/urllib3下即可

Q: 为什么我在设定了`itchat.auto_login()``enableCmdQR``True`后还是没有办法在命令行显示二维码?

A: 这是由于没有安装可选的包`pillow`,可以使用右边的命令安装:`pip install pillow`

## Author

[LittleCoder](https://github.com/littlecodersh): 整体构架及完成Python2版本。
Expand Down
8 changes: 6 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,15 @@ Here is the `code <https://github.com/littlecodersh/ItChat/tree/robot>`__.
**FAQ**

Why I can't send files whose name is encoded in utf8?
Q: Why I can't send files whose name is encoded in utf8?

That's because of the upload setting of requests, you can put `this file <https://github.com/littlecodersh/ItChat/blob/robot/plugin/config/fields.py>`__
A: That's because of the upload setting of requests, you can put `this file <https://github.com/littlecodersh/ItChat/blob/robot/plugin/config/fields.py>`__
into packages/urllib3 of requests package.

Q: Why I still can't show QRCode with command line after I set enableCmdQr key to True in itchat.auto_login()?

A: That's because you need to install optional site-package pillow, try this script: pip install pillow

**Comments**

If you have any problems or suggestions, you can talk to me in this `issue <https://github.com/littlecodersh/ItChat/issues/1>`__
Expand Down
4 changes: 4 additions & 0 deletions README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ Q: Why I can't upload files whose name is not purely english?

A: This is caused because of the encoding of `requests`, you may fix it by placing [fields.py](https://github.com/littlecodersh/ItChat/blob/robot/plugin/config/fields.py) in packages/urllib of requests.

Q: Why I still can't show QRCode with command line after I set enableCmdQr key to True in itchat.auto_login()?

A: That's because you need to install optional site-package pillow, try this script: pip install pillow

## Author

[LittleCoder](https://github.com/littlecodersh): Structure and py2 version
Expand Down
8 changes: 4 additions & 4 deletions itchat/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@
__version__ = '1.0.13'

__client = client()
def auto_login(hotReload = False, statusStorageDir = 'itchat.pkl'):
def auto_login(hotReload = False, statusStorageDir = 'itchat.pkl', enableCmdQR = False):
if hotReload:
if __client.load_login_status(statusStorageDir): return
__client.auto_login()
__client.auto_login(enableCmdQR = enableCmdQR)
__client.dump_login_status(statusStorageDir)
else:
__client.auto_login()
__client.auto_login(enableCmdQR = enableCmdQR)
# The following method are all included in __client.auto_login >>>
def get_QRuuid(): return __client.get_QRuuid()
def get_QR(uuid = None): return __client.get_QR(uuid)
def get_QR(uuid = None, enableCmdQR = False): return __client.get_QR(uuid, enableCmdQR)
def check_login(uuid = None): return __client.check_login(uuid)
def web_init(): return __client.web_init()
def get_batch_contract(groupUserName): return __client.get_batch_contract(groupUserName)
Expand Down
20 changes: 9 additions & 11 deletions itchat/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ def load_login_status(self, fileDir):
return True
else:
return False
def auto_login(self):
def auto_login(self, enableCmdQR = False):
def open_QR():
for get_count in range(10):
out.print_line('Getting uuid', True)
while not self.get_QRuuid(): time.sleep(1)
out.print_line('Getting QR Code', True)
if self.get_QR(): break
if self.get_QR(enableCmdQR = enableCmdQR): break
elif 9 <= get_count:
out.print_line('Failed to get QR Code, please restart the program')
sys.exit()
Expand Down Expand Up @@ -82,19 +82,17 @@ def get_QRuuid(self):
if data and data.group(1) == '200':
self.uuid = data.group(2)
return self.uuid
def get_QR(self, uuid = None):
def get_QR(self, uuid = None, enableCmdQR = False):
try:
if uuid == None: uuid = self.uuid
url = '%s/qrcode/%s'%(BASE_URL, uuid)
r = self.s.get(url, stream = True)
QR_DIR = 'QR.jpg'
with open(QR_DIR, 'wb') as f: f.write(r.content)
if config.OS == 'Darwin':
subprocess.call(['open', QR_DIR])
elif config.OS == 'Linux':
subprocess.call(['xdg-open', QR_DIR])
if enableCmdQR:
tools.print_cmd_qr(QR_DIR)
else:
os.startfile(QR_DIR)
tools.print_qr(QR_DIR)
return True
except:
return False
Expand Down Expand Up @@ -387,14 +385,14 @@ def download_video(videoDir):
def __produce_group_chat(self, msg):
r = re.match('(@[0-9a-z]*?):<br/>(.*)$', msg['Content'])
if not r: return
actualNickName, content = r.groups()
actualUserName, content = r.groups()
try:
self.storageClass.groupDict[msg['FromUserName']][ActualUserName]
except:
groupMemberList = self.get_batch_contract(msg['FromUserName'])['MemberList']
self.storageClass.groupDict[msg['FromUserName']] = {member['UserName']: member for member in groupMemberList}
msg['ActualUserName'] = actualNickName
msg['ActualNickName'] = self.storageClass.groupDict[msg['FromUserName']][actualNickName]['NickName']
msg['ActualUserName'] = actualUserName
msg['ActualNickName'] = self.storageClass.groupDict[msg['FromUserName']][actualUserName]['NickName']
msg['Content'] = content
msg['isAt'] = u'@%s\u2005'%self.storageClass.nickName in msg['Content']
def send_msg(self, msg = 'Test Message', toUserName = None):
Expand Down
41 changes: 40 additions & 1 deletion itchat/tools.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import re, os
import re, os, sys

try:
from HTMLParser import HTMLParser
except ImportError:
Expand All @@ -8,6 +9,14 @@

emojiRegex = re.compile(r'<span class="emoji emoji(.*?)"></span>')
htmlParser = HTMLParser()
try:
b = u'\u2588'
sys.stdout.write(b + '\r')
sys.stdout.flush()
except UnicodeEncodeError:
BLOCK = 'MM'
else:
BLOCK = b

def clear_screen():
os.system('cls' if config.OS == 'Windows' else 'clear')
Expand Down Expand Up @@ -35,3 +44,33 @@ def check_file(fileDir):
return True
except:
return False
def print_qr(fileDir):
if config.OS == 'Darwin':
subprocess.call(['open', fileDir])
elif config.OS == 'Linux':
subprocess.call(['xdg-open', fileDir])
else:
os.startfile(fileDir)
try:
from PIL import Image
def print_cmd_qr(fileDir, size = 37, padding = 3,
white = BLOCK, black = ' '):
img = Image.open(fileDir)
times = img.size[0] / (size + padding * 2)
rgb = img.convert('RGB')
sys.stdout.write(' '*50 + '\r')
sys.stdout.flush()
qr = white * (size + 2) + '\n'
startPoint = padding + 0.5
for y in range(size):
qr += white
for x in range(size):
r,g,b = rgb.getpixel(((x + startPoint) * times, (y + startPoint) * times))
qr += white if r > 127 else black
qr += white + '\n'
qr += white * (size + 2) + '\n'
sys.stdout.write(qr)
except ImportError:
def print_cmd_qr(fileDir, size = 37, padding = 3,
white = BLOCK, black = ' '):
print_qr(fileDir)

0 comments on commit dd72fcb

Please sign in to comment.