More support for offline_mode

This commit is contained in:
Edward Betts 2024-11-04 09:59:55 +00:00
parent 3c3939c525
commit 67b1adf956
4 changed files with 23 additions and 13 deletions

View file

@ -17,12 +17,10 @@ ttl_hours = 12
BristolSchedule = list[dict[str, typing.Any]]
async def get(
start_date: date, data_dir: str, uprn: str, refresh: bool = False
) -> list[Event]:
async def get(start_date: date, data_dir: str, uprn: str, cache: str) -> list[Event]:
"""Get waste collection schedule from Bristol City Council."""
by_date: defaultdict[date, list[str]] = defaultdict(list)
for item in await get_data(data_dir, uprn, refresh):
for item in await get_data(data_dir, uprn, cache):
service = get_service(item)
for d in collections(item):
if d < start_date and service not in by_date[d]:
@ -34,7 +32,7 @@ async def get(
]
async def get_data(data_dir: str, uprn: str, refresh: bool = False) -> BristolSchedule:
async def get_data(data_dir: str, uprn: str, cache: str) -> BristolSchedule:
"""Get Bristol Waste schedule, with cache."""
now = datetime.now()
waste_dir = os.path.join(data_dir, "waste")
@ -52,7 +50,11 @@ async def get_data(data_dir: str, uprn: str, refresh: bool = False) -> BristolSc
json_data = json.load(open(os.path.join(waste_dir, recent_filename)))
return typing.cast(BristolSchedule, json_data["data"])
if not refresh and existing and delta < timedelta(hours=ttl_hours):
if (
cache != "refresh"
and existing
and (cache == "force" or delta < timedelta(hours=ttl_hours))
):
return get_from_recent()
try:

View file

@ -62,20 +62,21 @@ def timezone_transition(
async def n_somerset_waste_collection_events(
data_dir: str, postcode: str, uprn: str
data_dir: str, postcode: str, uprn: str, force_cache: bool = False
) -> list[Event]:
"""Waste colllection events."""
html = await n_somerset_waste.get_html(data_dir, postcode, uprn)
html = await n_somerset_waste.get_html(data_dir, postcode, uprn, force_cache)
root = lxml.html.fromstring(html)
events = n_somerset_waste.parse(root)
return events
async def bristol_waste_collection_events(
data_dir: str, start_date: date, uprn: str
data_dir: str, start_date: date, uprn: str, force_cache: bool = False
) -> list[Event]:
"""Waste colllection events."""
return await bristol_waste.get(start_date, data_dir, uprn)
cache = "force" if force_cache else "recent"
return await bristol_waste.get(start_date, data_dir, uprn, cache)
def find_events_during_stay(
@ -193,6 +194,7 @@ async def get_data(now: datetime, config: flask.config.Config) -> AgendaData:
data_dir,
config["BACKWELL_POSTCODE"],
config["BACKWELL_UPRN"],
offline_mode,
),
time_function(
"bristol_bins",
@ -200,6 +202,7 @@ async def get_data(now: datetime, config: flask.config.Config) -> AgendaData:
data_dir,
today,
config["BRISTOL_UPRN"],
offline_mode,
),
)
rockets = thespacedevs.read_cached_launches(rocket_dir)

View file

@ -15,7 +15,9 @@ from .utils import make_waste_dir
ttl_hours = 12
async def get_html(data_dir: str, postcode: str, uprn: str) -> str:
async def get_html(
data_dir: str, postcode: str, uprn: str, force_cache: bool = False
) -> str:
"""Get waste schedule."""
now = datetime.now()
waste_dir = os.path.join(data_dir, "waste")
@ -29,7 +31,7 @@ async def get_html(data_dir: str, postcode: str, uprn: str) -> str:
recent = datetime.strptime(recent_filename, "%Y-%m-%d_%H:%M.html")
delta = now - recent
if existing and delta < timedelta(hours=ttl_hours):
if existing and (force_cache or delta < timedelta(hours=ttl_hours)):
return open(os.path.join(waste_dir, recent_filename)).read()
now_str = now.strftime("%Y-%m-%d_%H:%M")

View file

@ -40,7 +40,10 @@ async def update_bristol_bins(config: flask.config.Config) -> None:
"""Update waste schedule from Bristol City Council."""
t0 = time()
events = await agenda.bristol_waste.get(
date.today(), config["DATA_DIR"], config["BRISTOL_UPRN"], refresh=True
date.today(),
config["DATA_DIR"],
config["BRISTOL_UPRN"],
cache="refresh",
)
time_taken = time() - t0
if not sys.stdin.isatty():