update to a right mess for fetching data and doing some sanity checks

This commit is contained in:
2026-01-12 14:10:56 -07:00
parent 331d2757bf
commit c6aa52772d

View File

@@ -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'