From 2c9fa98efc231cc2c29e6a2618705d548e5fb51f Mon Sep 17 00:00:00 2001 From: "littlefatty.wong" Date: Tue, 15 Jan 2019 11:13:51 +0800 Subject: [PATCH 1/2] =?UTF-8?q?add=20ServerChan=20and=20PushBear=E6=94=AF?= =?UTF-8?q?=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 6 +++++- env.docker.py.example | 5 +++++ env.py.example | 5 +++++ main.py | 1 + py12306/app.py | 10 +++++++++- py12306/config.py | 5 +++++ py12306/helpers/notification.py | 31 ++++++++++++++++++++++++++++++- py12306/log/common_log.py | 10 ++++++++++ py12306/order/order.py | 6 ++++++ requirements.txt | 3 ++- 10 files changed, 78 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 993b978..0c0b78f 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ - [x] 动态修改配置文件 - [x] 邮件通知 - [x] Web 管理页面 -- [ ] 微信消息通知 +- [x] 微信消息通知 ## 使用 py12306 需要运行在 python 3.6 以上版本(其它版本暂未测试) @@ -125,6 +125,10 @@ docker run -d -v $(pwd):/config -v py12306:/data pjialin/py12306 新增 Web 页面支持 +- 19-01-15 + +新增ServerChan和PushBear支持 + ## 截图 ### Web 管理页面 diff --git a/env.docker.py.example b/env.docker.py.example index 88af7fb..d28f754 100644 --- a/env.docker.py.example +++ b/env.docker.py.example @@ -52,6 +52,11 @@ DINGTALK_WEBHOOK = 'https://oapi.dingtalk.com/robot/send?access_token=your token TELEGRAM_ENABLED = 0 TELEGRAM_BOT_API_URL = 'https://tgbot.lbyczf.com/sendMessage/:your_token' +# ServerChan和PushBear配置 +SERVERCHAN_ENABLED = 0 +PUSHBEAR_ENABLED = 0 +SKEY = 'SCUdafadsfasfdafdf45234234234' + # 输出日志到文件 (Docker 中不建议修改此组配置项) OUT_PUT_LOG_TO_FILE_ENABLED = 1 OUT_PUT_LOG_TO_FILE_PATH = '/config/12306.log' # 日志目录 diff --git a/env.py.example b/env.py.example index 5e17869..09939d9 100644 --- a/env.py.example +++ b/env.py.example @@ -52,6 +52,11 @@ DINGTALK_WEBHOOK = 'https://oapi.dingtalk.com/robot/send?access_token=your token TELEGRAM_ENABLED = 0 TELEGRAM_BOT_API_URL = 'https://tgbot.lbyczf.com/sendMessage/:your_token' +# ServerChan和PushBear配置 +SERVERCHAN_ENABLED = 0 +PUSHBEAR_ENABLED = 0 +SKEY = 'SCUdafadsfasfdafdf45234234234' + # 输出日志到文件 OUT_PUT_LOG_TO_FILE_ENABLED = 0 OUT_PUT_LOG_TO_FILE_PATH = 'runtime/12306.log' # 日志目录 diff --git a/main.py b/main.py index 1f92af2..f5fbe25 100644 --- a/main.py +++ b/main.py @@ -38,6 +38,7 @@ def test(): 座位验证 乘客验证 语音验证码验证 + 通知验证 :return: """ Const.IS_TEST = True diff --git a/py12306/app.py b/py12306/app.py index 620a823..ddf1ccd 100644 --- a/py12306/app.py +++ b/py12306/app.py @@ -105,7 +105,7 @@ class App: Notification.voice_code(Config().NOTIFICATION_VOICE_CODE_PHONE, '张三', OrderLog.MESSAGE_ORDER_SUCCESS_NOTIFICATION_OF_VOICE_CODE_CONTENT.format('北京', '深圳')) - if Config().EMAIL_ENABLED: # 语音通知 + if Config().EMAIL_ENABLED: # 邮件通知 CommonLog.add_quick_log(CommonLog.MESSAGE_TEST_SEND_EMAIL).flush() Notification.send_email(Config().EMAIL_RECEIVER, '测试发送邮件', 'By py12306') @@ -117,6 +117,14 @@ class App: CommonLog.add_quick_log(CommonLog.MESSAGE_TEST_SEND_TELEGRAM).flush() Notification.send_to_telegram('测试发送信息') + if Config().SERVERCHAN_ENABLED: # ServerChan通知 + CommonLog.add_quick_log(CommonLog.MESSAGE_TEST_SEND_SERVER_CHAN).flush() + Notification.server_chan(Config().SKEY, '测试发送消息', 'By py12306') + + if Config().PUSHBEAR_ENABLED: # PushBear通知 + CommonLog.add_quick_log(CommonLog.MESSAGE_TEST_SEND_PUSH_BEAR).flush() + Notification.push_bear(Config().SKEY, '测试发送消息', 'By py12306') + @classmethod def run_check(cls): """ diff --git a/py12306/config.py b/py12306/config.py index f789423..c3365bb 100644 --- a/py12306/config.py +++ b/py12306/config.py @@ -62,6 +62,11 @@ class Config: TELEGRAM_ENABLED = 0 TELEGRAM_BOT_API_URL = '' + # ServerChan和PushBear配置 + SERVERCHAN_ENABLED = 0 + PUSHBEAR_ENABLED = 0 + SKEY = '' + # 邮箱配置 EMAIL_ENABLED = 0 EMAIL_SENDER = '' diff --git a/py12306/helpers/notification.py b/py12306/helpers/notification.py index f97b7ed..9e4818f 100644 --- a/py12306/helpers/notification.py +++ b/py12306/helpers/notification.py @@ -5,7 +5,6 @@ from py12306.helpers.api import * from py12306.helpers.request import Request from py12306.log.common_log import CommonLog - class Notification(): """ 通知类 @@ -35,6 +34,16 @@ class Notification(): self = cls() self.send_to_telegram_bot(content=content) + @classmethod + def server_chan(cls, skey='', title='', content=''): + self = cls() + self.send_serverchan(skey=skey, title=title, content=content) + + @classmethod + def push_bear(cls, skey='', title='', content=''): + self = cls() + self.send_pushbear(skey=skey, title=title, content=content) + def send_voice_code_of_yiyuan(self, phone, name='', content=''): """ 发送语音验证码 @@ -110,6 +119,26 @@ class Notification(): response_error_message = result.get('description') CommonLog.add_quick_log(CommonLog.MESSAGE_SEND_TELEGRAM_FAIL.format(response_error_message)).flush() + def send_serverchan(self, skey, title, content): + from lightpush import lightpush + lgp = lightpush() + lgp.set_single_push(key=skey) + try: + lgp.single_push(title, content) + CommonLog.add_quick_log(CommonLog.MESSAGE_SEND_SERVER_CHAN_SUCCESS).flush() + except Exception as e: + CommonLog.add_quick_log(CommonLog.MESSAGE_SEND_SERVER_CHAN_FAIL.format(e)).flush() + + def send_pushbear(self, skey, title, content): + from lightpush import lightpush + lgp = lightpush() + lgp.set_group_push(key=skey) + try: + lgp.group_push(title, content) + CommonLog.add_quick_log(CommonLog.MESSAGE_SEND_PUSH_BEAR_SUCCESS).flush() + except Exception as e: + CommonLog.add_quick_log(CommonLog.MESSAGE_SEND_PUSH_BEAR_SUCCESS.format(e)).flush() + if __name__ == '__main__': name = '张三4' diff --git a/py12306/log/common_log.py b/py12306/log/common_log.py index dc6b29c..07f8bb5 100644 --- a/py12306/log/common_log.py +++ b/py12306/log/common_log.py @@ -25,6 +25,8 @@ class CommonLog(BaseLog): MESSAGE_TEST_SEND_EMAIL = '正在测试发送邮件...' MESSAGE_TEST_SEND_DINGTALK = '正在测试发送钉钉消息...' MESSAGE_TEST_SEND_TELEGRAM = '正在测试推送到Telegram...' + MESSAGE_TEST_SEND_SERVER_CHAN = '正在测试发送ServerChan消息...' + MESSAGE_TEST_SEND_PUSH_BEAR = '正在测试发送PushBear消息...' MESSAGE_CONFIG_FILE_DID_CHANGED = '配置文件已修改,正在重新加载中\n' MESSAGE_API_RESPONSE_CAN_NOT_BE_HANDLE = '接口返回错误' @@ -35,6 +37,12 @@ class CommonLog(BaseLog): MESSAGE_SEND_TELEGRAM_SUCCESS = 'Telegram推送成功' MESSAGE_SEND_TELEGRAM_FAIL = 'Telegram推送失败,错误原因 {}' + MESSAGE_SEND_SERVER_CHAN_SUCCESS = '发送成功,请检查微信' + MESSAGE_SEND_SERVER_CHAN_FAIL = 'ServerChan发送失败,请检查KEY' + + MESSAGE_SEND_PUSH_BEAR_SUCCESS = '发送成功,请检查微信' + MESSAGE_SEND_PUSH_BEAR_FAIL = 'PushBear发送失败,请检查KEY' + MESSAGE_OUTPUT_TO_FILE_IS_UN_ENABLE = '请先打开配置:输出到文件' MESSAGE_GET_RESPONSE_FROM_FREE_AUTO_CODE = '从免费打码获取结果失败' @@ -77,6 +85,8 @@ class CommonLog(BaseLog): self.add_quick_log('邮件通知: {}'.format(get_true_false_text(Config().EMAIL_ENABLED, enable, disable))) 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('PushBear通知: {}'.format(get_true_false_text(Config().PUSHBEAR_ENABLED, enable, disable))) self.add_quick_log('查询间隔: {} 秒'.format(Config().QUERY_INTERVAL)) self.add_quick_log('用户心跳检测间隔: {} 秒'.format(Config().USER_HEARTBEAT_INTERVAL)) self.add_quick_log('WEB 管理页面: {}'.format(get_true_false_text(Config().WEB_ENABLE, enable, disable))) diff --git a/py12306/order/order.py b/py12306/order/order.py index c799279..aeef212 100644 --- a/py12306/order/order.py +++ b/py12306/order/order.py @@ -87,6 +87,12 @@ class Order: if Config().TELEGRAM_ENABLED: # Telegram推送 Notification.send_to_telegram( OrderLog.MESSAGE_ORDER_SUCCESS_NOTIFICATION_OF_EMAIL_CONTENT.format(self.order_id)) + if Config().SERVERCHAN_ENABLED: # ServerChan通知 + Notification.server_chan(Config().SKEY, OrderLog.MESSAGE_ORDER_SUCCESS_NOTIFICATION_TITLE, + OrderLog.MESSAGE_ORDER_SUCCESS_NOTIFICATION_OF_EMAIL_CONTENT.format(self.order_id)) + if Config().PUSHBEAR_ENABLED: # PushBear通知 + Notification.push_bear(Config().SKEY, OrderLog.MESSAGE_ORDER_SUCCESS_NOTIFICATION_TITLE, + OrderLog.MESSAGE_ORDER_SUCCESS_NOTIFICATION_OF_EMAIL_CONTENT.format(self.order_id)) while sustain_time: # TODO 后面直接查询有没有待支付的订单就可以 num += 1 if Config().NOTIFICATION_BY_VOICE_CODE: # 语音通知 diff --git a/requirements.txt b/requirements.txt index 4b34b39..140e89b 100644 --- a/requirements.txt +++ b/requirements.txt @@ -28,4 +28,5 @@ urllib3==1.24.1 w3lib==1.19.0 websockets==7.0 Werkzeug==0.14.1 -DingtalkChatbot==1.3.0 \ No newline at end of file +DingtalkChatbot==1.3.0 +lightpush==0.1.3 \ No newline at end of file From 00d5ea4be6ee6be07d156010bec418e401dd183c Mon Sep 17 00:00:00 2001 From: "littlefatty.wong" Date: Tue, 15 Jan 2019 11:35:23 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E5=B0=86=E9=85=8D=E7=BD=AE=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E5=8F=82=E6=95=B0SKEY=E4=BF=AE=E6=94=B9=E4=B8=BAS=5FK?= =?UTF-8?q?EY=E4=BB=A5=E7=AC=A6=E5=90=88=E5=8F=82=E6=95=B0=E8=A7=84?= =?UTF-8?q?=E5=88=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- env.docker.py.example | 2 +- env.py.example | 2 +- py12306/app.py | 4 ++-- py12306/config.py | 2 +- py12306/order/order.py | 4 ++-- 5 files changed, 7 insertions(+), 7 deletions(-) diff --git a/env.docker.py.example b/env.docker.py.example index d28f754..ff8b415 100644 --- a/env.docker.py.example +++ b/env.docker.py.example @@ -55,7 +55,7 @@ TELEGRAM_BOT_API_URL = 'https://tgbot.lbyczf.com/sendMessage/:your_token' # ServerChan和PushBear配置 SERVERCHAN_ENABLED = 0 PUSHBEAR_ENABLED = 0 -SKEY = 'SCUdafadsfasfdafdf45234234234' +S_KEY = 'SCUdafadsfasfdafdf45234234234' # 输出日志到文件 (Docker 中不建议修改此组配置项) OUT_PUT_LOG_TO_FILE_ENABLED = 1 diff --git a/env.py.example b/env.py.example index 09939d9..8b3c77f 100644 --- a/env.py.example +++ b/env.py.example @@ -55,7 +55,7 @@ TELEGRAM_BOT_API_URL = 'https://tgbot.lbyczf.com/sendMessage/:your_token' # ServerChan和PushBear配置 SERVERCHAN_ENABLED = 0 PUSHBEAR_ENABLED = 0 -SKEY = 'SCUdafadsfasfdafdf45234234234' +S_KEY = 'SCUdafadsfasfdafdf45234234234' # 输出日志到文件 OUT_PUT_LOG_TO_FILE_ENABLED = 0 diff --git a/py12306/app.py b/py12306/app.py index ddf1ccd..b235359 100644 --- a/py12306/app.py +++ b/py12306/app.py @@ -119,11 +119,11 @@ class App: if Config().SERVERCHAN_ENABLED: # ServerChan通知 CommonLog.add_quick_log(CommonLog.MESSAGE_TEST_SEND_SERVER_CHAN).flush() - Notification.server_chan(Config().SKEY, '测试发送消息', 'By py12306') + Notification.server_chan(Config().S_KEY, '测试发送消息', 'By py12306') if Config().PUSHBEAR_ENABLED: # PushBear通知 CommonLog.add_quick_log(CommonLog.MESSAGE_TEST_SEND_PUSH_BEAR).flush() - Notification.push_bear(Config().SKEY, '测试发送消息', 'By py12306') + Notification.push_bear(Config().S_KEY, '测试发送消息', 'By py12306') @classmethod def run_check(cls): diff --git a/py12306/config.py b/py12306/config.py index c3365bb..ac7a1f7 100644 --- a/py12306/config.py +++ b/py12306/config.py @@ -65,7 +65,7 @@ class Config: # ServerChan和PushBear配置 SERVERCHAN_ENABLED = 0 PUSHBEAR_ENABLED = 0 - SKEY = '' + S_KEY = '' # 邮箱配置 EMAIL_ENABLED = 0 diff --git a/py12306/order/order.py b/py12306/order/order.py index aeef212..8c837e2 100644 --- a/py12306/order/order.py +++ b/py12306/order/order.py @@ -88,10 +88,10 @@ class Order: Notification.send_to_telegram( OrderLog.MESSAGE_ORDER_SUCCESS_NOTIFICATION_OF_EMAIL_CONTENT.format(self.order_id)) if Config().SERVERCHAN_ENABLED: # ServerChan通知 - Notification.server_chan(Config().SKEY, OrderLog.MESSAGE_ORDER_SUCCESS_NOTIFICATION_TITLE, + Notification.server_chan(Config().S_KEY, OrderLog.MESSAGE_ORDER_SUCCESS_NOTIFICATION_TITLE, OrderLog.MESSAGE_ORDER_SUCCESS_NOTIFICATION_OF_EMAIL_CONTENT.format(self.order_id)) if Config().PUSHBEAR_ENABLED: # PushBear通知 - Notification.push_bear(Config().SKEY, OrderLog.MESSAGE_ORDER_SUCCESS_NOTIFICATION_TITLE, + Notification.push_bear(Config().S_KEY, OrderLog.MESSAGE_ORDER_SUCCESS_NOTIFICATION_TITLE, OrderLog.MESSAGE_ORDER_SUCCESS_NOTIFICATION_OF_EMAIL_CONTENT.format(self.order_id)) while sustain_time: # TODO 后面直接查询有没有待支付的订单就可以 num += 1