add screenshotter test

This commit is contained in:
Jan-Henrik 2023-02-07 21:14:33 +01:00
parent 0d982d5438
commit a788e8d2d1
8 changed files with 148 additions and 0 deletions

17
screenshotter/Dockerfile Normal file
View file

@ -0,0 +1,17 @@
ARG BUILD_FROM=ghcr.io/hassio-addons/base-python/amd64:9.0.1
FROM ${BUILD_FROM}
RUN apk add --no-cache zlib-dev libjpeg-turbo-dev gcc chromium chromium-chromedriver
ADD rootfs /
RUN chmod 644 /usr/share/fontconfig/conf.avail/10-antialiasing.conf
RUN ln -s /usr/share/fontconfig/conf.avail/10-antialising.conf /etc/fonts/conf.d/10-antialiasing.conf
WORKDIR /app
RUN pip3 install -r requirements.txt
# Corrects permissions for s6 v3
RUN if [ -d /etc/cont-init.d ]; then chmod -R 755 /etc/cont-init.d; fi && \
if [ -d /etc/services.d ]; then chmod -R 755 /etc/services.d; fi && \
if [ -f /entrypoint.sh ]; then chmod 755 /entrypoint.sh; fi

7
screenshotter/build.json Normal file
View file

@ -0,0 +1,7 @@
{
"build_from": {
"aarch64": "ghcr.io/hassio-addons/base-python/aarch64:9.0.1",
"amd64": "ghcr.io/hassio-addons/base-python/amd64:9.0.1",
"armv7": "ghcr.io/hassio-addons/base-python/armv7:9.0.1"
}
}

30
screenshotter/config.yaml Normal file
View file

@ -0,0 +1,30 @@
---
name: Screenshotter
version: 1.0.0.41
#image: ghcr.io/maxwinterstein/homeassistant-addon-toogoodtogo-ha-mqtt-bridge-{arch}
slug: screenshotter
description: Screenshot images for E-Paper pricetags
panel_icon: mdi:label-variant
arch:
- amd64
- armv7
- aarch64
map:
- "share:rw"
- "config:rw"
options:
image_path: "/config/epaper-station/images"
config_file: "/config/screenshotter/config.yaml"
width: 296
height: 128
rotate: 270
wait_seconds: 5
schema:
image_path: str
config_file: str
width: int
height: int
rotate: int
wait_seconds: int
stage: experimental
init: false

View file

@ -0,0 +1,60 @@
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from PIL import Image
import time, os
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
import yaml
import datetime
# enable browser logging
d = DesiredCapabilities.CHROME
d['loggingPrefs'] = {'browser': 'ALL'}
image_path = os.environ.get("SCREEN_IMAGE_PATH", default='/tmp')
config_file = os.environ.get("SCREEN_CONFIG", default="/app/config.yaml")
width = os.environ.get("SCREEN_WIDTH", default="640")
height = os.environ.get("SCREEN_HEIGHT", default="480")
rotate = os.environ.get("SCREEN_ROTATE", default="0")
wait = os.environ.get("SCREEN_WAIT", default="5")
# Open another headless browser with height extracted above
chrome_options = Options()
chrome_options.add_argument("--headless")
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument(f"--window-size={width},{height}")
chrome_options.add_argument("--hide-scrollbars")
chrome_options.add_argument('user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, '
'like Gecko) Chrome/68.0.3440.84 Safari/537.36')
config = None
with open(config_file, "r") as stream:
config = yaml.safe_load(stream)
tasks = []
for image in config.images:
image["next_execution"] = datetime.datetime.now()
tasks += [image]
while True:
now = datetime.datetime.now()
for task in tasks:
if task["next_execution"] > now:
continue
driver = webdriver.Chrome(options=chrome_options, desired_capabilities=d)
driver.implicitly_wait(10)
driver.get(url + paths.get(display))
# pause 3 second to let page loads
time.sleep(wait)
# save screenshot
file_name = f'/tmp/{task["name"]}.png'
driver.save_screenshot(file_name)
# print messages
for entry in driver.get_log('browser'):
print(entry)
image = Image.open(file_name)
image = image.rotate(rotate, expand=True)
file_name = f'{image_path}/{task["name"]}.png'
image.save(file_name)
task["next_execution"] = now + datetime.timedelta(minutes=task["period_minutes"])
time.sleep(1)

View file

@ -0,0 +1,4 @@
images:
- name: "001100"
url: https://ha.baubs.net/lovelace-teflons/0?kiosk
period_minutes: 1

View file

@ -0,0 +1,3 @@
selenium
Pillow
pyyaml

View file

@ -0,0 +1,15 @@
#!/usr/bin/with-contenv bashio
bashio::log.info "Reading config..."
CONFIG_PATH=/data/options.json
export SCREEN_IMAGE_PATH="$(bashio::config 'image_path')"
export SCREEN_CONFIG="$(bashio::config 'config_file')"
export SCREEN_WIDTH="$(bashio::config 'width')"
export SCREEN_HEIGHT="$(bashio::config 'height')"
export SCREEN_ROTATE="$(bashio::config 'height')"
export SCREEN_WAIT="$(bashio::config 'wait_seconds')"
mkdir -p $SCREEN_IMAGE_PATH
bashio::log.info "Starting screenshotter..."
exec python /app/app.py

View file

@ -0,0 +1,12 @@
<?xml version="1.0"?>
<!DOCTYPE fontconfig SYSTEM "fonts.dtd">
<fontconfig>
<its:rules xmlns:its="http://www.w3.org/2005/11/its" version="1.0">
<its:translateRule translate="no" selector="/fontconfig/*[not(self::description)]"/>
</its:rules>
<match target="font">
<edit name="antialias" mode="assign">
<bool>false</bool>
</edit>
</match>
</fontconfig>