"""Waste collection schedules.""" import os import re from collections import defaultdict from datetime import date, datetime, time, timedelta import httpx import lxml.html from . import uk_time from .types import Event from .utils import make_waste_dir ttl_hours = 12 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") make_waste_dir(data_dir) existing_data = os.listdir(waste_dir) existing = [f for f in existing_data if f.endswith(".html")] if existing: recent_filename = max(existing) recent = datetime.strptime(recent_filename, "%Y-%m-%d_%H:%M.html") delta = now - recent if existing and delta < timedelta(hours=ttl_hours): return open(os.path.join(waste_dir, recent_filename)).read() now_str = now.strftime("%Y-%m-%d_%H:%M") filename = f"{waste_dir}/{now_str}.html" forms_base_url = "https://forms.n-somerset.gov.uk" url = "https://forms.n-somerset.gov.uk/Waste/CollectionSchedule" async with httpx.AsyncClient() as client: r = await client.post( url, data={ "PreviousHouse": "", "PreviousPostcode": "-", "Postcode": postcode, "SelectedUprn": uprn, }, ) form_post_html = r.text pattern = r'