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__()