import requests from datetime import datetime from wechat_message import send_wechat_message weather_url = 'https://p478kygkjw.re.qweatherapi.com/' weather_api_key = '4aa47f183c694aaa812ff018be8270e4' nanjing_location = '101190101' def get_today_weather(city_location: int): response = requests.get( f'{weather_url}v7/weather/3d', headers={ 'X-QW-Api-Key': f'{weather_api_key}' }, params={'location': city_location} ) return response.json() def get_today_indices(city_location: int): response = requests.get( f'{weather_url}v7/indices/1d', headers={ 'X-QW-Api-Key': f'{weather_api_key}' }, params={ 'location': city_location, 'type': '0' } ) return response.json() if __name__ == '__main__': weather = get_today_weather(nanjing_location) textDay = weather['daily'][0]['textDay'] textNight = weather['daily'][0]['textNight'] weather_desc = textDay if textDay == textNight else f"{textDay}转{textNight}" indices = get_today_indices(nanjing_location) # 定义天气数据变量 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内容 markdown_content = 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']}*""" message = { "msgtype": "markdown_v2", "markdown_v2": { "content": markdown_content } } send_wechat_message(message)