Refresh space launches from cron because API is slow

Closes: #98
This commit is contained in:
Edward Betts 2024-01-14 08:04:05 +00:00
parent cb2a2c7fb8
commit e0735b4185
2 changed files with 32 additions and 2 deletions

View file

@ -10,6 +10,8 @@ import httpx
Launch = dict[str, typing.Any]
Summary = dict[str, typing.Any]
ttl = 60 * 60 * 2 # two hours
async def next_launch_api(rocket_dir: str, limit: int = 200) -> list[Launch]:
"""Get the next upcoming launches from the API."""
@ -137,14 +139,16 @@ def summarize_launch(launch: Launch) -> Summary:
}
async def get_launches(rocket_dir: str, limit: int = 200) -> list[Summary]:
async def get_launches(
rocket_dir: str, limit: int = 200, refresh: bool = False
) -> list[Summary]:
"""Get rocket launches with caching."""
now = datetime.now()
existing = [x for x in (filename_timestamp(f) for f in os.listdir(rocket_dir)) if x]
existing.sort(reverse=True)
if not existing or (now - existing[0][0]).seconds > 3600: # one hour
if refresh or not existing or (now - existing[0][0]).seconds > ttl:
try:
return await next_launch_api(rocket_dir, limit=limit)
except httpx.ReadTimeout: