MihoyoBBSTools/push.py

83 lines
2.2 KiB
Python
Raw Normal View History

2022-01-09 09:31:44 +00:00
import os
from request import http
2022-01-11 07:54:47 +00:00
from loghelper import log
2022-01-09 09:31:44 +00:00
from configparser import ConfigParser
cfg = ConfigParser()
def load_config():
2022-01-09 10:57:42 +00:00
config_path = os.path.join(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'config'), 'push.ini')
if os.path.exists(config_path):
cfg.read(config_path, encoding='utf-8')
return True
else:
return False
2022-01-09 09:31:44 +00:00
def title(status):
if status == 0:
return "「米游社脚本」执行成功!"
else:
return "「米游社脚本」执行失败!"
2022-01-25 07:03:53 +00:00
def telegram(status, push_message):
http.post(
url="https://{}/bot{}/sendMessage".format(cfg.get('telegram', 'api_url'), cfg.get('telegram', 'bot_token')),
data={
"chat_id": cfg.get('telegram', 'chat_id'),
"text": title(status) + "\r\n" + push_message
}
)
2022-01-09 09:31:44 +00:00
def ftqq(status, push_message):
http.post(
url="https://sctapi.ftqq.com/{}.send".format(cfg.get('setting', 'push_token')),
data={
"title": title(status),
"desp": push_message
}
)
def pushplus(status, push_message):
http.post(
url="http://www.pushplus.plus/send",
data={
"token": cfg.get('setting', 'push_token'),
"title": title(status),
"content": push_message
}
)
def cq_http(status, push_message):
http.post(
url=cfg.get('cqhttp', 'cqhttp_url'),
json={
"user_id": cfg.getint('cqhttp', 'cqhttp_qq'),
"message": title(status) + "\r\n" + push_message
}
)
def push(status, push_message):
2022-01-09 10:57:42 +00:00
if not load_config():
return 0
2022-01-09 09:31:44 +00:00
if cfg.getboolean('setting', 'enable'):
2022-01-09 10:28:40 +00:00
push_server = cfg.get('setting', 'push_server').lower()
2022-01-11 07:54:47 +00:00
log.info("正在执行推送......")
log.debug(f"推送所用的服务为:{push_server}")
2022-01-09 10:28:40 +00:00
if push_server == "cqhttp":
2022-01-09 09:31:44 +00:00
cq_http(status, push_message)
2022-01-09 10:28:40 +00:00
elif push_server == "ftqq":
2022-01-09 09:31:44 +00:00
ftqq(status, push_message)
2022-01-09 10:28:40 +00:00
elif push_server == "pushplus":
2022-01-09 09:31:44 +00:00
pushplus(status, push_message)
2022-01-25 07:03:53 +00:00
elif push_server == "telegram":
telegram(status, push_message)
2022-01-11 07:54:47 +00:00
log.info("推送完毕......")
2022-01-09 09:31:44 +00:00
return 0