chore: перенос этапа прохождения антибот защиты в метод исполнения команд
это позволит выполнять проверку и обход защиты между командами
This commit is contained in:
1 parent
d6bb88ead2
commit
6124c604d9
2 files changed
+27
-31
No files matched your search
+22
-2
@@ -2,6 +2,7 @@ import asyncio
|
|||||||
|
|
||||||
from playwright.async_api import Page
|
from playwright.async_api import Page
|
||||||
|
|
||||||
|
from antibot.orchestrator import pass_challenges
|
||||||
from captcha import load_captcha
|
from captcha import load_captcha
|
||||||
from engine.schemas import ActionRequest
|
from engine.schemas import ActionRequest
|
||||||
|
|
||||||
@@ -63,11 +64,30 @@ async def wait_for(page: Page, act: ActionRequest, **kwargs) -> str | None:
|
|||||||
await page.wait_for_selector(act.selector, timeout=act.timeout * 1000)
|
await page.wait_for_selector(act.selector, timeout=act.timeout * 1000)
|
||||||
|
|
||||||
|
|
||||||
async def execute(page: Page, actions: list[ActionRequest], captcha: str | None = None) -> list[dict]:
|
@register
|
||||||
|
async def pass_challenge(page: Page, act: ActionRequest, *, antibot_name, success_locator = None, captcha_key= None, **kwargs) -> str | None:
|
||||||
|
assert act.selector
|
||||||
|
await pass_challenges(
|
||||||
|
antibot_name, page,
|
||||||
|
captcha=captcha_key, success_locator=act.selector
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
async def execute(
|
||||||
|
page: Page, actions: list[ActionRequest],
|
||||||
|
captcha_key: str | None = None, antibot_name: str | None = None,
|
||||||
|
success_locator: str | None = None) -> list[dict]:
|
||||||
|
"""Выполнение очереди автоматизации"""
|
||||||
actions_result = []
|
actions_result = []
|
||||||
ctx = {
|
ctx = {
|
||||||
"captcha_key": captcha,
|
"captcha_key": captcha_key,
|
||||||
|
"antibot_name": antibot_name,
|
||||||
|
"success_locator": success_locator
|
||||||
}
|
}
|
||||||
|
await pass_challenges(
|
||||||
|
antibot_name, page,
|
||||||
|
captcha=captcha, success_locator=success_locator
|
||||||
|
)
|
||||||
for act in actions:
|
for act in actions:
|
||||||
new_line = {
|
new_line = {
|
||||||
"type": act.type,
|
"type": act.type,
|
||||||
|
|||||||
+5
-29
@@ -16,8 +16,6 @@ from fastapi import Response
|
|||||||
from playwright.async_api import ProxySettings
|
from playwright.async_api import ProxySettings
|
||||||
from ua_parser import user_agent_parser
|
from ua_parser import user_agent_parser
|
||||||
|
|
||||||
import captcha
|
|
||||||
from antibot.orchestrator import pass_challenges
|
|
||||||
from engine import actions
|
from engine import actions
|
||||||
from engine.schemas import SolveRequest
|
from engine.schemas import SolveRequest
|
||||||
|
|
||||||
@@ -111,17 +109,6 @@ async def solve(
|
|||||||
config = load_or_create_identity(user_data_dir, user_agent=payload.user_agent)
|
config = load_or_create_identity(user_data_dir, user_agent=payload.user_agent)
|
||||||
ctx_opts = {**config["context_options"], "config": config["config"]}
|
ctx_opts = {**config["context_options"], "config": config["config"]}
|
||||||
|
|
||||||
# if payload.captcha:
|
|
||||||
# # "dl.formRow:nth-child(3) > dd:nth-child(2) > div:nth-child(1)"
|
|
||||||
# # hcaptcha
|
|
||||||
# # rucaptcha api_key
|
|
||||||
# captcha_solver = captcha.load_captcha(
|
|
||||||
# payload.captcha,
|
|
||||||
# payload.captcha_type
|
|
||||||
# )
|
|
||||||
# else:
|
|
||||||
# captcha_solver = None
|
|
||||||
|
|
||||||
async with AsyncCamoufox(
|
async with AsyncCamoufox(
|
||||||
persistent_context=True,
|
persistent_context=True,
|
||||||
user_data_dir=user_data_dir,
|
user_data_dir=user_data_dir,
|
||||||
@@ -141,27 +128,16 @@ async def solve(
|
|||||||
|
|
||||||
await page.goto(url=payload.url)
|
await page.goto(url=payload.url)
|
||||||
|
|
||||||
try:
|
actions_result = await actions.execute(
|
||||||
await pass_challenges(
|
page, payload.actions, payload.captcha,
|
||||||
payload.captcha_type, page,
|
antibot_name=payload.captcha_type, success_locator=payload.captcha_success_locator
|
||||||
captcha=payload.captcha, success_locator=payload.captcha_success_selector
|
)
|
||||||
)
|
|
||||||
except Exception:
|
|
||||||
result = {
|
|
||||||
"status": "err",
|
|
||||||
"user_agent": ctx_opts["user_agent"],
|
|
||||||
"cookies": None,
|
|
||||||
"actions": [],
|
|
||||||
"error": traceback.format_exc(),
|
|
||||||
}
|
|
||||||
return result
|
|
||||||
|
|
||||||
actions_result = await actions.execute(page, payload.actions, payload.captcha)
|
|
||||||
# test: is captcha present?
|
|
||||||
status = not "error" in actions_result[-1]
|
status = not "error" in actions_result[-1]
|
||||||
error = ["Something error", None][status]
|
error = ["Something error", None][status]
|
||||||
cookies = await camoufo.cookies()
|
cookies = await camoufo.cookies()
|
||||||
html = await page.inner_html('html')
|
html = await page.inner_html('html')
|
||||||
|
# TODO: добавить как опцию. передачу скрина в base64.
|
||||||
# await page.screenshot(
|
# await page.screenshot(
|
||||||
# path=(
|
# path=(
|
||||||
# f"/home/sokol/PycharmProjects/WebRoboApi/extra/screenshots/"
|
# f"/home/sokol/PycharmProjects/WebRoboApi/extra/screenshots/"
|
||||||
|
|||||||
Reference in new issue
Block a user