diff options
author | Johannes Löthberg <johannes@kyriasis.com> | 2017-01-12 15:05:05 +0100 |
---|---|---|
committer | Johannes Löthberg <johannes@kyriasis.com> | 2017-01-12 15:05:05 +0100 |
commit | afe0b8eb2f3ff67a481f1ad1cb9420c07a47ae20 (patch) | |
tree | c85589a50e59b55fa67d517daf2bcd44bd1f77fa | |
download | ktt-afe0b8eb2f3ff67a481f1ad1cb9420c07a47ae20.tar.xz |
Initial commit
Signed-off-by: Johannes Löthberg <johannes@kyriasis.com>
-rw-r--r-- | real.py | 57 | ||||
-rw-r--r-- | templates/layout.html | 124 | ||||
-rw-r--r-- | templates/layout.txt | 40 |
3 files changed, 221 insertions, 0 deletions
@@ -0,0 +1,57 @@ +from sys import stdin +from datetime import datetime +import argparse +import json + +import requests +from jinja2 import Environment, FileSystemLoader + + +def get_data(key, site_id): + url = 'http://api.sl.se/api2/realtimedeparturesV4.json?key={}&SiteId={}'.format(key, site_id) + res = requests.get(url) + write_data(res.text, site_id) + + return res.json() + + +def write_data(data, site_id): + timestamp = datetime.now().strftime('%Y-%m-%dT%H:%M:%S') + with open('{}.{}.raw.json'.format(site_id, timestamp), 'w+') as f: + f.write(data) + + +def write_html(data, site_id): + template = env.get_template('layout.html') + with open('{}.html'.format(site_id), 'w+') as f: + f.write(template.render(data=data)) + + +def write_txt(data, site_id): + template = env.get_template('layout.txt') + with open('{}.txt'.format(site_id), 'w+') as f: + f.write(template.render(data=data)) + + +if __name__ == '__main__': + 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') + parser.add_argument('--stdin', action='store_true') + args = parser.parse_args() + print(args) + + with open(args.config) as cf: + config = json.load(cf) + + env = Environment(loader=FileSystemLoader('templates'), + trim_blocks=True, + lstrip_blocks=True) + + if args.stdin: + data = json.load(stdin) + else: + data = get_data(config['key'], config['site_id']) + + write_html(data, config['site_id']) + write_txt(data, config['site_id']) diff --git a/templates/layout.html b/templates/layout.html new file mode 100644 index 0000000..aead13f --- /dev/null +++ b/templates/layout.html @@ -0,0 +1,124 @@ +<style> +h4 { + margin-top: 0.5em; + margin-bottom: 0.5em; +} +h5 { + margin-top: 0em; + margin-bottom: 0em; +} +div { + margin: 0; + padding: 0; + padding-left: 2rem; +} +div.root { + padding-left: 0em; +} +p { + margin: 0; + padding: 0; +} +</style> + + +<div class="root"> + <p>Last updated: {{ data['ResponseData']['LatestUpdate'] }}</p> + + <ul> + {%- for transport in ['Trains', 'Metros', 'Buses'] -%} + {%- if data['ResponseData'][transport] -%} + <li><a href="#{{ transport }}">{{ transport }}</a></li> + {%- endif -%} + {%- endfor -%} + </ul> + + <h4>Stop deviations:</h4> + + {%- if data['ResponseData']['StopPointDeviations'] -%} + {%- for d in data['ResponseData']['StopPointDeviations'] -%} + {%- set stopinfo = d['StopInfo'] -%} + {%- set deviation = d['Deviation'] -%} + <div> + <h5>{{ stopinfo['StopAreaName'] }} ({{ stopinfo['TransportMode'] }})</h5> + <div> + <p>{{ deviation['ImportanceLevel'] }} :: {{ deviation['Consequence'] }}</p> + <div> + <p>{{ deviation['Text'] }}</p> + </div> + </div> + </div> + </br> + {%- endfor -%} + {%- endif -%} +</div> + +{%- for transport in ['Trains', 'Metros', 'Buses'] -%} +{%- if data['ResponseData'][transport] -%} +<div class="root"> + <h4 id="{{ transport }}">:: {{ transport }} ::</h4> + {%- for departure in data['ResponseData'][transport] -%} + + {% if departure['GroupOfLine'] %} + {% if departure['GroupOfLine'] == 'tunnelbanans gröna linje' %} + {% set color='green' %} + {% elif departure['GroupOfLine'] == 'Tunnelbanans röda linje' %} + {% set color='red' %} + {% elif departure['GroupOfLine'] == 'tunnelbanans blå linje' %} + {% set color='blue' %} + {% else %} + {% set color='black' %} + {% endif %} + {% endif %} + + <h5>{% if departure['GroupOfLine'] %}<span style="color: {{color}}">●</span>{% endif %} + {{ departure['LineNumber'] }} :: {{ departure['Destination'] }} + {% if departure['SecondaryDestinationName'] %}({{ departure['SecondaryDestinationName'] }}){% endif %} + :: {{ departure['DisplayTime'] }} + </h5> + + {% if transport in ['Trains', 'Buses'] %} + <div> + <p>Stop: {{ departure['StopPointDesignation'] }}</p> + </div> + {% endif %} + + {% if departure['GroupOfLine'] %} + <div> + <p>{{ departure['GroupOfLine'] }}</p> + </div> + {% endif %} + + {%- if 'TimeTabledDateTime' in departure or 'ExpectedDateTime' in departure -%} + <div> + {%- if departure['TimeTabledDateTime'] -%} + <p>Timetable: {{ departure['TimeTabledDateTime'] }}</p> + {%- endif -%} + {%- if departure['ExpectedDateTime'] -%} + <p>Expected: {{ departure['ExpectedDateTime'] }}</p> + {%- endif -%} + </div> + {%- endif -%} + + {%- if departure['Deviations'] -%} + <div> + <p>Deviations:</p> + + {%- for deviation in departure['Deviations'] -%} + <div> + <p>{{ deviation['ImportanceLevel'] }} :: {{ deviation['Consequence'] }}</p> + <div> + <p>{{ deviation['Text'] }}</p> + </div> + </div> + {%- endfor -%} + </div> + {%- endif -%} + </br> + + {%- endfor -%} +</div> +{%- endif -%} +{%- endfor -%} + +<!-- vim: set ft=html: --> diff --git a/templates/layout.txt b/templates/layout.txt new file mode 100644 index 0000000..242fd94 --- /dev/null +++ b/templates/layout.txt @@ -0,0 +1,40 @@ +Last updated: {{ data['ResponseData']['LatestUpdate'] }} +{% if data['ResponseData']['StopPointDeviations'] %} +Stop deviations: +{% for d in data['ResponseData']['StopPointDeviations'] %} +{% set stopinfo = d['StopInfo'] %} +{% set deviation = d['Deviation'] %} + {{ stopinfo['StopAreaName'] }} ({{ stopinfo['TransportMode'] }}) + {{ deviation['ImportanceLevel'] }} :: {{ deviation['Consequence'] }} + {{ deviation['Text'] }} + +{% endfor %} + +{% endif %} +{% for transport in ['Trains', 'Metros', 'Buses'] %} +{% if data['ResponseData'][transport] %} +:: {{ transport }} :: +{% for departure in data['ResponseData'][transport] %} +{% if departure['SecondaryDestinationName'] %} +{{ departure['LineNumber'] }} :: {{ departure['Destination'] }} ({{ departure['SecondaryDestinationName'] }}) :: {{ departure['DisplayTime'] }} +{% else %} +{{ departure['LineNumber'] }} :: {{ departure['Destination'] }} :: {{ departure['DisplayTime'] }} +{% endif %} +{% if transport == 'Buses' %} + Stop: {{ departure['StopPointDesignation'] }} +{% endif %} +{% if 'TimeTabledDateTime' in departure or 'ExpectedDateTime' in departure %} + Timetable: {{ departure['TimeTabledDateTime'] }} + Expected: {{ departure['ExpectedDateTime'] }} +{% endif %} +{% if departure['Deviations'] %} + Deviations: + {% for deviation in departure['Deviations'] %} + {{ deviation['ImportanceLevel'] }} :: {{ deviation['Consequence'] }} + {{ deviation['Text'] }} + {% endfor %} +{% endif %} + +{% endfor %} +{% endif %} +{% endfor %} |