Compare commits

..

1 commit

Author SHA1 Message Date
bb25e7bbee WIP: refactor 2025-09-01 08:58:26 -04:00

50
main.py
View file

@ -20,6 +20,19 @@ POLLEN_MAX_INDEX = 12.0
POLLEN_PERCENTAGE_SCALE = 100 POLLEN_PERCENTAGE_SCALE = 100
HTTP_TIMEOUT = 10.0 HTTP_TIMEOUT = 10.0
# Location constants
DEFAULT_ZIP_CODE = "01970" # Salem, MA
DEFAULT_LATITUDE = "42.3554334"
DEFAULT_LONGITUDE = "-71.060511"
def get_required_env(key: str) -> str:
"""Get required environment variable or raise error."""
value = os.environ.get(key)
if not value:
raise ValueError(f"Required environment variable {key} is not set")
return value
class WeatherCondition(TypedDict): class WeatherCondition(TypedDict):
description: str description: str
@ -127,7 +140,7 @@ class FinalReport(TypedDict):
tomorrow: DailyData tomorrow: DailyData
app = FastAPI(title="TRMNL Weather & Pollen Report") app = FastAPI(title="trmnl weather & pollen report")
weather_cache: TTLCache = TTLCache(maxsize=CACHE_MAX_SIZE, ttl=CACHE_TTL_SECONDS) weather_cache: TTLCache = TTLCache(maxsize=CACHE_MAX_SIZE, ttl=CACHE_TTL_SECONDS)
pollen_cache: TTLCache = TTLCache(maxsize=CACHE_MAX_SIZE, ttl=CACHE_TTL_SECONDS) pollen_cache: TTLCache = TTLCache(maxsize=CACHE_MAX_SIZE, ttl=CACHE_TTL_SECONDS)
@ -136,23 +149,28 @@ pollen_cache: TTLCache = TTLCache(maxsize=CACHE_MAX_SIZE, ttl=CACHE_TTL_SECONDS)
class Config: class Config:
"""Application configuration.""" """Application configuration."""
def __init__(self): def __init__(
self.zip_code = "01970" # Salem, MA self,
self.latitude = "42.3554334" zip_code: str,
self.longitude = "-71.060511" latitude: str,
self.weather_api_key = self._get_required_env("WEATHER_API_KEY") longitude: str,
self.auth_token = self._get_required_env("AUTH_TOKEN") weather_api_key: str,
auth_token: str,
@staticmethod ):
def _get_required_env(key: str) -> str: self.zip_code = zip_code
"""Get required environment variable or raise error.""" self.latitude = latitude
value = os.environ.get(key) self.longitude = longitude
if not value: self.weather_api_key = weather_api_key
raise ValueError(f"Required environment variable {key} is not set") self.auth_token = auth_token
return value
config = Config() config = Config(
zip_code=DEFAULT_ZIP_CODE,
latitude=DEFAULT_LATITUDE,
longitude=DEFAULT_LONGITUDE,
weather_api_key=get_required_env("WEATHER_API_KEY"),
auth_token=get_required_env("AUTH_TOKEN"),
)
class DateTimeFormatter: class DateTimeFormatter: