从百度中提取节假日,上班日

import requests
import json
import random
import datetime

dataTime = str(random.random())
my_work = []
my_week = []
my_holiday = []


def get_calendar_data(calculation_year, month):
    header = {
        "Content-Type": "application/json;charset=UTF-8"
    }
    param = {
        "query": calculation_year + "年" + month + "月",
        "resource_id": "52109",
        "apiType": "yearMonthData",
        "type": "json",
        "tn": "reserved_all_res_tn",
        "cb": ""
    }
    r = requests.get(url="https://opendata.baidu.com/data/inner",
                     headers=header, params=param).text
    return json.loads(r)["Result"][0]["DisplayData"]["resultData"]["tplData"]["data"]["almanac"]


# 工作日、周末、法定假日分开
def get_work_week_holiday(calcultaion_year, month):
    # 提取almanac信息
    month_data = get_calendar_data(calcultaion_year, month)

    for one in month_data:  # 偶数上班,奇数休息
        # 非周末 且 无status 为正常工作日,记作:0
        if ((one["cnDay"] != '日' and one["cnDay"] != '六') and 'status' not in one):
            v = datetime.date(int(one['year']), int(one['month']), int(one['day']))
            my_work.append(v)
            # print(v,'\t0')

        # 周末 且 无status 为正常周末休息,记作:1
        elif ((one["cnDay"] == '日' or one["cnDay"] == '六') and 'status' not in one):
            v = datetime.date(int(one['year']), int(one['month']), int(one['day']))
            # my_holiday.append(v)
            my_week.append(v)
            # print(v,'\t1')

        # 周末 且 status=2(补班) 为周末补班,记作:2
        elif ((one["cnDay"] == '日' or one["cnDay"] == '六') and ('status' in one and one["status"] == '2')):
            v = datetime.date(int(one['year']), int(one['month']), int(one['day']))
            my_work.append(v)
            # print(v,'\t2')

        # status=1(节假日) 为节假日休息,记作:3
        elif ('status' in one and one["status"] == '1'):
            v = datetime.date(int(one['year']), int(one['month']), int(one['day']))
            my_holiday.append(v)
            # print(v,'\t3')

# 周末和法定假日合并
def get_work_WeekHoliday(calcultaion_year, month):
    # 提取almanac信息
    month_data = get_calendar_data(calcultaion_year, month)

    for one in month_data:  # 偶数上班,奇数休息
        # 非周末 且 无status 为正常工作日,记作:0
        if ((one["cnDay"] != '日' and one["cnDay"] != '六') and 'status' not in one):
            v = datetime.date(int(one['year']), int(one['month']), int(one['day']))
            my_work.append(v)
            # print(v,'\t0')

        # 周末 且 无status 为正常周末休息,记作:1
        elif ((one["cnDay"] == '日' or one["cnDay"] == '六') and 'status' not in one):
            v = datetime.date(int(one['year']), int(one['month']), int(one['day']))
            my_holiday.append(v)
            # my_week.append(v)
            # print(v,'\t1')

        # 周末 且 status=2(补班) 为周末补班,记作:2
        elif ((one["cnDay"] == '日' or one["cnDay"] == '六') and ('status' in one and one["status"] == '2')):
            v = datetime.date(int(one['year']), int(one['month']), int(one['day']))
            my_work.append(v)
            # print(v,'\t2')

        # status=1(节假日) 为节假日休息,记作:3
        elif ('status' in one and one["status"] == '1'):
            v = datetime.date(int(one['year']), int(one['month']), int(one['day']))
            my_holiday.append(v)
            # print(v,'\t3')


# 周末和法定假日合并
def get_WorkWeek_holiday(calcultaion_year, month):
    # 提取almanac信息
    month_data = get_calendar_data(calcultaion_year, month)

    for one in month_data:  # 偶数上班,奇数休息
        # 非周末 且 无status 为正常工作日,记作:0
        if ((one["cnDay"] != '日' and one["cnDay"] != '六') and 'status' not in one):
            v = datetime.date(int(one['year']), int(one['month']), int(one['day']))
            my_work.append(v)
            # print(v,'\t0')

        # 周末 且 无status 为正常周末休息,记作:1
        elif ((one["cnDay"] == '日' or one["cnDay"] == '六') and 'status' not in one):
            v = datetime.date(int(one['year']), int(one['month']), int(one['day']))
            # my_holiday.append(v)
            my_work.append(v)
            # print(v,'\t1')

        # 周末 且 status=2(补班) 为周末补班,记作:2
        elif ((one["cnDay"] == '日' or one["cnDay"] == '六') and ('status' in one and one["status"] == '2')):
            v = datetime.date(int(one['year']), int(one['month']), int(one['day']))
            my_work.append(v)
            # print(v,'\t2')

        # status=1(节假日) 为节假日休息,记作:3
        elif ('status' in one and one["status"] == '1'):
            v = datetime.date(int(one['year']), int(one['month']), int(one['day']))
            my_holiday.append(v)
            # print(v,'\t3')



def out_date_section(dateArr):
    delta = datetime.timedelta(days=1)
    date1 = dateArr[0]
    date_tmp = date1
    for i in range(1, len(dateArr)):
        date2 =dateArr[i]
        if ((date_tmp+delta)!=date2):
             print(date1.strftime('%Y%m%d')+'-'+date_tmp.strftime('%Y%m%d'))
             date1 = date2

        date_tmp = date2

    print(date1.strftime('%Y%m%d') + '-' + date_tmp.strftime('%Y%m%d'))

def out_date_line(dateArr):
    s=''
    for d in dateArr:

        s=s+','+d.strftime('%Y-%m-%d')

    print(s)


if __name__ == '__main__':
    # 此处只能算当年之前的,因为国务院是每年12月份才会发布第二年的放假计划,所以此接口对于下一年的统计是错的。
    calcultaion_year = "2024"
    # 因该接口传入的时间,查询了前一个月,当前月和后一个月的数据,所以只需要2、5、8、11即可全部获取到。比如查询5月份,则会查询4,5,6月分的数据
    calculation_month = ["2", "5", "8", "11"]

##############################################输入交互##########################################################
    while True:
        print("*************start****************")
        print("工作日、周末、法定假日分开,请输入1;")
        print("周末和法定假日合并,请输入2;")
        print("工作日、周末合并,请输入3;")
        print(",号分隔输出法定假日,请输入4;")
        print("退出请输入Q;")
        p=input('Enter a number:')
        if p == "q": break
        elif int(p) == 1:
            for one_month in calculation_month:
                get_work_week_holiday(calcultaion_year, one_month)

            print('##############以下为国家法定节假日(含调休)##########################')
            out_date_section(my_holiday)
            print('##############以下为上班日(含补班)##########################')
            out_date_section(my_work)
            print('##############以下为周末休息日##########################')
            out_date_section(my_week)
        elif int(p) == 2:
            for one_month in calculation_month:
                get_work_WeekHoliday(calcultaion_year, one_month)

            print('##############以下为国家法定节假日(含调休)、周末休息日##########################')
            out_date_section(my_holiday)
            print('##############以下为上班日(含补班)##########################')
            out_date_section(my_work)
        elif int(p) == 3:
            for one_month in calculation_month:
                get_WorkWeek_holiday(calcultaion_year, one_month)

            print('##############以下为国家法定节假日(含调休)##########################')
            out_date_section(my_holiday)
            print('##############以下为周末休息日、上班日(含补班)##########################')
            out_date_section(my_work)
        elif int(p) == 4:
            for one_month in calculation_month:
                get_work_week_holiday(calcultaion_year, one_month)
            out_date_line(my_holiday)

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值