From 4ebf7e62b6939051d5030e10d89aaba7c4de29fb Mon Sep 17 00:00:00 2001 From: Johannes Löthberg Date: Thu, 12 Jan 2017 21:26:09 +0000 Subject: Add proper error logging MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Johannes Löthberg --- real.py | 18 +++++++++++++++--- 1 file 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']) -- cgit v1.2.3-70-g09d2