Add offline mode

This commit is contained in:
Edward Betts 2024-10-31 11:13:55 +01:00
parent 9f1f64708f
commit eaa6369dc9
2 changed files with 10 additions and 5 deletions

View file

@ -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,

View file

@ -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)