1515import re
1616import traceback
1717from typing import List , Tuple
18+ from playwright .async_api import async_playwright
1819
1920# Initialize FastAPI application
2021app = FastAPI ()
@@ -35,6 +36,7 @@ def verify_password(stored_hash, password):
3536# Retrieve MongoDB connection string and admin password from environment variables
3637MONGO_URI = os .environ .get ('MONGO_URI' )
3738ADMIN_PASSWORD = hash_password (os .environ .get ('ADMIN_PASSWORD' ))
39+ BROWSER_WS = os .environ .get ('BROWSER_WS' )
3840
3941if not MONGO_URI :
4042 raise ValueError ("MONGO_URI environment variable is not set." )
@@ -150,15 +152,29 @@ async def execute_async_code(code):
150152 raise RuntimeError ("Error executing dynamic code." ) from e
151153
152154async def fetch_data (url ):
153- async with aiohttp .ClientSession () as session :
154- async with session .get (url ) as response :
155- if response .ok :
156- try :
157- return await response .json ()
158- except (aiohttp .ContentTypeError , json .JSONDecodeError ):
159- return await response .text ()
160- else :
161- return f"Request failed with status code { response .status } "
155+ if not BROWSER_WS :
156+ async with aiohttp .ClientSession () as session :
157+ async with session .get (url ) as response :
158+ if response .ok :
159+ try :
160+ return await response .json ()
161+ except (aiohttp .ContentTypeError , json .JSONDecodeError ):
162+ return await response .text ()
163+ else :
164+ return f"Request failed with status code { response .status } "
165+ else :
166+ async with async_playwright () as playwright :
167+ async with playwright .chromium .connect (BROWSER_WS ) as browser :
168+ async with browser .new_page () as page :
169+ async with page .goto (url ) as response :
170+ if response .ok :
171+ try :
172+ return await response .json ()
173+ except (json .JSONDecodeError ):
174+ return await response .text ()
175+ else :
176+ return f"Request failed with status code { response .status } "
177+
162178
163179@app .get ("/" , response_class = HTMLResponse )
164180async def home (request : Request ):
@@ -246,10 +262,17 @@ async def dynamic_download(request: Request, key: str, file_name: str = Query(..
246262 try :
247263 result = await execute_async_code (code )
248264
249- async with aiohttp .ClientSession () as session :
250- async with session .get (result ) as response :
251- response .raise_for_status ()
252- content = await response .read ()
265+ if not BROWSER_WS :
266+ async with aiohttp .ClientSession () as session :
267+ async with session .get (result ) as response :
268+ response .raise_for_status ()
269+ content = await response .read ()
270+ async with async_playwright () as playwright :
271+ async with playwright .chromium .connect (BROWSER_WS ) as browser :
272+ async with browser .new_page () as page :
273+ async with page .goto (url ) as response :
274+ if response .ok :
275+ content = await response .body ()
253276
254277 return StreamingResponse (
255278 iter ([content ]),
0 commit comments