From 22adb0eeb02e0e69e70ec33b6a61a77f70fb51eb Mon Sep 17 00:00:00 2001 From: Edward Betts Date: Sat, 7 Oct 2023 09:12:27 +0100 Subject: [PATCH] Show more upcoming holidays in the UK and US Closes: #18 --- agenda/__init__.py | 35 +++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/agenda/__init__.py b/agenda/__init__.py index c7e9850..f503a6c 100644 --- a/agenda/__init__.py +++ b/agenda/__init__.py @@ -112,7 +112,7 @@ 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) -> Event: +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") @@ -121,18 +121,19 @@ def get_next_bank_holiday(input_date: date) -> Event: r = requests.get(url) open(filename, "w").write(r.text) + year_later = input_date + timedelta(days=365) + events = json.load(open(filename))["england-and-wales"]["events"] - next_holiday = None + hols: list[Event] = [] for event in events: event_date = datetime.strptime(event["date"], "%Y-%m-%d").date() if event_date < input_date: continue - next_holiday = Event(name="bank_holiay", date=event_date, title=event["title"]) - break + if event_date > year_later: + break + hols.append(Event(name="bank_holiday", date=event_date, title=event["title"])) - assert next_holiday - - return next_holiday + return hols def get_gbpusd(now: datetime) -> Decimal: @@ -267,12 +268,14 @@ def stock_markets() -> list[str]: return reply -def get_us_holiday(input_date: date) -> Event: +def get_us_holidays(input_date: date) -> list[Event]: """Date and name of next US holiday.""" hols = holidays.UnitedStates(years=[input_date.year, input_date.year + 1]) - next_hol = next(x for x in sorted(hols.items()) if x[0] >= input_date) - - return Event(name="us_holiday", date=next_hol[0], title=next_hol[1]) + return [ + Event(name="us_holiday", date=hol_date, title=title) + for hol_date, title in sorted(hols.items()) + if hol_date >= input_date + ] def next_birthday(from_date: date, birth_date: date) -> tuple[date, int]: @@ -315,7 +318,7 @@ def get_data(now: datetime) -> dict[str, str | object]: "gbpusd": get_gbpusd(now), "next_economist": next_economist(today), "bank_holiday": get_next_bank_holiday(today), - "us_holiday": get_us_holiday(today), + "us_holiday": get_us_holidays(today), # "next_uk_general_election": next_uk_general_election, "next_us_presidential_election": next_us_presidential_election, "stock_markets": stock_markets(), @@ -325,7 +328,7 @@ def get_data(now: datetime) -> dict[str, str | object]: "fathers_day": next_uk_fathers_day(today), "uk_financial_year_end": uk_financial_year_end(today), "xmas_last_posting_dates": xmas_last_posting_dates, - "xmas_day": xmas_day(today), + # "xmas_day": xmas_day(today), "rockets": thespacedevs.get_launches(rocket_dir, limit=40), } @@ -335,12 +338,12 @@ def get_data(now: datetime) -> dict[str, str | object]: if key in skip: continue if "holiday" in key: - assert isinstance(value, Event) - event = value + assert isinstance(value, list) + events += value else: assert isinstance(value, date) event = Event(name=key, date=value) - events.append(event) + events.append(event) for key, value in xmas_last_posting_dates.items(): events.append(Event(name=f"xmas_last_{key}", date=value))