From 7c21310ea3fedddf142c0ad2f7e06319bc2ad473 Mon Sep 17 00:00:00 2001 From: Jalin Date: Fri, 18 Jan 2019 19:31:30 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=20cdn=20=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E4=B8=8B=E5=8D=95=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- py12306/helpers/cdn.py | 6 ++---- py12306/helpers/func.py | 1 + py12306/log/common_log.py | 2 +- py12306/order/order.py | 10 ++++++++-- py12306/user/job.py | 2 +- py12306/web/web.py | 1 + 6 files changed, 14 insertions(+), 8 deletions(-) diff --git a/py12306/helpers/cdn.py b/py12306/helpers/cdn.py index 5c27da7..8777b73 100644 --- a/py12306/helpers/cdn.py +++ b/py12306/helpers/cdn.py @@ -100,9 +100,7 @@ class Cdn: if self.last_check_at: self.last_check_at = str_to_time(self.last_check_at) self.available_items = result.get('items', []) self.unavailable_items = result.get('fail_items', []) - CommonLog.add_quick_log(CommonLog.MESSAGE_CDN_RESTORE_SUCCESS.format(self.last_check_at, - self.last_check_at + timedelta( - seconds=self.check_keep_second))).flush() + CommonLog.add_quick_log(CommonLog.MESSAGE_CDN_RESTORE_SUCCESS.format(self.last_check_at )).flush() return True return False @@ -219,7 +217,7 @@ class Cdn: @classmethod def get_cdn(cls): self = cls() - if self.is_ready: + if self.is_ready and self.available_items: return random.choice(self.available_items) return None diff --git a/py12306/helpers/func.py b/py12306/helpers/func.py index 2d55ab1..d49bd94 100644 --- a/py12306/helpers/func.py +++ b/py12306/helpers/func.py @@ -1,3 +1,4 @@ +# -*- coding: utf-8 -*- import datetime import hashlib import json diff --git a/py12306/log/common_log.py b/py12306/log/common_log.py index c554b47..25cc821 100644 --- a/py12306/log/common_log.py +++ b/py12306/log/common_log.py @@ -51,7 +51,7 @@ class CommonLog(BaseLog): MESSAGE_CDN_START_TO_CHECK = '正在筛选 {} 个 CDN...' MESSAGE_CDN_START_TO_RECHECK = '正在重新筛选 {} 个 CDN...当前时间 {}\n' - MESSAGE_CDN_RESTORE_SUCCESS = 'CDN 恢复成功,上次检测 {},下次检测 {}\n' + MESSAGE_CDN_RESTORE_SUCCESS = 'CDN 恢复成功,上次检测 {}\n' MESSAGE_CDN_CHECKED_SUCCESS = '# CDN 检测完成,可用 CDN {} #\n' MESSAGE_CDN_CLOSED = '# CDN 已关闭 #' diff --git a/py12306/order/order.py b/py12306/order/order.py index aa235e8..028f460 100644 --- a/py12306/order/order.py +++ b/py12306/order/order.py @@ -58,7 +58,11 @@ class Order: return self.normal_order() def normal_order(self): - if not self.submit_order_request(): return + order_request_res = self.submit_order_request() + if order_request_res == -1: + return self.order_did_success() + elif not order_request_res: + return if not self.user_ins.request_init_dc_page(): return if not self.check_order_info(): return if not self.get_queue_count(): return @@ -75,6 +79,7 @@ class Order: OrderLog.notification(OrderLog.MESSAGE_ORDER_SUCCESS_NOTIFICATION_TITLE, OrderLog.MESSAGE_ORDER_SUCCESS_NOTIFICATION_CONTENT) self.send_notification() + return True def send_notification(self): # num = 0 # 通知次数 @@ -117,6 +122,7 @@ class Order: # sleep(self.notification_interval) OrderLog.add_quick_log(OrderLog.MESSAGE_JOB_CLOSED).flush() + return True def submit_order_request(self): data = { @@ -136,7 +142,7 @@ class Order: else: if (str(result.get('messages', '')).find('未处理') >= 0): # 未处理订单 self.order_id = 0 # 需要拿到订单号 TODO - return self.order_did_success() + return -1 OrderLog.add_quick_log( OrderLog.MESSAGE_SUBMIT_ORDER_REQUEST_FAIL.format( result.get('messages', CommonLog.MESSAGE_RESPONSE_EMPTY_ERROR))).flush() diff --git a/py12306/user/job.py b/py12306/user/job.py index 94d9f0c..d352d23 100644 --- a/py12306/user/job.py +++ b/py12306/user/job.py @@ -306,7 +306,7 @@ class UserJob: if result.get('data.normal_passengers'): self.passengers = result.get('data.normal_passengers') # 将乘客写入到文件 - with open(Config().USER_PASSENGERS_FILE % self.user_name, 'w') as f: + with open(Config().USER_PASSENGERS_FILE % self.user_name, 'w', encoding='utf-8') as f: f.write(json.dumps(self.passengers,indent=4, ensure_ascii=False)) return self.passengers else: diff --git a/py12306/web/web.py b/py12306/web/web.py index df9fd79..f24307c 100644 --- a/py12306/web/web.py +++ b/py12306/web/web.py @@ -1,3 +1,4 @@ +# -*- coding: utf-8 -*- import json import logging from datetime import timedelta