apply "Filter already passed events" commit by octys from upstream

This commit is contained in:
Jan Koppe 2020-12-24 12:58:06 +00:00
parent ad8bd06f6a
commit bf5d375274
1 changed files with 17 additions and 2 deletions

View File

@ -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)