使用Python获取休息日和工作日还有调休日

我用企微的接口写了一个自动上下班提醒打卡的消息提醒,但是呢,节假日了还给我提醒上下班打卡,是不是很过分,所以就想用这个功能判断是不是节假日,从而判断用不用发送打卡提醒。

需要安装一个包:chinese_calendar

安装命令:

pip install chinesecalendar -i https://pypi.tuna.tsinghua.edu.cn/simple/

并附上企微提醒打卡的接口:

from apscheduler.schedulers.background import BlockingScheduler
from apscheduler.triggers.cron import CronTrigger
from chinese_calendar import is_workday, is_holiday, is_in_lieu
# 企业微信提醒点外卖打卡等
import datetime
import requests
import json


def work_on_remind(url, payload):
    # 提醒上班打卡
    headers = {
        'Content-Type': 'application/json'
    }
    response = requests.request("POST", url, headers=headers, data=payload)
    print(response.text)


def pay_lunch_remind(url, payload):
    # 提醒点外卖
    headers = {
        'Content-Type': 'application/json'
    }
    response = requests.request("POST", url, headers=headers, data=payload)
    print(response.text)


def work_off_remind(url, payload):
    # 提醒下班打卡
    headers = {
        'Content-Type': 'application/json'
    }
    response = requests.request("POST", url, headers=headers, data=payload)
    print(response.text)


def run(wx_url):
    current_year = datetime.datetime.now().year
    current_month = datetime.datetime.now().month
    current_day = datetime.datetime.now().day
    current_hour = datetime.datetime.now().hour
    current_minute = datetime.datetime.now().minute
    current_second = datetime.datetime.now().second
    current_time = f"{current_hour}点{current_minute}分{current_second}秒"
    dayOfWeek = datetime.datetime.now().weekday() + 1
    print(f"今天是星期{dayOfWeek}, 当前时间是:{current_time}")
    if is_holiday(datetime.datetime.now()):
        print(f"今天是休息日,不用打卡,日期:{current_year}年{current_month}月{current_day}日")
        return
    if (current_hour == 9) and (50 <= current_minute <= 59):
        content = f"上班打卡了,亲爱的宝子们~,需要艾特的话给我发手机号,今天是星期{dayOfWeek}, 当前时间是:{current_time}"
        payload = json.dumps({
            "msgtype": "text",
            "text": {
                "content": content,
                "mentioned_mobile_list": mentioned_mobile_list
            }
        })
        work_on_remind(wx_url, payload)
    elif (current_hour == 11) and (0 <= current_minute < 10):
        content = f"快点外卖吧,吃的胖胖的才有劲干活啊,亲爱的宝~, 需要艾特的话给我发手机号, 当前时间:{current_time}"
        payload = json.dumps({
            "msgtype": "text",
            "text": {
                "content": content,
                "mentioned_mobile_list": mentioned_mobile_list
            }
        })
        pay_lunch_remind(wx_url, payload)
    elif (current_hour == 19) and (0 <= current_minute < 10):
        content = f"我的宝,快下班打卡吧!总是忘记打卡的人,是不是你?!!!,需要艾特的话给我发手机号,今天是星期{dayOfWeek}, 当前时间是:{current_time}"
        payload = json.dumps({
            "msgtype": "text",
            "text": {
                "content": content,
                "mentioned_mobile_list": mentioned_mobile_list
            }
        })
        work_off_remind(wx_url, payload)
    else:
        print("不用发送打卡内容")


if __name__ == '__main__':
    """
    centos安装初始化文档:
    ps -aux | grep qiwei
    nohup python3 -u qiwei_remind.py > zet_remind.log 2>&1 &
    """
    scheduler = BlockingScheduler()
    # 企业微信机器人接口地址
    mentioned_mobile_list = ["需要艾特人的手机号"]
    wx_url = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=你的key"
    corn_tab = "*/10 * * * *"
    scheduler.add_job(run, CronTrigger.from_crontab(corn_tab), args=(wx_url,))
    print(f"定时任务启动了:定时提醒签到内容")
    scheduler.start()

猜你喜欢

转载自blog.csdn.net/weixin_44786530/article/details/130685990