61 lines
1.6 KiB
Python
61 lines
1.6 KiB
Python
from fastapi import FastAPI, HTTPException, Header
|
|
from pyppeteer import launch
|
|
import os
|
|
import asyncio
|
|
from typing import Optional
|
|
from urllib.parse import unquote
|
|
|
|
app = FastAPI()
|
|
|
|
# Get API key from environment variable
|
|
API_KEY = os.getenv('API_KEY')
|
|
if not API_KEY:
|
|
raise ValueError("API_KEY environment variable must be set")
|
|
|
|
async def wait_for_network_idle(page):
|
|
"""Wait until no network requests are in flight"""
|
|
await page.waitForNetworkIdle(idleTime=500, timeout=30000)
|
|
|
|
@app.head("/")
|
|
async def health_check():
|
|
return {"status": "ok"}
|
|
|
|
@app.get("/")
|
|
async def visit_url(url: str, x_api_key: Optional[str] = Header(None)):
|
|
# Validate API key
|
|
if not x_api_key or x_api_key != API_KEY:
|
|
raise HTTPException(status_code=401, detail="Invalid API key")
|
|
|
|
try:
|
|
# Decode URL if it's encoded
|
|
decoded_url = unquote(url)
|
|
print(decoded_url)
|
|
|
|
# Launch browser using installed Chrome
|
|
browser = await launch(
|
|
headless=True,
|
|
executablePath='/usr/bin/google-chrome',
|
|
args=['--no-sandbox', '--disable-setuid-sandbox']
|
|
)
|
|
|
|
# Create new page
|
|
page = await browser.newPage()
|
|
|
|
# Navigate to URL and wait for network idle
|
|
await page.goto(decoded_url, waitUntil='networkidle2')
|
|
|
|
# Get page content
|
|
content = await page.content()
|
|
|
|
# Close browser
|
|
await browser.close()
|
|
|
|
return {"status": "success", "content": content}
|
|
|
|
except Exception as e:
|
|
raise HTTPException(status_code=500, detail=str(e))
|
|
|
|
if __name__ == "__main__":
|
|
import uvicorn
|
|
uvicorn.run(app, host="0.0.0.0", port=8000)
|