Spaces:
Sleeping
Sleeping
kevindiependaele commited on
Commit ·
f9aedb3
1
Parent(s): f32d836
fix lat, lon parsing
Browse files- tools/weather_api.py +16 -4
tools/weather_api.py
CHANGED
|
@@ -18,18 +18,30 @@ class WeatherApiTool(Tool):
|
|
| 18 |
|
| 19 |
# Get the latitude and longitude of the location
|
| 20 |
geocoding_url = f"http://api.openweathermap.org/geo/1.0/direct?q={location}&appid={self.api_key}"
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
| 22 |
geocoding_data = response.json()
|
| 23 |
|
| 24 |
# TODO: match if multiple using IP country info
|
| 25 |
-
lat = geocoding_data[0]["
|
| 26 |
-
lon = geocoding_data[0]["
|
|
|
|
|
|
|
| 27 |
|
| 28 |
# Get the weather data for the location
|
| 29 |
weather_url = f"http://api.openweathermap.org/data/3.0/onecall?lat={lat}&lon={lon}&exclude=current,minutely,hourly,alerts&appid={self.api_key}"
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
| 31 |
weather_data = response.json()
|
| 32 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
# # Extract the weather data for the specified date
|
| 34 |
# for day in weather_data["daily"]:
|
| 35 |
# if date in day["dt"]:
|
|
|
|
| 18 |
|
| 19 |
# Get the latitude and longitude of the location
|
| 20 |
geocoding_url = f"http://api.openweathermap.org/geo/1.0/direct?q={location}&appid={self.api_key}"
|
| 21 |
+
try:
|
| 22 |
+
response = requests.get(geocoding_url)
|
| 23 |
+
except requests.exceptions.RequestException as e:
|
| 24 |
+
return f"Error fetching geocoding data: {str(e)}"
|
| 25 |
geocoding_data = response.json()
|
| 26 |
|
| 27 |
# TODO: match if multiple using IP country info
|
| 28 |
+
lat = geocoding_data[0]["lat"]
|
| 29 |
+
lon = geocoding_data[0]["lon"]
|
| 30 |
+
country = geocoding_data[0]["country"]
|
| 31 |
+
state = geocoding_data[0]["state"]
|
| 32 |
|
| 33 |
# Get the weather data for the location
|
| 34 |
weather_url = f"http://api.openweathermap.org/data/3.0/onecall?lat={lat}&lon={lon}&exclude=current,minutely,hourly,alerts&appid={self.api_key}"
|
| 35 |
+
try:
|
| 36 |
+
response = requests.get(weather_url)
|
| 37 |
+
except requests.exceptions.RequestException as e:
|
| 38 |
+
return f"Error fetching weather data: {str(e)}"
|
| 39 |
weather_data = response.json()
|
| 40 |
|
| 41 |
+
weather_data["location"] = location
|
| 42 |
+
weather_data["country"] = country
|
| 43 |
+
weather_data["state"] = state
|
| 44 |
+
|
| 45 |
# # Extract the weather data for the specified date
|
| 46 |
# for day in weather_data["daily"]:
|
| 47 |
# if date in day["dt"]:
|