From 1a45976356e77c8dd4a724892a8a1e4f7760ccf6 Mon Sep 17 00:00:00 2001 From: protokey Date: Thu, 10 Sep 2026 22:43:52 +0400 Subject: [PATCH] =?UTF-8?q?chore:=20=D0=BE=D0=B1=D1=89=D0=B8=D0=B5=20?= =?UTF-8?q?=D0=BF=D1=80=D0=B0=D0=B2=D0=BA=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/engine/actions.py | 33 ++++++++++++++++++++++----------- src/engine/schemas.py | 1 + src/main.py | 9 ++++++--- tests/test_main.http | 6 +++--- 4 files changed, 32 insertions(+), 17 deletions(-) diff --git a/src/engine/actions.py b/src/engine/actions.py index 17852d7..cdcde0a 100644 --- a/src/engine/actions.py +++ b/src/engine/actions.py @@ -25,25 +25,36 @@ async def click(page: Page, act: ActionRequest) -> str | None: ... +@register +async def inner_html(page: Page, act: ActionRequest) -> str | None: + assert act.selector + element = await page.query_selector(act.selector) + return await element.inner_html() + + @register async def wait_for(page: Page, act: ActionRequest) -> str | None: - try: - assert act.selector - await page.wait_for_selector(act.selector, timeout=act.timeout * 1000) - except Exception as e: - return str(e) - else: - return None + assert act.selector + assert 1 <= act.timeout <= 7200 + await page.wait_for_selector(act.selector, timeout=act.timeout * 1000) -async def execute(page: Page, actions: list[ActionRequest]) -> list[str]: +async def execute(page: Page, actions: list[ActionRequest]) -> list[dict]: actions_result = [] for act in actions: + new_line = { + "type": act.type, + } try: - await _actions[act.type](page, act) + result = await _actions[act.type](page, act) except Exception as e: - actions_result.append({"type": act.type, "ok": "false", "error": str(e)}) + new_line["ok"] = False + new_line["result"] = str(e) + actions_result.append(new_line) break else: - actions_result.append({"type": act.type, "ok": "true"}) + new_line["ok"] = True + if result: + new_line["result"] = result + actions_result.append(new_line) return actions_result diff --git a/src/engine/schemas.py b/src/engine/schemas.py index 483c4fa..d6390e1 100644 --- a/src/engine/schemas.py +++ b/src/engine/schemas.py @@ -24,3 +24,4 @@ class SolveRequest(BaseModel): timeout: int | None = None screen: str = "1280x720" user_agent: str | None = None + html: bool = False diff --git a/src/main.py b/src/main.py index 61639aa..3738d04 100644 --- a/src/main.py +++ b/src/main.py @@ -140,10 +140,13 @@ async def solve( ) BROWSER_SLOTS += 1 - return { + result = { "status": ["err", "ok"][status], - "html": html, "cookies": cookies, "actions": actions_result, - "error": error, } + if payload.html: + result["html"] = html + if error: + result["error"] = error + return result diff --git a/tests/test_main.http b/tests/test_main.http index f31dbff..3ab172f 100644 --- a/tests/test_main.http +++ b/tests/test_main.http @@ -37,14 +37,15 @@ POST http://127.0.0.1:8000/v1/solve Accept: application/json Content-Type: application/json X-API-Key: -Cookie: session_id=b8f770a3-38c2-45f2-944b-2703798e3503; Path=/; SameSite=lax +Cookie: session_id=2f2100be-6ce7-40d5-bae5-61424b2504c8; Path=/; SameSite=lax { "url": "https://browserleaks.com/canvas", "proxy": "-", "actions": [ - {"type": "wait_for", "selector": "#gl-image-src1", "timeout": 30} + {"type": "wait_for", "selector": "#canvas-hash", "timeout": 10}, + {"type": "inner_html", "selector": "#canvas-hash"} ] } @@ -53,7 +54,6 @@ POST http://127.0.0.1:8000/v1/solve Accept: application/json Content-Type: application/json X-API-Key: -Cookie: session_id=b8f770a3-38c2-45f2-944b-2703798e3503; Path=/; SameSite=lax {