Group waste schedule events on the same day

Closes: #24
This commit is contained in:
Edward Betts 2023-10-11 16:15:58 +01:00
parent 7b9362fef8
commit 83d3b835f4

View file

@ -1,6 +1,7 @@
#!/usr/bin/python3
import os
from collections import defaultdict
from datetime import date, datetime, timedelta
import lxml.html
@ -55,16 +56,19 @@ def parse_waste_schedule_date(day_and_month: str) -> date:
def parse(root: lxml.html.HtmlElement) -> list[Event]:
"""Parse waste schedule."""
events = []
tbody = root.find(".//table/tbody")
assert tbody is not None
by_date = defaultdict(list)
for e_service, e_next_date, e_following in tbody:
assert e_service.text and e_next_date.text and e_following.text
service = e_service.text
next_date = parse_waste_schedule_date(e_next_date.text)
following_date = parse_waste_schedule_date(e_following.text)
events.append(Event(name="waste_schedule", date=next_date, title=service))
events.append(Event(name="waste_schedule", date=following_date, title=service))
by_date[next_date].append(service)
by_date[following_date].append(service)
return events
return [
Event(name="waste_schedule", date=d, title=", ".join(services))
for d, services in by_date.items()
]