parent
7b9362fef8
commit
83d3b835f4
|
@ -1,6 +1,7 @@
|
||||||
#!/usr/bin/python3
|
#!/usr/bin/python3
|
||||||
|
|
||||||
import os
|
import os
|
||||||
|
from collections import defaultdict
|
||||||
from datetime import date, datetime, timedelta
|
from datetime import date, datetime, timedelta
|
||||||
|
|
||||||
import lxml.html
|
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]:
|
def parse(root: lxml.html.HtmlElement) -> list[Event]:
|
||||||
"""Parse waste schedule."""
|
"""Parse waste schedule."""
|
||||||
events = []
|
|
||||||
tbody = root.find(".//table/tbody")
|
tbody = root.find(".//table/tbody")
|
||||||
assert tbody is not None
|
assert tbody is not None
|
||||||
|
by_date = defaultdict(list)
|
||||||
for e_service, e_next_date, e_following in tbody:
|
for e_service, e_next_date, e_following in tbody:
|
||||||
assert e_service.text and e_next_date.text and e_following.text
|
assert e_service.text and e_next_date.text and e_following.text
|
||||||
service = e_service.text
|
service = e_service.text
|
||||||
next_date = parse_waste_schedule_date(e_next_date.text)
|
next_date = parse_waste_schedule_date(e_next_date.text)
|
||||||
following_date = parse_waste_schedule_date(e_following.text)
|
following_date = parse_waste_schedule_date(e_following.text)
|
||||||
|
|
||||||
events.append(Event(name="waste_schedule", date=next_date, title=service))
|
by_date[next_date].append(service)
|
||||||
events.append(Event(name="waste_schedule", date=following_date, title=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()
|
||||||
|
]
|
||||||
|
|
Loading…
Reference in a new issue