File size: 3,649 Bytes
b10df45
 
 
 
 
 
 
 
 
 
 
 
 
 
ceb407a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
928cdf1
 
b10df45
928cdf1
 
 
ceb407a
928cdf1
b10df45
928cdf1
b10df45
ceb407a
 
 
b10df45
 
ceb407a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
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)}")