first commit
This commit is contained in:
commit
dc1245f164
22 changed files with 1666 additions and 0 deletions
65
monitor/snapshot.py
Normal file
65
monitor/snapshot.py
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
"""Page snapshot capture logic."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import datetime
|
||||
|
||||
from playwright.sync_api import Error as PlaywrightError, Page
|
||||
|
||||
from monitor.models import PageSnapshot, WebsiteConfig
|
||||
from storage.snapshots import SnapshotStore
|
||||
|
||||
|
||||
def capture_page_snapshot(
|
||||
page: Page,
|
||||
website: WebsiteConfig,
|
||||
load_duration: float,
|
||||
snapshot_store: SnapshotStore,
|
||||
timestamp: float,
|
||||
) -> PageSnapshot:
|
||||
"""Capture page metadata, HTML, text, and screenshot."""
|
||||
captured_at = datetime.fromtimestamp(timestamp)
|
||||
screenshot_path = None
|
||||
title = ""
|
||||
visible_text = ""
|
||||
html = ""
|
||||
error_parts: list[str] = []
|
||||
|
||||
try:
|
||||
title = page.title()
|
||||
except PlaywrightError as exc:
|
||||
error_parts.append(f"title: {exc}")
|
||||
|
||||
try:
|
||||
visible_text = page.inner_text("body")
|
||||
except PlaywrightError as exc:
|
||||
error_parts.append(f"visible_text: {exc}")
|
||||
|
||||
try:
|
||||
html = page.content()
|
||||
except PlaywrightError as exc:
|
||||
error_parts.append(f"html: {exc}")
|
||||
|
||||
try:
|
||||
screenshot_path = snapshot_store.save_screenshot(
|
||||
website_id=website.id,
|
||||
timestamp=captured_at,
|
||||
image_bytes=page.screenshot(full_page=True),
|
||||
)
|
||||
except PlaywrightError as exc:
|
||||
error_parts.append(f"screenshot: {exc}")
|
||||
|
||||
return PageSnapshot(
|
||||
website_id=website.id,
|
||||
website_name=website.name,
|
||||
requested_url=website.url,
|
||||
final_url=page.url,
|
||||
timestamp=captured_at,
|
||||
title=title,
|
||||
visible_text=visible_text,
|
||||
html=html,
|
||||
screenshot_path=screenshot_path,
|
||||
load_duration=load_duration,
|
||||
success=len(error_parts) == 0,
|
||||
error="; ".join(error_parts) if error_parts else None,
|
||||
)
|
||||
Loading…
Add table
Add a link
Reference in a new issue