diff --git a/weather/static/images/sunny.jpg b/weather/static/images/day_clear.jpg
similarity index 100%
rename from weather/static/images/sunny.jpg
rename to weather/static/images/day_clear.jpg
diff --git a/weather/static/images/sunny_license.html b/weather/static/images/day_clear_license.html
similarity index 100%
rename from weather/static/images/sunny_license.html
rename to weather/static/images/day_clear_license.html
diff --git a/weather/static/images/day_cloudy.jpg b/weather/static/images/day_cloudy.jpg
new file mode 100644
index 0000000..9306ba4
Binary files /dev/null and b/weather/static/images/day_cloudy.jpg differ
diff --git a/weather/static/images/day_cloudy_license.html b/weather/static/images/day_cloudy_license.html
new file mode 100644
index 0000000..d48fd28
--- /dev/null
+++ b/weather/static/images/day_cloudy_license.html
@@ -0,0 +1 @@
+Image by Karsten Paulick from Pixabay
\ No newline at end of file
diff --git a/weather/static/images/day_rainy.jpg b/weather/static/images/day_rainy.jpg
new file mode 100644
index 0000000..b9a249b
Binary files /dev/null and b/weather/static/images/day_rainy.jpg differ
diff --git a/weather/static/images/day_rainy_license.html b/weather/static/images/day_rainy_license.html
new file mode 100644
index 0000000..5821748
--- /dev/null
+++ b/weather/static/images/day_rainy_license.html
@@ -0,0 +1 @@
+Image by Roman Grac from Pixabay
\ No newline at end of file
diff --git a/weather/static/images/day_snowy.jpg b/weather/static/images/day_snowy.jpg
new file mode 100644
index 0000000..21d0b8a
Binary files /dev/null and b/weather/static/images/day_snowy.jpg differ
diff --git a/weather/static/images/day_snowy_license.html b/weather/static/images/day_snowy_license.html
new file mode 100644
index 0000000..422117d
--- /dev/null
+++ b/weather/static/images/day_snowy_license.html
@@ -0,0 +1 @@
+Image by Juncala from Pixabay
\ No newline at end of file
diff --git a/weather/static/images/day_stormy.jpg b/weather/static/images/day_stormy.jpg
new file mode 100644
index 0000000..0fa9b42
Binary files /dev/null and b/weather/static/images/day_stormy.jpg differ
diff --git a/weather/static/images/day_stormy_license.html b/weather/static/images/day_stormy_license.html
new file mode 100644
index 0000000..7658159
--- /dev/null
+++ b/weather/static/images/day_stormy_license.html
@@ -0,0 +1 @@
+Image by sethink from Pixabay
\ No newline at end of file
diff --git a/weather/static/images/night_clear.jpg b/weather/static/images/night_clear.jpg
new file mode 100644
index 0000000..063aa9f
Binary files /dev/null and b/weather/static/images/night_clear.jpg differ
diff --git a/weather/static/images/night_clear_license.html b/weather/static/images/night_clear_license.html
new file mode 100644
index 0000000..b53c973
--- /dev/null
+++ b/weather/static/images/night_clear_license.html
@@ -0,0 +1 @@
+Image by Nicole Pankalla from Pixabay
\ No newline at end of file
diff --git a/weather/static/images/night_cloudy.jpg b/weather/static/images/night_cloudy.jpg
new file mode 100644
index 0000000..c89d94f
Binary files /dev/null and b/weather/static/images/night_cloudy.jpg differ
diff --git a/weather/static/images/night_cloudy_license.html b/weather/static/images/night_cloudy_license.html
new file mode 100644
index 0000000..4d5e878
--- /dev/null
+++ b/weather/static/images/night_cloudy_license.html
@@ -0,0 +1 @@
+Image by Pexels from Pixabay
\ No newline at end of file
diff --git a/weather/static/images/night_rainy.jpg b/weather/static/images/night_rainy.jpg
new file mode 100644
index 0000000..609b7e7
Binary files /dev/null and b/weather/static/images/night_rainy.jpg differ
diff --git a/weather/static/images/night_rainy_license.html b/weather/static/images/night_rainy_license.html
new file mode 100644
index 0000000..5730c4c
--- /dev/null
+++ b/weather/static/images/night_rainy_license.html
@@ -0,0 +1 @@
+Image by Pexels from Pixabay
\ No newline at end of file
diff --git a/weather/static/images/night_snowy.jpg b/weather/static/images/night_snowy.jpg
new file mode 100644
index 0000000..aa08896
Binary files /dev/null and b/weather/static/images/night_snowy.jpg differ
diff --git a/weather/static/images/night_snowy_license.html b/weather/static/images/night_snowy_license.html
new file mode 100644
index 0000000..1c9a743
--- /dev/null
+++ b/weather/static/images/night_snowy_license.html
@@ -0,0 +1 @@
+Image by Jerzy from Pixabay
\ No newline at end of file
diff --git a/weather/static/images/night_stormy.jpg b/weather/static/images/night_stormy.jpg
new file mode 100644
index 0000000..619e8e6
Binary files /dev/null and b/weather/static/images/night_stormy.jpg differ
diff --git a/weather/static/images/night_stormy_license.html b/weather/static/images/night_stormy_license.html
new file mode 100644
index 0000000..f6391cb
--- /dev/null
+++ b/weather/static/images/night_stormy_license.html
@@ -0,0 +1 @@
+Image by Claudia Hinz from Pixabay
\ No newline at end of file
diff --git a/weather/weather.py b/weather/weather.py
index c6d2081..b1ea70a 100644
--- a/weather/weather.py
+++ b/weather/weather.py
@@ -2,6 +2,7 @@ from flask import (
Blueprint, render_template
)
from werkzeug.exceptions import abort
+from datetime import datetime
from weather.db import get_db
@@ -10,20 +11,26 @@ bp = Blueprint('weather', __name__)
@bp.route('/')
def index():
db = get_db()
- latest_period = dict(db.execute(
- 'SELECT `id` FROM `reports` WHERE `type` = "hourly" ORDER BY `reported_at` DESC'
+ current_conditions = dict(db.execute(
+ f"SELECT * FROM `reports` WHERE `type` = 'hourly' ORDER BY `end_time` DESC LIMIT 1"
).fetchone())
- current_conditions = dict(db.execute(
- f"SELECT * FROM `periods` WHERE `report_id` = {latest_period['id']} 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
- condition_image = f"images/{current_conditions['short_forecast'].lower()}.jpg"
+ day_or_night = 'day'
+ if current_conditions['is_daytime']:
+ day_or_night = 'night'
- periods = db.execute(
+ condition = mapForecastToImage(current_conditions['short_forecast'])
+ condition_image = f"images/{time_of_day}_{condition}.jpg"
+
+ hourly_reports = db.execute(
'SELECT *'
- ' FROM `periods`'
+ ' FROM `reports`'
' ORDER BY `id` DESC'
' LIMIT 7'
).fetchall()
@@ -35,3 +42,25 @@ def index():
periods=periods
)
+def mapForecastToImage(condition: str):
+ if not condition:
+ return 'cloudy'
+
+ condition = condition.lower()
+ if 'thunder' in condition or 'storm' in condition:
+ return 'stormy'
+ elif 'snow' in condition:
+ return 'snowy'
+ elif 'rain' in condition or 'shower' in condition or 'drizzle' in condition:
+ return 'rainy'
+ elif 'wind' in condition:
+ return 'windy'
+ elif 'sunny' in condition or 'clear' in condition:
+ return 'sunny'
+ elif 'cloud' in condition or 'overcast' in condition:
+ return 'cloudy'
+ elif 'fog' in condition or 'mist' in condition:
+ return 'cloudy'
+ else:
+ return 'cloudy'
+