From bf5d375274bd6deea9b20ae900601fe16337045e Mon Sep 17 00:00:00 2001 From: Jan Koppe Date: Thu, 24 Dec 2020 12:58:06 +0000 Subject: [PATCH] apply "Filter already passed events" commit by octys from upstream --- fahrplan.py | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/fahrplan.py b/fahrplan.py index ec1eff9..1414def 100644 --- a/fahrplan.py +++ b/fahrplan.py @@ -97,6 +97,12 @@ def events(scheduleUrl, titlemap={}): else: subtitle = '' + if event.find('duration') is not None and ':' in event.find('duration').text: + parts = event.find('duration').text.split(':', 1) + duration = int(parts[0]) * 60 + int(parts[1]) + else: + duration = None + if event.find('start') is not None and event.find('start').text is not None: start = re.sub(r'\s+', ' ', event.find('start').text).strip() else: @@ -114,6 +120,7 @@ def events(scheduleUrl, titlemap={}): 'track': event.find('track').text, 'start': event.find('start').text, 'datetime': event.find('date').text, + 'duration': duration, 'roomguid': room.attrib['guid'] if 'guid' in room.attrib else '', #'url': event.find('url').text } @@ -121,10 +128,18 @@ def events(scheduleUrl, titlemap={}): if __name__ == "__main__": - events = list(events(scheduleUrl)) + events = events(scheduleUrl) events = sorted(events, key=lambda x: datetime.datetime.fromisoformat(x["datetime"])) + # Filter already passed events + # (There is a grace period of 30 mins after start for events longer than that) + grace = datetime.timedelta(minutes=30) + def is_upcoming(ev, now=datetime.datetime.now().astimezone()): + ev_date = datetime.datetime.fromisoformat(ev["datetime"]) + return ev_date + grace > now and ev_date + datetime.timedelta(minutes=ev["duration"]) > now + events = filter(is_upcoming, events) + with open("fahrplan.json", "w") as f: - json.dump(events, f) + json.dump(list(events), f)