add 100 max retries

This commit is contained in:
2026-03-04 05:54:29 +00:00
parent b5d6fc2152
commit 2748a5a75b
2 changed files with 21 additions and 5 deletions

View File

@@ -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. - Fetches random images from a configurable Szurubooru instance.
- Resizes and dithered images for e-ink display compatibility. - Resizes and dithered images for e-ink display compatibility.
- Supports monochrome and color dithering (for compatible displays). - Supports monochrome and color dithering (for compatible displays).
- Filters images by an optional allowed username.
- Uploads processed images to a Waveshare e-ink screen. - Uploads processed images to a Waveshare e-ink screen.
- Configurable via environment variables. - Configurable via environment variables.
- Designed to run as a Docker container on a cron job. - 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 **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 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 ### 3. Build and Run with Docker Compose

View File

@@ -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)) highest_post = next(booru.search_post("sort:id type:image", page_size=1))
post = None 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_) random_id = random.randint(0, highest_post.id_)
try: try:
temp_post = booru.getPost(random_id) 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}") print(f"[DEBUG] Found image post with ID: {post.id_}, MIME: {post.mime}")
else: else:
print( 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 # download image
image_downloaded = False image_downloaded = False
@@ -72,7 +79,7 @@ while not image_downloaded:
os.remove("image.jpg") os.remove("image.jpg")
# Find a new post # Find a new post
post = None post = None
while post is None: while post is None and retries < MAX_RETRIES:
random_id = random.randint(0, highest_post.id_) random_id = random.randint(0, highest_post.id_)
try: try:
temp_post = booru.getPost(random_id) temp_post = booru.getPost(random_id)
@@ -90,8 +97,14 @@ while not image_downloaded:
) )
else: else:
print( 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 # Process and upload
img = Image.open("image.jpg") img = Image.open("image.jpg")