From afe2962b21c03895f26134aad051aab5498cde45 Mon Sep 17 00:00:00 2001 From: Jalin Date: Mon, 7 Jan 2019 11:05:26 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=89=93=E7=A0=81=E9=94=99?= =?UTF-8?q?=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.py | 2 +- py12306/helpers/OCR.py | 1 - py12306/helpers/auth_code.py | 13 ++++++++++--- py12306/log/base.py | 4 ++-- py12306/log/common_log.py | 1 + 5 files changed, 14 insertions(+), 7 deletions(-) diff --git a/main.py b/main.py index 2e24645..6560fe3 100644 --- a/main.py +++ b/main.py @@ -10,7 +10,7 @@ from py12306.user.user import User def main(): # Thread(target=Query.run).start() # 余票查询 create_thread_and_run(User, 'run', wait=False) - Query.run() + # Query.run() # Query.run() pass diff --git a/py12306/helpers/OCR.py b/py12306/helpers/OCR.py index 1d250da..1e10b21 100644 --- a/py12306/helpers/OCR.py +++ b/py12306/helpers/OCR.py @@ -17,7 +17,6 @@ class OCR: """ self = cls() return self.get_img_position_by_ruokuai(img_path) - pass def get_img_position_by_ruokuai(self, img_path): ruokuai_account = config.AUTO_CODE_ACCOUNT diff --git a/py12306/helpers/auth_code.py b/py12306/helpers/auth_code.py index cf7151d..edf880e 100644 --- a/py12306/helpers/auth_code.py +++ b/py12306/helpers/auth_code.py @@ -17,7 +17,7 @@ class AuthCode: """ session = None data_path = config.RUNTIME_DIR - retry_time = 1 + retry_time = 5 def __init__(self, session): self.session = session @@ -27,12 +27,19 @@ class AuthCode: self = cls(session) img_path = self.download_code() position = OCR.get_img_position(img_path) + if not position: # 打码失败 + return self.retry_get_auth_code() + answer = ','.join(map(str, position)) if not self.check_code(answer): - time.sleep(self.retry_time) - return self.get_auth_code(session) + return self.retry_get_auth_code() return position + def retry_get_auth_code(self): # TODO 安全次数检测 + CommonLog.add_quick_log(CommonLog.MESSAGE_RETRY_AUTH_CODE.format(self.retry_time)) + time.sleep(self.retry_time) + 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' diff --git a/py12306/log/base.py b/py12306/log/base.py index e51d378..0896a4b 100644 --- a/py12306/log/base.py +++ b/py12306/log/base.py @@ -30,7 +30,7 @@ class BaseLog: if is_main_thread(): logs = self.logs else: - logs = self.thread_logs[current_thread_id()] + logs = self.thread_logs.get(current_thread_id()) # for i in logs: print(*logs, sep=sep, end=end, file=file) if self.quick_log: @@ -39,7 +39,7 @@ class BaseLog: if is_main_thread(): self.logs = [] else: - del self.thread_logs[current_thread_id()] + if logs: del self.thread_logs[current_thread_id()] # print(self.logs) @classmethod diff --git a/py12306/log/common_log.py b/py12306/log/common_log.py index 326bcfb..6e42af8 100644 --- a/py12306/log/common_log.py +++ b/py12306/log/common_log.py @@ -5,6 +5,7 @@ from py12306.helpers.func import * @singleton class CommonLog(BaseLog): MESSAGE_12306_IS_CLOSED = '当前时间: {} | 12306 休息时间,程序将在明天早上 6 点自动运行' + MESSAGE_RETRY_AUTH_CODE = '{} 秒后重新获取验证码' def __init__(self): super().__init__()