Skip to content

Instantly share code, notes, and snippets.

@clonejo
Created January 14, 2018 18:53
Show Gist options
  • Save clonejo/332f9f207ff4af15d51c598a6d216d6a to your computer and use it in GitHub Desktop.
Save clonejo/332f9f207ff4af15d51c598a6d216d6a to your computer and use it in GitHub Desktop.
#!/usr/bin/python3
from datetime import datetime
import json
import requests
import sys
# TODO: locally check first if we are in the correct wifi network (WLANonICE)
try:
trip_info = json.loads(
requests.get('https://portal.imice.de/api1/rs/tripInfo').text
)
except requests.exceptions.ConnectionError:
sys.exit(1)
stops = {}
for stop in trip_info['stops']:
stops[stop['station']['evaNr']] = stop
actual_next_stop = stops[trip_info['stopInfo']['actualNext']]
# print(actual_next_stop)
train = '{} {}'.format(trip_info['trainType'], trip_info['vzn'])
next_stop_name = actual_next_stop['station']['name']
scheduled_arrival_time = datetime.fromtimestamp(
actual_next_stop['timetable']['scheduledArrivalTime']/1000)
delay = actual_next_stop['timetable']['arrivalDelay']
color = '#ffffff'
if delay != '' and delay[0] == '+':
delay_int = int(delay)
if delay_int <= 2:
color = '#ffff00'
else:
color = '#ff0000'
s = '{}, next: {} at <span fgcolor="{}">{:%H:%M}{}</span>' \
.format(train, next_stop_name, color, scheduled_arrival_time, delay)
out_obj = {
'full_text': s,
'markup': 'pango',
}
print(json.dumps(out_obj) + ",")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment