This commit is contained in:
@@ -3,9 +3,11 @@ 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 pydantic import BaseModel
|
||||
from app.services.browser import (
|
||||
visit_url_service,
|
||||
screenshot_url_service,
|
||||
click_selector_service,
|
||||
extract_seo_service,
|
||||
extract_meta_tags_service,
|
||||
capture_outgoing_calls_service,
|
||||
@@ -14,6 +16,12 @@ from app.services.browser import (
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
class ClickRequest(BaseModel):
|
||||
url: str
|
||||
selector: str
|
||||
# Default behavior: return screenshot after clicking.
|
||||
returnType: Optional[str] = "screenshot"
|
||||
|
||||
@router.get("/")
|
||||
async def visit_url(url: str, skipCache: bool = False, x_api_key: Optional[str] = Header(None)):
|
||||
# Validate API key
|
||||
@@ -66,6 +74,36 @@ async def screenshot_url(url: str, fullPage: bool = True, x_api_key: Optional[st
|
||||
print(f"Error taking screenshot for URL {decoded_url}: {e}")
|
||||
raise HTTPException(status_code=500, detail=str(e))
|
||||
|
||||
@router.post("/click")
|
||||
async def click_selector(payload: ClickRequest, x_api_key: Optional[str] = Header(None)):
|
||||
if not x_api_key or x_api_key != API_KEY:
|
||||
raise HTTPException(status_code=401, detail="Invalid API key")
|
||||
|
||||
decoded_url = unquote(payload.url)
|
||||
return_type = (payload.returnType or "screenshot").lower()
|
||||
if return_type not in ("screenshot", "html"):
|
||||
raise HTTPException(status_code=400, detail="Invalid returnType. Use 'screenshot' or 'html'.")
|
||||
|
||||
try:
|
||||
result = await click_selector_service(decoded_url, payload.selector, return_type)
|
||||
|
||||
if isinstance(result, dict) and result.get("status") == "error":
|
||||
raise HTTPException(status_code=500, detail=result.get("error", "Click failed"))
|
||||
|
||||
if return_type == "html":
|
||||
if not isinstance(result, str):
|
||||
raise HTTPException(status_code=500, detail="Unexpected HTML response")
|
||||
return Response(content=result, media_type="text/html; charset=utf-8")
|
||||
|
||||
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 clicking selector on 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