diff --git a/env.py.example b/env.py.example index 613c15f..268d474 100644 --- a/env.py.example +++ b/env.py.example @@ -71,6 +71,11 @@ SERVERCHAN_KEY = '' PUSHBEAR_ENABLED = 0 PUSHBEAR_KEY = '' +# Bark 推送到ios设备 +# 参考 https://www.v2ex.com/t/467407 +BARK_ENABLED = 0 +BARK_PUSH_URL = 'https://api.day.app/:your_token' + # 输出日志到文件 OUT_PUT_LOG_TO_FILE_ENABLED = 0 OUT_PUT_LOG_TO_FILE_PATH = 'runtime/12306.log' # 日志目录 diff --git a/py12306/app.py b/py12306/app.py index 9fe52ee..0c4a42b 100644 --- a/py12306/app.py +++ b/py12306/app.py @@ -130,6 +130,10 @@ class App: CommonLog.add_quick_log(CommonLog.MESSAGE_TEST_SEND_PUSH_BEAR).flush() Notification.push_bear(Config().PUSHBEAR_KEY, '测试发送消息', 'By py12306') + if Config().BARK_ENABLED: # Bark通知 + CommonLog.add_quick_log(CommonLog.MESSAGE_TEST_SEND_PUSH_BARK).flush() + Notification.push_bark('测试发送信息') + @classmethod def run_check(cls): """ diff --git a/py12306/config.py b/py12306/config.py index 8709dcb..e1b203f 100644 --- a/py12306/config.py +++ b/py12306/config.py @@ -65,6 +65,10 @@ class Config: TELEGRAM_ENABLED = 0 TELEGRAM_BOT_API_URL = '' + # Bark 推送配置 + BARK_ENABLED = 0 + BARK_PUSH_URL = '' + # ServerChan和PushBear配置 SERVERCHAN_ENABLED = 0 SERVERCHAN_KEY = '8474-ca071ADSFADSF' diff --git a/py12306/helpers/notification.py b/py12306/helpers/notification.py index b6c3e42..6a1beb4 100644 --- a/py12306/helpers/notification.py +++ b/py12306/helpers/notification.py @@ -48,6 +48,11 @@ class Notification(): self = cls() self.send_pushbear(skey=skey, title=title, content=content) + @classmethod + def push_bark(cls, content=''): + self = cls() + self.push_to_bark(content) + def send_voice_code_of_yiyuan(self, phone, name='', content=''): """ 发送语音验证码 @@ -152,6 +157,20 @@ class Notification(): response_error_message = result.get('description') CommonLog.add_quick_log(CommonLog.MESSAGE_SEND_TELEGRAM_FAIL.format(response_error_message)).flush() + def push_to_bark(self, content): + bark_url = Config().BARK_PUSH_URL + if not bark_url: + return False + + response = self.session.request(url=bark_url + '/' + content, method='get') + result = response.json() + response_status = result.get('code') + if response_status == 200: + CommonLog.add_quick_log(CommonLog.MESSAGE_SEND_BARK_SUCCESS).flush() + else: + response_error_message = result.get('message') + CommonLog.add_quick_log(CommonLog.MESSAGE_SEND_BARK_FAIL.format(response_error_message)).flush() + def send_serverchan(self, skey, title, content): from lightpush import lightpush lgp = lightpush() diff --git a/py12306/log/common_log.py b/py12306/log/common_log.py index 25cc821..1eef770 100644 --- a/py12306/log/common_log.py +++ b/py12306/log/common_log.py @@ -27,6 +27,7 @@ class CommonLog(BaseLog): MESSAGE_TEST_SEND_TELEGRAM = '正在测试推送到Telegram...' MESSAGE_TEST_SEND_SERVER_CHAN = '正在测试发送ServerChan消息...' MESSAGE_TEST_SEND_PUSH_BEAR = '正在测试发送PushBear消息...' + MESSAGE_TEST_SEND_PUSH_BARK = '正在测试发送Bark消息...' MESSAGE_CONFIG_FILE_DID_CHANGED = '配置文件已修改,正在重新加载中\n' MESSAGE_API_RESPONSE_CAN_NOT_BE_HANDLE = '接口返回错误' @@ -43,6 +44,9 @@ class CommonLog(BaseLog): MESSAGE_SEND_PUSH_BEAR_SUCCESS = '发送成功,请检查微信' MESSAGE_SEND_PUSH_BEAR_FAIL = 'PushBear发送失败,请检查KEY' + MESSAGE_SEND_BARK_SUCCESS = 'Bark推送成功' + MESSAGE_SEND_BARK_FAIL = 'Bark推送失败,错误原因 {}' + MESSAGE_OUTPUT_TO_FILE_IS_UN_ENABLE = '请先打开配置项中的:OUT_PUT_LOG_TO_FILE_ENABLED ( 输出到文件 )' MESSAGE_GET_RESPONSE_FROM_FREE_AUTO_CODE = '从免费打码获取结果失败' @@ -96,6 +100,7 @@ class CommonLog(BaseLog): self.add_quick_log('钉钉通知: {}'.format(get_true_false_text(Config().DINGTALK_ENABLED, enable, disable))) self.add_quick_log('Telegram通知: {}'.format(get_true_false_text(Config().TELEGRAM_ENABLED, enable, disable))) self.add_quick_log('ServerChan通知: {}'.format(get_true_false_text(Config().SERVERCHAN_ENABLED, enable, disable))) + self.add_quick_log('Bark通知: {}'.format(get_true_false_text(Config().BARK_ENABLED, enable, disable))) self.add_quick_log( 'PushBear通知: {}'.format(get_true_false_text(Config().PUSHBEAR_ENABLED, enable, disable))).flush(sep='\t\t') self.add_quick_log('查询间隔: {} 秒'.format(Config().QUERY_INTERVAL)) diff --git a/py12306/order/order.py b/py12306/order/order.py index c632c9e..5b6c37b 100644 --- a/py12306/order/order.py +++ b/py12306/order/order.py @@ -99,6 +99,8 @@ class Order: if Config().PUSHBEAR_ENABLED: # PushBear通知 Notification.push_bear(Config().PUSHBEAR_KEY, OrderLog.MESSAGE_ORDER_SUCCESS_NOTIFICATION_TITLE, normal_message + info_message) + if Config().BARK_ENABLED: + Notification.push_bark(normal_message+info_message) if Config().NOTIFICATION_BY_VOICE_CODE: # 语音通知 if Config().NOTIFICATION_VOICE_CODE_TYPE == 'dingxin':