Compare commits
1 commit
be51eb920e
...
a9fec56176
Author | SHA1 | Date | |
---|---|---|---|
a9fec56176 |
1 changed files with 22 additions and 31 deletions
39
main.py
39
main.py
|
@ -1,14 +1,15 @@
|
||||||
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
|
|
||||||
|
|
||||||
import httpx
|
|
||||||
from cachetools import TTLCache
|
|
||||||
from fastapi import FastAPI, HTTPException
|
from fastapi import FastAPI, HTTPException
|
||||||
|
from cachetools import TTLCache
|
||||||
|
import httpx
|
||||||
|
|
||||||
|
|
||||||
pp = pprint.PrettyPrinter()
|
pp = pprint.PrettyPrinter()
|
||||||
logger = logging.getLogger("uvicorn.error")
|
logger = logging.getLogger("uvicorn.error")
|
||||||
|
@ -64,8 +65,7 @@ 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"],
|
"low": weather_data["low"],
|
||||||
"high": weather_data["high"],
|
"high": weather_data["high"],
|
||||||
"desc": weather_data["desc"],
|
"desc": weather_data["desc"],
|
||||||
|
@ -73,8 +73,7 @@ def build_daily_data(date, pollen_periods, weather_periods):
|
||||||
"sunrise": weather_data["sunrise"],
|
"sunrise": weather_data["sunrise"],
|
||||||
"sunset": weather_data["sunset"],
|
"sunset": weather_data["sunset"],
|
||||||
"pressure": weather_data["pressure"],
|
"pressure": weather_data["pressure"],
|
||||||
}
|
})
|
||||||
)
|
|
||||||
return daily_data
|
return daily_data
|
||||||
|
|
||||||
|
|
||||||
|
@ -118,7 +117,7 @@ async def fetch_pollen(zipcode):
|
||||||
),
|
),
|
||||||
"periods": [
|
"periods": [
|
||||||
{
|
{
|
||||||
"index": int(d["Index"] / 12.0 * 100),
|
"index": int(d["Index"] / 12. * 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"]
|
||||||
|
@ -145,8 +144,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": round(current["temp"]),
|
"current_temp": int(round(current["temp"])),
|
||||||
"current_feels_like": round(current["feels_like"]),
|
"current_feels_like": int(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"])),
|
||||||
|
@ -154,8 +153,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": round(p["temp"]["min"]),
|
"low": int(round(p["temp"]["min"])),
|
||||||
"high": round(p["temp"]["max"]),
|
"high": int(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"])),
|
||||||
|
@ -184,16 +183,8 @@ 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 = (
|
pollen_periods = {p["period"]: p for p in pollen[0]["periods"]} if pollen and pollen[0]["periods"] else {}
|
||||||
{p["period"]: p for p in pollen[0]["periods"]}
|
weather_periods = {p["period"]: p for p in weather[0]["periods"]} if weather and weather[0]["periods"] else {}
|
||||||
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]:
|
||||||
|
@ -206,7 +197,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())
|
||||||
|
@ -226,7 +217,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