From e475f98dd6b40fd74b53f6da5701faf1cffcc29a Mon Sep 17 00:00:00 2001 From: Edward Betts Date: Fri, 19 Jan 2024 19:53:21 +0000 Subject: [PATCH] Use cache for space launch data --- agenda/data.py | 6 +++--- agenda/thespacedevs.py | 12 ++++++++++++ update_thespacedevs.py | 2 +- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/agenda/data.py b/agenda/data.py index c7268e6..eb42c26 100644 --- a/agenda/data.py +++ b/agenda/data.py @@ -274,10 +274,10 @@ async def get_data( result_list = await asyncio.gather( time_function("gbpusd", fx.get_gbpusd, config), time_function("gwr_advance_tickets", gwr.advance_ticket_date, data_dir), - time_function("rockets", thespacedevs.get_launches, rocket_dir, limit=40), time_function("backwell_bins", waste_collection_events, data_dir), time_function("bristol_bins", bristol_waste_collection_events, data_dir, today), ) + rockets = thespacedevs.read_cached_launches(rocket_dir) results = {call[0]: call[1] for call in result_list} @@ -293,7 +293,7 @@ async def get_data( "now": now, "gbpusd": results["gbpusd"], "stock_markets": stock_market_times, - "rockets": results["rockets"], + "rockets": rockets, "gwr_advance_tickets": gwr_advance_tickets, "data_gather_seconds": data_gather_seconds, "stock_market_times_seconds": stock_market_times_seconds, @@ -350,7 +350,7 @@ async def get_data( for market in overlapping_markets: events.remove(market) - for launch in results["rockets"]: + for launch in rockets: dt = None if launch["net_precision"] == "Day": diff --git a/agenda/thespacedevs.py b/agenda/thespacedevs.py index 23e2887..c1c785e 100644 --- a/agenda/thespacedevs.py +++ b/agenda/thespacedevs.py @@ -139,6 +139,18 @@ def summarize_launch(launch: Launch) -> Summary: } +def read_cached_launches(rocket_dir: str) -> list[Summary]: + """Read the most recent cache of launches.""" + existing = [x for x in (filename_timestamp(f) for f in os.listdir(rocket_dir)) if x] + + existing.sort(reverse=True) + f = existing[0][1] + + filename = os.path.join(rocket_dir, f) + data = json.load(open(filename)) + return [summarize_launch(launch) for launch in data["results"]] + + async def get_launches( rocket_dir: str, limit: int = 200, refresh: bool = False ) -> list[Summary]: diff --git a/update_thespacedevs.py b/update_thespacedevs.py index 9ecec7e..44a96f8 100755 --- a/update_thespacedevs.py +++ b/update_thespacedevs.py @@ -14,7 +14,7 @@ rocket_dir = os.path.join(config.DATA_DIR, "thespacedevs") async def get_launches() -> list[agenda.thespacedevs.Launch]: """Call space launch API and cache results.""" - return await agenda.thespacedevs.get_launches(rocket_dir, limit=200, refresh=True) + return await agenda.thespacedevs.next_launch_api(rocket_dir) t0 = time()