在青龙面板定时运行,并通过配置好的消息通知,定时发送通知
天气情况 (2024-10-18)
实时天气情况: 大风
实时温度: 12°C (体感: 4°C)
每秒风速: 39.96米
降水监测: 雷达显示10公里区域内存在降水
当日天气情况: 大风
当日温度: 7°C ~ 15°C
当日湿度: 46% ~ 64%
注册免费API:http://caiyunai.com/api/weather_api.html#api
import requests
import notify
import asyncio
from datetime import datetime
# 用户输入的秘钥和经纬度(以逗号分隔)
API_KEY = "秘钥" # 这里输入用户自己的秘钥
LOCATION = "经纬度" # 输入用户的经纬度
# 天气状况翻译表
SKYCON_TRANSLATION = {
"CLEAR_DAY": "晴天",
"CLEAR_NIGHT": "晴夜",
"PARTLY_CLOUDY_DAY": "多云",
"PARTLY_CLOUDY_NIGHT": "多云夜晚",
"CLOUDY": "阴天",
"LIGHT_HAZE": "轻度雾霾",
"MODERATE_HAZE": "中度雾霾",
"HEAVY_HAZE": "重度雾霾",
"LIGHT_RAIN": "小雨",
"MODERATE_RAIN": "中雨",
"HEAVY_RAIN": "大雨",
"STORM_RAIN": "暴雨",
"FOG": "雾",
"LIGHT_SNOW": "小雪",
"MODERATE_SNOW": "中雪",
"HEAVY_SNOW": "大雪",
"STORM_SNOW": "暴雪",
"DUST": "浮尘",
"SAND": "沙尘",
"WIND": "大风"
}
def fetch_weather_data(url):
"""从指定 URL 获取天气数据"""
try:
response = requests.get(url)
response.raise_for_status() # 检查请求是否成功
return response.json().get('result', {})
except requests.exceptions.RequestException as e:
print(f"抓取天气信息失败: {e}")
return {}
def get_weather_info(api_key, location):
"""获取实时和当日天气信息"""
# 实时天气API
realtime_url = f"https://api.caiyunapp.com/v2.6/{api_key}/{location}/realtime"
# 当日天气API
daily_url = f"https://api.caiyunapp.com/v2.6/{api_key}/{location}/daily?dailysteps=1"
# 获取实时天气数据
realtime_data = fetch_weather_data(realtime_url).get('realtime', {})
# 获取当日天气数据
daily_data = fetch_weather_data(daily_url).get('daily', {})
if not realtime_data or not daily_data:
return "无法获取天气数据。"
# 提取实时天气数据
temperature = round(realtime_data.get('temperature', 0))
humidity = round(realtime_data.get('humidity', 0) * 100)
skycon = realtime_data.get('skycon', '未知')
wind_speed = realtime_data.get('wind', {}).get('speed', 0)
apparent_temperature = round(realtime_data.get('apparent_temperature', 0))
precipitation = realtime_data.get('precipitation', {}).get('local', {}).get('intensity', 0)
nearest_precip_distance = realtime_data.get('precipitation', {}).get('nearest', {}).get('distance', 0)
# 提取当日天气数据
daily_temp = daily_data.get('temperature', [{}])[0]
daily_humidity = daily_data.get('humidity', [{}])[0]
daily_skycon = daily_data.get('skycon', [{}])[0].get('value', '未知')
# 翻译天气状况
skycon_desc = SKYCON_TRANSLATION.get(skycon, "未知天气状况")
daily_skycon_desc = SKYCON_TRANSLATION.get(daily_skycon, "未知天气状况")
# 构建输出字符串
weather_info = (
f"实时天气情况: {skycon_desc}\n"
f"实时温度: {temperature}°C (体感: {apparent_temperature}°C)\n"
f"每秒风速: {wind_speed}米\n"
)
# 判断降水状况
if precipitation == 0 and nearest_precip_distance > 10000:
weather_info += "降水监测: 目前无降水\n(雷达显示最近降水距离超过10公里)"
else:
weather_info += "降水监测: 雷达显示10公里区域内存在降水"
# 加入当日天气信息
weather_info += (
f"\n当日天气情况: {daily_skycon_desc}\n"
f"当日温度: {round(daily_temp.get('min', 0))}°C ~ {round(daily_temp.get('max', 0))}°C\n"
f"当日湿度: {round(daily_humidity.get('min', 0) * 100)}% ~ {round(daily_humidity.get('max', 0) * 100)}%\n"
)
return weather_info
async def main():
"""主程序入口"""
# 调用天气信息函数
weather_result = get_weather_info(API_KEY, LOCATION)
if weather_result:
print("天气信息:\n", weather_result)
else:
print("未能提取到天气信息。")
summary_message = weather_result or "未能提取到天气信息。"
# 获取当前日期
current_date = datetime.now().strftime("%Y-%m-%d")
# 直接调用同步的 notify.send 方法,标题中包含当前日期
notify.send(f"天气情况 ({current_date})", summary_message)
print("通知发送成功")
if __name__ == "__main__":
asyncio.run(main())
评论区