|
前言:
用于调用“农历”的API提供商,或因“跑路”,或因“收费”,或因“次数限制”等各种问题,使获取农历信息不太稳定。
所以几经考虑,搭建(农历)本地离线库。
个人使用环境:
平台:PVE (LXC)
系统:Debian12
软件:node.js
离线库:lunar-JavaScript {lunar (6tail.cn)}
接入:node-red
注意:
因网络原因,安装较慢,耐心等待。
API服务器可用于内网获取,也可用于公网环境获取{(无验证措施)不建议 }。
过程:
使用root账户登录
修改Debian源为国内源 debian | 镜像站使用帮助 | 清华大学开源软件镜像站 | Tsinghua Open Source Mirror
更新软件包
使用NVM(Node Version Manager)来安装Node.js
必要依赖安装
apt install curl git sudo
安装NVM
<span style="color: rgba(0, 0, 0, 0.8); font-family: SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace; font-size: 13.6px; background-color: rgb(247, 247, 249);">bash -c "$(curl -fsSL https://gitee.com/RubyMetric/nvm-cn/raw/main/install.sh)"</span>
安装完成后重新启动终端
使用NVM 安装 Node.js npm
<span style="color: rgba(0, 0, 0, 0.85); font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Noto Sans", sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji"; white-space: pre; background-color: rgb(255, 255, 255);">nvm install --lts</span>
安装lunar-javascript
npm init -y
npm install lunar-javascript
新建test.js脚本
粘贴下方代码,按Ctrl键+X,按Y保存。
const http = require('http');
const lunar = require('lunar-javascript');
const server = http.createServer((req, res) => {
res.writeHead(200, { 'Content-Type': 'application/json; charset=utf-8' });
const lunarDate = lunar.Lunar.fromDate(new Date());
const eightCharString = lunarDate.getEightChar().toString();
const responseData = {
Lunar: {
LYear: lunarDate.getMonthShengXiaoExact(),
LMonth: lunarDate.getMonthInChinese(),
LDay: lunarDate.getDayInChinese(),
TimeInGanZhi: lunarDate.getTimeInGanZhi(),
Week: lunarDate.getWeekInChinese(),
LJie: lunarDate.getOtherFestivals(),
TianGanYear: lunarDate.getYearInGanZhiExact(),
TianGanMonth: lunarDate.getMonthInGanZhiExact(),
TianGanDay: lunarDate.getDayInGanZhiExact(),
DayLu: lunarDate.getDayLu(),
JieQi: lunarDate.getPrevJieQi(true),
Xiu: lunarDate.getXiu(),
Animal: lunarDate.getAnimal(),
XinLuck: lunarDate.getXiuLuck(),
Zheng: lunarDate.getZheng(),
Gong: lunarDate.getGong(),
PengZuGan: lunarDate.getPengZuGan(),
PengZuZhi: lunarDate.getPengZuZhi(),
XiShen: lunarDate.getDayPositionXi() + ' = ' + lunarDate.getDayPositionXiDesc(),
YangGui: lunarDate.getDayPositionYangGui() + ' = ' + lunarDate.getDayPositionYangGuiDesc(),
YinGui: lunarDate.getDayPositionYinGui() + ' = ' + lunarDate.getDayPositionYinGuiDesc(),
FUShen: lunarDate.getDayPositionFu() + ' = ' + lunarDate.getDayPositionFuDesc(),
CaiShen: lunarDate.getDayPositionCai() + ' = ' + lunarDate.getDayPositionCaiDesc(),
TaiShen: lunarDate.getDayPositionTai(),
ChongSha: lunarDate.getDayChongDesc() + ' ' + lunarDate.getDaySha(),
WinXingNaYear: lunarDate.getYearNaYin(),
WuxingNaMonth: lunarDate.getMonthNaYin(),
WuxingNaDay: lunarDate.getDayNaYin(),
WuXingZhiXing: lunarDate.getDayNaYin() + ' ' + lunarDate.getZhiXing(),
BaZi: eightCharString,
Yi: lunarDate.getDayYi(),
Ji: lunarDate.getDayJi(),
JiShen: lunarDate.getDayJiShen(),
XiongShen: lunarDate.getDayXiongSha(),
YueXiang: lunarDate.getYueXiang(),
}
};
const jsonResponse = JSON.stringify(responseData);
res.end(jsonResponse);
});
const port = 3000;
server.listen(port, () => {
console.log(`Server is running at http://localhost:${port}/`);
});
创建开机启动
新建my-nodejs-service.service
nano /etc/systemd/system/my-nodejs-service.service
在文件中添加以下内容
注意:需要更改ExecStart= 的node安装路径和test.js脚本路径
粘贴下方代码,按Ctrl键+X,按Y保存。
[Unit]
Description=My Node.js Service
After=network.target
[Service]
ExecStart=/usr/bin/node /root/test.js
Restart=always
RestartSec=10
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=nodejs-example
[Install]
WantedBy=multi-user.target
查找node.js路径
启用和启动服务
systemctl enable my-nodejs-service
systemctl start my-nodejs-service
检查服务状态
systemctl status my-nodejs-service
验证
使用浏览器打开 http://localhost:3000/ 将localhost 替换为主机IP地址
{
"Lunar": {
"LYear": "龙",
"LMonth": "二",
"LDay": "三十",
"TimeInGanZhi": "甲辰",
"Week": "一",
"LJie": [],
"TianGanYear": "甲辰",
"TianGanMonth": "戊辰",
"TianGanDay": "壬寅",
"DayLu": "亥命互禄 甲命进禄",
"JieQi": {
"_p": {
"name": "清明",
"solar": {
"_p": {
"year": 2024,
"month": 4,
"day": 4,
"hour": 15,
"minute": 2,
"second": 15
}
},
"jie": true,
"qi": false
}
},
"Xiu": "心",
"Animal": "狐",
"XinLuck": "凶",
"Zheng": "月",
"Gong": "东",
"PengZuGan": "壬不泱水更难提防",
"PengZuZhi": "寅不祭祀神鬼不尝",
"XiShen": "离 = 正南",
"YangGui": "震 = 正东",
"YinGui": "巽 = 东南",
"FUShen": "巽 = 东南",
"CaiShen": "离 = 正南",
"TaiShen": "仓库炉 房内南",
"ChongSha": "(丙申)猴 北",
"WinXingNaYear": "覆灯火",
"WuxingNaMonth": "大林木",
"WuxingNaDay": "金箔金",
"WuXingZhiXing": "金箔金 开",
"BaZi": "甲辰 戊辰 壬寅 甲辰",
"Yi": [
"嫁娶",
"开光",
"出行",
"出火",
"拆卸",
"进人口",
"开市",
"立券",
"交易",
"挂匾",
"入宅",
"移徙",
"安床",
"栽种"
],
"Ji": [
"祈福",
"入殓",
"祭祀",
"作灶",
"安葬",
"探病"
],
"JiShen": [
"王日",
"五富",
"普护",
"五合",
"青龙",
"鸣吠对"
],
"XiongShen": [
"游祸",
"血支",
"归忌"
],
"YueXiang": "晦"
}
}
|
评分
-
查看全部评分
|