修改验证码为 image64 方式
This commit is contained in:
@@ -83,7 +83,6 @@ python main.py
|
||||
|
||||
|
||||
## Docker 使用
|
||||
!!发现请求会有问题,正在调试中 01-10 18:54
|
||||
**1. 将配置文件下载到本地***
|
||||
```bash
|
||||
docker run --rm pjialin/py12306 cat /config/env.py > env.py
|
||||
|
||||
@@ -12,22 +12,21 @@ class OCR:
|
||||
"""
|
||||
|
||||
@classmethod
|
||||
def get_img_position(cls, img_path):
|
||||
def get_img_position(cls, img):
|
||||
"""
|
||||
获取图像坐标
|
||||
:param img_path:
|
||||
:return:
|
||||
"""
|
||||
self = cls()
|
||||
return self.get_img_position_by_ruokuai(img_path)
|
||||
return self.get_img_position_by_ruokuai(img)
|
||||
|
||||
def get_img_position_by_ruokuai(self, img_path):
|
||||
def get_img_position_by_ruokuai(self, img):
|
||||
ruokuai_account = Config().AUTO_CODE_ACCOUNT
|
||||
soft_id = '119671'
|
||||
soft_key = '6839cbaca1f942f58d2760baba5ed987'
|
||||
rc = RKClient(ruokuai_account.get('user'), ruokuai_account.get('pwd'), soft_id, soft_key)
|
||||
im = open(img_path, 'rb').read()
|
||||
result = rc.rk_create(im, 6113)
|
||||
result = rc.rk_create(img, 6113)
|
||||
if "Result" in result:
|
||||
return self.get_image_position_by_offset(list(result['Result']))
|
||||
CommonLog.print_auto_code_fail(result.get("Error", '-'))
|
||||
|
||||
@@ -20,6 +20,7 @@ API_USER_CHECK = {
|
||||
API_AUTH_CODE_DOWNLOAD = {
|
||||
'url': BASE_URL_OF_12306 + '/passport/captcha/captcha-image?login_site=E&module=login&rand=sjrand&_={random}'
|
||||
}
|
||||
API_AUTH_CODE_BASE64_DOWNLOAD = BASE_URL_OF_12306 + '/passport/captcha/captcha-image64?login_site=E&module=login&rand=sjrand&_={random}'
|
||||
API_AUTH_CODE_CHECK = {
|
||||
'url': BASE_URL_OF_12306 + '/passport/captcha/captcha-check?answer={answer}&rand=sjrand&login_site=E&_={random}'
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ from requests.exceptions import SSLError
|
||||
|
||||
from py12306.config import Config
|
||||
from py12306.helpers.OCR import OCR
|
||||
from py12306.helpers.api import API_AUTH_CODE_DOWNLOAD, API_AUTH_CODE_CHECK
|
||||
from py12306.helpers.api import *
|
||||
from py12306.helpers.request import Request
|
||||
from py12306.helpers.func import *
|
||||
from py12306.log.common_log import CommonLog
|
||||
@@ -27,8 +27,8 @@ class AuthCode:
|
||||
@classmethod
|
||||
def get_auth_code(cls, session):
|
||||
self = cls(session)
|
||||
img_path = self.download_code()
|
||||
position = OCR.get_img_position(img_path)
|
||||
img = self.download_code()
|
||||
position = OCR.get_img_position(img)
|
||||
if not position: # 打码失败
|
||||
return self.retry_get_auth_code()
|
||||
|
||||
@@ -43,17 +43,21 @@ class AuthCode:
|
||||
return self.get_auth_code(self.session)
|
||||
|
||||
def download_code(self):
|
||||
url = API_AUTH_CODE_DOWNLOAD.get('url').format(random=random.random())
|
||||
code_path = self.data_path + 'code.png'
|
||||
url = API_AUTH_CODE_BASE64_DOWNLOAD.format(random=random.random())
|
||||
# code_path = self.data_path + 'code.png'
|
||||
try:
|
||||
UserLog.add_quick_log(UserLog.MESSAGE_DOWNLAODING_THE_CODE).flush()
|
||||
response = self.session.save_to_file(url, code_path) # TODO 返回错误情况
|
||||
# response = self.session.save_to_file(url, code_path) # TODO 返回错误情况
|
||||
response = self.session.get(url)
|
||||
result = response.json()
|
||||
if result.get('image'):
|
||||
return result.get('image')
|
||||
raise SSLError
|
||||
except SSLError as e:
|
||||
UserLog.add_quick_log(
|
||||
UserLog.MESSAGE_DOWNLAOD_AUTH_CODE_FAIL.format(e, self.retry_time)).flush()
|
||||
time.sleep(self.retry_time)
|
||||
return self.download_code()
|
||||
return code_path
|
||||
|
||||
def check_code(self, answer):
|
||||
"""
|
||||
|
||||
@@ -21,7 +21,7 @@ class RKClient(object):
|
||||
'User-Agent': 'ben',
|
||||
}
|
||||
|
||||
def rk_create(self, im, im_type, timeout=60):
|
||||
def rk_create(self, image, im_type, timeout=20):
|
||||
"""
|
||||
im: 图片字节
|
||||
im_type: 题目类型
|
||||
@@ -29,10 +29,10 @@ class RKClient(object):
|
||||
params = {
|
||||
'typeid': im_type,
|
||||
'timeout': timeout,
|
||||
'image': image
|
||||
}
|
||||
params.update(self.base_params)
|
||||
files = {'image': ('a.jpg', im)}
|
||||
r = requests.post('http://api.ruokuai.com/create.json', data=params, files=files, headers=self.headers)
|
||||
r = requests.post('http://api.ruokuai.com/create.json', data=params, timeout=timeout)
|
||||
return r.json()
|
||||
|
||||
def rk_report_error(self, im_id):
|
||||
@@ -49,6 +49,6 @@ class RKClient(object):
|
||||
|
||||
if __name__ == '__main__':
|
||||
rc = RKClient('username', 'password', 'soft_id', 'soft_key')
|
||||
im = open('a.jpg', 'rb').read()
|
||||
# im = open('a.jpg', 'rb').read()
|
||||
# print rc.rk_create(im, 3040)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user