From 185e586766f56e6c7dd2b7b2405fac36a8cf8583 Mon Sep 17 00:00:00 2001 From: mag Date: Fri, 7 Jul 2023 18:55:37 +0200 Subject: [PATCH] first draft --- config.json | 8 ++ main.py | 119 ++++++++++++++++++++++++++ test_webdav_bhcloud.py => uploader.py | 12 ++- 3 files changed, 135 insertions(+), 4 deletions(-) create mode 100644 config.json create mode 100644 main.py rename test_webdav_bhcloud.py => uploader.py (79%) diff --git a/config.json b/config.json new file mode 100644 index 0000000..3b06ef0 --- /dev/null +++ b/config.json @@ -0,0 +1,8 @@ +{ + "photo_folder": "/home/pi/fotobox_bilder", + "qr_path": "/home/pi/fotobox/qr_small.png", + "photo_prefix": "bh_sommerparty_23", + "qr_image": null, + "width": 1920, + "height": 1080 +} \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 0000000..57345e7 --- /dev/null +++ b/main.py @@ -0,0 +1,119 @@ +import pygame + +import configparser +import logging +import RPi.GPIO as GPIO +import subprocess +import time +import _thread + +import uploader + + +LOGGER = logging.getLogger(__name__) +PIN_BUTTON = 22 + +def get_config(): + """Read config from ./config.ini""" + config = configparser.ConfigParser() + config.read('config.json') + return config + + +def get_file_name(prefix="photobox", path="~"): + # name mit Timestamp versehen + ts = time.gmtime() + readable_ts = time.strftime("%H_%M_%S", ts) + photo_name = "/" + prefix + readable_ts + ".jpg" + return path + photo_name + + +def display_qr(screen, qr): + screen.blit(qr, (1330, 700)) + pygame.display.flip() + pygame.display.update() + + +def load_image(img_path): + return pygame.image.load(img_path).convert() + +def display_image(screen, img, width, height): + try: + img = pygame.transform.scale(img, (width, height)) + screen.blit(img, (0, 0)) + except: + pass + pygame.display.flip() + pygame.display.update() + + +def display_text(screen, text, color=(20, 240, 100), size=70): + font = pygame.freetype.Font( + "/usr/share/fonts/truetype/liberation2/LiberationSans-Bold.ttf", size + ) + text_renderd, text_rect = font.render(text, color) + screen_rect = screen.get_rect() + text_rect.centerx = screen_rect.centerx + screen.blit(text_renderd, (50, 50)) + pygame.display.update() + +def io_remote(): + # wait for button press + GPIO.wait_for_edge(PIN_BUTTON, GPIO.RISING, bouncetime=100) + + +def signal_hook(running=True): + while running: + for event in pygame.event.get(): + if event.type == pygame.QUIT: + running = False + pygame.quit() + elif event.type == pygame.KEYDOWN and event.key == pygame.K_f: + pygame.display.toggle_fullscreen() + +def run(): + config = get_config() + pygame.init() + _thread.start_new_thread(signal_hook, ()) + screen = pygame.display.set_mode((config.WIDTH, config.HEIGHT), 0, 0) + qr_img = pygame.image.load(config.qr_path).convert() + + while 1: + LOGGER.info("Starting...") + while 1: + display_qr(screen, qr_img) + io_remote() + photo_path = get_file_name(prefix=config.prefix, path=config.photo_folder) + + + LOGGER.info(photo_path) + # Capture photo + try: + subprocess.run( + [ + "gphoto2", + "--capture-image-and-download", + "--camera='Canon EOS 350D (normal mode)'", + "--filename={}".format(photo_path), + "--force-overwrite", + ] + ) + except: + LOGGER.info("Failed to capture image.") + continue + + try: + img = load_image(photo_path) + display_image(screen, img, config.width, config.height) + display_text( + screen, "Zuerst das Vergnügen, dann der Upload in die cloud...", size=50 + ) + except: + LOGGER.info("Failed to display image.") + pass + + uploader.sync_photo(photo_path="/Users/rootrapp/Downloads/Screenshot 2023-06-19 at 18-43-27 QLC Quick Beginner Overview.png") + + +if __name__ == "__main__": + run() \ No newline at end of file diff --git a/test_webdav_bhcloud.py b/uploader.py similarity index 79% rename from test_webdav_bhcloud.py rename to uploader.py index 1e2dc54..2be02f9 100644 --- a/test_webdav_bhcloud.py +++ b/uploader.py @@ -1,17 +1,22 @@ +import logging import subprocess from webdav3.client import Client from xmlrpc.client import DateTime import os + +LOGGER = logging.getLogger(__name__) + + def sync_photo(photo_path): # hochladen des bildes - print("Uploading file..") - print(os.path.isfile(photo_path)) + LOGGER.info("Uploading file..") + LOGGER.info(os.path.isfile(photo_path)) subprocess.run( ["rclone", "copy", "{}".format(photo_path), "https://cloud.bauhaeusle.de/remote.php/dav/files/admin"] ) - print("photo uploaded!") + LOGGER.info("photo uploaded!") def sync_phot2(photo_path): @@ -26,6 +31,5 @@ def sync_phot2(photo_path): #client.session.auth(...) # To set proxy auth directly into the session (Optional) client.upload_sync(remote_path="BestOf/Fotobox", local_path=photo_path) -sync_photo(photo_path="/Users/rootrapp/Downloads/Screenshot 2023-06-19 at 18-43-27 QLC Quick Beginner Overview.png")