misc linting

This commit is contained in:
Matthew Ryan Dillon 2025-08-30 11:40:56 -04:00
parent b832ee42a6
commit 2a3538ac9e

21
main.py
View file

@ -17,7 +17,6 @@ eastern = zoneinfo.ZoneInfo("America/New_York")
app = FastAPI()
# Initialize caches for 15 minutes
weather_cache = TTLCache(maxsize=100, ttl=900) # 15 minutes
pollen_cache = TTLCache(maxsize=100, ttl=900) # 15 minutes
@ -93,10 +92,9 @@ def fallback_handler(func):
@fallback_handler
async def fetch_pollen(zipcode):
# Check the cache first
if zipcode in pollen_cache:
return pollen_cache[zipcode]
url = f"https://www.pollen.com/api/forecast/current/pollen/{zipcode}"
headers = {
"User-Agent": (
@ -127,14 +125,12 @@ async def fetch_pollen(zipcode):
],
}
]
# Cache the result
pollen_cache[zipcode] = result
return result
@fallback_handler
async def fetch_weather(lat, lon, weather_api_key):
# Check the cache first
cache_key = (lat, lon)
if cache_key in weather_cache:
return weather_cache[cache_key]
@ -170,7 +166,6 @@ async def fetch_weather(lat, lon, weather_api_key):
],
}
]
# Cache the result
weather_cache[cache_key] = result
return result
@ -190,7 +185,7 @@ async def read_root(token: str):
pollen_periods = {p["period"]: p for p in pollen[0]["periods"]} if pollen and pollen[0]["periods"] else {}
weather_periods = {p["period"]: p for p in weather[0]["periods"]} if weather and weather[0]["periods"] else {}
# Add current weather data as a "current" period
if weather and weather[0]:
data = weather[0]
@ -204,17 +199,17 @@ async def read_root(token: str):
"sunrise": data["sunrise"],
"sunset": data["sunset"]
}
today_date = format_date(datetime.now())
tomorrow_date = format_date(datetime.now() + timedelta(days=1))
result = {
"fetched_at": format_datetime(datetime.now()),
"current": {},
"today": {},
"tomorrow": {},
}
if "current" in weather_periods:
weather_data = weather_periods["current"]
result["current"] = {
@ -224,13 +219,13 @@ async def read_root(token: str):
"humidity": weather_data["humidity"],
"pressure": weather_data["pressure"]
}
today_data = build_daily_data(today_date, pollen_periods, weather_periods)
if today_data:
result["today"] = today_data
tomorrow_data = build_daily_data(tomorrow_date, pollen_periods, weather_periods)
if tomorrow_data:
result["tomorrow"] = tomorrow_data
return result