shell获取每年农历节日的日期

2021年02月26日

原创内容,转载请注明出处:https://www.myzhenai.com.cn/post/3711.html

我打算自己写一个每年节日就自动在网站上展示灯笼的源码功能,但是这里最重要的是要获得每年春节、端午节、中秋节等节日的日期,因为要自己写这判断这些日期的功能,那代码就多了,所以我用了一个万年历的接口来获取,这样我们只需要获取万年历接口返回的数据就可以了,其他类如情人节、国庆节、元旦、三八节、六一节、建军节等节日的时间是新历固定的,所以不依赖万年历接口都可以。

引导阅读:
为网站增加春节灯笼效果:https://www.myzhenai.com.cn/post/3645.html
万年历接口申请地址:https://www.juhe.cn/

我们需要先注册聚合数据,然后通过实名认证后,可以申请一个万年历的接口key,只有了这个key才可以引用万年历接口,但是免费版本的只有100次一个月,这个次数如果我们在网站PHP里调用的话,那么很容易就用完了,所以我把获取日期数据的功能写一个shell脚本,然后用php来读取这个脚本就可以了。把shell脚本定时为每月运行一次,这样就不会影响我们的使用次数。

万年历接口使用方法

获取当天的详细信息
http://v.juhe.cn/calendar/day?date=2015-1-1&key=您申请的appKey

获取近期假期
http://v.juhe.cn/calendar/month?year-month=2015-1&key=您申请的appKey

获取当年的假期列表
http://v.juhe.cn/calendar/year?year=2018&key=您申请的appKey

在这里我准备获取的是当年的假期列表,year后边的参数可以在shell里获取当前年份,然后用curl就可以准确得到我们需要的日期数据了,在浏览状态下获取到的是类如下图这样的。

{"reason":"Success","result":{"data":{"year":"2021","holidaylist":"[{\"name\":\"元旦\",\"startday\":\"2021-1-1\"},
{\"name\":\"除夕\",\"startday\":\"2021-2-11\"},{\"name\":\"春节\",\"startday\":\"2021-2-12\"},{\"name\":\"清明节\",\"startday\":\"2021-4-4\"},
{\"name\":\"劳动节\",\"startday\":\"2021-5-1\"},{\"name\":\"端午节\",\"startday\":\"2021-6-14\"},{\"name\":\"中秋节\",\"startday\":\"2021-9-21\"},
{\"name\":\"国庆节\",\"startday\":\"2021-10-1\"}]","holiday_list":[{"name":"元旦","startday":"2021-1-1"},{"name":"除夕","startday":"2021-2-11"},
{"name":"春节","startday":"2021-2-12"},{"name":"清明节","startday":"2021-4-4"},{"name":"劳动节","startday":"2021-5-1"},
{"name":"端午节","startday":"2021-6-14"},{"name":"中秋节","startday":"2021-9-21"},{"name":"国庆节","startday":"2021-10-1"}]}},"error_code":0}

 

    #!/usr/bin/env bash
    # !/bin/bash
    . /etc/profile
    ############################################################################################################################################
    #
    # This is a lottery number automatic generation tool
    # Autor: RucLinux
    # Web: https://www.myzhenai.com.cn/ https://www.myzhenai.com/ https://mangshe.python.hn.cn/ https://jiayu.mybabya.com/ 
    #  说明:转载引用请保留这段,谢谢
    #
    #############################################################################################################################################

var=$(date +%Y)
fen=$(curl -s http://v.juhe.cn/calendar/year?year=$var\&key=859195d15d********************** | awk -F "[" '{print $2}' | awk -F "]" '{print $1}' | sed 's/\\//g' | sed 's/\"//g' | sed 's/}\,/}\n/g')

这里有一点需要注意,在curl的时候,地址中的&符号需要用\进行转义,不然会出错。后边的都是截取和替换的命令。


sicnature ---------------------------------------------------------------------
Your current IP address is: 35.175.236.44
Your IP address location: 美国弗吉尼亚阿什本
Your IP address country and region: 美国 美国
Your current browser is:
Your current system is:
Original content, please indicate the source:
同福客栈论坛 | 蟒蛇科普海南乡情论坛 | JiaYu Blog
sicnature ---------------------------------------------------------------------
Welcome to reprint. Please indicate the source http://myzhenai.com/post/3711.html

1 评论

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注