Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -30,25 +30,16 @@ data_source = st.sidebar.selectbox(
|
|
| 30 |
# --- API CALLING FUNCTIONS ---
|
| 31 |
def fetch_openweather_data(lat, lon, datetime_input):
|
| 32 |
"""Fetches historical weather data from OpenWeather's One Call API."""
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
if datetime_input.tzinfo is None:
|
| 36 |
-
# Localize datetime input to UTC
|
| 37 |
-
datetime_input = pytz.utc.localize(datetime_input)
|
| 38 |
-
|
| 39 |
-
# Convert to Unix timestamp
|
| 40 |
-
timestamp = int(datetime_input.timestamp())
|
| 41 |
-
|
| 42 |
-
# Construct the URL for the API call
|
| 43 |
-
url = f"https://api.openweathermap.org/data/3.0/onecall/timemachine?lat={lat}&lon={lon}&dt={timestamp}&appid={OPENWEATHER_API_KEY}&units=metric"
|
| 44 |
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
|
| 53 |
def fetch_openweather_forecast(lat, lon):
|
| 54 |
"""Fetches 7-day weather forecast data from OpenWeather's One Call API."""
|
|
|
|
| 30 |
# --- API CALLING FUNCTIONS ---
|
| 31 |
def fetch_openweather_data(lat, lon, datetime_input):
|
| 32 |
"""Fetches historical weather data from OpenWeather's One Call API."""
|
| 33 |
+
# Construct the URL for the API call
|
| 34 |
+
url = f"https://api.openweathermap.org/data/3.0/onecall/timemachine?lat={lat}&lon={lon}&dt={int(datetime_input.timestamp())}&appid={OPENWEATHER_API_KEY}&units=metric"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
+
try:
|
| 37 |
+
response = requests.get(url)
|
| 38 |
+
response.raise_for_status() # Raise HTTPError for bad responses
|
| 39 |
+
return response.json()
|
| 40 |
+
except requests.exceptions.RequestException as e:
|
| 41 |
+
st.error(f"Error fetching OpenWeather data: {e}")
|
| 42 |
+
return None
|
| 43 |
|
| 44 |
def fetch_openweather_forecast(lat, lon):
|
| 45 |
"""Fetches 7-day weather forecast data from OpenWeather's One Call API."""
|