summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYuval Adam <yuval@y3xz.com>2015-11-07 13:05:37 +0200
committerYuval Adam <yuval@y3xz.com>2015-11-07 13:05:37 +0200
commitf7498b63cba98288a59e7619725ac5cbf7004764 (patch)
treea2d3ab9f7726718d95d65516da112f15980feab4
parent2618417142dc1382e7476406ccf0c3ad9169320d (diff)
Add overlay timestamps and caching
-rw-r--r--geshem.py15
-rw-r--r--requirements.txt1
-rw-r--r--templates/index.html2
3 files changed, 14 insertions, 4 deletions
diff --git a/geshem.py b/geshem.py
index 209eb5e..63bbace 100644
--- a/geshem.py
+++ b/geshem.py
@@ -1,7 +1,9 @@
import redis
import requests
+from datetime import datetime
from flask import Flask, render_template
+from flask.ext.cache import Cache
from os import environ
from pathlib import Path
from redis.exceptions import ConnectionError
@@ -10,21 +12,28 @@ MAPS_JSON = 'http://map.govmap.gov.il/rainradar/radar.json'
STATIC_DIR = Path(__file__).resolve().parents[0] / 'static'
app = Flask(__name__)
+cache = Cache(app, config={'CACHE_TYPE': 'simple'})
redis = redis.from_url(environ.get('REDIS_URL', 'redis://localhost'))
def fetch_latest_images():
maps_json = requests.get(MAPS_JSON).json()
for r in ['images140', 'images280']:
imgs = sorted(maps_json[r].items(), key=lambda x: x[0], reverse=True)[:1] # only take latest
- for _ts, url in imgs:
+ res = r[-3:]
+ for ts, url in imgs:
+ dt = datetime.strptime(ts, '%Y:%m:%d:%H:%M').strftime('%Y%m%d_%H%M%S')
image = requests.get('http://' + url)
- filename = r[-3:] + '.png'
+ filename = '{}_{}.png'.format(dt, res)
with open(str(STATIC_DIR / filename), 'wb+') as f:
f.write(image.content)
+ redis.set('latest_{}'.format(res), dt)
+@cache.cached(timeout=60)
@app.route('/')
def home():
- return render_template('index.html')
+ latests = redis.pipeline().get('latest_140').get('latest_280').execute()
+ latest_140, latest_280 = map(lambda x: x.decode(), latests)
+ return render_template('index.html', latest_140=latest_140, latest_280=latest_280)
@app.after_request
def update_images(response):
diff --git a/requirements.txt b/requirements.txt
index a7eacd1..fb25d3e 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -1,4 +1,5 @@
Flask==0.10.1
+Flask-Cache==0.13.1
Jinja2==2.8
MarkupSafe==0.23
Werkzeug==0.10.4
diff --git a/templates/index.html b/templates/index.html
index 47832d9..6e36f61 100644
--- a/templates/index.html
+++ b/templates/index.html
@@ -42,7 +42,7 @@ var mapStyle = {
},
"overlay": {
"type": "image",
- "url": "/static/280.png",
+ "url": "/static/{{ latest_280 }}_280.png",
"coordinates": RASTER_COORDS_280
}
},