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
|
import asyncio
|
||||||
from datetime import datetime, timedelta
|
|
||||||
import functools
|
import functools
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
import pprint
|
import pprint
|
||||||
import zoneinfo
|
import zoneinfo
|
||||||
|
from datetime import datetime, timedelta
|
||||||
|
|
||||||
from fastapi import FastAPI, HTTPException
|
|
||||||
from cachetools import TTLCache
|
|
||||||
import httpx
|
import httpx
|
||||||
|
from cachetools import TTLCache
|
||||||
|
from fastapi import FastAPI, HTTPException
|
||||||
|
|
||||||
pp = pprint.PrettyPrinter()
|
pp = pprint.PrettyPrinter()
|
||||||
logger = logging.getLogger("uvicorn.error")
|
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"]
|
daily_data["pollen"] = pollen_periods[date]["index"]
|
||||||
if date in weather_periods:
|
if date in weather_periods:
|
||||||
weather_data = weather_periods[date]
|
weather_data = weather_periods[date]
|
||||||
daily_data.update({
|
daily_data.update(
|
||||||
"low": weather_data["low"],
|
{
|
||||||
"high": weather_data["high"],
|
"low": weather_data["low"],
|
||||||
"desc": weather_data["desc"],
|
"high": weather_data["high"],
|
||||||
"humidity": weather_data["humidity"],
|
"desc": weather_data["desc"],
|
||||||
"sunrise": weather_data["sunrise"],
|
"humidity": weather_data["humidity"],
|
||||||
"sunset": weather_data["sunset"],
|
"sunrise": weather_data["sunrise"],
|
||||||
"pressure": weather_data["pressure"],
|
"sunset": weather_data["sunset"],
|
||||||
})
|
"pressure": weather_data["pressure"],
|
||||||
|
}
|
||||||
|
)
|
||||||
return daily_data
|
return daily_data
|
||||||
|
|
||||||
|
|
||||||
|
@ -117,7 +118,7 @@ async def fetch_pollen(zipcode):
|
||||||
),
|
),
|
||||||
"periods": [
|
"periods": [
|
||||||
{
|
{
|
||||||
"index": int(d["Index"] / 12. * 100),
|
"index": int(d["Index"] / 12.0 * 100),
|
||||||
"period": relative_day_to_date(d["Type"]),
|
"period": relative_day_to_date(d["Type"]),
|
||||||
}
|
}
|
||||||
for d in data["Location"]["periods"]
|
for d in data["Location"]["periods"]
|
||||||
|
@ -144,8 +145,8 @@ async def fetch_weather(lat, lon, weather_api_key):
|
||||||
result = [
|
result = [
|
||||||
{
|
{
|
||||||
"forecast_date": format_datetime(datetime.fromtimestamp(current["dt"])),
|
"forecast_date": format_datetime(datetime.fromtimestamp(current["dt"])),
|
||||||
"current_temp": int(round(current["temp"])),
|
"current_temp": round(current["temp"]),
|
||||||
"current_feels_like": int(round(current["feels_like"])),
|
"current_feels_like": round(current["feels_like"]),
|
||||||
"current_humidity": current["humidity"],
|
"current_humidity": current["humidity"],
|
||||||
"sunrise": format_time(datetime.fromtimestamp(current["sunrise"])),
|
"sunrise": format_time(datetime.fromtimestamp(current["sunrise"])),
|
||||||
"sunset": format_time(datetime.fromtimestamp(current["sunset"])),
|
"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"],
|
"current_desc": current["weather"][0]["description"],
|
||||||
"periods": [
|
"periods": [
|
||||||
{
|
{
|
||||||
"low": int(round(p["temp"]["min"])),
|
"low": round(p["temp"]["min"]),
|
||||||
"high": int(round(p["temp"]["max"])),
|
"high": round(p["temp"]["max"]),
|
||||||
"desc": p["weather"][0]["description"],
|
"desc": p["weather"][0]["description"],
|
||||||
"humidity": p["humidity"],
|
"humidity": p["humidity"],
|
||||||
"sunrise": format_time(datetime.fromtimestamp(p["sunrise"])),
|
"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"]),
|
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 {}
|
pollen_periods = (
|
||||||
weather_periods = {p["period"]: p for p in weather[0]["periods"]} if weather and weather[0]["periods"] else {}
|
{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
|
# Add current weather data as a "current" period
|
||||||
if weather and weather[0]:
|
if weather and weather[0]:
|
||||||
|
@ -197,7 +206,7 @@ async def read_root(token: str):
|
||||||
"pressure": data["current_pressure"],
|
"pressure": data["current_pressure"],
|
||||||
"desc": data["current_desc"],
|
"desc": data["current_desc"],
|
||||||
"sunrise": data["sunrise"],
|
"sunrise": data["sunrise"],
|
||||||
"sunset": data["sunset"]
|
"sunset": data["sunset"],
|
||||||
}
|
}
|
||||||
|
|
||||||
today_date = format_date(datetime.now())
|
today_date = format_date(datetime.now())
|
||||||
|
@ -217,7 +226,7 @@ async def read_root(token: str):
|
||||||
"feels_like": weather_data["feels_like"],
|
"feels_like": weather_data["feels_like"],
|
||||||
"desc": weather_data["desc"],
|
"desc": weather_data["desc"],
|
||||||
"humidity": weather_data["humidity"],
|
"humidity": weather_data["humidity"],
|
||||||
"pressure": weather_data["pressure"]
|
"pressure": weather_data["pressure"],
|
||||||
}
|
}
|
||||||
|
|
||||||
today_data = build_daily_data(today_date, pollen_periods, weather_periods)
|
today_data = build_daily_data(today_date, pollen_periods, weather_periods)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue