"""Waste collection schedules.""" import json import os import re import typing from collections import defaultdict from datetime import date, datetime, time, timedelta import httpx import lxml.html from . import uk_time from .types import Event ttl_hours = 12 def make_waste_dir(data_dir: str) -> None: """Make waste dir if missing.""" waste_dir = os.path.join(data_dir, "waste") if not os.path.exists(waste_dir): os.mkdir(waste_dir) 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" # url2 = "https://forms.n-somerset.gov.uk/Waste/CollectionSchedule/ViewSchedule" 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'