summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJohannes Löthberg <johannes@kyriasis.com>2017-01-12 21:26:09 +0000
committerJohannes Löthberg <johannes@kyriasis.com>2017-01-12 21:26:09 +0000
commit4ebf7e62b6939051d5030e10d89aaba7c4de29fb (patch)
tree03148b9dbb8cc120ac6ed4a3d30764304d4539ca
parent67ed8fe98390e30ffa7d032c24ce645b42c9fbf0 (diff)
downloadktt-4ebf7e62b6939051d5030e10d89aaba7c4de29fb.tar.xz
Add proper error logging
Signed-off-by: Johannes Löthberg <johannes@kyriasis.com>
-rw-r--r--real.py18
1 files changed, 15 insertions, 3 deletions
diff --git a/real.py b/real.py
index d19b7e1..e254c69 100644
--- a/real.py
+++ b/real.py
@@ -1,15 +1,22 @@
-from sys import stdin
+from sys import stdin, exit
from datetime import datetime
import argparse
import json
import requests
+import structlog
from jinja2 import Environment, FileSystemLoader
-def get_data(key, site_id):
+def get_data(logger, key, site_id):
url = 'http://api.sl.se/api2/realtimedeparturesV4.json?key={}&SiteId={}'.format(key, site_id)
res = requests.get(url)
+ if res.status_code != requests.codes.ok:
+ logger.error('Failed to get departure data',
+ status_code=res.status_code,
+ response=res.text)
+ exit(1)
+
write_data(res.text, site_id)
return res.json()
@@ -34,6 +41,8 @@ def write_txt(data, site_id, stop_name, path):
if __name__ == '__main__':
+ logger = structlog.get_logger()
+
parser = argparse.ArgumentParser(description='Process the SL Realtime Departure API')
parser.add_argument('-c', '--config', metavar='FILE', type=str, nargs='?',
required=True, help='path to config to use')
@@ -44,6 +53,9 @@ if __name__ == '__main__':
with open(args.config) as cf:
config = json.load(cf)
+ logger = logger.bind(site_id=config['site_id'],
+ stop_name=config['stop_name'])
+
env = Environment(loader=FileSystemLoader('templates'),
trim_blocks=True,
lstrip_blocks=True)
@@ -51,7 +63,7 @@ if __name__ == '__main__':
if args.stdin:
data = json.load(stdin)
else:
- data = get_data(config['key'], config['site_id'])
+ data = get_data(logger, config['key'], config['site_id'])
write_html(data, config['site_id'], config['stop_name'], config['output_path'])
write_txt(data, config['site_id'], config['stop_name'], config['output_path'])