Commit da469f3e authored by Jalin's avatar Jalin

修改验证码为 image64 方式

parent 247c46db
...@@ -83,7 +83,6 @@ python main.py ...@@ -83,7 +83,6 @@ python main.py
## Docker 使用 ## Docker 使用
!!发现请求会有问题,正在调试中 01-10 18:54
**1. 将配置文件下载到本地*** **1. 将配置文件下载到本地***
```bash ```bash
docker run --rm pjialin/py12306 cat /config/env.py > env.py docker run --rm pjialin/py12306 cat /config/env.py > env.py
......
...@@ -12,22 +12,21 @@ class OCR: ...@@ -12,22 +12,21 @@ class OCR:
""" """
@classmethod @classmethod
def get_img_position(cls, img_path): def get_img_position(cls, img):
""" """
获取图像坐标 获取图像坐标
:param img_path: :param img_path:
:return: :return:
""" """
self = cls() 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 ruokuai_account = Config().AUTO_CODE_ACCOUNT
soft_id = '119671' soft_id = '119671'
soft_key = '6839cbaca1f942f58d2760baba5ed987' soft_key = '6839cbaca1f942f58d2760baba5ed987'
rc = RKClient(ruokuai_account.get('user'), ruokuai_account.get('pwd'), soft_id, soft_key) rc = RKClient(ruokuai_account.get('user'), ruokuai_account.get('pwd'), soft_id, soft_key)
im = open(img_path, 'rb').read() result = rc.rk_create(img, 6113)
result = rc.rk_create(im, 6113)
if "Result" in result: if "Result" in result:
return self.get_image_position_by_offset(list(result['Result'])) return self.get_image_position_by_offset(list(result['Result']))
CommonLog.print_auto_code_fail(result.get("Error", '-')) CommonLog.print_auto_code_fail(result.get("Error", '-'))
......
...@@ -20,6 +20,7 @@ API_USER_CHECK = { ...@@ -20,6 +20,7 @@ API_USER_CHECK = {
API_AUTH_CODE_DOWNLOAD = { API_AUTH_CODE_DOWNLOAD = {
'url': BASE_URL_OF_12306 + '/passport/captcha/captcha-image?login_site=E&module=login&rand=sjrand&_={random}' '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 = { API_AUTH_CODE_CHECK = {
'url': BASE_URL_OF_12306 + '/passport/captcha/captcha-check?answer={answer}&rand=sjrand&login_site=E&_={random}' '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 ...@@ -5,7 +5,7 @@ from requests.exceptions import SSLError
from py12306.config import Config from py12306.config import Config
from py12306.helpers.OCR import OCR 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.request import Request
from py12306.helpers.func import * from py12306.helpers.func import *
from py12306.log.common_log import CommonLog from py12306.log.common_log import CommonLog
...@@ -27,8 +27,8 @@ class AuthCode: ...@@ -27,8 +27,8 @@ class AuthCode:
@classmethod @classmethod
def get_auth_code(cls, session): def get_auth_code(cls, session):
self = cls(session) self = cls(session)
img_path = self.download_code() img = self.download_code()
position = OCR.get_img_position(img_path) position = OCR.get_img_position(img)
if not position: # 打码失败 if not position: # 打码失败
return self.retry_get_auth_code() return self.retry_get_auth_code()
...@@ -43,17 +43,21 @@ class AuthCode: ...@@ -43,17 +43,21 @@ class AuthCode:
return self.get_auth_code(self.session) return self.get_auth_code(self.session)
def download_code(self): def download_code(self):
url = API_AUTH_CODE_DOWNLOAD.get('url').format(random=random.random()) url = API_AUTH_CODE_BASE64_DOWNLOAD.format(random=random.random())
code_path = self.data_path + 'code.png' # code_path = self.data_path + 'code.png'
try: try:
UserLog.add_quick_log(UserLog.MESSAGE_DOWNLAODING_THE_CODE).flush() 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: except SSLError as e:
UserLog.add_quick_log( UserLog.add_quick_log(
UserLog.MESSAGE_DOWNLAOD_AUTH_CODE_FAIL.format(e, self.retry_time)).flush() UserLog.MESSAGE_DOWNLAOD_AUTH_CODE_FAIL.format(e, self.retry_time)).flush()
time.sleep(self.retry_time) time.sleep(self.retry_time)
return self.download_code() return self.download_code()
return code_path
def check_code(self, answer): def check_code(self, answer):
""" """
......
...@@ -21,7 +21,7 @@ class RKClient(object): ...@@ -21,7 +21,7 @@ class RKClient(object):
'User-Agent': 'ben', 'User-Agent': 'ben',
} }
def rk_create(self, im, im_type, timeout=60): def rk_create(self, image, im_type, timeout=20):
""" """
im: 图片字节 im: 图片字节
im_type: 题目类型 im_type: 题目类型
...@@ -29,10 +29,10 @@ class RKClient(object): ...@@ -29,10 +29,10 @@ class RKClient(object):
params = { params = {
'typeid': im_type, 'typeid': im_type,
'timeout': timeout, 'timeout': timeout,
'image': image
} }
params.update(self.base_params) params.update(self.base_params)
files = {'image': ('a.jpg', im)} r = requests.post('http://api.ruokuai.com/create.json', data=params, timeout=timeout)
r = requests.post('http://api.ruokuai.com/create.json', data=params, files=files, headers=self.headers)
return r.json() return r.json()
def rk_report_error(self, im_id): def rk_report_error(self, im_id):
...@@ -49,6 +49,6 @@ class RKClient(object): ...@@ -49,6 +49,6 @@ class RKClient(object):
if __name__ == '__main__': if __name__ == '__main__':
rc = RKClient('username', 'password', 'soft_id', 'soft_key') 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) # print rc.rk_create(im, 3040)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment