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:
38
app.py
38
app.py
@@ -54,21 +54,31 @@ def get_weather():
|
|||||||
forecast_response.raise_for_status()
|
forecast_response.raise_for_status()
|
||||||
forecast_data = forecast_response.json()
|
forecast_data = forecast_response.json()
|
||||||
|
|
||||||
# Process forecast to get daily summaries
|
# Group all 3-hour slots by date so we can find the true daily min/max
|
||||||
daily_forecast = []
|
from collections import defaultdict
|
||||||
seen_dates = set()
|
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)
|
today_date = datetime.now().date()
|
||||||
date = datetime.fromtimestamp(item['dt']).date()
|
future_dates = sorted(d for d in slots_by_date if d > today_date)[:3]
|
||||||
if date not in seen_dates and len(daily_forecast) < 3:
|
|
||||||
seen_dates.add(date)
|
daily_forecast = []
|
||||||
daily_forecast.append({
|
for d in future_dates:
|
||||||
'date': date.strftime('%a'),
|
slots = slots_by_date[d]
|
||||||
'temp_max': round(item['main']['temp_max']),
|
# True daily min/max across all 3-hour periods
|
||||||
'temp_min': round(item['main']['temp_min']),
|
temp_max = round(max(s['main']['temp_max'] for s in slots))
|
||||||
'description': item['weather'][0]['description'],
|
temp_min = round(min(s['main']['temp_min'] for s in slots))
|
||||||
'icon': item['weather'][0]['icon']
|
# Use the midday slot for icon/description
|
||||||
})
|
midday = min(slots, key=lambda s: abs(datetime.fromtimestamp(s['dt']).hour - 12))
|
||||||
|
daily_forecast.append({
|
||||||
|
'date': d.strftime('%a'),
|
||||||
|
'temp_max': temp_max,
|
||||||
|
'temp_min': temp_min,
|
||||||
|
'description': midday['weather'][0]['description'],
|
||||||
|
'icon': midday['weather'][0]['icon']
|
||||||
|
})
|
||||||
|
|
||||||
weather_data = {
|
weather_data = {
|
||||||
'current': {
|
'current': {
|
||||||
|
|||||||
Reference in New Issue
Block a user