|
import os |
|
import requests |
|
from datetime import datetime |
|
|
|
|
|
OPENWEATHER_API_KEY = os.getenv('OPENWEATHER_API_KEY') |
|
|
|
def get_weather(): |
|
|
|
lat = 40.7864 |
|
lon = -119.2065 |
|
|
|
|
|
url = f"https://api.openweathermap.org/data/2.5/onecall?lat={lat}&lon={lon}&exclude=current,minutely,hourly&units=imperial&appid={OPENWEATHER_API_KEY}" |
|
|
|
try: |
|
response = requests.get(url) |
|
response.raise_for_status() |
|
data = response.json() |
|
|
|
forecast = [] |
|
for day in data['daily'][:7]: |
|
forecast.append({ |
|
"date": datetime.fromtimestamp(day['dt']).strftime("%Y-%m-%d"), |
|
"high": round(day['temp']['max']), |
|
"low": round(day['temp']['min']), |
|
"conditions": day['weather'][0]['main'], |
|
"wind_speed": round(day['wind_speed']) |
|
}) |
|
|
|
return forecast |
|
except requests.RequestException as e: |
|
print(f"Error fetching weather data: {e}") |
|
return None |
|
|
|
def format_weather_report(forecast): |
|
if not forecast: |
|
return "I'm sorry, I couldn't fetch the weather information at the moment. Please try again later." |
|
|
|
report = "Here's the weather forecast for the next 7 days in Black Rock City:\n\n" |
|
for day in forecast: |
|
report += f"{day['date']}: High of {day['high']}°F, Low of {day['low']}°F. {day['conditions']} with wind speeds up to {day['wind_speed']} mph.\n" |
|
report += "\nRemember, weather in the desert can be unpredictable. Always be prepared for extreme conditions!" |
|
return report |