From 656a5ef89747ccda4cc2f05ad5cb4c00466317ee Mon Sep 17 00:00:00 2001 From: Edward Betts Date: Fri, 10 Nov 2023 11:49:04 +0100 Subject: [PATCH 1/3] Remove blank line --- agenda/uk_holiday.py | 1 - 1 file changed, 1 deletion(-) diff --git a/agenda/uk_holiday.py b/agenda/uk_holiday.py index 817dc9d..ae27fe1 100644 --- a/agenda/uk_holiday.py +++ b/agenda/uk_holiday.py @@ -39,7 +39,6 @@ async def bank_holiday_list( def get_mothers_day(input_date: date) -> date: """Calculate the date of the next UK Mother's Day from the current date.""" current_year = input_date.year - easter_date = easter(current_year) # Calculate the date of Mother's Day, which is the fourth Sunday of Lent From f60aab364bf5b2529a38c12bc56a2d1cc88e9561 Mon Sep 17 00:00:00 2001 From: Edward Betts Date: Fri, 10 Nov 2023 11:49:27 +0100 Subject: [PATCH 2/3] Rename us_presidential_election event --- templates/index.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/index.html b/templates/index.html index 77fdedb..0c454eb 100644 --- a/templates/index.html +++ b/templates/index.html @@ -63,7 +63,7 @@ "us_holiday": "US holiday", "uk_clock_change": "UK clock change", "us_clock_change": "US clock change", - "next_us_presidential_election": "US pres. election", + "us_presidential_election": "US pres. election", "xmas_last_second": "Christmas last posting 2nd class", "xmas_last_first": "Christmas last posting 1st class", "up_series": "Up documentary", From 25bb52d88d49563da4e84fa17fb25f8119b21b1d Mon Sep 17 00:00:00 2001 From: Edward Betts Date: Fri, 10 Nov 2023 11:59:53 +0100 Subject: [PATCH 3/3] Catch HTTP timeout and return cached data Closes: #65 --- agenda/thespacedevs.py | 6 ++++-- agenda/waste_schedule.py | 13 ++++++++----- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/agenda/thespacedevs.py b/agenda/thespacedevs.py index 3ea2b23..0523518 100644 --- a/agenda/thespacedevs.py +++ b/agenda/thespacedevs.py @@ -1,3 +1,5 @@ +"""Get details of upcoming space launches.""" + import json import os import typing @@ -118,8 +120,8 @@ async def get_launches(rocket_dir: str, limit: int = 200) -> list[Summary]: if not existing or (now - existing[0][0]).seconds > 3600: # one hour try: return await next_launch_api(rocket_dir, limit=limit) - except ValueError: - print("*** SpaceX next launch error ***") + except httpx.ReadTimeout: + pass f = existing[0][1] diff --git a/agenda/waste_schedule.py b/agenda/waste_schedule.py index 7e5e9de..c013362 100644 --- a/agenda/waste_schedule.py +++ b/agenda/waste_schedule.py @@ -118,16 +118,19 @@ async def get_bristol_data(data_dir: str, uprn: str) -> BristolSchedule: recent = datetime.strptime(recent_filename, f"%Y-%m-%d_%H:%M_{uprn}.json") delta = now - recent - if existing and delta < timedelta(hours=ttl_hours): + def get_from_recent() -> BristolSchedule: json_data = json.load(open(os.path.join(waste_dir, recent_filename))) return typing.cast(BristolSchedule, json_data["data"]) - now_str = now.strftime("%Y-%m-%d_%H:%M") - filename = f"{waste_dir}/{now_str}_{uprn}.json" + if existing and delta < timedelta(hours=ttl_hours): + return get_from_recent() - r = await get_bristol_gov_uk_data(uprn) + try: + r = await get_bristol_gov_uk_data(uprn) + except httpx.ReadTimeout: + return get_from_recent() - with open(filename, "wb") as out: + with open(f'{waste_dir}/{now.strftime("%Y-%m-%d_%H:%M")}_{uprn}.json', "wb") as out: out.write(r.content) return typing.cast(BristolSchedule, r.json()["data"])