Skip to content

Commit

Permalink
优化错误处理
Browse files Browse the repository at this point in the history
  • Loading branch information
pjialin committed Jan 8, 2019
1 parent bb136d1 commit 054c476
Show file tree
Hide file tree
Showing 10 changed files with 77 additions and 24 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
- [ ] 邮件通知

## 使用
py12306 需要运行 python 3.6 以上版本(其它版本暂未测试)
py12306 需要运行在 python 3.6 以上版本(其它版本暂未测试)

**1. 安装依赖**
```bash
Expand Down Expand Up @@ -57,6 +57,8 @@ python main.py -t -n
python main.py
```

## 下单成功图片
![下单成功图片](./data/images/order_success.png)

## Thanks
感谢大佬 [testerSunshine](https://github.com/testerSunshine/12306),借鉴了部分实现
Expand Down
Binary file added data/images/order_success.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 21 additions & 0 deletions py12306/helpers/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,24 @@ def run_check(cls):
if not cls.check_user_account_is_empty():
CommonLog.add_quick_log(CommonLog.MESSAGE_CHECK_EMPTY_USER_ACCOUNT).flush(exit=True)
if Const.IS_TEST_NOTIFICATION: cls.test_send_notifications()


# Expand
class Dict(dict):
def get(self, key, default=None, sep='.'):
keys = key.split(sep)
for i, key in enumerate(keys):
try:
value = self[key]
if len(keys[i + 1:]) and isinstance(value, Dict):
return value.get(sep.join(keys[i + 1:]), default=default, sep=sep)
return value
except:
return self.dict_to_dict(default)

def __getitem__(self, k):
return self.dict_to_dict(super().__getitem__(k))

@staticmethod
def dict_to_dict(value):
return Dict(value) if isinstance(value, dict) else value
9 changes: 8 additions & 1 deletion py12306/helpers/func.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
import functools

from time import sleep
from types import MethodType

from py12306 import config



def singleton(cls):
"""
将一个类作为单例
Expand Down Expand Up @@ -120,6 +120,13 @@ def sleep_forever_when_in_test():
if Const.IS_TEST: sleep_forever()


def expand_class(cls, key, value, keep_old=True):
if (keep_old):
setattr(cls, 'old_' + key, getattr(cls, key))
setattr(cls, key, MethodType(value, cls))
return cls


@singleton
class Const:
IS_TEST = False
Expand Down
28 changes: 27 additions & 1 deletion py12306/helpers/request.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from requests_html import HTMLSession
from py12306.helpers.app import *
from py12306.helpers.func import *
from requests_html import HTMLSession, HTMLResponse


class Request(HTMLSession):
Expand All @@ -18,3 +20,27 @@ def save_to_file(self, url, path):
for chunk in response.iter_content(chunk_size=1024):
f.write(chunk)
return response

@staticmethod
def _handle_response(response, **kwargs) -> HTMLResponse:
"""
扩充 response
:param response:
:param kwargs:
:return:
"""
response = HTMLSession._handle_response(response, **kwargs)
expand_class(response, 'json', Request.json)
return response

def json(self, default={}):
"""
重写 json 方法,拦截错误
:return:
"""
from py12306.helpers.app import Dict
try:
result = self.old_json()
return Dict(result)
except:
return Dict(default)
4 changes: 2 additions & 2 deletions py12306/log/query_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,9 @@ def print_query_error(cls, reason, code=None):
self = cls()
self.add_quick_log('查询余票请求失败')
if code:
self.add_quick_log('状态码{} '.format(code))
self.add_quick_log('状态码 {} '.format(code))
if reason:
self.add_quick_log('错误原因{} '.format(reason))
self.add_quick_log('错误原因 {} '.format(reason))
self.flush(sep='\t')
return self

Expand Down
14 changes: 8 additions & 6 deletions py12306/order/order.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ def submit_order_request(self):
OrderLog.add_quick_log(OrderLog.MESSAGE_SUBMIT_ORDER_REQUEST_SUCCESS).flush()
return True
else:
if (str(result.get('messages', '')).find('未处理') >= 0): # 未处理订单
stay_second(self.retry_time)
OrderLog.add_quick_log(
OrderLog.MESSAGE_SUBMIT_ORDER_REQUEST_FAIL.format(result.get('messages', '-'))).flush()
return False
Expand Down Expand Up @@ -134,9 +136,9 @@ def check_order_info(self):
}
response = self.session.post(API_CHECK_ORDER_INFO, data)
result = response.json()
if 'data' in result and result['data'].get('submitStatus'): # 成功
if result.get('data.submitStatus'): # 成功
OrderLog.add_quick_log(OrderLog.MESSAGE_CHECK_ORDER_INFO_SUCCESS).flush()
if result['data'].get("ifShowPassCode") != 'N':
if result.get('data.ifShowPassCode') != 'N':
self.is_need_auth_code = True
return True
else:
Expand Down Expand Up @@ -181,7 +183,7 @@ def get_queue_count(self):
}
response = self.session.post(API_GET_QUEUE_COUNT, data)
result = response.json()
if 'data' in result and ('countT' in result['data'] or 'ticket' in result['data']): # 成功
if result.get('data.countT') or result.get('data.ticket'): # 成功
"""
"data": {
"count": "66",
Expand All @@ -191,7 +193,7 @@ def get_queue_count(self):
"op_1": "true"
}
"""
ticket = result['data']['ticket'].split(',') # 暂不清楚具体作用
ticket = result.get('data.ticket').split(',') # 暂不清楚具体作用
ticket_number = sum(map(int, ticket))
current_position = int(data.get('countT', 0))
OrderLog.add_quick_log(
Expand Down Expand Up @@ -251,13 +253,13 @@ def confirm_single_for_queue(self):
"submitStatus": true
}
"""
if result['data'].get('submitStatus'): # 成功
if result.get('data.submitStatus'): # 成功
OrderLog.add_quick_log(OrderLog.MESSAGE_CONFIRM_SINGLE_FOR_QUEUE_SUCCESS).flush()
return True
else:
# 加入小黑屋 TODO
OrderLog.add_quick_log(
OrderLog.MESSAGE_CONFIRM_SINGLE_FOR_QUEUE_ERROR.format(result['data'].get('errMsg', '-'))).flush()
OrderLog.MESSAGE_CONFIRM_SINGLE_FOR_QUEUE_ERROR.format(result.get('data.errMsg', '-'))).flush()
else:
OrderLog.add_quick_log(OrderLog.MESSAGE_CONFIRM_SINGLE_FOR_QUEUE_FAIL.format(
result.get('messages', '-'))).flush()
Expand Down
6 changes: 1 addition & 5 deletions py12306/query/job.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,7 @@ def get_results(self, response):
"""
if response.status_code != 200:
QueryLog.print_query_error(response.reason, response.status_code)
try:
result_data = response.json().get('data', {})
result = result_data.get('result', [])
except:
pass # TODO
result = response.json().get('data.result')
return result if result else False

def is_has_ticket(self, ticket_info):
Expand Down
4 changes: 2 additions & 2 deletions py12306/query/query.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import threading

from requests_html import HTMLSession

from py12306.helpers.app import app_available_check
from py12306.helpers.func import *
from py12306.helpers.request import Request
from py12306.log.query_log import QueryLog
from py12306.query.job import Job

Expand All @@ -21,7 +21,7 @@ class Query:

def __init__(self):
self.interval = init_interval_by_number(config.QUERY_INTERVAL)
self.session = HTMLSession()
self.session = Request()

@classmethod
def run(cls):
Expand Down
11 changes: 5 additions & 6 deletions py12306/user/job.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,9 @@ def did_loaded_user(self):
def get_user_info(self):
response = self.session.get(API_USER_INFO.get('url'))
result = response.json()
user_data = result.get('data')
if user_data.get('userDTO') and user_data['userDTO'].get('loginUserDTO'):
user_data = user_data['userDTO']['loginUserDTO']
self.update_user_info({**user_data, **{'user_name': user_data['name']}})
user_data = result.get('data.userDTO.loginUserDTO')
if user_data:
self.update_user_info({**user_data, **{'user_name': user_data.get('name')}})
return True
return None

Expand All @@ -201,8 +200,8 @@ def get_user_passengers(self):
if self.passengers: return self.passengers
response = self.session.post(API_USER_PASSENGERS)
result = response.json()
if result.get('data') and result.get('data').get('normal_passengers'):
self.passengers = result.get('data').get('normal_passengers')
if result.get('data.normal_passengers'):
self.passengers = result.get('data.normal_passengers')
return self.passengers
else:
UserLog.add_quick_log(
Expand Down

0 comments on commit 054c476

Please sign in to comment.