优化查询错误

This commit is contained in:
Jalin
2019-01-14 12:34:48 +08:00
parent f724139d8c
commit 01cf6cca33
7 changed files with 44 additions and 11 deletions

View File

@@ -23,6 +23,7 @@ class Cluster():
KEY_CHANNEL_LOG = KEY_PREFIX + 'channel_log'
KEY_CHANNEL_EVENT = KEY_PREFIX + 'channel_even'
KEY_USER_COOKIES = KEY_PREFIX + 'user_cookies'
KEY_USER_INFOS = KEY_PREFIX + 'user_infos'
KEY_USER_LAST_HEARTBEAT = KEY_PREFIX + 'user_last_heartbeat'
KEY_NODES_ALIVE = KEY_PREFIX + 'nodes_alive'
@@ -253,3 +254,14 @@ class Cluster():
def set_user_cookie(cls, key, value):
self = cls()
return self.session.hset(Cluster.KEY_USER_COOKIES, key, pickle.dumps(value, 0).decode())
@classmethod
def set_user_info(cls, key, info):
self = cls()
return self.session.hset(Cluster.KEY_USER_INFOS, key, pickle.dumps(info, 0).decode())
@classmethod
def get_user_info(cls, key, default=None):
self = cls()
res = self.session.hget(Cluster.KEY_USER_INFOS, key)
return pickle.loads(res.encode()) if res else default

View File

@@ -73,7 +73,7 @@ class OCR:
position = check_result.get('res')
return position.replace('(', '').replace(')', '').split(',')
CommonLog.print_auto_code_fail(result.get("Error", '-'))
CommonLog.print_auto_code_fail(CommonLog.MESSAGE_GET_RESPONSE_FROM_FREE_AUTO_CODE)
return None

View File

@@ -46,4 +46,3 @@ API_NOTIFICATION_BY_VOICE_CODE = 'http://ali-voice.showapi.com/sendVoice?'
API_FREE_CODE_QCR_API = 'http://60.205.200.159/api'
API_FREE_CODE_QCR_API_CHECK = 'http://check.huochepiao.360.cn/img_vcode'

View File

@@ -1,3 +1,5 @@
from requests.exceptions import *
from py12306.helpers.func import *
from requests_html import HTMLSession, HTMLResponse
@@ -43,3 +45,15 @@ class Request(HTMLSession):
return Dict(result)
except:
return Dict(default)
def request(self, *args, **kwargs): # 拦截所有错误
try:
return super().request(*args, **kwargs)
except RequestException as e:
if e.response:
response = e.response
else:
response = HTMLResponse(HTMLSession)
response.status_code = 500
expand_class(response, 'json', Request.json)
return response

View File

@@ -32,6 +32,8 @@ class CommonLog(BaseLog):
MESSAGE_OUTPUT_TO_FILE_IS_UN_ENABLE = '请先打开配置:输出到文件'
MESSAGE_GET_RESPONSE_FROM_FREE_AUTO_CODE = '从免费打码获取结果失败'
def __init__(self):
super().__init__()
self.init_data()

View File

@@ -152,10 +152,10 @@ class QueryLog(BaseLog):
@classmethod
def print_job_start(cls, job_name):
self = cls()
self.add_log(
'=== 正在进行第 {query_count} 次查询 {job_name} === {time}'.format(
query_count=int(self.data.get('query_count', 0)) + 1,
job_name=job_name, time=datetime.datetime.now()))
message = '=== 正在进行第 {query_count} 次查询 {job_name} === {time}'.format(
query_count=int(self.data.get('query_count', 0)) + 1,
job_name=job_name, time=datetime.datetime.now())
self.add_log(message)
self.refresh_data()
if is_main_thread():
self.flush(publish=False)

View File

@@ -69,7 +69,7 @@ class UserJob:
app_available_check()
if Config().is_slave():
self.load_user_from_remote()
pass # 虽然同一个 cookie同时请求之后会导致失效暂时不在子节点中加载用户
pass
else:
if Config().is_master() and not self.cookie: self.load_user_from_remote() # 主节点加载一次 Cookie
self.check_heartbeat()
@@ -203,8 +203,9 @@ class UserJob:
return self.info.get('user_name', '')
def save_user(self):
if Config().is_cluster_enabled():
return self.cluster.set_user_cookie(self.key, self.session.cookies)
if Config().is_master():
self.cluster.set_user_cookie(self.key, self.session.cookies)
self.cluster.set_user_info(self.key, self.info)
with open(self.get_cookie_path(), 'wb') as f:
pickle.dump(self.session.cookies, f)
@@ -257,16 +258,21 @@ class UserJob:
def load_user_from_remote(self):
cookie = self.cluster.get_user_cookie(self.key)
if not cookie and Config().is_slave():
info = self.cluster.get_user_info(self.key)
if Config().is_slave() and (not cookie or not info):
while True: # 子节点只能取
UserLog.add_quick_log(UserLog.MESSAGE_USER_COOKIE_NOT_FOUND_FROM_REMOTE.format(self.user_name)).flush()
stay_second(self.retry_time)
return self.load_user_from_remote()
if info: self.info = info
if cookie:
self.session.cookies.update(cookie)
if not self.cookie: # 第一次加载
self.cookie = True
self.did_loaded_user()
if not Config().is_slave():
self.did_loaded_user()
else:
UserLog.print_welcome_user(self)
return True
return False