Skip to content

Supercharge your AI agents with data from the web and beyond. Building the library for superintelligence. 🔥

Update main.py

Nicolasf2af7408e802
diff --git a/apps/playwright-service/main.py b/apps/playwright-service/main.py
index b4b83de9b..7a6e620c7 100644
--- a/apps/playwright-service/main.py
+++ b/apps/playwright-service/main.py
@@ -1,29 +1,36 @@
-from fastapi import FastAPI, Response
-from playwright.async_api import async_playwright
-import os
+from fastapi import FastAPI
+from playwright.async_api import async_playwright, Browser
from fastapi.responses import JSONResponse
from pydantic import BaseModel
+
app = FastAPI()
-from pydantic import BaseModel
class UrlModel(BaseModel):
url: str
[email protected]("/html") # Kept as POST to accept body parameters
-async def root(body: UrlModel): # Using Pydantic model for request body
- async with async_playwright() as p:
- browser = await p.chromium.launch()
- context = await browser.new_context()
- page = await context.new_page()
+browser: Browser = None
+
+
[email protected]_event("startup")
+async def startup_event():
+ global browser
+ playwright = await async_playwright().start()
+ browser = await playwright.chromium.launch()
+
- await page.goto(body.url) # Adjusted to use the url from the request body model
- page_content = await page.content() # Get the HTML content of the page
[email protected]_event("shutdown")
+async def shutdown_event():
+ await browser.close()
- await context.close()
- await browser.close()
- json_compatible_item_data = {"content": page_content}
- return JSONResponse(content=json_compatible_item_data)
-
+async def root(body: UrlModel):
+ context = await browser.new_context()
+ page = await context.new_page()
+ await page.goto(body.url)
+ page_content = await page.content()
+ await context.close()
+ json_compatible_item_data = {"content": page_content}
+ return JSONResponse(content=json_compatible_item_data)