mirror of
https://github.com/ChuckBuilds/LEDMatrix.git
synced 2026-04-12 21:43:00 +00:00
Switch to One Call API for accurate daily temperature extremes - Use daily.temp.min/max for current weather display - Update forecast processing for new API format
This commit is contained in:
@@ -99,20 +99,30 @@ class WeatherManager:
|
|||||||
lat = geo_data[0]['lat']
|
lat = geo_data[0]['lat']
|
||||||
lon = geo_data[0]['lon']
|
lon = geo_data[0]['lon']
|
||||||
|
|
||||||
# Get current weather and forecast using coordinates
|
# Get current weather and daily forecast using One Call API
|
||||||
weather_url = f"https://api.openweathermap.org/data/2.5/weather?lat={lat}&lon={lon}&appid={api_key}&units={units}"
|
one_call_url = f"https://api.openweathermap.org/data/3.0/onecall?lat={lat}&lon={lon}&exclude=minutely,hourly,alerts&appid={api_key}&units={units}"
|
||||||
forecast_url = f"https://api.openweathermap.org/data/2.5/forecast?lat={lat}&lon={lon}&appid={api_key}&units={units}"
|
|
||||||
|
|
||||||
# Fetch current weather
|
# Fetch current weather and daily forecast
|
||||||
response = requests.get(weather_url)
|
response = requests.get(one_call_url)
|
||||||
response.raise_for_status()
|
response.raise_for_status()
|
||||||
self.weather_data = response.json()
|
one_call_data = response.json()
|
||||||
|
|
||||||
# Fetch forecast
|
# Store current weather data
|
||||||
response = requests.get(forecast_url)
|
self.weather_data = {
|
||||||
response.raise_for_status()
|
'main': {
|
||||||
self.forecast_data = response.json()
|
'temp': one_call_data['current']['temp'],
|
||||||
|
'temp_max': one_call_data['daily'][0]['temp']['max'],
|
||||||
|
'temp_min': one_call_data['daily'][0]['temp']['min'],
|
||||||
|
'humidity': one_call_data['current']['humidity'],
|
||||||
|
'pressure': one_call_data['current']['pressure']
|
||||||
|
},
|
||||||
|
'weather': one_call_data['current']['weather'],
|
||||||
|
'wind': one_call_data['current'].get('wind', {})
|
||||||
|
}
|
||||||
|
|
||||||
|
# Store forecast data (for hourly and daily forecasts)
|
||||||
|
self.forecast_data = one_call_data
|
||||||
|
|
||||||
# Process forecast data
|
# Process forecast data
|
||||||
self._process_forecast_data(self.forecast_data)
|
self._process_forecast_data(self.forecast_data)
|
||||||
|
|
||||||
@@ -144,12 +154,12 @@ class WeatherManager:
|
|||||||
return
|
return
|
||||||
|
|
||||||
# Process hourly forecast (next 5 hours)
|
# Process hourly forecast (next 5 hours)
|
||||||
hourly_list = forecast_data.get('list', [])[:5] # Changed from 6 to 5 to match image
|
hourly_list = forecast_data.get('hourly', [])[:5] # Get next 5 hours
|
||||||
self.hourly_forecast = []
|
self.hourly_forecast = []
|
||||||
|
|
||||||
for hour_data in hourly_list:
|
for hour_data in hourly_list:
|
||||||
dt = datetime.fromtimestamp(hour_data['dt'])
|
dt = datetime.fromtimestamp(hour_data['dt'])
|
||||||
temp = round(hour_data['main']['temp'])
|
temp = round(hour_data['temp'])
|
||||||
condition = hour_data['weather'][0]['main']
|
condition = hour_data['weather'][0]['main']
|
||||||
self.hourly_forecast.append({
|
self.hourly_forecast.append({
|
||||||
'hour': dt.strftime('%I:00 %p').lstrip('0'), # Format as "2:00 PM"
|
'hour': dt.strftime('%I:00 %p').lstrip('0'), # Format as "2:00 PM"
|
||||||
@@ -158,38 +168,18 @@ class WeatherManager:
|
|||||||
})
|
})
|
||||||
|
|
||||||
# Process daily forecast
|
# Process daily forecast
|
||||||
daily_data = {}
|
daily_list = forecast_data.get('daily', [])[1:4] # Skip today (index 0) and get next 3 days
|
||||||
full_forecast_list = forecast_data.get('list', []) # Use the full list
|
|
||||||
for item in full_forecast_list: # Iterate over the full list
|
|
||||||
date = datetime.fromtimestamp(item['dt']).strftime('%Y-%m-%d')
|
|
||||||
if date not in daily_data:
|
|
||||||
daily_data[date] = {
|
|
||||||
'temps': [],
|
|
||||||
'conditions': [],
|
|
||||||
'date': datetime.fromtimestamp(item['dt'])
|
|
||||||
}
|
|
||||||
daily_data[date]['temps'].append(item['main']['temp'])
|
|
||||||
daily_data[date]['conditions'].append(item['weather'][0]['main'])
|
|
||||||
|
|
||||||
# Calculate daily summaries, excluding today
|
|
||||||
self.daily_forecast = []
|
self.daily_forecast = []
|
||||||
today_str = datetime.now().strftime('%Y-%m-%d')
|
|
||||||
|
|
||||||
# Sort data by date to ensure chronological order
|
for day_data in daily_list:
|
||||||
sorted_daily_items = sorted(daily_data.items(), key=lambda item: item[1]['date'])
|
dt = datetime.fromtimestamp(day_data['dt'])
|
||||||
|
temp_high = round(day_data['temp']['max'])
|
||||||
# Filter out today's data and take the next 3 days
|
temp_low = round(day_data['temp']['min'])
|
||||||
future_days_data = [item for item in sorted_daily_items if item[0] != today_str][:3]
|
condition = day_data['weather'][0]['main']
|
||||||
|
|
||||||
for date_str, data in future_days_data:
|
|
||||||
temps = data['temps']
|
|
||||||
temp_high = round(max(temps))
|
|
||||||
temp_low = round(min(temps))
|
|
||||||
condition = max(set(data['conditions']), key=data['conditions'].count)
|
|
||||||
|
|
||||||
self.daily_forecast.append({
|
self.daily_forecast.append({
|
||||||
'date': data['date'].strftime('%a'), # Day name (Mon, Tue, etc.)
|
'date': dt.strftime('%a'), # Day name (Mon, Tue, etc.)
|
||||||
'date_str': data['date'].strftime('%m/%d'), # Date (4/8, 4/9, etc.)
|
'date_str': dt.strftime('%m/%d'), # Date (4/8, 4/9, etc.)
|
||||||
'temp_high': temp_high,
|
'temp_high': temp_high,
|
||||||
'temp_low': temp_low,
|
'temp_low': temp_low,
|
||||||
'condition': condition
|
'condition': condition
|
||||||
|
|||||||
Reference in New Issue
Block a user