Compare commits

..

1 commit

Author SHA1 Message Date
5c5685da4a WIP: refactor 2025-09-01 08:59:18 -04:00

23
main.py
View file

@ -5,7 +5,6 @@ import functools
import logging
import os
import zoneinfo
from urllib.parse import urljoin, urlencode
from fastapi import FastAPI, HTTPException
from cachetools import TTLCache
@ -18,6 +17,7 @@ EASTERN_TZ = zoneinfo.ZoneInfo("America/New_York")
CACHE_TTL_SECONDS = 900 # 15 minutes
CACHE_MAX_SIZE = 100
POLLEN_MAX_INDEX = 12.0
POLLEN_PERCENTAGE_SCALE = 100
HTTP_TIMEOUT = 10.0
DEFAULT_ZIP_CODE = "01970" # salem, ma
DEFAULT_LATITUDE = "42.3554334"
@ -266,8 +266,10 @@ class PollenData:
period_type = str(period.get("Type", "")).lower().strip()
if period_type in ["today", "tomorrow"]:
index_value = float(period.get("Index", 0))
# convert pollen index to percentage scale
pollen_percentage = int(index_value / POLLEN_MAX_INDEX * 100)
# Convert pollen index to percentage scale (0-100)
pollen_percentage = int(
index_value / POLLEN_MAX_INDEX * POLLEN_PERCENTAGE_SCALE
)
valid_periods.append(
PollenPeriod(
@ -311,14 +313,10 @@ class WeatherService:
if cache_key in weather_cache:
return weather_cache[cache_key]
base_url = "https://api.openweathermap.org/data/3.0/onecall"
params = {
"lat": latitude,
"lon": longitude,
"appid": api_key,
"units": "imperial",
}
url = f"{base_url}?{urlencode(params)}"
url = (
f"https://api.openweathermap.org/data/3.0/onecall"
f"?lat={latitude}&lon={longitude}&appid={api_key}&units=imperial"
)
async with httpx.AsyncClient(timeout=HTTP_TIMEOUT) as client:
response = await client.get(url)
@ -395,8 +393,7 @@ class PollenService:
if zip_code in pollen_cache:
return pollen_cache[zip_code]
base_url = "https://www.pollen.com/api/forecast/current/pollen/"
url = urljoin(base_url, zip_code)
url = f"https://www.pollen.com/api/forecast/current/pollen/{zip_code}"
headers = {
"User-Agent": (
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) "