Files
daily-schedule/work/daily_weather.py
2025-10-15 13:37:09 +08:00

87 lines
3.1 KiB
Python

import requests
from datetime import datetime
weather_url = 'https://p478kygkjw.re.qweatherapi.com'
weather_api_key = '4aa47f183c694aaa812ff018be8270e4'
nanjing_location = '101190101'
def get_today_weather(city_location=nanjing_location):
url = f'{weather_url}/v7/weather/3d'
response = requests.get(
url=url,
headers={'X-QW-Api-Key': f'{weather_api_key}'},
params={'location': city_location}
)
return response.json()
def get_today_indices(city_location=nanjing_location):
url = f'{weather_url}/v7/indices/1d'
response = requests.get(
url=url,
headers={'X-QW-Api-Key': f'{weather_api_key}'},
params={'location': city_location, 'type': '0'}
)
return response.json()
def get_weather_message(weather, indices) -> str:
textDay = weather['daily'][0]['textDay']
textNight = weather['daily'][0]['textNight']
weather_desc = textDay if textDay == textNight else f"{textDay}{textNight}"
# 定义天气数据变量
weather_data = {
"city": "南京",
"date": weather['daily'][0]['fxDate'],
"update_time": datetime.now().strftime("%H:%M:%S"),
"temp_max": weather['daily'][0]['tempMax'],
"temp_min": weather['daily'][0]['tempMin'],
"weather": weather_desc,
"humidity": weather['daily'][0]['humidity'] + "%",
"wind": weather['daily'][0]['windDirDay'] + " " + weather['daily'][0]['windScaleDay'] + "",
"life_index": [
{"type": "舒适度指数", "icon": "😊", "level": indices['daily'][7]['category'],
"advice": indices['daily'][7]['text']},
{"type": "穿衣指数", "icon": "👕", "level": indices['daily'][2]['category'],
"advice": indices['daily'][2]['text']},
{"type": "运动指数", "icon": "🏃", "level": indices['daily'][0]['category'],
"advice": indices['daily'][0]['text']},
{"type": "洗车指数", "icon": "🚗", "level": indices['daily'][1]['category'],
"advice": indices['daily'][1]['text']}
],
"data_source": "中央气象台"
}
# 构建markdown内容
return f"""# 🌤️ 今日天气播报
## 📍 基本信息
**城市:** {weather_data["city"]}
**日期:** {weather_data["date"]}
**更新时间:** {weather_data["update_time"]}
## 🌡️ 今日天气
- **温度:** 最高{weather_data["temp_max"]}℃/最低{weather_data["temp_min"]}
- **天气:** {weather_data["weather"]}
- **湿度:** {weather_data["humidity"]}
- **风力:** {weather_data["wind"]}
## 📊 生活指数
😊 **舒适度指数:** {weather_data["life_index"][0]["level"]}
建议:{weather_data["life_index"][0]["advice"]}
👕 **穿衣指数:** {weather_data["life_index"][1]["level"]}
建议:{weather_data["life_index"][1]["advice"]}
🏃 **运动指数:** {weather_data["life_index"][2]["level"]}
建议:{weather_data["life_index"][2]["advice"]}
🚗 **洗车指数:** {weather_data["life_index"][3]["level"]}
建议:{weather_data["life_index"][3]["advice"]}
---
*数据来源:{weather_data['data_source']}*"""