Code to run from cron to update bank holiday list

This commit is contained in:
Edward Betts 2024-01-16 11:12:43 +00:00
parent 3cb03a787c
commit 8df94aaafb
2 changed files with 40 additions and 5 deletions

View file

@ -10,12 +10,24 @@ from dateutil.easter import easter
from .types import Holiday, StrDict
url = "https://www.gov.uk/bank-holidays.json"
async def get_holiday_list(data_dir: str) -> list[StrDict]:
"""Download holiday list and save cache."""
filename = os.path.join(data_dir, "bank-holidays.json")
async with httpx.AsyncClient() as client:
r = await client.get(url)
events: list[StrDict] = r.json()["england-and-wales"]["events"] # check valid
open(filename, "w").write(r.text)
return events
async def bank_holiday_list(
start_date: date, end_date: date, data_dir: str
) -> list[Holiday]:
"""Date and name of the next UK bank holiday."""
url = "https://www.gov.uk/bank-holidays.json"
filename = os.path.join(data_dir, "bank-holidays.json")
use_cached = False
events: list[StrDict]
@ -29,10 +41,7 @@ async def bank_holiday_list(
use_cached = False
if not use_cached:
async with httpx.AsyncClient() as client:
r = await client.get(url)
open(filename, "w").write(r.text)
events = json.load(open(filename))["england-and-wales"]["events"]
events = await get_holiday_list(data_dir)
hols: list[Holiday] = []
for event in events:

26
update_bank_holiday_list.py Executable file
View file

@ -0,0 +1,26 @@
#!/usr/bin/python3
"""Update cached copy of UK Bank holidays."""
import asyncio
import sys
from time import time
import agenda.types
import agenda.uk_holiday
from agenda.types import StrDict
config = __import__("config.default", fromlist=[""])
async def get_bank_holidays() -> list[StrDict]:
"""Call space launch API and cache results."""
return await agenda.uk_holiday.get_holiday_list(config.DATA_DIR)
t0 = time()
events: list[StrDict] = asyncio.run(get_bank_holidays())
time_taken = time() - t0
if not sys.stdin.isatty():
sys.exit(0)
print(len(events), "bank holidays in list")
print(f"took {time_taken:.1f} seconds")