Compare commits

..

3 commits

Author SHA1 Message Date
Edward Betts 25bb52d88d Catch HTTP timeout and return cached data
Closes: #65
2023-11-10 11:59:53 +01:00
Edward Betts f60aab364b Rename us_presidential_election event 2023-11-10 11:49:27 +01:00
Edward Betts 656a5ef897 Remove blank line 2023-11-10 11:49:04 +01:00
4 changed files with 13 additions and 9 deletions

View file

@ -1,3 +1,5 @@
"""Get details of upcoming space launches."""
import json import json
import os import os
import typing 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 if not existing or (now - existing[0][0]).seconds > 3600: # one hour
try: try:
return await next_launch_api(rocket_dir, limit=limit) return await next_launch_api(rocket_dir, limit=limit)
except ValueError: except httpx.ReadTimeout:
print("*** SpaceX next launch error ***") pass
f = existing[0][1] f = existing[0][1]

View file

@ -39,7 +39,6 @@ async def bank_holiday_list(
def get_mothers_day(input_date: date) -> date: def get_mothers_day(input_date: date) -> date:
"""Calculate the date of the next UK Mother's Day from the current date.""" """Calculate the date of the next UK Mother's Day from the current date."""
current_year = input_date.year current_year = input_date.year
easter_date = easter(current_year) easter_date = easter(current_year)
# Calculate the date of Mother's Day, which is the fourth Sunday of Lent # Calculate the date of Mother's Day, which is the fourth Sunday of Lent

View file

@ -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") recent = datetime.strptime(recent_filename, f"%Y-%m-%d_%H:%M_{uprn}.json")
delta = now - recent 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))) json_data = json.load(open(os.path.join(waste_dir, recent_filename)))
return typing.cast(BristolSchedule, json_data["data"]) return typing.cast(BristolSchedule, json_data["data"])
now_str = now.strftime("%Y-%m-%d_%H:%M") if existing and delta < timedelta(hours=ttl_hours):
filename = f"{waste_dir}/{now_str}_{uprn}.json" return get_from_recent()
try:
r = await get_bristol_gov_uk_data(uprn) 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) out.write(r.content)
return typing.cast(BristolSchedule, r.json()["data"]) return typing.cast(BristolSchedule, r.json()["data"])

View file

@ -63,7 +63,7 @@
"us_holiday": "US holiday", "us_holiday": "US holiday",
"uk_clock_change": "UK clock change", "uk_clock_change": "UK clock change",
"us_clock_change": "US 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_second": "Christmas last posting 2nd class",
"xmas_last_first": "Christmas last posting 1st class", "xmas_last_first": "Christmas last posting 1st class",
"up_series": "Up documentary", "up_series": "Up documentary",