From c6aa52772d67ddd7fc40781a047127e922e50cc4 Mon Sep 17 00:00:00 2001 From: Brian Rogers Date: Mon, 12 Jan 2026 14:10:56 -0700 Subject: [PATCH] update to a right mess for fetching data and doing some sanity checks --- weather/weather.py | 52 +++++++++++++++++++++++++++++++--------------- 1 file changed, 35 insertions(+), 17 deletions(-) diff --git a/weather/weather.py b/weather/weather.py index b1ea70a..98b7ff4 100644 --- a/weather/weather.py +++ b/weather/weather.py @@ -5,41 +5,58 @@ from werkzeug.exceptions import abort from datetime import datetime from weather.db import get_db +from weather.ingest import ( + fetchHourlyForecasts, fetchDailyForecasts +) bp = Blueprint('weather', __name__) @bp.route('/') def index(): db = get_db() - current_conditions = dict(db.execute( - f"SELECT * FROM `reports` WHERE `type` = 'hourly' ORDER BY `end_time` DESC LIMIT 1" - ).fetchone()) + current_conditions = db.execute( + "SELECT * FROM `current_forecasts` ORDER BY `end_time` DESC LIMIT 1" + ).fetchone() + + if current_conditions is None: + fetchHourlyForecasts() + current_conditions = db.execute( + "SELECT * FROM `current_forecasts` ORDER BY `end_time` DESC LIMIT 1" + ).fetchone() - stale_datetime = datetime.strptime(current_conditions['end_time'], "%Y-%m-%d %H:%M:%S") - if datetime.now() > stale_datetime: - # fetch new data - # save new data - # TODO: add conditions to check for day/night day_or_night = 'day' if current_conditions['is_daytime']: day_or_night = 'night' condition = mapForecastToImage(current_conditions['short_forecast']) - condition_image = f"images/{time_of_day}_{condition}.jpg" + condition_image = f"images/{day_or_night}_{condition}.jpg" - hourly_reports = db.execute( - 'SELECT *' - ' FROM `reports`' - ' ORDER BY `id` DESC' - ' LIMIT 7' + hourly_conditions = db.execute( + 'SELECT * FROM `current_forecasts` WHERE `start_time` > DATETIME("now", "+1 hour") LIMIT 6' ).fetchall() + if len(hourly_conditions) < 6: + fetchHourlyForecasts() + hourly_conditions = db.execute( + 'SELECT * FROM `current_forecasts` WHERE `start_time` > DATETIME("now", "+1 hour") LIMIT 6' + ).fetchall() + + + week_forcasts = db.execute( + 'SELECT * FROM `daily_forecasts` WHERE `forecasted_date` > DATETIME("now") LIMIT 7' + ).fetchall() + if len(week_forcasts) < 7: + fetchDailyForecasts() + week_forcasts = db.execute( + 'SELECT * FROM `current_forecasts` WHERE `start_time` > DATETIME("now", "+1 hour") LIMIT 7' + ).fetchall() return render_template( 'weather/index.html', - current_conditions=current_conditions, condition_image=condition_image, - periods=periods + current_conditions=current_conditions, + hourly_conditions=hourly_conditions, + week_forcasts=week_forcasts ) def mapForecastToImage(condition: str): @@ -56,7 +73,7 @@ def mapForecastToImage(condition: str): elif 'wind' in condition: return 'windy' elif 'sunny' in condition or 'clear' in condition: - return 'sunny' + return 'clear' elif 'cloud' in condition or 'overcast' in condition: return 'cloudy' elif 'fog' in condition or 'mist' in condition: @@ -64,3 +81,4 @@ def mapForecastToImage(condition: str): else: return 'cloudy' +