fix view
This commit is contained in:
+33
-11
@@ -135,27 +135,41 @@ class BrowserService:
|
||||
) -> VisitResult:
|
||||
"""Выполняет просмотр страницы."""
|
||||
page = await browser.new_page()
|
||||
|
||||
initial_url = url
|
||||
final_url = url
|
||||
redirect = False
|
||||
reading_completed = False
|
||||
|
||||
try:
|
||||
await page.set_viewport_size({
|
||||
"width": settings.VIEWPORT_WIDTH,
|
||||
"height": settings.VIEWPORT_HEIGHT
|
||||
})
|
||||
|
||||
|
||||
goto_task = asyncio.create_task(
|
||||
page.goto(url, wait_until="commit", timeout=30000)
|
||||
)
|
||||
await self._move_mouse(page, goto_task)
|
||||
await goto_task
|
||||
|
||||
|
||||
initial_url = page.url
|
||||
final_url, redirect = await self._wait_redirect(page, initial_url)
|
||||
|
||||
try:
|
||||
final_url, redirect = await self._wait_redirect(page, initial_url)
|
||||
except Exception as e:
|
||||
logger.warning(f"Redirect wait error (ignored): {e}")
|
||||
final_url = page.url
|
||||
|
||||
await self._simulate_reading(page, reading_time)
|
||||
|
||||
reading_completed = True
|
||||
|
||||
screenshot_path = None
|
||||
if settings.SCREENSHOTS_DIR:
|
||||
screenshot_path = await self._take_screenshot(page, final_url)
|
||||
|
||||
try:
|
||||
screenshot_path = await self._take_screenshot(page, final_url)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
return VisitResult(
|
||||
url=url,
|
||||
initial_url=initial_url,
|
||||
@@ -166,12 +180,20 @@ class BrowserService:
|
||||
screenshot_path=screenshot_path,
|
||||
success=True
|
||||
)
|
||||
except asyncio.CancelledError:
|
||||
raise
|
||||
except Exception as e:
|
||||
return VisitResult(url=url, reading_time=reading_time,
|
||||
success=False, error=str(e))
|
||||
return VisitResult(
|
||||
url=url,
|
||||
initial_url=initial_url,
|
||||
final_url=final_url,
|
||||
redirect_occurred=redirect,
|
||||
proxy_info=proxy_info,
|
||||
reading_time=reading_time,
|
||||
success=reading_completed,
|
||||
error=str(e) if not reading_completed else None
|
||||
)
|
||||
finally:
|
||||
if page.url == url:
|
||||
logger.error()
|
||||
await page.close()
|
||||
|
||||
async def _move_mouse(self, page, task):
|
||||
|
||||
Reference in New Issue
Block a user