Compare commits
2 commits
0c36591e2f
...
b38ec99628
Author | SHA1 | Date | |
---|---|---|---|
Edward Betts | b38ec99628 | ||
Edward Betts | d1898686e9 |
|
@ -16,7 +16,7 @@ Summary = dict[str, typing.Any]
|
||||||
ttl = 60 * 60 * 2 # two hours
|
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."""
|
"""Get the next upcoming launches from the API."""
|
||||||
now = datetime.now()
|
now = datetime.now()
|
||||||
filename = os.path.join(rocket_dir, now.strftime("%Y-%m-%d_%H:%M:%S.json"))
|
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}
|
params: dict[str, str | int] = {"limit": limit}
|
||||||
r = requests.get(url, params=params)
|
r = requests.get(url, params=params)
|
||||||
|
try:
|
||||||
data: StrDict = r.json()
|
data: StrDict = r.json()
|
||||||
|
except requests.exceptions.JSONDecodeError:
|
||||||
|
return None
|
||||||
open(filename, "w").write(r.text)
|
open(filename, "w").write(r.text)
|
||||||
return data
|
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."""
|
"""Get the next upcoming launches from the API."""
|
||||||
data = next_launch_api_data(rocket_dir, limit)
|
data = next_launch_api_data(rocket_dir, limit)
|
||||||
|
if not data:
|
||||||
|
return None
|
||||||
return [summarize_launch(launch) for launch in data["results"]]
|
return [summarize_launch(launch) for launch in data["results"]]
|
||||||
|
|
||||||
|
|
||||||
|
|
12
update.py
12
update.py
|
@ -9,6 +9,7 @@ import typing
|
||||||
from datetime import date, datetime
|
from datetime import date, datetime
|
||||||
from time import time
|
from time import time
|
||||||
|
|
||||||
|
import deepdiff # type: ignore
|
||||||
import flask
|
import flask
|
||||||
import requests
|
import requests
|
||||||
|
|
||||||
|
@ -87,16 +88,14 @@ def report_space_launch_change(
|
||||||
name = prev_launch["name"]
|
name = prev_launch["name"]
|
||||||
subject = f"Change to {name}"
|
subject = f"Change to {name}"
|
||||||
|
|
||||||
|
differences = deepdiff.DeepDiff(prev_launch, cur_launch)
|
||||||
|
|
||||||
body = f"""
|
body = f"""
|
||||||
A space launch of interest was updated.
|
A space launch of interest was updated.
|
||||||
|
|
||||||
Get ready for two big piles of JSON.
|
Get ready for two big piles of JSON.
|
||||||
|
|
||||||
Old launch data
|
{json.dumps(differences, indent=2)}
|
||||||
{json.dumps(prev_launch, indent=2)}
|
|
||||||
|
|
||||||
New launch data
|
|
||||||
{json.dumps(cur_launch, indent=2)}
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
agenda.mail.send_mail(config, subject, body)
|
agenda.mail.send_mail(config, subject, body)
|
||||||
|
@ -114,6 +113,7 @@ def update_thespacedevs(config: flask.config.Config) -> None:
|
||||||
rocket_dir = os.path.join(config["DATA_DIR"], "thespacedevs")
|
rocket_dir = os.path.join(config["DATA_DIR"], "thespacedevs")
|
||||||
|
|
||||||
existing_data = agenda.thespacedevs.load_cached_launches(rocket_dir)
|
existing_data = agenda.thespacedevs.load_cached_launches(rocket_dir)
|
||||||
|
assert existing_data
|
||||||
prev_launches = {
|
prev_launches = {
|
||||||
slug: get_launch_by_slug(existing_data, slug)
|
slug: get_launch_by_slug(existing_data, slug)
|
||||||
for slug in config["FOLLOW_LAUNCHES"]
|
for slug in config["FOLLOW_LAUNCHES"]
|
||||||
|
@ -121,6 +121,8 @@ def update_thespacedevs(config: flask.config.Config) -> None:
|
||||||
|
|
||||||
t0 = time()
|
t0 = time()
|
||||||
data = agenda.thespacedevs.next_launch_api_data(rocket_dir)
|
data = agenda.thespacedevs.next_launch_api_data(rocket_dir)
|
||||||
|
if not data:
|
||||||
|
return # thespacedevs API call failed
|
||||||
cur_launches = {
|
cur_launches = {
|
||||||
slug: get_launch_by_slug(data, slug) for slug in config["FOLLOW_LAUNCHES"]
|
slug: get_launch_by_slug(data, slug) for slug in config["FOLLOW_LAUNCHES"]
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue