diff options
author | lonkaars <loek@pipeframe.xyz> | 2022-11-22 12:14:35 +0100 |
---|---|---|
committer | lonkaars <loek@pipeframe.xyz> | 2022-11-22 12:14:35 +0100 |
commit | 6e55e01baab0f929bce7aed438b0a8c74188b62d (patch) | |
tree | fd9eba6ebbe8c78761fbe1da481225296713b3c0 /trein.py | |
parent | 0ffe2d19a953a0f935dce80dee91ee8faac3dabe (diff) |
working
Diffstat (limited to 'trein.py')
-rwxr-xr-x | trein.py | 48 |
1 files changed, 44 insertions, 4 deletions
@@ -1,8 +1,18 @@ #!/bin/python3 -import http.client, urllib.request, urllib.parse, urllib.error, base64 +import http.client +import urllib.request +import urllib.parse +import urllib.error +import json +import dateutil.parser +from icalendar import Calendar, Event + +cal = Calendar() +cal.add('prodid', 'trein') +cal.add('version', '2.0') def read_file(filename): - f = open(filename) + f = open(filename, "r") r = str(f.read()) f.close() return r @@ -22,7 +32,37 @@ def real_disruptions(): return data def get_disruptions(): - return fake_disruptions() + return real_disruptions() + +def disruption2ical(disruption): + ev = Event() + ev['uid'] = disruption['id'] + ev.add('name', f"{disruption['timespans'][0]['cause']['label']} {disruption['title']}") + description = disruption['expectedDuration']['description'] + "\n\n" + for timespan in disruption['timespans']: + description += timespan['situation']['label'] + "\n\n" + description += timespan['alternativeTransport']['label'] + "\n\n" + ev.add('description', description) + ev.add('dtstart', dateutil.parser.parse(disruption['start'])) + ev.add('dtend', dateutil.parser.parse(disruption['end'])) + cal.add_component(ev) + +def main(): + disruptions = json.loads(get_disruptions()) + relevant_stations = read_file("./config").strip().split("\n") + + for disruption in disruptions: + relevant = False + for section in disruption['publicationSections']: + consequence_stations = list(map(lambda x: x['name'], section['consequence']['section']['stations'])) + for i in range(len(relevant_stations)): + if relevant_stations[(i + 0) % len(relevant_stations)] in consequence_stations and \ + relevant_stations[(i + 1) % len(relevant_stations)] in consequence_stations: + relevant = True # only relevant if consequence contains current and next station + if relevant: disruption2ical(disruption) + + print(cal.to_ical()) if __name__ == "__main__": - print(get_disruptions()) + main() + |