weather update
Browse files- utils/weather.py +44 -8
utils/weather.py
CHANGED
@@ -1,12 +1,48 @@
|
|
|
|
1 |
import requests
|
2 |
-
from
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
|
4 |
def get_weather():
|
5 |
-
|
6 |
-
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
data = response.json()
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
import requests
|
3 |
+
from datetime import datetime
|
4 |
+
from dotenv import load_dotenv
|
5 |
+
|
6 |
+
# Load environment variables
|
7 |
+
load_dotenv()
|
8 |
+
|
9 |
+
# Get API key from environment variable
|
10 |
+
OPENWEATHER_API_KEY = os.getenv('OPENWEATHER_API_KEY')
|
11 |
|
12 |
def get_weather():
|
13 |
+
# Coordinates for Black Rock Desert, Nevada
|
14 |
+
lat = 40.7864
|
15 |
+
lon = -119.2065
|
16 |
+
|
17 |
+
# API endpoint
|
18 |
+
url = f"https://api.openweathermap.org/data/2.5/onecall?lat={lat}&lon={lon}&exclude=current,minutely,hourly&units=imperial&appid={OPENWEATHER_API_KEY}"
|
19 |
+
|
20 |
+
try:
|
21 |
+
response = requests.get(url)
|
22 |
+
response.raise_for_status() # Raise an exception for bad responses
|
23 |
data = response.json()
|
24 |
+
|
25 |
+
forecast = []
|
26 |
+
for day in data['daily'][:7]: # Get 7-day forecast
|
27 |
+
forecast.append({
|
28 |
+
"date": datetime.fromtimestamp(day['dt']).strftime("%Y-%m-%d"),
|
29 |
+
"high": round(day['temp']['max']),
|
30 |
+
"low": round(day['temp']['min']),
|
31 |
+
"conditions": day['weather'][0]['main'],
|
32 |
+
"wind_speed": round(day['wind_speed'])
|
33 |
+
})
|
34 |
+
|
35 |
+
return forecast
|
36 |
+
except requests.RequestException as e:
|
37 |
+
print(f"Error fetching weather data: {e}")
|
38 |
+
return None
|
39 |
+
|
40 |
+
def format_weather_report(forecast):
|
41 |
+
if not forecast:
|
42 |
+
return "I'm sorry, I couldn't fetch the weather information at the moment. Please try again later."
|
43 |
+
|
44 |
+
report = "Here's the weather forecast for the next 7 days in Black Rock City:\n\n"
|
45 |
+
for day in forecast:
|
46 |
+
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"
|
47 |
+
report += "\nRemember, weather in the desert can be unpredictable. Always be prepared for extreme conditions!"
|
48 |
+
return report
|