From 2748a5a75bb8649ecf1ad4976cdf8bae7eb0f181 Mon Sep 17 00:00:00 2001 From: shibao Date: Wed, 4 Mar 2026 05:54:29 +0000 Subject: [PATCH] add 100 max retries --- README.md | 5 ++++- szuru-eink.py | 21 +++++++++++++++++---- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 3a85320..a87edd5 100644 --- a/README.md +++ b/README.md @@ -13,6 +13,7 @@ screen](https://gitea.bubbletea.dev/shibao/szuru-eink-bot/raw/branch/master/eink - Fetches random images from a configurable Szurubooru instance. - Resizes and dithered images for e-ink display compatibility. - Supports monochrome and color dithering (for compatible displays). +- Filters images by an optional allowed username. - Uploads processed images to a Waveshare e-ink screen. - Configurable via environment variables. - Designed to run as a Docker container on a cron job. @@ -41,7 +42,9 @@ display. **Note:** For `EPD_TYPE`, refer to the `epd/epd_config.py` file for a full list of supported display types and their corresponding string labels. For color -displays, ensure `DITHERING_MODE` is set to `color`. +displays, ensure `DITHERING_MODE` is set to `color`. The bot +will retry a maximum of 100 times to find a valid image (matching the user filter, +if provided) before giving up. ### 3. Build and Run with Docker Compose diff --git a/szuru-eink.py b/szuru-eink.py index f7d846e..cbed386 100644 --- a/szuru-eink.py +++ b/szuru-eink.py @@ -26,7 +26,10 @@ booru = pyszuru.API(szuru_url, username=szuru_user, token=szuru_token) highest_post = next(booru.search_post("sort:id type:image", page_size=1)) post = None -while post is None: +retries = 0 +MAX_RETRIES = 100 + +while post is None and retries < MAX_RETRIES: random_id = random.randint(0, highest_post.id_) try: temp_post = booru.getPost(random_id) @@ -42,8 +45,12 @@ while post is None: print(f"[DEBUG] Found image post with ID: {post.id_}, MIME: {post.mime}") else: print( - f"[DEBUG] Skipping post ID: {random_id} (MIME: {temp_post.mime if temp_post else 'None'}, Content: {bool(temp_post.content) if temp_post else 'None'}) - not a valid image." + f"[DEBUG] Skipping post ID: {random_id} (MIME: {temp_post.mime if temp_post else 'None'}, Content: {bool(temp_post.content) if temp_post else 'None'}) - not a valid image. (Retry {retries + 1}/{MAX_RETRIES})" ) + retries += 1 + +if post is None: + raise RuntimeError(f"Failed to find a valid post after {MAX_RETRIES} retries.") # download image image_downloaded = False @@ -72,7 +79,7 @@ while not image_downloaded: os.remove("image.jpg") # Find a new post post = None - while post is None: + while post is None and retries < MAX_RETRIES: random_id = random.randint(0, highest_post.id_) try: temp_post = booru.getPost(random_id) @@ -90,8 +97,14 @@ while not image_downloaded: ) else: print( - f"[DEBUG] Skipping post ID: {random_id} (MIME: {temp_post.mime if temp_post else 'None'}, Content: {bool(temp_post.content) if temp_post else 'None'}) - not a valid image." + f"[DEBUG] Skipping post ID: {random_id} (MIME: {temp_post.mime if temp_post else 'None'}, Content: {bool(temp_post.content) if temp_post else 'None'}) - not a valid image. (Retry {retries + 1}/{MAX_RETRIES})" ) + retries += 1 + + if post is None: + raise RuntimeError( + f"Failed to find a valid post after {MAX_RETRIES} retries." + ) # Process and upload img = Image.open("image.jpg")