Spaces:
Sleeping
Sleeping
Delete utils/website_capturer.py
Browse files- utils/website_capturer.py +0 -129
utils/website_capturer.py
DELETED
|
@@ -1,129 +0,0 @@
|
|
| 1 |
-
"""
|
| 2 |
-
Enhanced Website Capturer - Full Page Screenshot
|
| 3 |
-
Captures complete page height while maintaining fixed width
|
| 4 |
-
"""
|
| 5 |
-
|
| 6 |
-
import asyncio
|
| 7 |
-
from pathlib import Path
|
| 8 |
-
from typing import Dict, Tuple
|
| 9 |
-
import logging
|
| 10 |
-
|
| 11 |
-
logger = logging.getLogger(__name__)
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
async def capture_website_fullpage(
|
| 15 |
-
website_url: str,
|
| 16 |
-
output_dir: str = "./reports",
|
| 17 |
-
desktop_width: int = 1440,
|
| 18 |
-
mobile_width: int = 375
|
| 19 |
-
) -> Dict[str, str]:
|
| 20 |
-
"""
|
| 21 |
-
Capture full-page screenshots of website at multiple viewports.
|
| 22 |
-
|
| 23 |
-
Args:
|
| 24 |
-
website_url: URL of the website to capture
|
| 25 |
-
output_dir: Directory to save screenshots
|
| 26 |
-
desktop_width: Desktop viewport width (height auto-calculated)
|
| 27 |
-
mobile_width: Mobile viewport width (height auto-calculated)
|
| 28 |
-
|
| 29 |
-
Returns:
|
| 30 |
-
Dictionary with paths to captured screenshots
|
| 31 |
-
"""
|
| 32 |
-
try:
|
| 33 |
-
from playwright.async_api import async_playwright
|
| 34 |
-
except ImportError:
|
| 35 |
-
raise ImportError("Playwright not installed. Run: pip install playwright")
|
| 36 |
-
|
| 37 |
-
Path(output_dir).mkdir(parents=True, exist_ok=True)
|
| 38 |
-
screenshots = {}
|
| 39 |
-
|
| 40 |
-
async with async_playwright() as p:
|
| 41 |
-
browser = await p.chromium.launch(headless=True)
|
| 42 |
-
|
| 43 |
-
try:
|
| 44 |
-
# Desktop capture - Full page height
|
| 45 |
-
print(" 📱 Capturing desktop (1440px width, full height)...")
|
| 46 |
-
page = await browser.new_page(viewport={"width": desktop_width, "height": 1080})
|
| 47 |
-
await page.goto(website_url, wait_until="networkidle")
|
| 48 |
-
|
| 49 |
-
# Get full page height
|
| 50 |
-
desktop_height = await page.evaluate("() => document.documentElement.scrollHeight")
|
| 51 |
-
print(f" ℹ️ Desktop full height: {desktop_height}px")
|
| 52 |
-
|
| 53 |
-
# Set viewport to full height and capture
|
| 54 |
-
await page.set_viewport_size({"width": desktop_width, "height": desktop_height})
|
| 55 |
-
desktop_path = f"{output_dir}/desktop_{desktop_width}x{desktop_height}.png"
|
| 56 |
-
await page.screenshot(path=desktop_path, full_page=True)
|
| 57 |
-
screenshots["desktop"] = desktop_path
|
| 58 |
-
print(f" ✓ Saved: {desktop_path}")
|
| 59 |
-
|
| 60 |
-
await page.close()
|
| 61 |
-
|
| 62 |
-
# Mobile capture - Full page height
|
| 63 |
-
print(" 📱 Capturing mobile (375px width, full height)...")
|
| 64 |
-
page = await browser.new_page(viewport={"width": mobile_width, "height": 812})
|
| 65 |
-
await page.goto(website_url, wait_until="networkidle")
|
| 66 |
-
|
| 67 |
-
# Get full page height
|
| 68 |
-
mobile_height = await page.evaluate("() => document.documentElement.scrollHeight")
|
| 69 |
-
print(f" ℹ️ Mobile full height: {mobile_height}px")
|
| 70 |
-
|
| 71 |
-
# Set viewport to full height and capture
|
| 72 |
-
await page.set_viewport_size({"width": mobile_width, "height": mobile_height})
|
| 73 |
-
mobile_path = f"{output_dir}/mobile_{mobile_width}x{mobile_height}.png"
|
| 74 |
-
await page.screenshot(path=mobile_path, full_page=True)
|
| 75 |
-
screenshots["mobile"] = mobile_path
|
| 76 |
-
print(f" ✓ Saved: {mobile_path}")
|
| 77 |
-
|
| 78 |
-
await page.close()
|
| 79 |
-
|
| 80 |
-
finally:
|
| 81 |
-
await browser.close()
|
| 82 |
-
|
| 83 |
-
return screenshots
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
def capture_website_sync(
|
| 87 |
-
website_url: str,
|
| 88 |
-
output_dir: str = "./reports",
|
| 89 |
-
desktop_width: int = 1440,
|
| 90 |
-
mobile_width: int = 375
|
| 91 |
-
) -> Dict[str, str]:
|
| 92 |
-
"""
|
| 93 |
-
Synchronous wrapper for capturing full-page website screenshots.
|
| 94 |
-
|
| 95 |
-
Args:
|
| 96 |
-
website_url: URL of the website to capture
|
| 97 |
-
output_dir: Directory to save screenshots
|
| 98 |
-
desktop_width: Desktop viewport width
|
| 99 |
-
mobile_width: Mobile viewport width
|
| 100 |
-
|
| 101 |
-
Returns:
|
| 102 |
-
Dictionary with paths to captured screenshots
|
| 103 |
-
"""
|
| 104 |
-
return asyncio.run(
|
| 105 |
-
capture_website_fullpage(
|
| 106 |
-
website_url,
|
| 107 |
-
output_dir,
|
| 108 |
-
desktop_width,
|
| 109 |
-
mobile_width
|
| 110 |
-
)
|
| 111 |
-
)
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
if __name__ == "__main__":
|
| 115 |
-
# Test the function
|
| 116 |
-
import sys
|
| 117 |
-
|
| 118 |
-
if len(sys.argv) < 2:
|
| 119 |
-
print("Usage: python website_capturer_fullpage.py <url> [output_dir]")
|
| 120 |
-
sys.exit(1)
|
| 121 |
-
|
| 122 |
-
url = sys.argv[1]
|
| 123 |
-
output = sys.argv[2] if len(sys.argv) > 2 else "./reports"
|
| 124 |
-
|
| 125 |
-
print(f"Capturing website: {url}")
|
| 126 |
-
result = capture_website_sync(url, output)
|
| 127 |
-
print(f"\nScreenshots saved:")
|
| 128 |
-
for key, path in result.items():
|
| 129 |
-
print(f" {key}: {path}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|