scripts/alert/calendar_tips.py
2023-06-05 23:04:30 +08:00

73 lines
2.6 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import json
import time
import datetime
import requests
def send_msg_tip(msg_tip):
# get the datetime, which is using at a failed situation
alert_datetime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
# Enterprise WeChat Bot API and the format of body to send
hook_url = 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=ddea3f5f-fbfc-4c21-994a-71e9fc50e4ef'
body = {
"msgtype": "text",
"text": {
"content": msg_tip
}
}
# get the result of API call
res = requests.post(hook_url, data=json.dumps(body, ensure_ascii=False).encode('utf-8'))
# when failed, log it in /opt/logs/alert.log file
if res.status_code != 200:
with open('/opt/logs/alert.log', 'a', encoding='utf-8') as alert_log:
alert_log.write(alert_datetime + ' >>>> ')
alert_log.write('Failed sending message: ')
alert_log.write(msg_tip + '\n')
# 查询当天的节假日情况
def holiday_today(app_id, app_secret):
week_index = {1: '', 2: '', 3: '', 4: '', 5: '', 6: '', 7: '', }
today = datetime.date.today()
today_fmt = str(today).replace('-', '')
api_url = f'https://www.mxnzp.com/api/holiday/single/{today_fmt}?ignoreHoliday=false&app_id={app_id}&app_secret={app_secret}'
res = requests.get(api_url)
res_http_code = res.status_code
res_text = json.loads(res.text)
res_code = res_text['code']
res_msg = res_text['msg']
if res_http_code != 200 or res_code == 0:
msg_tip = res_msg
else:
res_weekday = res_text['data']['weekDay']
res_yeartips = res_text['data']['yearTips']
res_chinesezodiac = res_text['data']['chineseZodiac']
res_typedes = res_text['data']['typeDes']
res_type = res_text['data']['type']
res_dayofyear = res_text['data']['dayOfYear']
res_weekofyear = res_text['data']['weekOfYear']
res_constellation = res_text['data']['constellation']
msg_tip = f'{today}{res_yeartips}{res_chinesezodiac}年,星期{week_index[res_weekday]}{res_constellation}。本周是今年的第{res_weekofyear}周,今天是今年的第{res_dayofyear}天,是{res_typedes}'
if res_type == 2 or res_type == 1:
msg_tip += f"请好好休息,享用美好的一天。"
else:
msg_tip += f"请努力工作,保持良好的心态。"
send_msg_tip(msg_tip)
def main():
app_id = "nrsngdkvknqkrwko"
app_secret = "SFFmQWo2dnNBRjdNYkVSclZxa2ZvUT09"
holiday_today(app_id, app_secret)
if __name__ == '__main__':
main()