Make get_next_bank_holiday async

This commit is contained in:
Edward Betts 2023-11-05 14:44:00 +00:00
parent 144e8337b8
commit 35ec8008a8

View file

@ -13,10 +13,10 @@ import dateutil.parser
import dateutil.tz
import exchange_calendars
import holidays
import httpx
import lxml
import pandas
import pytz
import requests
import yaml
from dateutil.easter import easter
from dateutil.relativedelta import FR, relativedelta
@ -116,13 +116,14 @@ def get_next_timezone_transition(from_dt: datetime, tz_name: str) -> date:
return typing.cast(date, dt.date())
def get_next_bank_holiday(input_date: date) -> list[Event]:
async def get_next_bank_holiday(input_date: date) -> list[Event]:
"""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")
mtime = os.path.getmtime(filename)
if (unixtime() - mtime) > 3600: # one hour
r = requests.get(url)
async with httpx.AsyncClient() as client:
r = await client.get(url)
open(filename, "w").write(r.text)
year_later = input_date + timedelta(days=365)
@ -296,16 +297,21 @@ async def get_data(now: datetime) -> typing.Mapping[str, str | object]:
last_week = today - timedelta(weeks=1)
last_year = today - timedelta(days=365)
gbpusd, gwr_advance_tickets = await asyncio.gather(
(
gbpusd,
gwr_advance_tickets,
bank_holiday,
) = await asyncio.gather(
fx.get_gbpusd(config),
gwr.advance_ticket_date(data_dir),
get_next_bank_holiday(today),
)
reply = {
"now": now,
"gbpusd": gbpusd,
"next_economist": economist.next_pub_date(today),
"bank_holiday": get_next_bank_holiday(today),
"bank_holiday": bank_holiday,
"us_holiday": get_us_holidays(today),
"next_us_presidential_election": next_us_presidential_election,
"stock_markets": stock_markets(),