replace puppeteer with playwright
Build and Push Docker Images / build-and-push (push) Has been cancelled
Build and Push Docker Images / build-and-push (push) Has been cancelled
This commit is contained in:
@@ -1,31 +1,34 @@
|
||||
from pyppeteer import launch
|
||||
from playwright.async_api import async_playwright
|
||||
from app.config import CUSTOM_USER_AGENT
|
||||
import asyncio
|
||||
|
||||
async def wait_for_network_idle(page):
|
||||
"""Wait until no network requests are in flight"""
|
||||
await page.waitForNetworkIdle(idleTime=500, timeout=30000)
|
||||
await page.wait_for_load_state('networkidle')
|
||||
|
||||
async def safe_browser_operation(url, operation_func):
|
||||
"""Safely perform browser operations with proper cleanup"""
|
||||
browser = None
|
||||
context = None
|
||||
page = None
|
||||
try:
|
||||
browser = await launch(
|
||||
# Get or create playwright instance
|
||||
playwright = await async_playwright().start()
|
||||
|
||||
browser = await playwright.chromium.launch(
|
||||
headless=True,
|
||||
executablePath='/usr/bin/google-chrome',
|
||||
args=['--no-sandbox', '--disable-setuid-sandbox'],
|
||||
handleSIGINT=False,
|
||||
handleSIGTERM=False,
|
||||
handleSIGHUP=False
|
||||
)
|
||||
|
||||
# Create new page with timeout
|
||||
page = await browser.newPage()
|
||||
page.setDefaultNavigationTimeout(30000)
|
||||
# Create context and page
|
||||
context = await browser.new_context(
|
||||
user_agent=CUSTOM_USER_AGENT,
|
||||
viewport={'width': 1920, 'height': 1080},
|
||||
ignore_https_errors=True,
|
||||
)
|
||||
|
||||
# Set custom user agent
|
||||
await page.setUserAgent(CUSTOM_USER_AGENT)
|
||||
page = await context.new_page()
|
||||
page.set_default_timeout(30000)
|
||||
|
||||
# Call the operation function that uses the page
|
||||
result = await operation_func(page)
|
||||
@@ -48,6 +51,13 @@ async def safe_browser_operation(url, operation_func):
|
||||
except Exception as e:
|
||||
print(f"Error closing page: {e}")
|
||||
|
||||
# Ensure context is closed properly
|
||||
if context:
|
||||
try:
|
||||
await context.close()
|
||||
except Exception as e:
|
||||
print(f"Error closing context: {e}")
|
||||
|
||||
# Ensure browser is closed properly
|
||||
if browser:
|
||||
try:
|
||||
|
||||
Reference in New Issue
Block a user