Skip to content

Commit 3e84dab

Browse files
im pretty special smh
1 parent e6488eb commit 3e84dab

1 file changed

Lines changed: 25 additions & 15 deletions

File tree

app.py

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -164,16 +164,21 @@ async def fetch_data(url):
164164
return f"Request failed with status code {response.status}"
165165
else:
166166
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}"
167+
browser = await playwright.chromium.connect(BROWSER_WS)
168+
async with browser:
169+
page = await browser.new_page()
170+
try:
171+
response = await page.goto(url)
172+
if response.ok:
173+
try:
174+
return await response.json()
175+
except (json.JSONDecodeError):
176+
return await response.text()
177+
else:
178+
return f"Request failed with status code {response.status}"
179+
finally:
180+
await page.close()
181+
await browser.close()
177182

178183

179184
@app.get("/", response_class=HTMLResponse)
@@ -269,11 +274,16 @@ async def dynamic_download(request: Request, key: str, file_name: str = Query(..
269274
content = await response.read()
270275
else:
271276
async with async_playwright() as playwright:
272-
async with playwright.chromium.connect(BROWSER_WS) as browser:
273-
async with browser.new_page() as page:
274-
async with page.goto(url) as response:
275-
if response.ok:
276-
content = await response.body()
277+
browser = await playwright.chromium.connect(BROWSER_WS)
278+
async with browser:
279+
page = await browser.new_page()
280+
try:
281+
response = await page.goto(url)
282+
if response.ok:
283+
content = await response.body()
284+
finally:
285+
await page.close()
286+
await browser.close()
277287

278288
return StreamingResponse(
279289
iter([content]),

0 commit comments

Comments
 (0)