Compare commits

...

2 commits

Author SHA1 Message Date
c08cfffc65 caching 2025-07-20 07:46:11 -04:00
88b932d2e5 tweaking layout 2025-07-20 07:45:20 -04:00
5 changed files with 135 additions and 90 deletions

View file

@ -0,0 +1,35 @@
# TRMNL Weather & Pollen Report
A custom TRMNL plugin that fetches and displays weather and pollen data.
## Setup
1. Set up a virtual environment and install dependencies:
```bash
python -m venv .venv
source .venv/bin/activate
uv sync
```
2. Set required environment variables:
```bash
export WEATHER_API_KEY="your_openweathermap_api_key"
export AUTH_TOKEN="your_chosen_auth_token"
```
3. Run the application:
```bash
fastapi run main.py --port 8887
```
## Docker
Build and run with Docker:
```bash
docker build -t trmnl-report .
docker run -p 8887:8887 -e WEATHER_API_KEY=your_key -e AUTH_TOKEN=your_token trmnl-report
```
## API
Access the API at `http://localhost:8887/?token=your_auth_token`

23
main.py
View file

@ -7,6 +7,7 @@ import pprint
import zoneinfo
from fastapi import FastAPI, HTTPException
from cachetools import TTLCache
import httpx
@ -16,6 +17,9 @@ eastern = zoneinfo.ZoneInfo("America/New_York")
app = FastAPI()
# Initialize caches for 15 minutes
weather_cache = TTLCache(maxsize=100, ttl=900) # 15 minutes
pollen_cache = TTLCache(maxsize=100, ttl=900) # 15 minutes
CONFIG = {
# salem, ma
@ -89,6 +93,10 @@ def fallback_handler(func):
@fallback_handler
async def fetch_pollen(zipcode):
# Check the cache first
if zipcode in pollen_cache:
return pollen_cache[zipcode]
url = f"https://www.pollen.com/api/forecast/current/pollen/{zipcode}"
headers = {
"User-Agent": (
@ -104,7 +112,7 @@ async def fetch_pollen(zipcode):
response = await client.get(url, headers=headers)
response.raise_for_status()
data = response.json()
return [
result = [
{
"forecast_date": format_datetime(
datetime.fromisoformat(data["ForecastDate"])
@ -119,17 +127,25 @@ async def fetch_pollen(zipcode):
],
}
]
# Cache the result
pollen_cache[zipcode] = result
return result
@fallback_handler
async def fetch_weather(lat, lon, weather_api_key):
# Check the cache first
cache_key = (lat, lon)
if cache_key in weather_cache:
return weather_cache[cache_key]
url = f"https://api.openweathermap.org/data/3.0/onecall?lat={lat}&lon={lon}&appid={weather_api_key}&units=imperial"
async with httpx.AsyncClient() as client:
response = await client.get(url)
response.raise_for_status()
data = response.json()
current, periods = data["current"], data["daily"][:2]
return [
result = [
{
"forecast_date": format_datetime(datetime.fromtimestamp(current["dt"])),
"current_temp": int(round(current["temp"])),
@ -154,6 +170,9 @@ async def fetch_weather(lat, lon, weather_api_key):
],
}
]
# Cache the result
weather_cache[cache_key] = result
return result
@app.get("/")

View file

@ -7,6 +7,7 @@ requires-python = ">=3.13"
dependencies = [
"fastapi[standard]>=0.115.12",
"httpx>=0.28.1",
"cachetools>=5.0.0",
]
[tool.pyright]

View file

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

11
uv.lock generated
View file

@ -24,6 +24,15 @@ wheels = [
{ url = "https://files.pythonhosted.org/packages/a1/ee/48ca1a7c89ffec8b6a0c5d02b89c305671d5ffd8d3c94acf8b8c408575bb/anyio-4.9.0-py3-none-any.whl", hash = "sha256:9f76d541cad6e36af7beb62e978876f3b41e3e04f2c1fbf0884604c0a9c4d93c", size = 100916, upload-time = "2025-03-17T00:02:52.713Z" },
]
[[package]]
name = "cachetools"
version = "6.1.0"
source = { registry = "https://pypi.org/simple" }
sdist = { url = "https://files.pythonhosted.org/packages/8a/89/817ad5d0411f136c484d535952aef74af9b25e0d99e90cdffbe121e6d628/cachetools-6.1.0.tar.gz", hash = "sha256:b4c4f404392848db3ce7aac34950d17be4d864da4b8b66911008e430bc544587", size = 30714, upload-time = "2025-06-16T18:51:03.07Z" }
wheels = [
{ url = "https://files.pythonhosted.org/packages/00/f0/2ef431fe4141f5e334759d73e81120492b23b2824336883a91ac04ba710b/cachetools-6.1.0-py3-none-any.whl", hash = "sha256:1c7bb3cf9193deaf3508b7c5f2a79986c13ea38965c5adcff1f84519cf39163e", size = 11189, upload-time = "2025-06-16T18:51:01.514Z" },
]
[[package]]
name = "certifi"
version = "2025.4.26"
@ -390,12 +399,14 @@ name = "trmnl-report"
version = "0.1.0"
source = { virtual = "." }
dependencies = [
{ name = "cachetools" },
{ name = "fastapi", extra = ["standard"] },
{ name = "httpx" },
]
[package.metadata]
requires-dist = [
{ name = "cachetools", specifier = ">=5.0.0" },
{ name = "fastapi", extras = ["standard"], specifier = ">=0.115.12" },
{ name = "httpx", specifier = ">=0.28.1" },
]