節(jié)假日api
今天項目中有一個需求需要用到節(jié)假日,項目需求是
- 要有調休日
- 要有周末
于是就去網上找倦零。
看到比較合適的大概就這幾個:
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"
}
}
優(yōu)點是可以一次性獲取全年的误续。缺點是只有節(jié)假日,沒有周末扫茅,沒有調休
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"
}
優(yōu)點是有周末蹋嵌,缺點是沒有調休
3.http://api.goseek.cn/Tools/holiday?date=20180616
返回數據:
{
"code": 10000,
"data": 2
}
優(yōu)點是可以區(qū)分周末,節(jié)假日葫隙,工作日栽烂。缺點是API太過簡陋,只能針對某一天進行查詢恋脚。
綜合比較腺办,打算用第三個api來進行查詢,利用調休日一般都是周末的規(guī)律糟描,如果是周末并且是工作日那一定是調休怀喉。基于此生成一個節(jié)假日船响,調休日躬拢,工作日都有區(qū)分的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估灿,法定節(jié)假日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)