Skip to content

Commit

Permalink
优化查询错误
Browse files Browse the repository at this point in the history
  • Loading branch information
pjialin committed Jan 14, 2019
1 parent f724139 commit 01cf6cc
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 11 deletions.
12 changes: 12 additions & 0 deletions py12306/cluster/cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -253,3 +254,14 @@ def get_user_cookie(cls, key, default=None):
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
2 changes: 1 addition & 1 deletion py12306/helpers/OCR.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def get_image_by_free_site(self, img):
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


Expand Down
1 change: 0 additions & 1 deletion py12306/helpers/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,3 @@

API_FREE_CODE_QCR_API = 'http://60.205.200.159/api'
API_FREE_CODE_QCR_API_CHECK = 'http://check.huochepiao.360.cn/img_vcode'

14 changes: 14 additions & 0 deletions py12306/helpers/request.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from requests.exceptions import *

from py12306.helpers.func import *
from requests_html import HTMLSession, HTMLResponse

Expand Down Expand Up @@ -43,3 +45,15 @@ def json(self, default={}):
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
2 changes: 2 additions & 0 deletions py12306/log/common_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
8 changes: 4 additions & 4 deletions py12306/log/query_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,10 @@ def print_query_error(cls, reason, code=None):
@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)
Expand Down
16 changes: 11 additions & 5 deletions py12306/user/job.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def start(self):
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()
Expand Down Expand Up @@ -203,8 +203,9 @@ def get_name(self):
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)

Expand Down Expand Up @@ -257,16 +258,21 @@ def load_user(self):

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

Expand Down

0 comments on commit 01cf6cc

Please sign in to comment.