Compare commits
1 commit
5c5685da4a
...
91677f56ed
Author | SHA1 | Date | |
---|---|---|---|
91677f56ed |
1 changed files with 13 additions and 10 deletions
23
main.py
23
main.py
|
@ -5,6 +5,7 @@ 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
|
||||||
|
@ -17,7 +18,6 @@ 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,10 +266,8 @@ 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 (0-100)
|
# convert pollen index to percentage scale
|
||||||
pollen_percentage = int(
|
pollen_percentage = int(index_value / POLLEN_MAX_INDEX * 100)
|
||||||
index_value / POLLEN_MAX_INDEX * POLLEN_PERCENTAGE_SCALE
|
|
||||||
)
|
|
||||||
|
|
||||||
valid_periods.append(
|
valid_periods.append(
|
||||||
PollenPeriod(
|
PollenPeriod(
|
||||||
|
@ -313,10 +311,14 @@ class WeatherService:
|
||||||
if cache_key in weather_cache:
|
if cache_key in weather_cache:
|
||||||
return weather_cache[cache_key]
|
return weather_cache[cache_key]
|
||||||
|
|
||||||
url = (
|
base_url = "https://api.openweathermap.org/data/3.0/onecall"
|
||||||
f"https://api.openweathermap.org/data/3.0/onecall"
|
params = {
|
||||||
f"?lat={latitude}&lon={longitude}&appid={api_key}&units=imperial"
|
"lat": latitude,
|
||||||
)
|
"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)
|
||||||
|
@ -393,7 +395,8 @@ class PollenService:
|
||||||
if zip_code in pollen_cache:
|
if zip_code in pollen_cache:
|
||||||
return pollen_cache[zip_code]
|
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 = {
|
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) "
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue