BurnerBot / utils /weather.py
Abhlash's picture
weather update
d1a5601 verified
raw
history blame
1.79 kB
import os
import requests
from datetime import datetime
from dotenv import load_dotenv
# Load environment variables
load_dotenv()
# Get API key from environment variable
OPENWEATHER_API_KEY = os.getenv('OPENWEATHER_API_KEY')
def get_weather():
# Coordinates for Black Rock Desert, Nevada
lat = 40.7864
lon = -119.2065
# API endpoint
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() # Raise an exception for bad responses
data = response.json()
forecast = []
for day in data['daily'][:7]: # Get 7-day forecast
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