From 58610dc0f96e41daebb58f1d8bfff2c8a48b9ea3 Mon Sep 17 00:00:00 2001 From: lonkaars Date: Tue, 22 Nov 2022 21:18:45 +0100 Subject: autoplanner sort of working --- storingen.py | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100755 storingen.py (limited to 'storingen.py') diff --git a/storingen.py b/storingen.py new file mode 100755 index 0000000..9178b74 --- /dev/null +++ b/storingen.py @@ -0,0 +1,64 @@ +#!/bin/python3 +import http.client +import urllib.request +import urllib.parse +import urllib.error +import json +import dateutil.parser +from icalendar import Calendar, Event +from shared import * + +cal = Calendar() + +def fake_disruptions(): + return read_file("./api_response.json") + +def real_disruptions(): + key = read_file("./storingen.key").strip() + headers = { 'Ocp-Apim-Subscription-Key': key } + params = urllib.parse.urlencode({ 'isActive': 'true' }) + conn = http.client.HTTPSConnection('gateway.apiportal.ns.nl') + conn.request("GET", f"/reisinformatie-api/api/v3/disruptions?{params}", "{body}", headers) + response = conn.getresponse() + data = response.read() + conn.close() + return data + +def get_disruptions(): + return real_disruptions() + +def disruption2ical(disruption): + ev = Event() + ev['uid'] = disruption['id'] + ev.add('summary', 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(): + cal.add('prodid', 'trein') + cal.add('version', '2.0') + + disruptions = json.loads(get_disruptions()) + relevant_stations = read_file("./storingen.cfg").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(str(cal.to_ical(), 'utf-8')) + +if __name__ == "__main__": + main() + -- cgit v1.2.3