Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -39,13 +39,28 @@ def get_weather(city: str) -> str:
|
|
| 39 |
Args:
|
| 40 |
city: A string representing a city
|
| 41 |
"""
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
else:
|
| 48 |
-
|
| 49 |
|
| 50 |
final_answer = FinalAnswerTool()
|
| 51 |
|
|
|
|
| 39 |
Args:
|
| 40 |
city: A string representing a city
|
| 41 |
"""
|
| 42 |
+
# Endpoint lat e long extraction
|
| 43 |
+
geocoding_url = f"https://geocoding-api.open-meteo.com/v1/search?name={city}&count=1"
|
| 44 |
+
geocoding_response = requests.get(geocoding_url)
|
| 45 |
+
|
| 46 |
+
if geocoding_response.status_code == 200:
|
| 47 |
+
data = geocoding_response.json()
|
| 48 |
+
if data['results']:
|
| 49 |
+
# Estrai le coordinate della città
|
| 50 |
+
latitude = data['results'][0]['latitude']
|
| 51 |
+
longitude = data['results'][0]['longitude']
|
| 52 |
+
|
| 53 |
+
url = f"https://api.open-meteo.com/v1/forecast?latitude={latitude}&longitude={longitude}¤t_weather=true&temperature_unit=celsius" # In Celsius
|
| 54 |
+
response = requests.get(url)
|
| 55 |
+
if response.status_code == 200:
|
| 56 |
+
temperature = data['main']['temp']
|
| 57 |
+
return f"{temperature} °C"
|
| 58 |
+
else:
|
| 59 |
+
return "Error on fetching weather data."
|
| 60 |
+
else:
|
| 61 |
+
print(f"City {city} not found.")
|
| 62 |
else:
|
| 63 |
+
print("Error on fetching geo coordinates.")
|
| 64 |
|
| 65 |
final_answer = FinalAnswerTool()
|
| 66 |
|