ninooo96 commited on
Commit
ff679d4
·
verified ·
1 Parent(s): d20c4d2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -6
app.py CHANGED
@@ -39,13 +39,28 @@ def get_weather(city: str) -> str:
39
  Args:
40
  city: A string representing a city
41
  """
42
- url = f"https://api.openweathermap.org/data/2.5/weather?q={city}&units=metric" # In Celsius
43
- response = requests.get(url)
44
- if response.status_code == 200:
45
- temperature = data['main']['temp']
46
- return f"{temperature} °C"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47
  else:
48
- return "Error on fetching weather data."
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}&current_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