MihoyoBBSTools/push.py

114 lines
3.1 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-04-20 05:00:29 +00:00
# telegram的推送
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-04-20 05:00:29 +00:00
# server酱
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
}
)
2022-04-20 05:00:29 +00:00
# pushplus
2022-01-09 09:31:44 +00:00
def pushplus(status, push_message):
http.post(
2022-04-20 05:00:29 +00:00
url="https://www.pushplus.plus/send",
2022-01-09 09:31:44 +00:00
data={
"token": cfg.get('setting', 'push_token'),
"title": title(status),
"content": push_message
}
)
2022-04-20 05:00:29 +00:00
# cq http协议的推送
2022-01-09 09:31:44 +00:00
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
}
)
2022-04-20 05:00:29 +00:00
# 企业微信 感谢linjie5492@github
2022-04-20 05:44:55 +00:00
def wecom(status, push_message):
2022-04-20 05:00:29 +00:00
secret = cfg.get('wecom', 'secret')
wechat_id = cfg.get('wecom', 'wechat_id')
push_token = http.post(
url=f'https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={wechat_id}&corpsecret={secret}',
data="").json()['access_token']
push_data = {
"agentid": cfg.get('wecom', 'agentid'),
"msgtype": "text",
"touser": "@all",
"text": {
"content": title(status) + "\r\n" + push_message
}, "safe": 0}
http.post(f'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={push_token}', json=push_data)
2022-04-20 05:44:55 +00:00
# pushdeer
def pushdeer(status, push_message):
http.get(
url=f'{cfg.get("pushdeer", "api_url")}/message/push',
params={
2022-04-21 09:08:41 +00:00
"pushkey": cfg.get("pushdeer", "token"),
2022-04-20 05:44:55 +00:00
"text": title(status),
"desp": str(push_message).replace("\r\n", "\r\n\r\n"),
"type": "markdown"
}
)
2022-01-09 09:31:44 +00:00
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("正在执行推送......")
2022-04-20 05:00:29 +00:00
try:
log.debug(f"推送所用的服务为:{push_server}")
eval(push_server[:10].lower() + "(status, push_message)")
except NameError:
log.warning("推送服务名称错误")
else:
log.info("推送完毕......")
2022-01-09 09:31:44 +00:00
return 0