Commit
·
cf7cbe0
1
Parent(s):
e264582
Added middleware
Browse files- webrify2.py +56 -2
webrify2.py
CHANGED
@@ -186,11 +186,63 @@ async def get_metadata(url: str):
|
|
186 |
# finally:
|
187 |
# await browser.close()
|
188 |
# await pw.stop()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
189 |
@app.get("/screenshot", response_model=ScreenshotResponse)
|
190 |
async def get_screenshot(url: str):
|
191 |
page, browser, pw = await get_page(url)
|
192 |
try:
|
193 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
194 |
await page.evaluate("""
|
195 |
() => {
|
196 |
return new Promise((resolve) => {
|
@@ -208,11 +260,13 @@ async def get_screenshot(url: str):
|
|
208 |
}
|
209 |
""")
|
210 |
|
211 |
-
#
|
212 |
await page.wait_for_timeout(2000)
|
213 |
|
|
|
214 |
image_bytes = await page.screenshot(full_page=True)
|
215 |
image_base64 = base64.b64encode(image_bytes).decode()
|
|
|
216 |
return {"screenshot": image_base64}
|
217 |
finally:
|
218 |
await browser.close()
|
|
|
186 |
# finally:
|
187 |
# await browser.close()
|
188 |
# await pw.stop()
|
189 |
+
# @app.get("/screenshot", response_model=ScreenshotResponse)
|
190 |
+
# async def get_screenshot(url: str):
|
191 |
+
# page, browser, pw = await get_page(url)
|
192 |
+
# try:
|
193 |
+
# # Scroll to bottom to trigger lazy-loaded content
|
194 |
+
# await page.evaluate("""
|
195 |
+
# () => {
|
196 |
+
# return new Promise((resolve) => {
|
197 |
+
# let totalHeight = 0;
|
198 |
+
# const distance = 100;
|
199 |
+
# const timer = setInterval(() => {
|
200 |
+
# window.scrollBy(0, distance);
|
201 |
+
# totalHeight += distance;
|
202 |
+
# if (totalHeight >= document.body.scrollHeight) {
|
203 |
+
# clearInterval(timer);
|
204 |
+
# resolve();
|
205 |
+
# }
|
206 |
+
# }, 100);
|
207 |
+
# });
|
208 |
+
# }
|
209 |
+
# """)
|
210 |
+
|
211 |
+
# # Give time for images and content to load
|
212 |
+
# await page.wait_for_timeout(2000)
|
213 |
+
|
214 |
+
# image_bytes = await page.screenshot(full_page=True)
|
215 |
+
# image_base64 = base64.b64encode(image_bytes).decode()
|
216 |
+
# return {"screenshot": image_base64}
|
217 |
+
# finally:
|
218 |
+
# await browser.close()
|
219 |
+
# await pw.stop()
|
220 |
+
|
221 |
@app.get("/screenshot", response_model=ScreenshotResponse)
|
222 |
async def get_screenshot(url: str):
|
223 |
page, browser, pw = await get_page(url)
|
224 |
try:
|
225 |
+
# Go to the page and wait until the network is idle
|
226 |
+
await page.goto(url, wait_until="networkidle", timeout=60000)
|
227 |
+
|
228 |
+
# Wait for the header (or similar element) to load
|
229 |
+
try:
|
230 |
+
await page.wait_for_selector("header", timeout=10000)
|
231 |
+
except:
|
232 |
+
pass # Don't fail if the header doesn't exist
|
233 |
+
|
234 |
+
# Remove sticky or fixed header issues before full-page screenshot
|
235 |
+
await page.add_style_tag(content="""
|
236 |
+
* {
|
237 |
+
scroll-behavior: auto !important;
|
238 |
+
}
|
239 |
+
header, .sticky, .fixed, [style*="position:fixed"] {
|
240 |
+
position: static !important;
|
241 |
+
top: auto !important;
|
242 |
+
}
|
243 |
+
""")
|
244 |
+
|
245 |
+
# Scroll down to trigger lazy loading
|
246 |
await page.evaluate("""
|
247 |
() => {
|
248 |
return new Promise((resolve) => {
|
|
|
260 |
}
|
261 |
""")
|
262 |
|
263 |
+
# Wait to ensure lazy content and animations complete
|
264 |
await page.wait_for_timeout(2000)
|
265 |
|
266 |
+
# Take full-page screenshot
|
267 |
image_bytes = await page.screenshot(full_page=True)
|
268 |
image_base64 = base64.b64encode(image_bytes).decode()
|
269 |
+
|
270 |
return {"screenshot": image_base64}
|
271 |
finally:
|
272 |
await browser.close()
|