Compare commits
1 commit
a9fec56176
...
be51eb920e
Author | SHA1 | Date | |
---|---|---|---|
be51eb920e |
1 changed files with 31 additions and 22 deletions
53
main.py
53
main.py
|
@ -1,15 +1,14 @@
|
|||
import asyncio
|
||||
from datetime import datetime, timedelta
|
||||
import functools
|
||||
import logging
|
||||
import os
|
||||
import pprint
|
||||
import zoneinfo
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
from fastapi import FastAPI, HTTPException
|
||||
from cachetools import TTLCache
|
||||
import httpx
|
||||
|
||||
from cachetools import TTLCache
|
||||
from fastapi import FastAPI, HTTPException
|
||||
|
||||
pp = pprint.PrettyPrinter()
|
||||
logger = logging.getLogger("uvicorn.error")
|
||||
|
@ -65,15 +64,17 @@ def build_daily_data(date, pollen_periods, weather_periods):
|
|||
daily_data["pollen"] = pollen_periods[date]["index"]
|
||||
if date in weather_periods:
|
||||
weather_data = weather_periods[date]
|
||||
daily_data.update({
|
||||
"low": weather_data["low"],
|
||||
"high": weather_data["high"],
|
||||
"desc": weather_data["desc"],
|
||||
"humidity": weather_data["humidity"],
|
||||
"sunrise": weather_data["sunrise"],
|
||||
"sunset": weather_data["sunset"],
|
||||
"pressure": weather_data["pressure"],
|
||||
})
|
||||
daily_data.update(
|
||||
{
|
||||
"low": weather_data["low"],
|
||||
"high": weather_data["high"],
|
||||
"desc": weather_data["desc"],
|
||||
"humidity": weather_data["humidity"],
|
||||
"sunrise": weather_data["sunrise"],
|
||||
"sunset": weather_data["sunset"],
|
||||
"pressure": weather_data["pressure"],
|
||||
}
|
||||
)
|
||||
return daily_data
|
||||
|
||||
|
||||
|
@ -117,7 +118,7 @@ async def fetch_pollen(zipcode):
|
|||
),
|
||||
"periods": [
|
||||
{
|
||||
"index": int(d["Index"] / 12. * 100),
|
||||
"index": int(d["Index"] / 12.0 * 100),
|
||||
"period": relative_day_to_date(d["Type"]),
|
||||
}
|
||||
for d in data["Location"]["periods"]
|
||||
|
@ -144,8 +145,8 @@ async def fetch_weather(lat, lon, weather_api_key):
|
|||
result = [
|
||||
{
|
||||
"forecast_date": format_datetime(datetime.fromtimestamp(current["dt"])),
|
||||
"current_temp": int(round(current["temp"])),
|
||||
"current_feels_like": int(round(current["feels_like"])),
|
||||
"current_temp": round(current["temp"]),
|
||||
"current_feels_like": round(current["feels_like"]),
|
||||
"current_humidity": current["humidity"],
|
||||
"sunrise": format_time(datetime.fromtimestamp(current["sunrise"])),
|
||||
"sunset": format_time(datetime.fromtimestamp(current["sunset"])),
|
||||
|
@ -153,8 +154,8 @@ async def fetch_weather(lat, lon, weather_api_key):
|
|||
"current_desc": current["weather"][0]["description"],
|
||||
"periods": [
|
||||
{
|
||||
"low": int(round(p["temp"]["min"])),
|
||||
"high": int(round(p["temp"]["max"])),
|
||||
"low": round(p["temp"]["min"]),
|
||||
"high": round(p["temp"]["max"]),
|
||||
"desc": p["weather"][0]["description"],
|
||||
"humidity": p["humidity"],
|
||||
"sunrise": format_time(datetime.fromtimestamp(p["sunrise"])),
|
||||
|
@ -183,8 +184,16 @@ async def read_root(token: str):
|
|||
fetch_weather(CONFIG["lat"], CONFIG["lon"], CONFIG["weather_api_key"]),
|
||||
)
|
||||
|
||||
pollen_periods = {p["period"]: p for p in pollen[0]["periods"]} if pollen and pollen[0]["periods"] else {}
|
||||
weather_periods = {p["period"]: p for p in weather[0]["periods"]} if weather and weather[0]["periods"] else {}
|
||||
pollen_periods = (
|
||||
{p["period"]: p for p in pollen[0]["periods"]}
|
||||
if pollen and pollen[0]["periods"]
|
||||
else {}
|
||||
)
|
||||
weather_periods = (
|
||||
{p["period"]: p for p in weather[0]["periods"]}
|
||||
if weather and weather[0]["periods"]
|
||||
else {}
|
||||
)
|
||||
|
||||
# Add current weather data as a "current" period
|
||||
if weather and weather[0]:
|
||||
|
@ -197,7 +206,7 @@ async def read_root(token: str):
|
|||
"pressure": data["current_pressure"],
|
||||
"desc": data["current_desc"],
|
||||
"sunrise": data["sunrise"],
|
||||
"sunset": data["sunset"]
|
||||
"sunset": data["sunset"],
|
||||
}
|
||||
|
||||
today_date = format_date(datetime.now())
|
||||
|
@ -217,7 +226,7 @@ async def read_root(token: str):
|
|||
"feels_like": weather_data["feels_like"],
|
||||
"desc": weather_data["desc"],
|
||||
"humidity": weather_data["humidity"],
|
||||
"pressure": weather_data["pressure"]
|
||||
"pressure": weather_data["pressure"],
|
||||
}
|
||||
|
||||
today_data = build_daily_data(today_date, pollen_periods, weather_periods)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue