美文网首页
节假日api及其整合

节假日api及其整合

作者: Android笨鸟之旅 | 来源:发表于2018-06-02 15:44 被阅读0次

节假日api

今天项目中有一个需求需要用到节假日,项目需求是

  • 要有调休日
  • 要有周末

于是就去网上找。
看到比较合适的大概就这几个:
1.http://tool.bitefu.net/jiari?d=2018
返回数据:
返回结果:

{
    "2018": {
        "1001": "2",
        "1002": "2",
        "1003": "2",
        "1004": "1",
        "1005": "1",
        "1006": "1",
        "1007": "1",
        "0101": "2",
        "0215": "1",
        "0216": "2",
        "0217": "2",
        "0218": "2",
        "0219": "1",
        "0220": "1",
        "0221": "1",
        "0405": "2",
        "0406": "1",
        "0407": "1",
        "0429": "1",
        "0430": "1",
        "0501": "2",
        "0616": "1",
        "0617": "1",
        "0618": "2",
        "0922": "1",
        "0923": "1",
        "0924": "2"
    }
}

优点是可以一次性获取全年的。缺点是只有节假日,没有周末,没有调休

2.http://www.easybots.cn/api/holiday.php?m=201801,201802
返回数据:

{
    "201801": {
        "13": "1",
        "14": "1",
        "20": "1",
        "21": "1",
        "27": "1",
        "28": "1",
        "01": "2",
        "06": "1",
        "07": "1"
    }

优点是有周末,缺点是没有调休

3.http://api.goseek.cn/Tools/holiday?date=20180616
返回数据:

{
    "code": 10000,
    "data": 2
}

优点是可以区分周末,节假日,工作日。缺点是API太过简陋,只能针对某一天进行查询。

综合比较,打算用第三个api来进行查询,利用调休日一般都是周末的规律,如果是周末并且是工作日那一定是调休。基于此生成一个节假日,调休日,工作日都有区分的json文件。

Python代码附带如下:

import requests  
import re  
import json  
import time  
import datetime  
import random  

startday = [2016, 1, 1]
num = 1096   # 开始天之后的多少天,这里设为3年
workdays = 5   # 开始天是星期几. 这里周日为0,周一到周六分别为1-6

def getHoliday(start, index, days):
    daylist = []   # 记录下所有的调休日0,法定节假日2,和周末3
    startday = datetime.date(start[0], start[1], start[2])  
    for i in range(0 , days):  
        workindex = (index + i) % 7
        rawDate = startday + datetime.timedelta(days = i)
        regex=re.compile(r'\d\d')  
        tmpDate = regex.findall(str(rawDate))  
        date = str(tmpDate[0]) + str(tmpDate[1]) + str(tmpDate[2]) + str(tmpDate[3])
        r = requests.get(r'http://api.goseek.cn/Tools/holiday?date=' + date)  
        r.encoding = r.apparent_encoding  
        json_data = json.loads(r.text)  
        if int(json_data['data']) == 0: 
            if workindex == 0 or workindex == 6:
                daylist.append({"date":str(rawDate), "val":0})
        elif int(json_data['data']) == 1:
            daylist.append({"date":str(rawDate), "val":3})    
        elif int(json_data['data']) == 2:  
            daylist.append({"date":str(rawDate), "val":2})
    return daylist
  
def buildJson(daylist):
    date_1 = {"date": daylist}
    date_2 = {"date": date_1, "version": 1}
    with open('yourfile.json', 'w') as holiday:
        json.dump(date_2, holiday)
    print("写出完成")

daylist = getHoliday(startday, workdays, num)
buildJson(daylist)

相关文章

网友评论

      本文标题:节假日api及其整合

      本文链接:https://www.haomeiwen.com/subject/bwersftx.html