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.""" """Waste colllection events."""
postcode = "BS48 3HG" postcode = "BS48 3HG"
uprn = "24071046" 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) root = lxml.html.fromstring(html)
events = waste_schedule.parse(root) events = waste_schedule.parse(root)
return events return events
@ -324,11 +324,13 @@ async def get_data(now: datetime) -> typing.Mapping[str, str | object]:
gwr_advance_tickets, gwr_advance_tickets,
bank_holiday, bank_holiday,
rockets, rockets,
backwell_bins,
) = await asyncio.gather( ) = await asyncio.gather(
fx.get_gbpusd(config), fx.get_gbpusd(config),
gwr.advance_ticket_date(data_dir), gwr.advance_ticket_date(data_dir),
get_next_bank_holiday(last_year, next_year), get_next_bank_holiday(last_year, next_year),
thespacedevs.get_launches(rocket_dir, limit=40), thespacedevs.get_launches(rocket_dir, limit=40),
waste_collection_events(),
) )
reply = { 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 += get_accommodation(os.path.join(my_data, "accommodation.yaml"))
events += travel.all_events(today, config["data"]["personal-data"]) events += travel.all_events(today, config["data"]["personal-data"])
events += conference.get_list(os.path.join(my_data, "conferences.yaml")) 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( next_up_series = Event(
date=date(2026, 6, 1), date=date(2026, 6, 1),

View file

@ -8,6 +8,7 @@ from datetime import date, datetime, timedelta
import lxml.html import lxml.html
import requests import requests
import httpx
from .types import Event from .types import Event
@ -21,7 +22,7 @@ def make_waste_dir(data_dir: str) -> None:
os.mkdir(waste_dir) 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.""" """Get waste schedule."""
now = datetime.now() now = datetime.now()
waste_dir = os.path.join(data_dir, "waste") 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") now_str = now.strftime("%Y-%m-%d_%H:%M")
filename = f"{waste_dir}/{now_str}.html" filename = f"{waste_dir}/{now_str}.html"
r = requests.post( async with httpx.AsyncClient() as client:
"https://forms.n-somerset.gov.uk/Waste/CollectionSchedule", r = await client.post(
data={ "https://forms.n-somerset.gov.uk/Waste/CollectionSchedule",
"PreviousHouse": "", data={
"PreviousPostcode": "-", "PreviousHouse": "",
"Postcode": postcode, "PreviousPostcode": "-",
"SelectedUprn": uprn, "Postcode": postcode,
}, "SelectedUprn": uprn,
) },
)
html = r.text html = r.text
open(filename, "w").write(html) open(filename, "w").write(html)
return html return html