Skip to content

Commit

Permalink
Fix TypeError in Python 3 #8
Browse files Browse the repository at this point in the history
  • Loading branch information
bluedazzle committed Jun 10, 2017
1 parent 3248d33 commit 9d7d1f5
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
1 change: 1 addition & 0 deletions wechat_sender/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@

from wechat_sender.listener import listen
from wechat_sender.sender import Sender, LoggingSenderHandler
from wechat_sender.compatible import PY2, SYS_ENCODE

__author__ = 'rapospectre'
7 changes: 7 additions & 0 deletions wechat_sender/compatible.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# coding: utf-8
from __future__ import unicode_literals
import sys

PY_VERSION = sys.version
PY2 = PY_VERSION < '3'
SYS_ENCODE = sys.stdout.encoding or 'utf-8'
15 changes: 11 additions & 4 deletions wechat_sender/sender.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import logging

from wechat_sender.utils import STATUS_SUCCESS, DEFAULT_REMIND_TIME
from wechat_sender.compatible import PY2, SYS_ENCODE
from functools import reduce


Expand Down Expand Up @@ -47,6 +48,12 @@ def _wrap_post_data(self, **kwargs):
self.data['receiver'] = self.receiver
return self.data

def _convert_bytes(self, msg):
if not PY2:
if isinstance(msg, bytes):
return str(msg, encoding=SYS_ENCODE)
return msg

def send(self, message):
"""
发送基本文字消息
Expand All @@ -59,7 +66,7 @@ def send(self, message):
data = self._wrap_post_data(content=message)
res = requests.post(url, data=data, timeout=self.timeout)
if res.status_code == requests.codes.ok:
res_data = json.loads(res.content)
res_data = json.loads(self._convert_bytes(res.content))
if res_data.get('status') == STATUS_SUCCESS:
return True, res_data.get('message')
return False, res_data.get('message')
Expand Down Expand Up @@ -87,7 +94,7 @@ def delay_send(self, content, time, title='', remind=DEFAULT_REMIND_TIME):
data = self._wrap_post_data(title=title, content=content, time=time, remind=remind)
res = requests.post(url, data=data, timeout=self.timeout)
if res.status_code == requests.codes.ok:
res_data = json.loads(res.content)
res_data = json.loads(self._convert_bytes(res.content))
if res_data.get('status') == STATUS_SUCCESS:
return True, res_data.get('message')
return False, res_data.get('message')
Expand All @@ -112,7 +119,7 @@ def periodic_send(self, content, interval, title=''):
data = self._wrap_post_data(title=title, content=content, interval=interval)
res = requests.post(url, data, timeout=self.timeout)
if res.status_code == requests.codes.ok:
res_data = json.loads(res.content)
res_data = json.loads(self._convert_bytes(res.content))
if res_data.get('status') == STATUS_SUCCESS:
return True, res_data.get('message')
return False, res_data.get('message')
Expand All @@ -136,7 +143,7 @@ def send_to(self, content, search):
data = self._wrap_post_data(content=content, search=search)
res = requests.post(url, data=data, timeout=self.timeout)
if res.status_code == requests.codes.ok:
res_data = json.loads(res.content)
res_data = json.loads(self._convert_bytes(res.content))
if res_data.get('status') == STATUS_SUCCESS:
return True, res_data.get('message')
return False, res_data.get('message')
Expand Down

0 comments on commit 9d7d1f5

Please sign in to comment.