add python blinkie example

This commit is contained in:
2026-05-15 20:41:41 +01:00
parent 49ee2d15be
commit 83467531b0
6 changed files with 83 additions and 2 deletions

View File

@@ -20,3 +20,12 @@ command = ["bash", "-c", "/usr/bin/uptime -p | cut -d, -f-2"]
font = "./res/curie.bdf" font = "./res/curie.bdf"
text_filters = [ "lowercase" ] text_filters = [ "lowercase" ]
text_offset = [21, 4] text_offset = [21, 4]
[[image]]
output = "out/pdweather.gif"
template = "./res/templates/forest.gif"
command = [ "python", "./res/pdweather.py" ]
font = "./res/curie.bdf"
text_filters = [ "lowercase" ]
text_offset = [45, 4]

View File

@@ -18,6 +18,12 @@ a [dockerfile](./Dockerfile) and [compose file](./compose.yaml) have been provid
keep in mind the commands will run in the docker container, too. keep in mind the commands will run in the docker container, too.
you do not need to clone this repository to use the compose file provided. you do not need to clone this repository to use the compose file provided.
the docker image installs the python
[`requests` library](https://docs.python-requests.org/en/latest/index.html),
so you can use it for calling external APIs inside the container.
the `pdweather` blinkie example does this.
### custom images ### custom images
to use utilities not installed in the standard docker image, to use utilities not installed in the standard docker image,

View File

@@ -1 +1,6 @@
certifi==2026.4.22
charset-normalizer==3.4.7
idna==3.15
pillow==12.2.0 pillow==12.2.0
requests==2.34.2
urllib3==2.7.0

61
res/pdweather.py Executable file
View File

@@ -0,0 +1,61 @@
#!/usr/bin/env python3
#
# Akbar Rahman <hi@alv.cx>
#
import sys
from datetime import datetime as dt
import requests
def get_args():
""" Get command line arguments """
import argparse
parser = argparse.ArgumentParser()
return parser.parse_args()
def main(args):
""" Entry point for script """
resp = requests.get("https://api.open-meteo.com/v1/forecast?latitude=53.35&longitude=-1.8333&hourly=temperature_2m,rain,cloud_cover&timezone=GMT&forecast_days=1").json()
now = dt.now()
closest_past_date = None
for idx, datetime_string in enumerate(resp['hourly']['time']):
date = dt.fromisoformat(datetime_string)
if closest_past_date is None:
closest_past_date = (idx, date)
continue
if date > now: # date > now means date is in future, ignore
continue
if (now - date) < (now - closest_past_date[1]):
closest_past_date = (idx, date)
continue
cur_weather = {}
for key in resp['hourly'].keys():
cur_weather[key] = resp['hourly'][key][closest_past_date[0]]
output = "sunny in peaks"
if cur_weather['cloud_cover'] > 40:
output = "cloudy in peaks"
if cur_weather['rain'] > 0.5:
output = "raining in peaks"
print(output, end='')
return 0
if __name__ == '__main__':
try:
sys.exit(main(get_args()))
except KeyboardInterrupt:
sys.exit(0)

BIN
res/templates/forest.gif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

View File

@@ -3,7 +3,6 @@
# Akbar Rahman <hi@alv.cx> # Akbar Rahman <hi@alv.cx>
# #
import shlex
import sys import sys
import subprocess import subprocess
import time import time
@@ -21,6 +20,7 @@ def get_args():
parser.add_argument("-L", "--no-loop", action="store_true") parser.add_argument("-L", "--no-loop", action="store_true")
return parser.parse_args() return parser.parse_args()
def generate(config): def generate(config):
if config.get('template'): if config.get('template'):
img = Image.open(config['template']) img = Image.open(config['template'])
@@ -44,7 +44,7 @@ def generate(config):
elif filter == "uppercase": elif filter == "uppercase":
text = text.upper() text = text.upper()
draw.text(config.get('text_offset', [0, 0]), text, font=font) draw.text(config.get('text_offset', [0, 0]), text, font=font, fill=(255, 255, 255, 255))
img.save(config['output'], save_all=True) img.save(config['output'], save_all=True)