diff --git a/Dockers/puppeteer-api/README.md b/Dockers/puppeteer-api/README.md index d9b0c0a..3d91859 100644 --- a/Dockers/puppeteer-api/README.md +++ b/Dockers/puppeteer-api/README.md @@ -176,6 +176,7 @@ POSTGRES_PASSWORD= # PostgreSQL password (optional) ### Core Endpoints - `GET /` - Visit URL and get HTML content (with Cloudflare bypass) +- `GET /screenshot` - Capture a screenshot of a URL (PNG output) - `GET /seo` - Extract SEO information (with Cloudflare bypass) - `GET /meta` - Extract meta tags and Open Graph data (with Cloudflare bypass) - `GET /test-cloudflare` - Test Cloudflare bypass functionality on a specific URL @@ -198,6 +199,9 @@ POSTGRES_PASSWORD= # PostgreSQL password (optional) # Visit a URL curl -H "X-API-Key: your-api-key" "http://localhost:8000/?url=https://example.com" +# Capture a full-page screenshot (PNG) +curl -H "X-API-Key: your-api-key" "http://localhost:8000/screenshot?url=https://example.com" --output screenshot.png + # Extract SEO data curl -H "X-API-Key: your-api-key" "http://localhost:8000/seo?url=https://example.com" diff --git a/Dockers/puppeteer-api/app/routes/__pycache__/browser.cpython-313.pyc b/Dockers/puppeteer-api/app/routes/__pycache__/browser.cpython-313.pyc new file mode 100644 index 0000000..22600c9 Binary files /dev/null and b/Dockers/puppeteer-api/app/routes/__pycache__/browser.cpython-313.pyc differ diff --git a/Dockers/puppeteer-api/app/routes/browser.py b/Dockers/puppeteer-api/app/routes/browser.py index 2c08b78..987b1f6 100644 --- a/Dockers/puppeteer-api/app/routes/browser.py +++ b/Dockers/puppeteer-api/app/routes/browser.py @@ -1,10 +1,11 @@ -from fastapi import APIRouter, HTTPException, Header +from fastapi import APIRouter, HTTPException, Header, Response from typing import Optional from urllib.parse import unquote from app.config import API_KEY from app.database import get_cached_data, save_to_cache from app.services.browser import ( visit_url_service, + screenshot_url_service, extract_seo_service, extract_meta_tags_service, capture_outgoing_calls_service, @@ -41,6 +42,30 @@ async def visit_url(url: str, skipCache: bool = False, x_api_key: Optional[str] print(f"Error visiting URL {decoded_url}: {e}") raise HTTPException(status_code=500, detail=str(e)) +@router.get("/screenshot") +async def screenshot_url(url: str, fullPage: bool = True, x_api_key: Optional[str] = Header(None)): + """Capture a screenshot of a website and return it as PNG""" + if not x_api_key or x_api_key != API_KEY: + raise HTTPException(status_code=401, detail="Invalid API key") + + decoded_url = unquote(url) + + try: + result = await screenshot_url_service(decoded_url, fullPage) + + if isinstance(result, dict) and result.get("status") == "error": + raise HTTPException(status_code=500, detail=result.get("error", "Screenshot capture failed")) + + if not isinstance(result, (bytes, bytearray)): + raise HTTPException(status_code=500, detail="Unexpected screenshot response") + + return Response(content=result, media_type="image/png") + except HTTPException: + raise + except Exception as e: + print(f"Error taking screenshot for URL {decoded_url}: {e}") + raise HTTPException(status_code=500, detail=str(e)) + @router.get("/seo") async def extract_seo(url: str, skipCache: bool = False, x_api_key: Optional[str] = Header(None)): """Extract SEO information from a website""" diff --git a/Dockers/puppeteer-api/app/services/__pycache__/browser.cpython-313.pyc b/Dockers/puppeteer-api/app/services/__pycache__/browser.cpython-313.pyc new file mode 100644 index 0000000..a4ed96a Binary files /dev/null and b/Dockers/puppeteer-api/app/services/__pycache__/browser.cpython-313.pyc differ diff --git a/Dockers/puppeteer-api/app/services/browser.py b/Dockers/puppeteer-api/app/services/browser.py index 22b0635..435d853 100644 --- a/Dockers/puppeteer-api/app/services/browser.py +++ b/Dockers/puppeteer-api/app/services/browser.py @@ -55,6 +55,22 @@ async def visit_url_service(decoded_url): # Perform the operation return await safe_browser_operation(decoded_url, visit_operation) +async def screenshot_url_service(decoded_url, full_page=True): + """Service function to visit a URL and return a screenshot as PNG bytes""" + print(f"Taking screenshot of: {decoded_url}") + + async def screenshot_operation(page): + try: + await page.goto(decoded_url, wait_until='networkidle', timeout=30000) + await page.wait_for_timeout(1000) + screenshot_bytes = await page.screenshot(full_page=full_page, type='png') + return screenshot_bytes + except Exception as e: + print(f"Error during screenshot capture: {e}") + return {"status": "error", "url": decoded_url, "error": str(e)} + + return await safe_browser_operation(decoded_url, screenshot_operation) + async def extract_seo_service(decoded_url): """Service function to extract SEO information from a website""" print(f"Extracting SEO from: {decoded_url}")