From 95bab9a62b5c50198f9e4d78cb6c8f2c0813ffaf Mon Sep 17 00:00:00 2001 From: Edward Betts Date: Sun, 5 Nov 2023 21:46:06 +0000 Subject: [PATCH] Make North Somerset bin collection code async Closes: #55 --- agenda/__init__.py | 8 +++++--- agenda/waste_schedule.py | 22 ++++++++++++---------- 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/agenda/__init__.py b/agenda/__init__.py index 1641601..86bb2fb 100644 --- a/agenda/__init__.py +++ b/agenda/__init__.py @@ -290,12 +290,12 @@ def get_accommodation(filepath: str) -> list[Event]: ] -def waste_collection_events() -> list[Event]: +async def waste_collection_events() -> list[Event]: """Waste colllection events.""" postcode = "BS48 3HG" uprn = "24071046" - html = waste_schedule.get_html(data_dir, postcode, uprn) + html = await waste_schedule.get_html(data_dir, postcode, uprn) root = lxml.html.fromstring(html) events = waste_schedule.parse(root) return events @@ -324,11 +324,13 @@ async def get_data(now: datetime) -> typing.Mapping[str, str | object]: gwr_advance_tickets, bank_holiday, rockets, + backwell_bins, ) = await asyncio.gather( fx.get_gbpusd(config), gwr.advance_ticket_date(data_dir), get_next_bank_holiday(last_year, next_year), thespacedevs.get_launches(rocket_dir, limit=40), + waste_collection_events(), ) reply = { @@ -383,7 +385,7 @@ async def get_data(now: datetime) -> typing.Mapping[str, str | object]: events += get_accommodation(os.path.join(my_data, "accommodation.yaml")) events += travel.all_events(today, config["data"]["personal-data"]) events += conference.get_list(os.path.join(my_data, "conferences.yaml")) - events += waste_collection_events() + bristol_waste_collection_events(today) + events += backwell_bins + bristol_waste_collection_events(today) next_up_series = Event( date=date(2026, 6, 1), diff --git a/agenda/waste_schedule.py b/agenda/waste_schedule.py index 2fdaa24..62c965c 100644 --- a/agenda/waste_schedule.py +++ b/agenda/waste_schedule.py @@ -8,6 +8,7 @@ from datetime import date, datetime, timedelta import lxml.html import requests +import httpx from .types import Event @@ -21,7 +22,7 @@ def make_waste_dir(data_dir: str) -> None: os.mkdir(waste_dir) -def get_html(data_dir: str, postcode: str, uprn: str) -> str: +async def get_html(data_dir: str, postcode: str, uprn: str) -> str: """Get waste schedule.""" now = datetime.now() waste_dir = os.path.join(data_dir, "waste") @@ -41,15 +42,16 @@ def get_html(data_dir: str, postcode: str, uprn: str) -> str: now_str = now.strftime("%Y-%m-%d_%H:%M") filename = f"{waste_dir}/{now_str}.html" - r = requests.post( - "https://forms.n-somerset.gov.uk/Waste/CollectionSchedule", - data={ - "PreviousHouse": "", - "PreviousPostcode": "-", - "Postcode": postcode, - "SelectedUprn": uprn, - }, - ) + async with httpx.AsyncClient() as client: + r = await client.post( + "https://forms.n-somerset.gov.uk/Waste/CollectionSchedule", + data={ + "PreviousHouse": "", + "PreviousPostcode": "-", + "Postcode": postcode, + "SelectedUprn": uprn, + }, + ) html = r.text open(filename, "w").write(html) return html