summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYuval Adam <yuval@y3xz.com>2015-11-07 00:45:07 +0200
committerYuval Adam <yuval@y3xz.com>2015-11-07 00:45:07 +0200
commit58f684d0aeded258f36160a8f957e8104abec708 (patch)
treecaadaaa72a40fdb717ac280c9ac9be1db93b17e1
parent13447f97f73d9a94b13775ef8947c53253d93212 (diff)
Add poor man's update_images() background task
-rw-r--r--geshem.py12
1 files changed, 10 insertions, 2 deletions
diff --git a/geshem.py b/geshem.py
index 0d4ab75..c15c8b1 100644
--- a/geshem.py
+++ b/geshem.py
@@ -1,13 +1,15 @@
+import redis
import requests
from flask import Flask, render_template
+from os import environ
from pathlib import Path
MAPS_JSON = 'http://map.govmap.gov.il/rainradar/radar.json'
STATIC_DIR = Path(__file__).resolve().parents[0] / 'static'
app = Flask(__name__)
-
+redis = redis.from_url(environ.get('REDIS_URL'))
def fetch_latest_images():
maps_json = requests.get(MAPS_JSON).json()
@@ -21,8 +23,14 @@ def fetch_latest_images():
@app.route('/')
def home():
- fetch_latest_images()
return render_template('index.html')
+@app.after_request
+def update_images():
+ # poor man's background task
+ if not redis.get('fresh'):
+ redis.setex('fresh', 60, 'yes')
+ fetch_latest_images()
+
if __name__ == '__main__':
app.run(debug=True)