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