From a788e8d2d14543d13482dbaeb73e55ca8c6142d6 Mon Sep 17 00:00:00 2001 From: Jan-Henrik Bruhn Date: Tue, 7 Feb 2023 21:14:33 +0100 Subject: [PATCH] add screenshotter test --- screenshotter/Dockerfile | 17 ++++++ screenshotter/build.json | 7 +++ screenshotter/config.yaml | 30 ++++++++++ screenshotter/rootfs/app/app.py | 60 +++++++++++++++++++ screenshotter/rootfs/app/config.yaml | 4 ++ screenshotter/rootfs/app/requirements.txt | 3 + .../rootfs/etc/services.d/screenshotter/run | 15 +++++ .../conf.avail/10-antialiasing.conf | 12 ++++ 8 files changed, 148 insertions(+) create mode 100644 screenshotter/Dockerfile create mode 100644 screenshotter/build.json create mode 100644 screenshotter/config.yaml create mode 100644 screenshotter/rootfs/app/app.py create mode 100644 screenshotter/rootfs/app/config.yaml create mode 100644 screenshotter/rootfs/app/requirements.txt create mode 100644 screenshotter/rootfs/etc/services.d/screenshotter/run create mode 100644 screenshotter/rootfs/usr/share/fontconfig/conf.avail/10-antialiasing.conf diff --git a/screenshotter/Dockerfile b/screenshotter/Dockerfile new file mode 100644 index 0000000..2b10a63 --- /dev/null +++ b/screenshotter/Dockerfile @@ -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 diff --git a/screenshotter/build.json b/screenshotter/build.json new file mode 100644 index 0000000..4e10c4f --- /dev/null +++ b/screenshotter/build.json @@ -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" + } +} diff --git a/screenshotter/config.yaml b/screenshotter/config.yaml new file mode 100644 index 0000000..8761793 --- /dev/null +++ b/screenshotter/config.yaml @@ -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 diff --git a/screenshotter/rootfs/app/app.py b/screenshotter/rootfs/app/app.py new file mode 100644 index 0000000..8d56ab1 --- /dev/null +++ b/screenshotter/rootfs/app/app.py @@ -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) diff --git a/screenshotter/rootfs/app/config.yaml b/screenshotter/rootfs/app/config.yaml new file mode 100644 index 0000000..e24bffb --- /dev/null +++ b/screenshotter/rootfs/app/config.yaml @@ -0,0 +1,4 @@ +images: + - name: "001100" + url: https://ha.baubs.net/lovelace-teflons/0?kiosk + period_minutes: 1 diff --git a/screenshotter/rootfs/app/requirements.txt b/screenshotter/rootfs/app/requirements.txt new file mode 100644 index 0000000..9c38586 --- /dev/null +++ b/screenshotter/rootfs/app/requirements.txt @@ -0,0 +1,3 @@ +selenium +Pillow +pyyaml diff --git a/screenshotter/rootfs/etc/services.d/screenshotter/run b/screenshotter/rootfs/etc/services.d/screenshotter/run new file mode 100644 index 0000000..6f09d72 --- /dev/null +++ b/screenshotter/rootfs/etc/services.d/screenshotter/run @@ -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 diff --git a/screenshotter/rootfs/usr/share/fontconfig/conf.avail/10-antialiasing.conf b/screenshotter/rootfs/usr/share/fontconfig/conf.avail/10-antialiasing.conf new file mode 100644 index 0000000..14ffa2b --- /dev/null +++ b/screenshotter/rootfs/usr/share/fontconfig/conf.avail/10-antialiasing.conf @@ -0,0 +1,12 @@ + + + + + + + + + false + + + \ No newline at end of file