add 100 max retries
This commit is contained in:
@@ -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")
|
||||
|
||||
Reference in New Issue
Block a user