Skip to content

Commit

Permalink
fix: 修复py3兼容 (#248)
Browse files Browse the repository at this point in the history
  • Loading branch information
chyroc committed Apr 2, 2019
1 parent f3a58d2 commit 5ca1aad
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 4 deletions.
51 changes: 51 additions & 0 deletions test/fateadm.py
Original file line number Diff line number Diff line change
@@ -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)
15 changes: 11 additions & 4 deletions wechatsogou/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -110,21 +111,24 @@ 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:
raise WechatSogouVcodeOcrException(e)

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

Expand Down Expand Up @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions wechatsogou/five.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

7 changes: 7 additions & 0 deletions wechatsogou/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 5ca1aad

Please sign in to comment.