diff --git a/app.py b/app.py index 0d3bc15..995807e 100644 --- a/app.py +++ b/app.py @@ -54,21 +54,31 @@ 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) - 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'] - }) + 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': d.strftime('%a'), + 'temp_max': temp_max, + 'temp_min': temp_min, + 'description': midday['weather'][0]['description'], + 'icon': midday['weather'][0]['icon'] + }) weather_data = { 'current': {