From f936931dc0521ac838fc5f3d045ee14d2cc68910 Mon Sep 17 00:00:00 2001 From: Chyroc Date: Tue, 2 Apr 2019 14:28:24 +0800 Subject: [PATCH 1/4] release/v4.5.2 (#246) --- wechatsogou/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wechatsogou/__init__.py b/wechatsogou/__init__.py index db5ccd5..62dc26d 100644 --- a/wechatsogou/__init__.py +++ b/wechatsogou/__init__.py @@ -31,7 +31,7 @@ 'WechatSogouRequestsException'] __title__ = 'wechatsogou' -__version__ = "4.5.1" +__version__ = "4.5.2" __author__ = 'Chyroc' """doc string From f3a58d21ee0dc2b7e7821721640b734ecaeb619d Mon Sep 17 00:00:00 2001 From: Mider Date: Tue, 2 Apr 2019 23:02:34 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E7=A9=BA=E8=BF=94?= =?UTF-8?q?=E5=9B=9E=E5=80=BC=E6=98=AF=E7=A9=BAgenerator=E7=9A=84=E5=A4=84?= =?UTF-8?q?=E7=90=86=EF=BC=8C=E9=81=BF=E5=85=8D=E5=87=BA=E7=8E=B0StopItera?= =?UTF-8?q?tion=E9=94=99=E8=AF=AF=20(#247)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- wechatsogou/api.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/wechatsogou/api.py b/wechatsogou/api.py index 76ea5e1..b4568ba 100644 --- a/wechatsogou/api.py +++ b/wechatsogou/api.py @@ -224,13 +224,11 @@ def get_gzh_info(self, wecgat_id_or_name, unlock_callback=None, identify_image_c 'authentication': '' # 认证 } """ - info = self.search_gzh(wecgat_id_or_name, 1, unlock_callback, identify_image_callback) - if not info: - return None - if decode_url: + info = self.search_gzh(wecgat_id_or_name, 1, unlock_callback, identify_image_callback, decode_url) + try: return next(info) - - return info + except StopIteration: + return None def search_gzh(self, keyword, page=1, unlock_callback=None, identify_image_callback=None, decode_url=True): """搜索 公众号 From 5ca1aad64784d562084120363f100b338cfaae24 Mon Sep 17 00:00:00 2001 From: Chyroc Date: Tue, 2 Apr 2019 23:15:50 +0800 Subject: [PATCH 3/4] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8Dpy3=E5=85=BC?= =?UTF-8?q?=E5=AE=B9=20(#248)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/fateadm.py | 51 ++++++++++++++++++++++++++++++++++++++++++++ wechatsogou/api.py | 15 +++++++++---- wechatsogou/five.py | 7 ++++++ wechatsogou/tools.py | 7 ++++++ 4 files changed, 76 insertions(+), 4 deletions(-) create mode 100644 test/fateadm.py diff --git a/test/fateadm.py b/test/fateadm.py new file mode 100644 index 0000000..179e145 --- /dev/null +++ b/test/fateadm.py @@ -0,0 +1,51 @@ +# -*- coding: utf-8 -*- + +import base64 +import hashlib +import json +import time + +import requests + + +class FateadmAPI(): + def __init__(self, app_id, app_key, usr_id, usr_key): + self.app_id = app_id + self.app_key = app_key + self.usr_id = usr_id + self.usr_key = usr_key + self.host = 'http://pred.fateadm.com' + + def calc_sign(self, usr_id, passwd, timestamp): + md5 = hashlib.md5() + md5.update(timestamp + passwd) + csign = md5.hexdigest() + + md5 = hashlib.md5() + md5.update(usr_id + timestamp + csign) + csign = md5.hexdigest() + return csign + + # 识别验证码 + def predict(self, pred_type, img_data): + tm = str(int(time.time())) + + param = { + 'user_id': self.usr_id, + 'timestamp': tm, + 'sign': self.calc_sign(self.usr_id, self.usr_key, tm), + 'predict_type': pred_type, + 'img_data': base64.b64encode(img_data), + } + + if self.app_id != '': + asign = self.calc_sign(self.app_id, self.app_key, tm) + param['appid'] = self.app_id + param['asign'] = asign + + r = requests.post('{}/api/capreg'.format(self.host), param) + try: + data = r.json() + return json.loads(data['RspData'])['result'] + except Exception: + raise Exception(r.text) diff --git a/wechatsogou/api.py b/wechatsogou/api.py index b4568ba..3b658e4 100644 --- a/wechatsogou/api.py +++ b/wechatsogou/api.py @@ -11,10 +11,11 @@ import requests from wechatsogou.const import agents, WechatSogouConst from wechatsogou.exceptions import WechatSogouException, WechatSogouRequestsException, WechatSogouVcodeOcrException -from wechatsogou.five import quote +from wechatsogou.five import must_str, quote from wechatsogou.identify_image import (identify_image_callback_by_hand, unlock_sogou_callback_example, unlock_weixin_callback_example, ws_cache) from wechatsogou.request import WechatSogouRequest from wechatsogou.structuring import WechatSogouStructuring +from wechatsogou.tools import may_int class WechatSogouAPI(object): @@ -110,10 +111,11 @@ def __get_by_unlock(self, url, referer=None, unlock_platform=None, unlock_callba if not session: session = requests.session() resp = self.__get(url, session, headers=self.__set_cookie(referer=referer)) + resp.encoding = 'utf-8' if 'antispider' in resp.url or '请输入验证码' in resp.text: for i in range(self.captcha_break_times): try: - unlock_platform(url, resp, session, unlock_callback, identify_image_callback) + unlock_platform(url=url, resp=resp, session=session, unlock_callback=unlock_callback, identify_image_callback=identify_image_callback) break except WechatSogouVcodeOcrException as e: if i == self.captcha_break_times - 1: @@ -121,10 +123,12 @@ def __get_by_unlock(self, url, referer=None, unlock_platform=None, unlock_callba if '请输入验证码' in resp.text: resp = session.get(url) + resp.encoding = 'utf-8' else: headers = self.__set_cookie(referer=referer) headers['User-Agent'] = 'Mozilla/5.0 (Windows NT 6.1; WOW64)' resp = self.__get(url, session, headers) + resp.encoding = 'utf-8' return resp @@ -169,9 +173,12 @@ def _parse_url(url, pads): a = url.find("url=") c = url.find("&k=") if a != -1 and c == -1: - a = url[a + sum([int(i) for i in pads]) + b] + sum = 0 + for i in list(pads) + [a, b]: + sum += int(must_str(i)) + a = url[sum] - return '{}&k={}&h={}'.format(url, b, a) + return '{}&k={}&h={}'.format(url, may_int(b), may_int(a)) if url.startswith('/link?url='): url = 'https://weixin.sogou.com{}'.format(url) diff --git a/wechatsogou/five.py b/wechatsogou/five.py index df7cced..ed5d7cf 100644 --- a/wechatsogou/five.py +++ b/wechatsogou/five.py @@ -18,6 +18,10 @@ def readimg(content): sys.setdefaultencoding('utf-8') input = raw_input str_to_bytes = bytes + def must_str(s): + if isinstance(s,unicode): + s = s.encode('utf-8') + return s else: import urllib.parse as url_parse import urllib.parse @@ -33,3 +37,6 @@ def readimg(content): urlencode = urllib.parse.urlencode input = input str_to_bytes = lambda x: bytes(x, encoding='utf-8') + def must_str(s): + return s + diff --git a/wechatsogou/tools.py b/wechatsogou/tools.py index f6db203..d079c3b 100644 --- a/wechatsogou/tools.py +++ b/wechatsogou/tools.py @@ -127,3 +127,10 @@ def format_image_url(url): if url.startswith('//'): url = 'https:{}'.format(url) return url + + +def may_int(i): + try: + return int(i) + except Exception: + return i From dc9b55b45f95cbe7ef810df6c3da231704a305a2 Mon Sep 17 00:00:00 2001 From: Chyroc Date: Tue, 2 Apr 2019 23:17:05 +0800 Subject: [PATCH 4/4] version --- wechatsogou/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wechatsogou/__init__.py b/wechatsogou/__init__.py index 62dc26d..b3458c8 100644 --- a/wechatsogou/__init__.py +++ b/wechatsogou/__init__.py @@ -31,7 +31,7 @@ 'WechatSogouRequestsException'] __title__ = 'wechatsogou' -__version__ = "4.5.2" +__version__ = "4.5.3" __author__ = 'Chyroc' """doc string