Compare commits

...

2 commits

2 changed files with 132 additions and 106 deletions

83
main.py
View file

@ -42,19 +42,6 @@ def format_datetime(dt):
return f"{format_date(dt)} {format_time(dt)}" return f"{format_date(dt)} {format_time(dt)}"
def pollen_desc(index):
if index < 2.5:
return f"{index} l"
elif index < 4.9:
return f"{index} l-m"
elif index < 7.3:
return f"{index} m"
elif index < 9.7:
return f"{index} m-h"
else:
return f"{index} h"
def relative_day_to_date(rel_dt): def relative_day_to_date(rel_dt):
dt = datetime.now() dt = datetime.now()
day = timedelta(days=1) day = timedelta(days=1)
@ -69,6 +56,24 @@ def relative_day_to_date(rel_dt):
return passthrough return passthrough
def build_daily_data(date, pollen_periods, weather_periods):
daily_data = {}
if date in pollen_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"],
})
return daily_data
def fallback_handler(func): def fallback_handler(func):
@functools.wraps(func) @functools.wraps(func)
async def wrapper(*args, **kwargs): async def wrapper(*args, **kwargs):
@ -106,10 +111,11 @@ async def fetch_pollen(zipcode):
), ),
"periods": [ "periods": [
{ {
"index": pollen_desc(d["Index"]), "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"]
if d["Type"].lower().strip() in ["today", "tomorrow"]
], ],
} }
] ]
@ -122,7 +128,7 @@ async def fetch_weather(lat, lon, weather_api_key):
response = await client.get(url) response = await client.get(url)
response.raise_for_status() response.raise_for_status()
data = response.json() data = response.json()
current, periods = data["current"], data["daily"][:3] current, periods = data["current"], data["daily"][:2]
return [ return [
{ {
"forecast_date": format_datetime(datetime.fromtimestamp(current["dt"])), "forecast_date": format_datetime(datetime.fromtimestamp(current["dt"])),
@ -163,8 +169,49 @@ 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"]),
) )
return { 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]:
data = weather[0]
weather_periods["current"] = {
"period": "current",
"temp": data["current_temp"],
"feels_like": data["current_feels_like"],
"humidity": data["current_humidity"],
"pressure": data["current_pressure"],
"desc": data["current_desc"],
"sunrise": data["sunrise"],
"sunset": data["sunset"]
}
today_date = format_date(datetime.now())
tomorrow_date = format_date(datetime.now() + timedelta(days=1))
result = {
"fetched_at": format_datetime(datetime.now()), "fetched_at": format_datetime(datetime.now()),
"pollen": pollen, "current": {},
"weather": weather, "today": {},
"tomorrow": {},
} }
if "current" in weather_periods:
weather_data = weather_periods["current"]
result["current"] = {
"temp": weather_data["temp"],
"feels_like": weather_data["feels_like"],
"desc": weather_data["desc"],
"humidity": weather_data["humidity"],
"pressure": weather_data["pressure"]
}
today_data = build_daily_data(today_date, pollen_periods, weather_periods)
if today_data:
result["today"] = today_data
tomorrow_data = build_daily_data(tomorrow_date, pollen_periods, weather_periods)
if tomorrow_data:
result["tomorrow"] = tomorrow_data
return result

View file

@ -1,94 +1,73 @@
<div class="columns"> <div>
<div class="column"> <div class="flex flex--row">
<span class="title title--small text--gray-4 text--center">fetched: {{ fetched_at }}</span> <div>
{% for pollen in pollen %} <span class="value">{{ current.temp }}°F</span>
<span class="title title--small bg--black text--white text--center">pollen index</span> <span class="label text--gray-4">feels like {{ current.feels_like }}°F</span>
<div class="richtext gap--xxsmall">
<p class="content content--small">forecast at {{pollen.forecast_date}}</p>
</div> </div>
<table class="table table--condensed"> <div class="stretch text--right">
<thead> <span class="value">{{ current.desc }}</span>
<tr> <span class="label text--gray-4">conditions</span>
{% for period in pollen.periods %} </div>
<th><span class="label label--small text--center">{{period.period}}</span></th> </div>
{% endfor %}
</tr>
</thead>
<tbody>
<tr>
{% for period in pollen.periods %}
<td><span class="label label--small text--center">{{period.index}}</span></td>
{% endfor %}
</tr>
</tbody>
</table>
{% endfor %}
{% for weather in weather %} <div class="flex flex--row">
<span class="title title--small bg--black text--white text--center">weather forecast</span> <div>
<div class="richtext gap--xxsmall"> <span class="value">{{ current.humidity }}%</span>
<p class="content content--small">forecast at {{weather.forecast_date}}</p> <span class="label text--gray-4">humidity</span>
</div> </div>
<table class="table table--condensed"> <div class="stretch text--center">
<thead> <span class="value">{{ current.pressure }}</span>
<tr> <span class="label text--gray-4">pressure</span>
<th><span class="label label--small w--16"></span></th> </div>
{% for period in weather.periods %} <div class="text--right">
<th><span class="label label--small w--16 text--center">{{period.period}}</span></th> <span class="value">{{ fetched_at }}</span>
{% endfor %} <span class="label text--gray-4">date</span>
</tr> </div>
</thead> </div>
<tbody> </div>
<tr>
<td><span class="label label--small text--right">low</span></td> <div class="border--h-1 pt--4"></div>
{% for period in weather.periods %}
<td><span class="label label--small text--center">{{period.low}} F</span></td> <div class="pt--4">
{% endfor %} <div class="flex flex--row">
</tr> {% assign periods = "today,tomorrow" | split: "," %}
<tr> {% for period in periods %}
<td><span class="label label--small text--right">high</span></td> {% assign data = [period] %}
{% for period in weather.periods %} <div class="stretch ">
<td><span class="label label--small text--center">{{period.high}} F</span></td> <div>
{% endfor %} <div class="label label--inverted text--center">{{ period }}</div>
</tr>
<tr> <div class="pt--2 text--center">
<td><span class="label label--small text--right">humidity</span></td> <span class="value">
{% for period in weather.periods %} {{ data.low }}°F / {{ data.high }}°F
<td><span class="label label--small text--center">{{period.humidity}}%</span></td> </span>
{% endfor %} </div>
</tr>
<tr> <div class="text--center">
<td><span class="label label--small text--right">pressure</span></td> <span class="value">{{ data.desc }}</span>
{% for period in weather.periods %} </div>
<td><span class="label label--small text--center">{{period.pressure}}</span></td>
{% endfor %} <div class="text--center">
</tr> <span class="value">{{ data.sunrise }} - {{ data.sunset }}</span>
<tr> </div>
<td><span class="label label--small text--right">sunrise</span></td>
{% for period in weather.periods %} <div class="flex flex--row">
<td><span class="label label--small text--center">{{period.sunrise}}</span></td> <div class="text--center">
{% endfor %} <span class="value">{{ data.humidity }}%</span>
</tr> <span class="label text--gray-4">humidity</span>
<tr> </div>
<td><span class="label label--small text--right">sunset</span></td> <div class="stretch text--center">
{% for period in weather.periods %} <span class="value">{{ data.pressure }}</span>
<td><span class="label label--small text--center">{{period.sunset}}</span></td> <span class="label text--gray-4">pressure</span>
{% endfor %} </div>
</tr> <div class="text--center">
<tr> <span class="value">{{ data.pollen }}%</span>
<td><span class="label label--small text--right">desc</span></td> <span class="label text--gray-4">pollen</span>
{% for period in weather.periods %} </div>
<td><span class="label label--small h--10 clamp--none text--center">{{period.desc}}</span></td> </div>
{% endfor %}
</tr> </div>
</tbody> </div>
</table>
{% endfor %} {% endfor %}
</div> </div>
<div class="column">
{{ Content for column 2 }}
</div>
<div class="column">
{{ Content for column 3 }}
</div>
</div> </div>