diff --git a/agenda/waste_schedule.py b/agenda/bristol_waste.py similarity index 60% rename from agenda/waste_schedule.py rename to agenda/bristol_waste.py index 947eb6c..dfe7052 100644 --- a/agenda/waste_schedule.py +++ b/agenda/bristol_waste.py @@ -2,103 +2,18 @@ import json import os -import re import typing from collections import defaultdict -from datetime import date, datetime, time, timedelta +from datetime import date, datetime, timedelta import httpx -import lxml.html -from . import uk_time from .types import Event +from .utils import make_waste_dir 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" - 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'