This commit is contained in:
Binary file not shown.
@@ -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"""
|
||||
|
||||
Reference in New Issue
Block a user