diff --git a/main.py b/main.py index 6ebe104..4427772 100644 --- a/main.py +++ b/main.py @@ -5,6 +5,7 @@ import functools import logging import os import zoneinfo +from urllib.parse import urljoin, urlencode from fastapi import FastAPI, HTTPException from cachetools import TTLCache @@ -17,7 +18,6 @@ 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,10 +266,8 @@ 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 (0-100) - pollen_percentage = int( - index_value / POLLEN_MAX_INDEX * POLLEN_PERCENTAGE_SCALE - ) + # convert pollen index to percentage scale + pollen_percentage = int(index_value / POLLEN_MAX_INDEX * 100) valid_periods.append( PollenPeriod( @@ -313,10 +311,14 @@ class WeatherService: if cache_key in weather_cache: return weather_cache[cache_key] - url = ( - f"https://api.openweathermap.org/data/3.0/onecall" - f"?lat={latitude}&lon={longitude}&appid={api_key}&units=imperial" - ) + 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)}" async with httpx.AsyncClient(timeout=HTTP_TIMEOUT) as client: response = await client.get(url) @@ -393,7 +395,8 @@ class PollenService: if zip_code in pollen_cache: return pollen_cache[zip_code] - url = f"https://www.pollen.com/api/forecast/current/pollen/{zip_code}" + base_url = "https://www.pollen.com/api/forecast/current/pollen/" + url = urljoin(base_url, zip_code) headers = { "User-Agent": ( "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) "