From d1898686e9a758fca2d994eb2eef719e04a8429a Mon Sep 17 00:00:00 2001 From: Edward Betts Date: Tue, 9 Jul 2024 08:22:33 +0100 Subject: [PATCH] Catch and ignore space launch API errors --- agenda/thespacedevs.py | 11 ++++++++--- update.py | 3 +++ 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/agenda/thespacedevs.py b/agenda/thespacedevs.py index 8ca9428..4092f40 100644 --- a/agenda/thespacedevs.py +++ b/agenda/thespacedevs.py @@ -16,7 +16,7 @@ Summary = dict[str, typing.Any] ttl = 60 * 60 * 2 # two hours -def next_launch_api_data(rocket_dir: str, limit: int = 200) -> StrDict: +def next_launch_api_data(rocket_dir: str, limit: int = 200) -> StrDict | None: """Get the next upcoming launches from the API.""" now = datetime.now() filename = os.path.join(rocket_dir, now.strftime("%Y-%m-%d_%H:%M:%S.json")) @@ -24,14 +24,19 @@ def next_launch_api_data(rocket_dir: str, limit: int = 200) -> StrDict: params: dict[str, str | int] = {"limit": limit} r = requests.get(url, params=params) - data: StrDict = r.json() + try: + data: StrDict = r.json() + except requests.exceptions.JSONDecodeError: + return None open(filename, "w").write(r.text) return data -def next_launch_api(rocket_dir: str, limit: int = 200) -> list[Launch]: +def next_launch_api(rocket_dir: str, limit: int = 200) -> list[Launch] | None: """Get the next upcoming launches from the API.""" data = next_launch_api_data(rocket_dir, limit) + if not data: + return None return [summarize_launch(launch) for launch in data["results"]] diff --git a/update.py b/update.py index 01ec251..bb21070 100755 --- a/update.py +++ b/update.py @@ -114,6 +114,7 @@ def update_thespacedevs(config: flask.config.Config) -> None: rocket_dir = os.path.join(config["DATA_DIR"], "thespacedevs") existing_data = agenda.thespacedevs.load_cached_launches(rocket_dir) + assert existing_data prev_launches = { slug: get_launch_by_slug(existing_data, slug) for slug in config["FOLLOW_LAUNCHES"] @@ -121,6 +122,8 @@ def update_thespacedevs(config: flask.config.Config) -> None: t0 = time() data = agenda.thespacedevs.next_launch_api_data(rocket_dir) + if not data: + return # thespacedevs API call failed cur_launches = { slug: get_launch_by_slug(data, slug) for slug in config["FOLLOW_LAUNCHES"] }