From ace517c482e0d0cecad034a831ff1170f518ff73 Mon Sep 17 00:00:00 2001 From: Edward Betts Date: Mon, 15 Jul 2024 03:17:29 +0800 Subject: [PATCH] Reorder functions --- agenda/bristol_waste.py | 42 +++++++++++++++++------------------------ 1 file changed, 17 insertions(+), 25 deletions(-) diff --git a/agenda/bristol_waste.py b/agenda/bristol_waste.py index 9800b5a..0738d32 100644 --- a/agenda/bristol_waste.py +++ b/agenda/bristol_waste.py @@ -17,6 +17,23 @@ ttl_hours = 12 BristolSchedule = list[dict[str, typing.Any]] +async def get( + start_date: date, data_dir: str, uprn: str, refresh: bool = False +) -> list[Event]: + """Get waste collection schedule from Bristol City Council.""" + by_date: defaultdict[date, list[str]] = defaultdict(list) + for item in await get_data(data_dir, uprn, refresh): + service = get_service(item) + for d in collections(item): + if d < start_date and service not in by_date[d]: + by_date[d].append(service) + + return [ + Event(name="waste_schedule", date=d, title="Bristol: " + ", ".join(services)) + for d, services in by_date.items() + ] + + async def get_data(data_dir: str, uprn: str, refresh: bool = False) -> BristolSchedule: """Get Bristol Waste schedule, with cache.""" now = datetime.now() @@ -110,28 +127,3 @@ def collections(item: dict[str, typing.Any]) -> typing.Iterable[date]: for collection in item["collection"]: for collection_date_key in ["nextCollectionDate", "lastCollectionDate"]: yield date.fromisoformat(collection[collection_date_key][:10]) - - -def get_by_date(data: BristolSchedule, start_date: date) -> dict[date, list[str]]: - """Build list of Bristol services by date.""" - by_date: defaultdict[date, list[str]] = defaultdict(list) - - for item in data: - service = get_service(item) - for d in collections(item): - if d < start_date and service not in by_date[d]: - by_date[d].append(service) - return by_date - - -async def get( - start_date: date, data_dir: str, uprn: str, refresh: bool = False -) -> list[Event]: - """Get waste collection schedule from Bristol City Council.""" - data = await get_data(data_dir, uprn, refresh) - - by_date = get_by_date(data, start_date) - return [ - Event(name="waste_schedule", date=d, title="Bristol: " + ", ".join(services)) - for d, services in by_date.items() - ]