Arkm20 commited on
Commit
bd5ae68
·
verified ·
1 Parent(s): e4114fe

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -0
app.py CHANGED
@@ -43,6 +43,29 @@ def fetch_html(url: str):
43
  except Exception as e:
44
  return {"error": str(e)}
45
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46
  if __name__ == "__main__":
47
  import uvicorn
48
  os.system("playwright install-deps")
 
43
  except Exception as e:
44
  return {"error": str(e)}
45
 
46
+ @app.get("/download")
47
+ def download_screenshot(url: str):
48
+ try:
49
+ with sync_playwright() as p:
50
+ browser = p.chromium.launch(headless=True)
51
+ page = browser.new_page()
52
+
53
+ # Set custom headers before navigating to the page
54
+ headers = get_custom_headers(url)
55
+ page.set_extra_http_headers(headers)
56
+
57
+ # Navigate to the URL and wait for network idle
58
+ page.goto(url, timeout=15000, wait_until="networkidle")
59
+
60
+ # Take a screenshot of the page once content is fully loaded
61
+ screenshot = page.screenshot(full_page=True) # Set full_page=True to capture the entire page
62
+ browser.close()
63
+
64
+ # Return the screenshot as a response with image/png content type
65
+ return Response(content=screenshot, media_type="image/png")
66
+ except Exception as e:
67
+ return {"error": str(e)}
68
+
69
  if __name__ == "__main__":
70
  import uvicorn
71
  os.system("playwright install-deps")