Make North Somerset bin collection code async

Closes: #55
This commit is contained in:
Edward Betts 2023-11-05 21:46:06 +00:00
parent bda05d214c
commit 95bab9a62b
2 changed files with 17 additions and 13 deletions

View file

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

View file

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