update to a right mess for fetching data and doing some sanity checks
This commit is contained in:
@@ -5,41 +5,58 @@ from werkzeug.exceptions import abort
|
|||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
||||||
from weather.db import get_db
|
from weather.db import get_db
|
||||||
|
from weather.ingest import (
|
||||||
|
fetchHourlyForecasts, fetchDailyForecasts
|
||||||
|
)
|
||||||
|
|
||||||
bp = Blueprint('weather', __name__)
|
bp = Blueprint('weather', __name__)
|
||||||
|
|
||||||
@bp.route('/')
|
@bp.route('/')
|
||||||
def index():
|
def index():
|
||||||
db = get_db()
|
db = get_db()
|
||||||
current_conditions = dict(db.execute(
|
current_conditions = db.execute(
|
||||||
f"SELECT * FROM `reports` WHERE `type` = 'hourly' ORDER BY `end_time` DESC LIMIT 1"
|
"SELECT * FROM `current_forecasts` ORDER BY `end_time` DESC LIMIT 1"
|
||||||
).fetchone())
|
).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'
|
day_or_night = 'day'
|
||||||
if current_conditions['is_daytime']:
|
if current_conditions['is_daytime']:
|
||||||
day_or_night = 'night'
|
day_or_night = 'night'
|
||||||
|
|
||||||
condition = mapForecastToImage(current_conditions['short_forecast'])
|
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(
|
hourly_conditions = db.execute(
|
||||||
'SELECT *'
|
'SELECT * FROM `current_forecasts` WHERE `start_time` > DATETIME("now", "+1 hour") LIMIT 6'
|
||||||
' FROM `reports`'
|
).fetchall()
|
||||||
' ORDER BY `id` DESC'
|
if len(hourly_conditions) < 6:
|
||||||
' LIMIT 7'
|
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()
|
).fetchall()
|
||||||
|
|
||||||
return render_template(
|
return render_template(
|
||||||
'weather/index.html',
|
'weather/index.html',
|
||||||
current_conditions=current_conditions,
|
|
||||||
condition_image=condition_image,
|
condition_image=condition_image,
|
||||||
periods=periods
|
current_conditions=current_conditions,
|
||||||
|
hourly_conditions=hourly_conditions,
|
||||||
|
week_forcasts=week_forcasts
|
||||||
)
|
)
|
||||||
|
|
||||||
def mapForecastToImage(condition: str):
|
def mapForecastToImage(condition: str):
|
||||||
@@ -56,7 +73,7 @@ def mapForecastToImage(condition: str):
|
|||||||
elif 'wind' in condition:
|
elif 'wind' in condition:
|
||||||
return 'windy'
|
return 'windy'
|
||||||
elif 'sunny' in condition or 'clear' in condition:
|
elif 'sunny' in condition or 'clear' in condition:
|
||||||
return 'sunny'
|
return 'clear'
|
||||||
elif 'cloud' in condition or 'overcast' in condition:
|
elif 'cloud' in condition or 'overcast' in condition:
|
||||||
return 'cloudy'
|
return 'cloudy'
|
||||||
elif 'fog' in condition or 'mist' in condition:
|
elif 'fog' in condition or 'mist' in condition:
|
||||||
@@ -64,3 +81,4 @@ def mapForecastToImage(condition: str):
|
|||||||
else:
|
else:
|
||||||
return 'cloudy'
|
return 'cloudy'
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user