chore: можно открыть страницу и подождать
This commit is contained in:
1 parent
0602d89012
commit
0f60aa9046
2 files changed
+84
-2
No files matched your search
+69
-2
@@ -1,4 +1,9 @@
|
||||
from datetime import datetime
|
||||
|
||||
from camoufox.async_api import AsyncCamoufox
|
||||
from fastapi import FastAPI
|
||||
from fastapi import Request
|
||||
from playwright.async_api import Page
|
||||
from pydantic import BaseModel
|
||||
|
||||
|
||||
@@ -27,6 +32,68 @@ async def root():
|
||||
return {"message": "Hello World"}
|
||||
|
||||
|
||||
async def _fill(page: "Page", act: ActionRequest) -> str | None:
|
||||
...
|
||||
|
||||
|
||||
async def _mouse_move(page: "Page", act: ActionRequest) -> str | None:
|
||||
...
|
||||
|
||||
|
||||
async def _click(page: "Page", act: ActionRequest) -> str | None:
|
||||
...
|
||||
|
||||
|
||||
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
|
||||
|
||||
|
||||
@app.post("/v1/solve")
|
||||
async def solve(payload: SolveRequest):
|
||||
return {"message": payload.model_dump(exclude_none=True)}
|
||||
async def solve(request: Request, payload: SolveRequest):
|
||||
api_key = request.headers.get('X-API-Key')
|
||||
async with AsyncCamoufox() as camoufo:
|
||||
page = await camoufo.new_page()
|
||||
page.set_default_timeout(30_000)
|
||||
await page.goto(url=payload.url)
|
||||
actions_result = []
|
||||
status = True
|
||||
error = None
|
||||
|
||||
for act in payload.actions:
|
||||
result = await {
|
||||
"fill": _fill,
|
||||
"mouse_move": _mouse_move,
|
||||
"click": _click,
|
||||
"wait_for": _wait_for,
|
||||
}[act.type](page, act)
|
||||
if( ok := result ) is None:
|
||||
act_log = {"type": act.type, "ok": ok}
|
||||
else:
|
||||
act_log = {"type": act.type, "ok": ok, "error": result}
|
||||
actions_result.append(act_log)
|
||||
if "error" in act_log:
|
||||
status = False
|
||||
error = act_log["error"]
|
||||
break
|
||||
|
||||
cookies = await camoufo.cookies()
|
||||
html = await page.inner_html('html')
|
||||
await page.screenshot(
|
||||
path=(
|
||||
f"/home/sokol/PycharmProjects/WebRoboApi/extra/screenshots/"
|
||||
f"{datetime.timestamp(datetime.now())}.png"
|
||||
)
|
||||
)
|
||||
return {
|
||||
"status": ["err", "ok"][status],
|
||||
"html": html,
|
||||
"cookies": cookies,
|
||||
"actions": actions_result,
|
||||
"error": error,
|
||||
}
|
||||
@@ -33,3 +33,18 @@ X-API-Key: <key>
|
||||
}
|
||||
|
||||
###
|
||||
POST http://127.0.0.1:8000/v1/solve
|
||||
Accept: application/json
|
||||
Content-Type: application/json
|
||||
X-API-Key: <key>
|
||||
|
||||
|
||||
{
|
||||
"url": "https://camoufox.com/python/usage/",
|
||||
"proxy": "-",
|
||||
"actions": [
|
||||
{"type": "wait_for", "selector": "span.dark\\:text-white", "timeout": 30}
|
||||
]
|
||||
}
|
||||
|
||||
###
|
||||
Reference in new issue
Block a user