2024-03-05 09:26:54 +00:00
|
|
|
#!/usr/bin/python3
|
|
|
|
"""Combined update script for various data sources."""
|
|
|
|
|
|
|
|
import asyncio
|
|
|
|
import os
|
|
|
|
import sys
|
2024-06-15 21:35:18 +01:00
|
|
|
import typing
|
2024-03-05 09:26:54 +00:00
|
|
|
from datetime import date, datetime
|
2024-09-09 19:16:21 +01:00
|
|
|
from pprint import pformat
|
2024-03-05 09:26:54 +00:00
|
|
|
from time import time
|
|
|
|
|
2024-07-09 08:22:48 +01:00
|
|
|
import deepdiff # type: ignore
|
2024-04-20 07:52:00 +01:00
|
|
|
import flask
|
2024-03-05 09:26:54 +00:00
|
|
|
import requests
|
|
|
|
|
2024-07-14 15:19:13 +01:00
|
|
|
import agenda.bristol_waste
|
2024-04-18 22:26:04 +01:00
|
|
|
import agenda.fx
|
2024-07-08 11:48:56 +01:00
|
|
|
import agenda.geomob
|
2024-07-08 10:44:39 +01:00
|
|
|
import agenda.gwr
|
|
|
|
import agenda.mail
|
2024-03-05 09:26:54 +00:00
|
|
|
import agenda.thespacedevs
|
|
|
|
import agenda.types
|
|
|
|
import agenda.uk_holiday
|
2024-06-15 21:35:18 +01:00
|
|
|
from agenda.types import StrDict
|
2024-04-20 07:52:00 +01:00
|
|
|
from web_view import app
|
2024-03-05 09:26:54 +00:00
|
|
|
|
|
|
|
|
2024-04-20 07:52:00 +01:00
|
|
|
async def update_bank_holidays(config: flask.config.Config) -> None:
|
2024-03-05 09:26:54 +00:00
|
|
|
"""Update cached copy of UK Bank holidays."""
|
|
|
|
t0 = time()
|
2024-04-20 07:52:00 +01:00
|
|
|
events = await agenda.uk_holiday.get_holiday_list(config["DATA_DIR"])
|
2024-03-05 09:26:54 +00:00
|
|
|
time_taken = time() - t0
|
|
|
|
if not sys.stdin.isatty():
|
|
|
|
return
|
|
|
|
print(len(events), "bank holidays in list")
|
|
|
|
print(f"took {time_taken:.1f} seconds")
|
|
|
|
|
|
|
|
|
2024-04-20 07:52:00 +01:00
|
|
|
async def update_bristol_bins(config: flask.config.Config) -> None:
|
2024-03-05 09:26:54 +00:00
|
|
|
"""Update waste schedule from Bristol City Council."""
|
|
|
|
t0 = time()
|
2024-07-14 16:10:00 +01:00
|
|
|
events = await agenda.bristol_waste.get(
|
2024-04-20 07:52:00 +01:00
|
|
|
date.today(), config["DATA_DIR"], config["BRISTOL_UPRN"], refresh=True
|
2024-03-05 09:26:54 +00:00
|
|
|
)
|
|
|
|
time_taken = time() - t0
|
|
|
|
if not sys.stdin.isatty():
|
|
|
|
return
|
|
|
|
for event in events:
|
|
|
|
print(event)
|
|
|
|
print(f"took {time_taken:.1f} seconds")
|
|
|
|
|
|
|
|
|
2024-04-20 07:52:00 +01:00
|
|
|
def update_gwr_advance_ticket_date(config: flask.config.Config) -> None:
|
2024-03-05 09:26:54 +00:00
|
|
|
"""Update GWR advance ticket date cache."""
|
2024-04-20 07:52:00 +01:00
|
|
|
filename = os.path.join(config["DATA_DIR"], "advance-tickets.html")
|
2024-03-05 09:26:54 +00:00
|
|
|
existing_html = open(filename).read()
|
2024-09-23 09:22:20 +01:00
|
|
|
|
|
|
|
existing_dates = agenda.gwr.extract_dates(existing_html)
|
|
|
|
assert existing_dates
|
|
|
|
assert list(existing_dates.keys()) == ["Weekdays", "Saturdays", "Sundays"]
|
2024-03-05 09:26:54 +00:00
|
|
|
|
2024-07-08 10:44:39 +01:00
|
|
|
new_html = requests.get(agenda.gwr.url).text
|
2024-03-05 09:26:54 +00:00
|
|
|
|
2024-09-23 09:22:20 +01:00
|
|
|
new_dates = agenda.gwr.extract_dates(new_html)
|
|
|
|
assert new_dates
|
|
|
|
assert list(new_dates.keys()) == ["Weekdays", "Saturdays", "Sundays"]
|
2024-03-05 09:26:54 +00:00
|
|
|
|
2024-09-23 09:22:20 +01:00
|
|
|
if existing_dates == new_dates:
|
2024-03-05 09:26:54 +00:00
|
|
|
if sys.stdin.isatty():
|
2024-09-23 09:22:20 +01:00
|
|
|
print(filename)
|
|
|
|
print(agenda.gwr.url)
|
|
|
|
print("dates haven't changed:", existing_dates)
|
2024-03-05 09:26:54 +00:00
|
|
|
return
|
|
|
|
|
2024-09-23 09:22:20 +01:00
|
|
|
open(filename, "w").write(new_html)
|
|
|
|
|
|
|
|
subject = (
|
|
|
|
"New GWR advance ticket booking date: "
|
|
|
|
+ f'{new_dates["Weekdays"].strftime("%d %b %Y")} (Weekdays)'
|
|
|
|
)
|
|
|
|
body = f"""
|
|
|
|
{"\n".join(f'{key}: {when.strftime("%d %b %Y")}' for key, when in new_dates.items())}
|
2024-03-05 09:26:54 +00:00
|
|
|
|
2024-07-08 10:44:39 +01:00
|
|
|
{agenda.gwr.url}
|
2024-03-05 09:26:54 +00:00
|
|
|
|
|
|
|
Agenda: https://edwardbetts.com/agenda/
|
|
|
|
"""
|
2024-09-23 09:22:20 +01:00
|
|
|
|
|
|
|
if sys.stdin.isatty():
|
|
|
|
print(filename)
|
|
|
|
print(agenda.gwr.url)
|
|
|
|
print()
|
|
|
|
print("dates have changed")
|
|
|
|
print("old:", existing_dates)
|
|
|
|
print("new:", new_dates)
|
|
|
|
print()
|
|
|
|
print(subject)
|
|
|
|
print(body)
|
|
|
|
|
2024-07-08 10:44:39 +01:00
|
|
|
agenda.mail.send_mail(config, subject, body)
|
2024-03-05 09:26:54 +00:00
|
|
|
|
|
|
|
|
2024-06-15 21:35:18 +01:00
|
|
|
def report_space_launch_change(
|
2024-06-26 08:48:58 +01:00
|
|
|
config: flask.config.Config, prev_launch: StrDict | None, cur_launch: StrDict | None
|
2024-06-15 21:35:18 +01:00
|
|
|
) -> None:
|
|
|
|
"""Send mail to announce change to space launch data."""
|
2024-06-26 08:48:58 +01:00
|
|
|
if cur_launch:
|
|
|
|
name = cur_launch["name"]
|
|
|
|
else:
|
|
|
|
assert prev_launch
|
|
|
|
name = prev_launch["name"]
|
|
|
|
subject = f"Change to {name}"
|
2024-06-15 21:35:18 +01:00
|
|
|
|
2024-07-09 08:22:48 +01:00
|
|
|
differences = deepdiff.DeepDiff(prev_launch, cur_launch)
|
|
|
|
|
2024-06-15 21:35:18 +01:00
|
|
|
body = f"""
|
|
|
|
A space launch of interest was updated.
|
|
|
|
|
2024-09-09 19:16:21 +01:00
|
|
|
{pformat(differences)}
|
2024-06-15 21:35:18 +01:00
|
|
|
"""
|
|
|
|
|
2024-07-08 10:44:39 +01:00
|
|
|
agenda.mail.send_mail(config, subject, body)
|
2024-06-15 21:35:18 +01:00
|
|
|
|
|
|
|
|
2024-06-26 08:48:58 +01:00
|
|
|
def get_launch_by_slug(data: StrDict, slug: str) -> StrDict | None:
|
2024-06-15 21:35:18 +01:00
|
|
|
"""Find last update for space launch."""
|
2024-06-26 08:48:58 +01:00
|
|
|
return {item["slug"]: typing.cast(StrDict, item) for item in data["results"]}.get(
|
|
|
|
slug
|
|
|
|
)
|
2024-06-15 21:35:18 +01:00
|
|
|
|
|
|
|
|
2024-04-20 07:52:00 +01:00
|
|
|
def update_thespacedevs(config: flask.config.Config) -> None:
|
2024-03-05 09:26:54 +00:00
|
|
|
"""Update cache of space launch API."""
|
2024-04-20 07:52:00 +01:00
|
|
|
rocket_dir = os.path.join(config["DATA_DIR"], "thespacedevs")
|
2024-03-05 09:26:54 +00:00
|
|
|
|
2024-06-15 21:35:18 +01:00
|
|
|
existing_data = agenda.thespacedevs.load_cached_launches(rocket_dir)
|
2024-07-09 08:22:33 +01:00
|
|
|
assert existing_data
|
2024-06-15 21:35:18 +01:00
|
|
|
prev_launches = {
|
|
|
|
slug: get_launch_by_slug(existing_data, slug)
|
|
|
|
for slug in config["FOLLOW_LAUNCHES"]
|
|
|
|
}
|
|
|
|
|
2024-03-05 09:26:54 +00:00
|
|
|
t0 = time()
|
2024-06-15 21:35:18 +01:00
|
|
|
data = agenda.thespacedevs.next_launch_api_data(rocket_dir)
|
2024-07-09 08:22:33 +01:00
|
|
|
if not data:
|
|
|
|
return # thespacedevs API call failed
|
2024-06-15 21:35:18 +01:00
|
|
|
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]
|
2024-06-26 08:48:58 +01:00
|
|
|
if prev is None and cur is None:
|
|
|
|
continue
|
|
|
|
if prev and cur and prev["last_updated"] == cur["last_updated"]:
|
|
|
|
continue
|
|
|
|
report_space_launch_change(config, prev, cur)
|
2024-06-15 21:35:18 +01:00
|
|
|
|
2024-03-05 09:26:54 +00:00
|
|
|
time_taken = time() - t0
|
|
|
|
if not sys.stdin.isatty():
|
|
|
|
return
|
2024-06-15 21:35:18 +01:00
|
|
|
rockets = [agenda.thespacedevs.summarize_launch(item) for item in data["results"]]
|
2024-03-05 09:26:54 +00:00
|
|
|
print(len(rockets), "launches")
|
|
|
|
print(f"took {time_taken:.1f} seconds")
|
|
|
|
|
|
|
|
|
2024-04-20 07:52:00 +01:00
|
|
|
def update_gandi(config: flask.config.Config) -> None:
|
2024-03-27 17:47:25 +00:00
|
|
|
"""Retrieve list of domains from gandi.net."""
|
|
|
|
url = "https://api.gandi.net/v5/domain/domains"
|
2024-04-20 07:52:00 +01:00
|
|
|
headers = {"authorization": "Bearer " + config["GANDI_TOKEN"]}
|
|
|
|
filename = os.path.join(config["DATA_DIR"], "gandi_domains.json")
|
2024-03-27 17:47:25 +00:00
|
|
|
|
|
|
|
r = requests.request("GET", url, headers=headers)
|
|
|
|
items = r.json()
|
|
|
|
assert isinstance(items, list)
|
|
|
|
assert all(item["fqdn"] and item["dates"]["registry_ends_at"] for item in items)
|
|
|
|
with open(filename, "w") as out:
|
|
|
|
out.write(r.text)
|
|
|
|
|
|
|
|
|
2024-03-05 09:26:54 +00:00
|
|
|
def main() -> None:
|
|
|
|
"""Update caches."""
|
|
|
|
now = datetime.now()
|
|
|
|
hour = now.hour
|
|
|
|
|
2024-04-20 07:52:00 +01:00
|
|
|
with app.app_context():
|
|
|
|
if hour % 3 == 0:
|
|
|
|
asyncio.run(update_bank_holidays(app.config))
|
|
|
|
asyncio.run(update_bristol_bins(app.config))
|
|
|
|
update_gwr_advance_ticket_date(app.config)
|
|
|
|
update_gandi(app.config)
|
2024-07-08 11:48:56 +01:00
|
|
|
agenda.geomob.update(app.config)
|
2024-03-05 09:26:54 +00:00
|
|
|
|
2024-09-09 19:16:21 +01:00
|
|
|
agenda.fx.get_rates(app.config)
|
2024-04-18 22:26:04 +01:00
|
|
|
|
2024-04-20 07:52:00 +01:00
|
|
|
update_thespacedevs(app.config)
|
2024-03-05 09:26:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|