Sellid commited on
Commit
08a43c2
·
verified ·
1 Parent(s): e214477

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -14
app.py CHANGED
@@ -43,28 +43,43 @@ def get_current_time_in_timezone(timezone: str) -> str:
43
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
44
 
45
  @tool
46
- def get_open_meteo_weather(latitude: float, longitude: float) -> str:
47
- """Fetches the current weather using the Open-Meteo API.
 
48
 
49
  Args:
50
- latitude: The latitude of the location.
51
- longitude: The longitude of the location.
52
-
53
  Returns:
54
- A string summarizing the current weather conditions.
55
  """
56
  try:
57
- # Build the API URL with the desired parameters:
58
- # - current_weather=true returns an object with temperature, windspeed, etc.
59
- url = (
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60
  f"https://api.open-meteo.com/v1/forecast"
61
  f"?latitude={latitude}&longitude={longitude}&current_weather=true"
62
  )
63
- response = requests.get(url)
64
- response.raise_for_status() # raise an exception for HTTP errors
65
- data = response.json()
66
 
67
- current = data.get("current_weather")
68
  if not current:
69
  return "Current weather data not available."
70
 
@@ -75,7 +90,7 @@ def get_open_meteo_weather(latitude: float, longitude: float) -> str:
75
  weather_time = current.get("time")
76
 
77
  return (
78
- f"At {weather_time}, the temperature is {temperature}°C, "
79
  f"wind speed is {windspeed} km/h, wind direction is {winddirection}°, "
80
  f"weather code: {weathercode}."
81
  )
 
43
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
44
 
45
  @tool
46
+ def get_open_meteo_weather(city: str) -> str:
47
+ """
48
+ Fetches the current weather for a given city using Open-Meteo's APIs.
49
 
50
  Args:
51
+ city: The name of the city (e.g., "Berlin", "New York").
52
+
 
53
  Returns:
54
+ A string summarizing the current weather conditions, or an error message.
55
  """
56
  try:
57
+ # First, use the geocoding API to convert the city name to coordinates.
58
+ geo_url = f"https://geocoding-api.open-meteo.com/v1/search?name={city}"
59
+ geo_response = requests.get(geo_url)
60
+ geo_response.raise_for_status()
61
+ geo_data = geo_response.json()
62
+
63
+ results = geo_data.get("results")
64
+ if not results:
65
+ return f"No coordinates found for city: {city}"
66
+
67
+ # Use the first matching result.
68
+ first_result = results[0]
69
+ latitude = first_result.get("latitude")
70
+ longitude = first_result.get("longitude")
71
+ resolved_name = first_result.get("name", city)
72
+
73
+ # Now, query the Open-Meteo weather API with the obtained coordinates.
74
+ weather_url = (
75
  f"https://api.open-meteo.com/v1/forecast"
76
  f"?latitude={latitude}&longitude={longitude}&current_weather=true"
77
  )
78
+ weather_response = requests.get(weather_url)
79
+ weather_response.raise_for_status()
80
+ weather_data = weather_response.json()
81
 
82
+ current = weather_data.get("current_weather")
83
  if not current:
84
  return "Current weather data not available."
85
 
 
90
  weather_time = current.get("time")
91
 
92
  return (
93
+ f"In {resolved_name} at {weather_time}, the temperature is {temperature}°C, "
94
  f"wind speed is {windspeed} km/h, wind direction is {winddirection}°, "
95
  f"weather code: {weathercode}."
96
  )