parent
ce07ae65b1
commit
e400360697
2 changed files with 63 additions and 5 deletions
47
update.py
47
update.py
|
|
@ -2,9 +2,11 @@
|
|||
"""Combined update script for various data sources."""
|
||||
|
||||
import asyncio
|
||||
import json
|
||||
import os
|
||||
import smtplib
|
||||
import sys
|
||||
import typing
|
||||
from datetime import date, datetime
|
||||
from email.message import EmailMessage
|
||||
from email.utils import formatdate, make_msgid
|
||||
|
|
@ -19,6 +21,7 @@ import agenda.types
|
|||
import agenda.uk_holiday
|
||||
import agenda.waste_schedule
|
||||
from agenda import gwr
|
||||
from agenda.types import StrDict
|
||||
from web_view import app
|
||||
|
||||
|
||||
|
|
@ -95,15 +98,57 @@ Agenda: https://edwardbetts.com/agenda/
|
|||
send_mail(config, subject, body)
|
||||
|
||||
|
||||
def report_space_launch_change(
|
||||
config: flask.config.Config, prev_launch: StrDict, cur_launch: StrDict
|
||||
) -> None:
|
||||
"""Send mail to announce change to space launch data."""
|
||||
subject = f'Change to {cur_launch["name"]}'
|
||||
|
||||
body = f"""
|
||||
A space launch of interest was updated.
|
||||
|
||||
Get ready for two big piles of JSON.
|
||||
|
||||
Old launch data
|
||||
{json.dumps(prev_launch, indent=2)}
|
||||
|
||||
New launch data
|
||||
{json.dumps(cur_launch, indent=2)}
|
||||
"""
|
||||
|
||||
send_mail(config, subject, body)
|
||||
|
||||
|
||||
def get_launch_by_slug(data: StrDict, slug: str) -> StrDict:
|
||||
"""Find last update for space launch."""
|
||||
return {item["slug"]: typing.cast(StrDict, item) for item in data["results"]}[slug]
|
||||
|
||||
|
||||
def update_thespacedevs(config: flask.config.Config) -> None:
|
||||
"""Update cache of space launch API."""
|
||||
rocket_dir = os.path.join(config["DATA_DIR"], "thespacedevs")
|
||||
|
||||
existing_data = agenda.thespacedevs.load_cached_launches(rocket_dir)
|
||||
prev_launches = {
|
||||
slug: get_launch_by_slug(existing_data, slug)
|
||||
for slug in config["FOLLOW_LAUNCHES"]
|
||||
}
|
||||
|
||||
t0 = time()
|
||||
rockets = agenda.thespacedevs.next_launch_api(rocket_dir)
|
||||
data = agenda.thespacedevs.next_launch_api_data(rocket_dir)
|
||||
cur_launches = {
|
||||
slug: get_launch_by_slug(data, slug) for slug in config["FOLLOW_LAUNCHES"]
|
||||
}
|
||||
|
||||
for slug in config["FOLLOW_LAUNCHES"]:
|
||||
prev, cur = prev_launches[slug], cur_launches[slug]
|
||||
if prev["last_updated"] != cur["last_updated"]:
|
||||
report_space_launch_change(config, prev, cur)
|
||||
|
||||
time_taken = time() - t0
|
||||
if not sys.stdin.isatty():
|
||||
return
|
||||
rockets = [agenda.thespacedevs.summarize_launch(item) for item in data["results"]]
|
||||
print(len(rockets), "launches")
|
||||
print(f"took {time_taken:.1f} seconds")
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue