From eaa6369dc98f79d806515d759c7c351f6ca3e246 Mon Sep 17 00:00:00 2001 From: Edward Betts Date: Thu, 31 Oct 2024 11:13:55 +0100 Subject: [PATCH] Add offline mode --- agenda/data.py | 5 ++++- agenda/gwr.py | 10 ++++++---- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/agenda/data.py b/agenda/data.py index 4bbd76f..c1f6f4d 100644 --- a/agenda/data.py +++ b/agenda/data.py @@ -182,8 +182,11 @@ async def get_data(now: datetime, config: flask.config.Config) -> AgendaData: plus_365 = now + timedelta(days=365) t0 = time() + offline_mode = bool(config.get("OFFLINE_MODE")) result_list = await asyncio.gather( - time_function("gwr_advance_tickets", gwr.advance_ticket_date, data_dir), + time_function( + "gwr_advance_tickets", gwr.advance_ticket_date, data_dir, offline_mode + ), time_function( "backwell_bins", n_somerset_waste_collection_events, diff --git a/agenda/gwr.py b/agenda/gwr.py index 750af90..cc7e968 100644 --- a/agenda/gwr.py +++ b/agenda/gwr.py @@ -48,11 +48,13 @@ def extract_weekday_date(html: str) -> date | None: return None -async def advance_tickets_page_html(data_dir: str, ttl: int = 60 * 60 * 6) -> str: +async def advance_tickets_page_html( + data_dir: str, ttl: int = 60 * 60 * 6, force_cache: bool = False +) -> str: """Get advance-tickets web page HTML with cache.""" filename = os.path.join(data_dir, "advance-tickets.html") mtime = os.path.getmtime(filename) if os.path.exists(filename) else 0 - if (time() - mtime) < ttl: # use cache + if force_cache or (time() - mtime) < ttl: # use cache return open(filename).read() async with httpx.AsyncClient() as client: r = await client.get(url) @@ -61,7 +63,7 @@ async def advance_tickets_page_html(data_dir: str, ttl: int = 60 * 60 * 6) -> st return html -async def advance_ticket_date(data_dir: str) -> date | None: +async def advance_ticket_date(data_dir: str, force_cache: bool = False) -> date | None: """Get GWR advance tickets date with cache.""" - html = await advance_tickets_page_html(data_dir) + html = await advance_tickets_page_html(data_dir, force_cache=force_cache) return extract_weekday_date(html)