Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -64,7 +64,7 @@ def get_adcode_by_city(city_name: str) -> Optional[str]:
|
|
| 64 |
|
| 65 |
return None
|
| 66 |
|
| 67 |
-
|
| 68 |
"""向高德地图API发送请求并获取天气数据
|
| 69 |
|
| 70 |
Args:
|
|
@@ -79,10 +79,10 @@ async def make_amap_request(params: Dict[str, str]) -> Dict[str, Any]:
|
|
| 79 |
headers = {
|
| 80 |
"User-Agent": USER_AGENT
|
| 81 |
}
|
| 82 |
-
|
| 83 |
-
|
| 84 |
try:
|
| 85 |
-
response =
|
| 86 |
response.raise_for_status()
|
| 87 |
return response.json()
|
| 88 |
except Exception as e:
|
|
@@ -129,7 +129,7 @@ def get_current_weather(city: str) -> str:
|
|
| 129 |
"extensions": "base" # 获取实时天气
|
| 130 |
}
|
| 131 |
|
| 132 |
-
data =
|
| 133 |
|
| 134 |
if not data:
|
| 135 |
return f"获取{city}的天气信息失败"
|
|
|
|
| 64 |
|
| 65 |
return None
|
| 66 |
|
| 67 |
+
def make_amap_request(params: Dict[str, str]) -> Dict[str, Any]:
|
| 68 |
"""向高德地图API发送请求并获取天气数据
|
| 69 |
|
| 70 |
Args:
|
|
|
|
| 79 |
headers = {
|
| 80 |
"User-Agent": USER_AGENT
|
| 81 |
}
|
| 82 |
+
|
| 83 |
+
with httpx.Client() as client:
|
| 84 |
try:
|
| 85 |
+
response = client.get(AMAP_API_BASE, params=params, headers=headers, timeout=30.0)
|
| 86 |
response.raise_for_status()
|
| 87 |
return response.json()
|
| 88 |
except Exception as e:
|
|
|
|
| 129 |
"extensions": "base" # 获取实时天气
|
| 130 |
}
|
| 131 |
|
| 132 |
+
data = make_amap_request(params)
|
| 133 |
|
| 134 |
if not data:
|
| 135 |
return f"获取{city}的天气信息失败"
|