From a4b355bdf1bcf59fa50cc7cc79a0706afda32d9e Mon Sep 17 00:00:00 2001 From: Jalin Date: Thu, 10 Jan 2019 20:48:58 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E9=AA=8C=E8=AF=81=E7=A0=81?= =?UTF-8?q?=E4=B8=BA=20image64=20=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 - py12306/helpers/OCR.py | 9 ++++----- py12306/helpers/api.py | 1 + py12306/helpers/auth_code.py | 18 +++++++++++------- py12306/vender/ruokuai/main.py | 8 ++++---- 5 files changed, 20 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index d91a046..2969967 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/py12306/helpers/OCR.py b/py12306/helpers/OCR.py index c7a0347..ed514a9 100644 --- a/py12306/helpers/OCR.py +++ b/py12306/helpers/OCR.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", '-')) diff --git a/py12306/helpers/api.py b/py12306/helpers/api.py index 06f5f51..4979cc8 100644 --- a/py12306/helpers/api.py +++ b/py12306/helpers/api.py @@ -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}' } diff --git a/py12306/helpers/auth_code.py b/py12306/helpers/auth_code.py index ed121d7..2f4caff 100644 --- a/py12306/helpers/auth_code.py +++ b/py12306/helpers/auth_code.py @@ -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): """ diff --git a/py12306/vender/ruokuai/main.py b/py12306/vender/ruokuai/main.py index 8ff2cde..9efbee1 100755 --- a/py12306/vender/ruokuai/main.py +++ b/py12306/vender/ruokuai/main.py @@ -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)