Skip to content

Commit 06bdf1c

Browse files
fix downloading stuff lol
1 parent 70f25dc commit 06bdf1c

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

app.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
import traceback
1717
from typing import List, Tuple
1818
from playwright.async_api import async_playwright
19+
from playwright._impl._errors import Error as PlaywrightError
20+
1921

2022
# Initialize FastAPI application
2123
app = FastAPI()
@@ -281,6 +283,18 @@ async def dynamic_download(request: Request, key: str, file_name: str = Query(..
281283
response = await page.goto(result)
282284
if response.ok:
283285
content = await response.body()
286+
except Error:
287+
try:
288+
download_future = page.wait_for_event("download")
289+
response = await page.goto(result)
290+
if response.ok:
291+
stream = await (await download_future).create_read_stream() or await response.body()
292+
buffer = io.BytesIO()
293+
async for chunk in stream:
294+
buffer.write(chunk)
295+
content = buffer.getvalue()
296+
except:
297+
pass
284298
finally:
285299
await page.close()
286300
await browser.close()

0 commit comments

Comments
 (0)