Fix StaleElementReferenceException, add Ollama offline fallback, and PIL visual search generator

This commit is contained in:
abhijitgorde5-crypto
2026-08-29 23:41:57 +05:30
parent 9e919e4ae2
commit 27a98d9b93
6 changed files with 196 additions and 105 deletions
+37 -50
View File
@@ -117,7 +117,7 @@ def download_image(url):
allow_redirects=True,
)
except requests.RequestException as e:
except (requests.RequestException, ConnectionResetError, OSError) as e:
print(f"Download failed: {e}")
return None
@@ -194,6 +194,31 @@ def convert_to_jpeg(image_data):
return None
def generate_fallback_image():
"""Generate a synthetic local JPEG image using PIL as a fallback."""
print("[INFO] Generating synthetic local fallback image for visual search...")
from PIL import ImageDraw
import random
img = Image.new("RGB", (800, 600), color=(random.randint(50, 200), random.randint(50, 200), random.randint(50, 200)))
draw = ImageDraw.Draw(img)
for _ in range(10):
x0 = random.randint(0, 700)
y0 = random.randint(0, 500)
x1 = x0 + random.randint(50, 200)
y1 = y0 + random.randint(50, 200)
fill = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
draw.rectangle([x0, y0, x1, y1], fill=fill)
output = io.BytesIO()
img.save(output, format="JPEG", quality=90)
jpeg_data = output.getvalue()
OUTPUT_FILE.write_bytes(jpeg_data)
print(f"Saved fallback image to {OUTPUT_FILE.absolute()}")
return {"title": "Fallback Synthetic Image", "width": 800, "height": 600}
# ============================================================
# RANDOM IMAGE
# ============================================================
@@ -213,10 +238,6 @@ def get_random_image():
f"{attempt}/{MAX_ATTEMPTS}"
)
# ----------------------------------------------------
# RANDOM FILE
# ----------------------------------------------------
params = {
"action": "query",
"format": "json",
@@ -241,14 +262,10 @@ def get_random_image():
timeout=20,
)
except requests.RequestException as e:
except (requests.RequestException, ConnectionResetError, OSError) as e:
print(f"API request failed: {e}")
continue
# ----------------------------------------------------
# API RATE LIMIT
# ----------------------------------------------------
if response.status_code == 429:
wait_after_429(
response,
@@ -267,10 +284,6 @@ def get_random_image():
print(f"API error: {e}")
continue
# ----------------------------------------------------
# GET PAGE
# ----------------------------------------------------
pages = (
data
.get("query", {})
@@ -330,10 +343,6 @@ def get_random_image():
"url"
)
# ----------------------------------------------------
# FILTER
# ----------------------------------------------------
if mime not in {
"image/jpeg",
"image/png",
@@ -363,27 +372,15 @@ def get_random_image():
print("No thumbnail URL.")
continue
# ----------------------------------------------------
# FOUND
# ----------------------------------------------------
print(f"Found: {title}")
print(
f"Size: {width}x{height}"
)
# ----------------------------------------------------
# DOWNLOAD THUMBNAIL
# ----------------------------------------------------
image_data = download_image(
thumbnail_url
)
# ----------------------------------------------------
# FALLBACK TO ORIGINAL
# ----------------------------------------------------
if image_data is None and original_url:
print(
"Trying original..."
@@ -399,10 +396,6 @@ def get_random_image():
)
continue
# ----------------------------------------------------
# CONVERT TO JPEG
# ----------------------------------------------------
print("Converting to JPEG...")
jpeg_data = convert_to_jpeg(
@@ -412,10 +405,6 @@ def get_random_image():
if jpeg_data is None:
continue
# ----------------------------------------------------
# SAVE JPEG
# ----------------------------------------------------
try:
OUTPUT_FILE.write_bytes(
jpeg_data
@@ -427,10 +416,6 @@ def get_random_image():
)
continue
# ----------------------------------------------------
# SAVE METADATA
# ----------------------------------------------------
metadata = {
"title": title,
"source": "Wikimedia Commons",
@@ -476,10 +461,6 @@ def get_random_image():
f"metadata: {e}"
)
# ----------------------------------------------------
# DONE
# ----------------------------------------------------
print()
print("=" * 50)
print("SUCCESS")
@@ -498,10 +479,16 @@ def get_random_image():
return metadata
raise RuntimeError(
"Unable to obtain a suitable "
"Wikimedia image."
)
print("[WARNING] Could not download image from Wikimedia Commons. Generating local fallback image.")
return generate_fallback_image()
# ============================================================
# MAIN
# ============================================================
if __name__ == "__main__":
get_random_image()
# ============================================================