Fix forecast min/max to use true daily range across all 3-hour slots
Previously took min/max from only the first 3-hour entry of each day, giving a near-flat range. Now aggregates all slots for each date and uses the midday slot for icon/description. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
34
app.py
34
app.py
@@ -54,20 +54,30 @@ def get_weather():
|
||||
forecast_response.raise_for_status()
|
||||
forecast_data = forecast_response.json()
|
||||
|
||||
# Process forecast to get daily summaries
|
||||
daily_forecast = []
|
||||
seen_dates = set()
|
||||
# Group all 3-hour slots by date so we can find the true daily min/max
|
||||
from collections import defaultdict
|
||||
slots_by_date = defaultdict(list)
|
||||
for item in forecast_data['list']:
|
||||
d = datetime.fromtimestamp(item['dt']).date()
|
||||
slots_by_date[d].append(item)
|
||||
|
||||
for item in forecast_data['list'][:40]: # Next 3 days (8 forecasts per day)
|
||||
date = datetime.fromtimestamp(item['dt']).date()
|
||||
if date not in seen_dates and len(daily_forecast) < 3:
|
||||
seen_dates.add(date)
|
||||
today_date = datetime.now().date()
|
||||
future_dates = sorted(d for d in slots_by_date if d > today_date)[:3]
|
||||
|
||||
daily_forecast = []
|
||||
for d in future_dates:
|
||||
slots = slots_by_date[d]
|
||||
# True daily min/max across all 3-hour periods
|
||||
temp_max = round(max(s['main']['temp_max'] for s in slots))
|
||||
temp_min = round(min(s['main']['temp_min'] for s in slots))
|
||||
# Use the midday slot for icon/description
|
||||
midday = min(slots, key=lambda s: abs(datetime.fromtimestamp(s['dt']).hour - 12))
|
||||
daily_forecast.append({
|
||||
'date': date.strftime('%a'),
|
||||
'temp_max': round(item['main']['temp_max']),
|
||||
'temp_min': round(item['main']['temp_min']),
|
||||
'description': item['weather'][0]['description'],
|
||||
'icon': item['weather'][0]['icon']
|
||||
'date': d.strftime('%a'),
|
||||
'temp_max': temp_max,
|
||||
'temp_min': temp_min,
|
||||
'description': midday['weather'][0]['description'],
|
||||
'icon': midday['weather'][0]['icon']
|
||||
})
|
||||
|
||||
weather_data = {
|
||||
|
||||
Reference in New Issue
Block a user