Abhlash commited on
Commit
d1a5601
1 Parent(s): a8578d1

weather update

Browse files
Files changed (1) hide show
  1. utils/weather.py +44 -8
utils/weather.py CHANGED
@@ -1,12 +1,48 @@
 
1
  import requests
2
- from config import WEATHER_API_KEY
 
 
 
 
 
 
 
3
 
4
  def get_weather():
5
- url = f"http://api.weatherapi.com/v1/current.json?key={WEATHER_API_KEY}&q=Black Rock Desert"
6
- response = requests.get(url)
7
- if response.status_code == 200:
 
 
 
 
 
 
 
8
  data = response.json()
9
- weather_info = f"Current temperature: {data['current']['temp_c']}°C\nCondition: {data['current']['condition']['text']}\nWind: {data['current']['wind_kph']} kph"
10
- return weather_info
11
- else:
12
- return "Unable to fetch weather information at the moment."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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