Update app.py
Browse files
app.py
CHANGED
@@ -42,29 +42,27 @@ def download_image(url: str):
|
|
42 |
with sync_playwright() as p:
|
43 |
browser = p.chromium.launch(headless=True)
|
44 |
page = browser.new_page()
|
45 |
-
|
46 |
# Set custom headers before navigating to the page
|
47 |
headers = get_custom_headers(url)
|
48 |
page.set_extra_http_headers(headers)
|
49 |
-
|
50 |
# Navigate to the URL and wait for network idle
|
51 |
page.goto(url, timeout=15000, wait_until="networkidle")
|
52 |
-
|
53 |
-
#
|
54 |
-
|
55 |
-
|
56 |
-
if
|
57 |
-
#
|
58 |
-
|
59 |
-
image_data = page.request.get(img_url).body # Fetch the image data from the source URL
|
60 |
-
|
61 |
browser.close()
|
62 |
|
63 |
-
# Return the image
|
64 |
-
return Response(content=
|
65 |
else:
|
66 |
browser.close()
|
67 |
-
return {"error": "No image found on the page
|
68 |
except Exception as e:
|
69 |
return {"error": str(e)}
|
70 |
|
|
|
42 |
with sync_playwright() as p:
|
43 |
browser = p.chromium.launch(headless=True)
|
44 |
page = browser.new_page()
|
45 |
+
|
46 |
# Set custom headers before navigating to the page
|
47 |
headers = get_custom_headers(url)
|
48 |
page.set_extra_http_headers(headers)
|
49 |
+
|
50 |
# Navigate to the URL and wait for network idle
|
51 |
page.goto(url, timeout=15000, wait_until="networkidle")
|
52 |
+
|
53 |
+
# Select the first image on the page
|
54 |
+
img_element = page.query_selector("img")
|
55 |
+
|
56 |
+
if img_element:
|
57 |
+
# Take a screenshot of the first image element
|
58 |
+
screenshot = img_element.screenshot()
|
|
|
|
|
59 |
browser.close()
|
60 |
|
61 |
+
# Return the image as a response
|
62 |
+
return Response(content=screenshot, media_type="image/png")
|
63 |
else:
|
64 |
browser.close()
|
65 |
+
return {"error": "No image found on the page"}
|
66 |
except Exception as e:
|
67 |
return {"error": str(e)}
|
68 |
|