diff --git a/agenda/thespacedevs.py b/agenda/thespacedevs.py index 5b5cbf9..648d4ea 100644 --- a/agenda/thespacedevs.py +++ b/agenda/thespacedevs.py @@ -322,6 +322,18 @@ def summarize_launch(launch: Launch) -> Summary: } +def is_launches_cache_fresh(rocket_dir: str) -> bool: + """Return True if the launches cache is younger than the TTL.""" + now = datetime.now() + existing = [ + x for x in (filename_timestamp(f, "json") for f in os.listdir(rocket_dir)) if x + ] + if not existing: + return False + existing.sort(reverse=True) + return (now - existing[0][0]).total_seconds() <= ttl + + def load_cached_launches(rocket_dir: str) -> StrDict | None: """Read the most recent cache of launches.""" filename = get_most_recent_file(rocket_dir, "json") diff --git a/update.py b/update.py index 97e4d41..f6e5ef3 100755 --- a/update.py +++ b/update.py @@ -321,6 +321,9 @@ def update_thespacedevs(config: flask.config.Config) -> None: existing_data = agenda.thespacedevs.load_cached_launches(rocket_dir) assert existing_data + if agenda.thespacedevs.is_launches_cache_fresh(rocket_dir): + return + # Update active crewed mission cache used by the launches page. # Uses the 2-hour TTL; failures are handled internally with cache fallback. active_crewed = agenda.thespacedevs.get_active_crewed_flights(rocket_dir)