公曆轉農曆重要節日並發送line_notify
公曆轉農曆重要節日並發送line_notify
功能說明:
當初一,五,十五,還有農曆重要節日,會發送圖片和訊息到手機,在node-red dashboard上也可看見今日的公曆和對應的農曆。
第一步:
打開CMD並下載公曆轉農曆
'''
pip install requests lunarcalendar
'''
安裝好後用vscode寫程式,這個程式碼要給node-red的exec節點執行用的
然後去line notify申請token
import datetime
from lunarcalendar import Converter, Solar, Lunar, DateNotExist
import requests
# 定義重要的農曆節日
important_festivals = {
(1, 1): "春節",
(1, 15): "元宵節",
(5, 5): "端午節",
(7, 7): "七夕情人節",
(8, 15): "中秋節",
(9, 9): "重陽節",
(2, 2): "媽媽農曆生日"
# ... 其他節日
}
def line_notify(message, image_url=None):
# 使用LINE Notify
line_notify_token = '自行到line_notify申請token'
line_notify_api = 'https://notify-api.line.me/api/notify'
headers = {
'Authorization': f'Bearer {line_notify_token}',
}
data = {'message': message}
if image_url:
data['imageThumbnail'] = image_url
data['imageFullsize'] = image_url
response = requests.post(line_notify_api, headers=headers, data=data)
print("提醒已發送:", response.text)
def solar_to_lunar(solar_date):
try:
lunar_date = Converter.Solar2Lunar(solar_date)
festival = important_festivals.get((lunar_date.month, lunar_date.day))
images = {
1: 'https://upload.shejihz.com/2020/01/11f73cf198e7d1017c5c122aa5211ddf.jpg',
5: 'https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhUuAh_FpCPQJE5BfXfurDIB8u9gmhtwSuj9o8YPSRvpHwhLGKlr14CAZy6tAxW0V_ncGEbkJDt0V8bXWZM-NucXo5Wr2WXHpIUffbwGNJ_XL_mi_1Xi7ItEliXqtCwgaNyllu2VdQ3M_Q/s1600/%25E8%25A5%25BF%25E5%25A4%25A928%25E7%25A5%2596%25E4%25BB%2598%25E6%25B3%2595%25E5%2581%2588+29%25E6%25A9%25AB_%25E7%25B9%2581%25E9%25AB%2594.jpg',
15: 'https://i.pinimg.com/564x/58/79/37/587937e5f1860eda3d1879c5487f66d4.jpg',
# 22: 'https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhUuAh_FpCPQJE5BfXfurDIB8u9gmhtwSuj9o8YPSRvpHwhLGKlr14CAZy6tAxW0V_ncGEbkJDt0V8bXWZM-NucXo5Wr2WXHpIUffbwGNJ_XL_mi_1Xi7ItEliXqtCwgaNyllu2VdQ3M_Q/s1600/%25E8%25A5%25BF%25E5%25A4%25A928%25E7%25A5%2596%25E4%25BB%2598%25E6%25B3%2595%25E5%2581%2588+29%25E6%25A9%25AB_%25E7%25B9%2581%25E9%25AB%2594.jpg',
# ... 其他重要節日的圖片
}
if lunar_date.day in images:
message = f"今天是農曆{lunar_date.year}年{lunar_date.month}月{lunar_date.day}日,別忘了金龍寺吃齋!"
image_url = images[lunar_date.day]
line_notify(message, image_url)
if festival:
message = f"今天是{festival},祝節日快樂!"
# 如果節日有特定的圖片,您可以在這裡添加相應的邏輯來設置image_url
line_notify(message)
formatted_lunar_date = f"{lunar_date.year}年{lunar_date.month}月{lunar_date.day}日"
return formatted_lunar_date
except DateNotExist:
return "輸入的日期不存在。"
today = datetime.date.today()
# print("今天的日期是:", today)
solar_today = Solar(today.year, today.month, today.day)
formatted_lunar_date = solar_to_lunar(solar_today)
print("今天的日期是:", today,"\n","對應的農曆日期是:", formatted_lunar_date)
第二步
然後進入node-red編輯介面
匯入以下json
[
{
"id": "dd8c3c98dc31a448",
"type": "tab",
"label": "公曆轉農曆重要節日提醒",
"disabled": false,
"info": "",
"env": []
},
{
"id": "a1fa02adf5a85f40",
"type": "exec",
"z": "dd8c3c98dc31a448",
"command": "python3 /home/blairan/Documents/lunar_converter.py",
"addpay": "payload",
"append": "",
"useSpawn": "false",
"timer": "",
"winHide": false,
"oldrc": false,
"name": "",
"x": 440,
"y": 200,
"wires": [
[
"7b501b8b21e11098",
"484d0a0874779035"
],
[],
[]
]
},
{
"id": "35d1d301137b86d4",
"type": "inject",
"z": "dd8c3c98dc31a448",
"name": "",
"props": [
{
"p": "payload"
},
{
"p": "topic",
"vt": "str"
}
],
"repeat": "",
"crontab": "00 06 * * *",
"once": true,
"onceDelay": 0.1,
"topic": "",
"payload": "",
"payloadType": "date",
"x": 320,
"y": 160,
"wires": [
[
"a1fa02adf5a85f40"
]
]
},
{
"id": "484d0a0874779035",
"type": "debug",
"z": "dd8c3c98dc31a448",
"name": "debug 4",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "false",
"statusVal": "",
"statusType": "auto",
"x": 600,
"y": 240,
"wires": []
},
{
"id": "7b501b8b21e11098",
"type": "ui_text",
"z": "dd8c3c98dc31a448",
"group": "78226a68f27063f2",
"order": 2,
"width": 15,
"height": 2,
"name": "",
"label": "",
"format": "{{msg.payload}}",
"layout": "col-center",
"className": "",
"style": true,
"font": "Times New Roman,Times,serif",
"fontSize": "25",
"color": "#a22525",
"x": 590,
"y": 280,
"wires": []
},
{
"id": "20347c258fd67ffd",
"type": "ui_template",
"z": "dd8c3c98dc31a448",
"group": "78226a68f27063f2",
"name": "背景圖",
"order": 1,
"width": 15,
"height": 1,
"format": "<style>\n body {\n background-image: url(\"https://pic.616pic.com/bg_w1180/00/02/97/x0EgGXCwbM.jpg\");\n background-size: 100% 100%;\n }\n \n //body.nr-dashboard-theme md-content md-card {\n // background: rgba(1,200,1,0.1);\n //}\n \n \n</style>",
"storeOutMessages": true,
"fwdInMessages": true,
"resendOnRefresh": true,
"templateScope": "local",
"className": "",
"x": 310,
"y": 280,
"wires": [
[]
]
},
{
"id": "78226a68f27063f2",
"type": "ui_group",
"name": "重要節日提醒",
"tab": "58d683e10b4b812e",
"order": 1,
"disp": true,
"width": 15,
"collapse": false,
"className": ""
},
{
"id": "58d683e10b4b812e",
"type": "ui_tab",
"name": "農曆重要日提醒",
"icon": "dashboard",
"disabled": false,
"hidden": false
}
]
exec節點有個地方要更改為你自己的路徑
例如:python3 /home/blairan/Documents/lunar_converter.py




留言
張貼留言