import requests from config import CITY_NAME_MAPPING, WEATHER_API_KEY def get_weather_data(city, api_key=WEATHER_API_KEY): """获取天气数据""" base_url = "http://api.openweathermap.org/data/2.5/weather" params = { "q": city, "appid": api_key, "units": "metric", "lang": "zh_cn" } try: response = requests.get(base_url, params=params, timeout=10) data = response.json() if response.status_code == 200: normalized_input = city.lower().replace(" ", "") chinese_name = CITY_NAME_MAPPING.get(normalized_input, data.get("name", city)) return { "city": chinese_name, "temp": data["main"]["temp"], "humidity": data["main"]["humidity"], "description": data["weather"][0]["description"], "icon": data["weather"][0]["icon"] } else: return None except Exception as e: print(f"获取天气数据时出错: {str(e)}") return None