fignjk / app.py
himanshu-muk
Improve flowPages detection with multiple attempts and better waiting
ceb407a
import asyncio
from fastapi import FastAPI, HTTPException
from playwright.async_api import async_playwright
app = FastAPI()
@app.post("/run")
async def run_flow_pages(url: str):
async with async_playwright() as p:
browser = await p.chromium.launch(headless=True)
page = await browser.new_page()
try:
# Navigate to the page and wait for network to be idle
await page.goto(url, wait_until="networkidle", timeout=60000)
# Wait for page to be fully interactive
await page.wait_for_load_state("domcontentloaded")
await asyncio.sleep(3)
# Try to get flowPages with multiple attempts and different methods
flow_pages_data = None
max_attempts = 5
for attempt in range(max_attempts):
# Try multiple ways to access flowPages
flow_pages_data = await page.evaluate("""() => {
// Try direct access
try {
if (typeof flowPages !== 'undefined' && flowPages !== null) {
return JSON.parse(JSON.stringify(flowPages));
}
} catch(e) {}
// Try window.flowPages
try {
if (typeof window !== 'undefined' && window.flowPages !== undefined && window.flowPages !== null) {
return JSON.parse(JSON.stringify(window.flowPages));
}
} catch(e) {}
// Try globalThis.flowPages
try {
if (typeof globalThis !== 'undefined' && globalThis.flowPages !== undefined && globalThis.flowPages !== null) {
return JSON.parse(JSON.stringify(globalThis.flowPages));
}
} catch(e) {}
// Try document.flowPages
try {
if (typeof document !== 'undefined' && document.flowPages !== undefined && document.flowPages !== null) {
return JSON.parse(JSON.stringify(document.flowPages));
}
} catch(e) {}
// Try to find it in window properties
try {
for (let key in window) {
if (key.toLowerCase().includes('flow') && window[key] !== null) {
return JSON.parse(JSON.stringify(window[key]));
}
}
} catch(e) {}
return null;
}""")
if flow_pages_data is not None:
break
# Wait a bit more before next attempt
await asyncio.sleep(2)
await browser.close()
if flow_pages_data is None:
raise HTTPException(
status_code=500,
detail="flowPages not found on the page after multiple attempts. The page may not have loaded flowPages yet or it may be in a different format."
)
return flow_pages_data
except HTTPException:
await browser.close()
raise
except Exception as e:
await browser.close()
raise HTTPException(status_code=500, detail=f"Error fetching flowPages: {str(e)}")